✨ Post deletion
This commit is contained in:
@@ -4,6 +4,7 @@ import 'package:solian/models/post.dart';
|
||||
import 'package:solian/providers/auth.dart';
|
||||
import 'package:solian/router.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:solian/widgets/posts/item_deletion.dart';
|
||||
|
||||
class PostItemAction extends StatelessWidget {
|
||||
final Post item;
|
||||
@@ -16,7 +17,7 @@ class PostItemAction extends StatelessWidget {
|
||||
final auth = context.read<AuthProvider>();
|
||||
|
||||
return SizedBox(
|
||||
height: 280,
|
||||
height: 320,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
@@ -29,43 +30,62 @@ class PostItemAction extends StatelessWidget {
|
||||
),
|
||||
Expanded(
|
||||
child: FutureBuilder(
|
||||
future: auth.getProfiles(),
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.hasData) {
|
||||
final authorizedItems = [
|
||||
ListTile(
|
||||
leading: const Icon(Icons.edit),
|
||||
title: Text(AppLocalizations.of(context)!.edit),
|
||||
onTap: () {
|
||||
router
|
||||
.pushNamed('posts.moments.editor', extra: item)
|
||||
.then((did) {
|
||||
if(did == true && onUpdate != null) {
|
||||
onUpdate!();
|
||||
}
|
||||
});
|
||||
},
|
||||
)
|
||||
];
|
||||
future: auth.getProfiles(),
|
||||
builder: (context, snapshot) {
|
||||
print(snapshot);
|
||||
if (snapshot.hasData) {
|
||||
final authorizedItems = [
|
||||
ListTile(
|
||||
leading: const Icon(Icons.edit),
|
||||
title: Text(AppLocalizations.of(context)!.edit),
|
||||
onTap: () {
|
||||
router
|
||||
.pushNamed('posts.moments.editor', extra: item)
|
||||
.then((did) {
|
||||
if (did == true && onUpdate != null) {
|
||||
onUpdate!();
|
||||
}
|
||||
});
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.delete),
|
||||
title: Text(AppLocalizations.of(context)!.delete),
|
||||
onTap: () {
|
||||
final dataset = '${item.modelType}s';
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => ItemDeletionDialog(
|
||||
item: item,
|
||||
dataset: dataset,
|
||||
onDelete: (did) {
|
||||
if(did == true && onUpdate != null) onUpdate!();
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
)
|
||||
];
|
||||
|
||||
return ListView(
|
||||
children: [
|
||||
...(snapshot.data['id'] == item.authorId
|
||||
? authorizedItems
|
||||
: List.empty()),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.report),
|
||||
title: Text(AppLocalizations.of(context)!.report),
|
||||
onTap: () {},
|
||||
)
|
||||
],
|
||||
);
|
||||
} else {
|
||||
return const Center(
|
||||
child: CircularProgressIndicator(),
|
||||
);
|
||||
}
|
||||
}),
|
||||
return ListView(
|
||||
children: [
|
||||
...(snapshot.data['id'] == item.authorId
|
||||
? authorizedItems
|
||||
: List.empty()),
|
||||
ListTile(
|
||||
leading: const Icon(Icons.report),
|
||||
title: Text(AppLocalizations.of(context)!.report),
|
||||
onTap: () {},
|
||||
)
|
||||
],
|
||||
);
|
||||
} else {
|
||||
return const Center(
|
||||
child: CircularProgressIndicator(),
|
||||
);
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
66
lib/widgets/posts/item_deletion.dart
Normal file
66
lib/widgets/posts/item_deletion.dart
Normal file
@@ -0,0 +1,66 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:solian/models/post.dart';
|
||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||
import 'package:solian/providers/auth.dart';
|
||||
import 'package:solian/utils/service_url.dart';
|
||||
|
||||
class ItemDeletionDialog extends StatefulWidget {
|
||||
final Post item;
|
||||
final String dataset;
|
||||
final Function? onDelete;
|
||||
|
||||
const ItemDeletionDialog({
|
||||
super.key,
|
||||
required this.item,
|
||||
required this.dataset,
|
||||
this.onDelete,
|
||||
});
|
||||
|
||||
@override
|
||||
State<ItemDeletionDialog> createState() => _ItemDeletionDialogState();
|
||||
}
|
||||
|
||||
class _ItemDeletionDialogState extends State<ItemDeletionDialog> {
|
||||
bool _isSubmitting = false;
|
||||
|
||||
void doDeletion(BuildContext context) async {
|
||||
final auth = context.read<AuthProvider>();
|
||||
if (!await auth.isAuthorized()) return;
|
||||
|
||||
final uri =
|
||||
getRequestUri('interactive', '/api/p/moments/${widget.item.id}');
|
||||
|
||||
setState(() => _isSubmitting = true);
|
||||
final res = await auth.client!.delete(uri);
|
||||
if (res.statusCode != 200) {
|
||||
var message = utf8.decode(res.bodyBytes);
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text("Something went wrong... $message")),
|
||||
);
|
||||
setState(() => _isSubmitting = false);
|
||||
} else {
|
||||
Navigator.pop(context, true);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return AlertDialog(
|
||||
title: Text(AppLocalizations.of(context)!.confirmation),
|
||||
content: Text(AppLocalizations.of(context)!.postDeleteConfirm),
|
||||
actions: <Widget>[
|
||||
TextButton(
|
||||
onPressed: _isSubmitting ? null : () => Navigator.pop(context, false),
|
||||
child: Text(AppLocalizations.of(context)!.confirmCancel),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: _isSubmitting ? null : () => doDeletion(context),
|
||||
child: Text(AppLocalizations.of(context)!.confirmOkay),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user