Implement deleting reviews
This commit is contained in:
parent
f44cd9cc9f
commit
e8ed845f10
@ -3,6 +3,7 @@ import 'dart:convert';
|
|||||||
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/cupertino.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:mobile/base/sidemenu.dart';
|
import 'package:mobile/base/sidemenu.dart';
|
||||||
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
import 'models.dart' as models;
|
import 'models.dart' as models;
|
||||||
import 'api.dart' as api;
|
import 'api.dart' as api;
|
||||||
|
|
||||||
@ -15,6 +16,8 @@ class ReviewListPage extends StatefulWidget {
|
|||||||
|
|
||||||
class _ReviewListState extends State<ReviewListPage> {
|
class _ReviewListState extends State<ReviewListPage> {
|
||||||
List<models.User> _users = [];
|
List<models.User> _users = [];
|
||||||
|
List<models.Review> _reviews = [];
|
||||||
|
String? _currentUserId;
|
||||||
|
|
||||||
models.User? _getReviewUser(models.Review review) {
|
models.User? _getReviewUser(models.Review review) {
|
||||||
try {
|
try {
|
||||||
@ -24,18 +27,47 @@ class _ReviewListState extends State<ReviewListPage> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _confirmDeleteReview(models.Review review) {
|
||||||
|
showDialog(context: context, builder: (BuildContext context) =>
|
||||||
|
AlertDialog(
|
||||||
|
title: const Text('Delete review'),
|
||||||
|
content: const Text('Are you sure you want to delete this review?'),
|
||||||
|
actions: [
|
||||||
|
TextButton(child: const Text('Cancel'), onPressed: () => Navigator.pop(context)),
|
||||||
|
TextButton(child: const Text('Delete', style: TextStyle(color: Colors.red)), onPressed: () => _deleteReview(review)),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
void _deleteReview(models.Review review) async {
|
||||||
|
Navigator.pop(context);
|
||||||
|
|
||||||
|
final response = await api.request(context, api.ApiService.app, 'DELETE', '/reviews/${review.id}', null);
|
||||||
|
if (response == null) return;
|
||||||
|
|
||||||
|
setState(() {
|
||||||
|
_reviews = _reviews.where((r) => r.id != review.id).toList();
|
||||||
|
});
|
||||||
|
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Review deleted successfully')));
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void didChangeDependencies() async {
|
void didChangeDependencies() async {
|
||||||
super.didChangeDependencies();
|
super.didChangeDependencies();
|
||||||
|
|
||||||
final arg = ModalRoute.of(context)!.settings.arguments as models.ReviewList;
|
final prefs = await SharedPreferences.getInstance();
|
||||||
final reviews = arg.reviews;
|
_currentUserId = prefs.getString('id');
|
||||||
|
|
||||||
if (reviews.isEmpty) {
|
final arg = ModalRoute.of(context)!.settings.arguments as models.ReviewList;
|
||||||
|
_reviews = arg.reviews;
|
||||||
|
|
||||||
|
if (_reviews.isEmpty || !mounted) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
final userIds = reviews.map((review) => review.userId).toSet().toList();
|
final userIds = _reviews.map((review) => review.userId).toSet().toList();
|
||||||
|
|
||||||
final response = await api.request(context, api.ApiService.auth, 'GET', '/api/Users/UsersByIds?userIds=' + userIds.join(','), null);
|
final response = await api.request(context, api.ApiService.auth, 'GET', '/api/Users/UsersByIds?userIds=' + userIds.join(','), null);
|
||||||
if (response == null) return;
|
if (response == null) return;
|
||||||
@ -48,21 +80,20 @@ class _ReviewListState extends State<ReviewListPage> {
|
|||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final arg = ModalRoute.of(context)!.settings.arguments as models.ReviewList;
|
final arg = ModalRoute.of(context)!.settings.arguments as models.ReviewList;
|
||||||
final reviews = arg.reviews;
|
|
||||||
final place = arg.place;
|
final place = arg.place;
|
||||||
|
|
||||||
return SideMenu(
|
return SideMenu(
|
||||||
selectedIndex: -1,
|
selectedIndex: -1,
|
||||||
body: Scaffold(
|
body: Scaffold(
|
||||||
backgroundColor: const Color(0xFFF9F9F9),
|
backgroundColor: const Color(0xFFF9F9F9),
|
||||||
body: reviews.isEmpty
|
body: _reviews.isEmpty
|
||||||
? const Center(child: Text('No reviews yet. Be the first to review this place'))
|
? const Center(child: Text('No reviews yet. Be the first to review this place'))
|
||||||
: SingleChildScrollView(child: Container(
|
: SingleChildScrollView(child: Container(
|
||||||
decoration: const BoxDecoration(color: Color(0xFFF9F9F9)),
|
decoration: const BoxDecoration(color: Color(0xFFF9F9F9)),
|
||||||
width: MediaQuery.of(context).size.width,
|
width: MediaQuery.of(context).size.width,
|
||||||
padding: const EdgeInsets.all(20.0),
|
padding: const EdgeInsets.all(20.0),
|
||||||
child: Column(children: [
|
child: Column(children: [
|
||||||
for (final review in reviews)
|
for (final review in _reviews)
|
||||||
Container(
|
Container(
|
||||||
width: double.infinity,
|
width: double.infinity,
|
||||||
padding: const EdgeInsets.all(20),
|
padding: const EdgeInsets.all(20),
|
||||||
@ -103,7 +134,10 @@ class _ReviewListState extends State<ReviewListPage> {
|
|||||||
child: Column(
|
child: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
children: [
|
children: [
|
||||||
Text(review.title, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 24)),
|
Row(children: [
|
||||||
|
Expanded(child: Text(review.title, style: const TextStyle(fontWeight: FontWeight.bold, fontSize: 24))),
|
||||||
|
if (review.userId == _currentUserId) IconButton(onPressed: () => _confirmDeleteReview(review), icon: const Icon(Icons.delete, color: Colors.grey)),
|
||||||
|
]),
|
||||||
Text(review.content),
|
Text(review.content),
|
||||||
const SizedBox(height: 10),
|
const SizedBox(height: 10),
|
||||||
if (review.image != null) Image.network(review.image!.imageUrl, height: 200),
|
if (review.image != null) Image.network(review.image!.imageUrl, height: 200),
|
||||||
@ -130,7 +164,7 @@ class _ReviewListState extends State<ReviewListPage> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final review = await Navigator.pushNamed(context, '/create-review', arguments: place) as models.Review?;
|
final review = await Navigator.pushNamed(context, '/create-review', arguments: place) as models.Review?;
|
||||||
if (review != null) reviews.add(review);
|
if (review != null) _reviews.add(review);
|
||||||
},
|
},
|
||||||
backgroundColor: Colors.blue,
|
backgroundColor: Colors.blue,
|
||||||
focusColor: Colors.blueGrey,
|
focusColor: Colors.blueGrey,
|
||||||
|
Loading…
Reference in New Issue
Block a user