From e2ada16786c29bd766a28e2fef745765c771e66d Mon Sep 17 00:00:00 2001 From: Philip Abakah <68474949+Strange-Philip@users.noreply.github.com> Date: Sat, 6 Jan 2024 17:56:22 +0000 Subject: [PATCH] added delete post (#14) Co-authored-by: Strange-Philip --- frontend/lib/blog/blog_detail.dart | 52 ++++++++++++++++++- frontend/lib/blog/widgets/article_card.dart | 18 +++---- .../lib/data/providers/article_provider.dart | 13 +++++ pubspec.lock | 16 +++--- 4 files changed, 82 insertions(+), 17 deletions(-) diff --git a/frontend/lib/blog/blog_detail.dart b/frontend/lib/blog/blog_detail.dart index d8cb047..23c20ea 100644 --- a/frontend/lib/blog/blog_detail.dart +++ b/frontend/lib/blog/blog_detail.dart @@ -1,4 +1,5 @@ import 'package:fluent_ui/fluent_ui.dart'; +import 'package:frontend/data/providers/article_provider.dart'; import 'package:frontend/data/providers/auth_provider.dart'; import 'package:frontend/main.dart'; import 'package:frontend/utils/misc.dart'; @@ -20,6 +21,8 @@ class BlogDetail extends StatelessWidget { articleId: int.tryParse(articleId), child: (detail, layout) { final currentUser = context.read().user; + final articleProv = context.read(); + final article = detail.article; final owner = detail.owner; @@ -49,6 +52,49 @@ class BlogDetail extends StatelessWidget { label: const Text('Edit'), onPressed: () => router.pushReplacement('/posts/${article.id}/edit'), ), + if (isPostOwner) + CommandBarButton( + icon: Icon(FluentIcons.delete, color: Colors.red), + label: Text( + 'Delete', + style: TextStyle(color: Colors.red), + ), + onPressed: () { + showDialog( + context: context, + builder: (context) { + return ContentDialog( + title: const Text('Delete Blog permanently?'), + content: const Text( + 'If you delete this file, you won\'t be able to recover it. Do you want to delete it?', + ), + actions: [ + FilledButton( + style: ButtonStyle( + backgroundColor: ButtonState.all(Colors.red), + shape: ButtonState.all( + const RoundedRectangleBorder( + borderRadius: BorderRadius.zero), + )), + onPressed: () async { + await articleProv + .deleteArticle(int.tryParse(articleId)!) + .then((value) => router.pushReplacement('/')); + }, + child: const Text("Delete"), + ), + FilledButton( + style: ButtonStyle( + shape: ButtonState.all(const RoundedRectangleBorder( + borderRadius: BorderRadius.zero))), + onPressed: () => router.pop(context), + child: const Text("Cancel"), + ), + ], + ); + }); + }, + ), ], ), padding: 0, @@ -56,7 +102,8 @@ class BlogDetail extends StatelessWidget { Divider( style: DividerThemeData( thickness: 0.2, - decoration: BoxDecoration(border: Border(top: BorderSide(color: Colors.grey.withOpacity(0.05)))), + decoration: BoxDecoration( + border: Border(top: BorderSide(color: Colors.grey.withOpacity(0.05)))), ), ), Padding( @@ -73,7 +120,8 @@ class BlogDetail extends StatelessWidget { Expanded(child: Card(child: imageView(article.imageUrl!))), if (imageHost != null) ...[ const SizedBox(height: 8), - Text(imageHost, style: const TextStyle(fontWeight: FontWeight.w300, fontSize: 12)), + Text(imageHost, + style: const TextStyle(fontWeight: FontWeight.w300, fontSize: 12)), ] ], ), diff --git a/frontend/lib/blog/widgets/article_card.dart b/frontend/lib/blog/widgets/article_card.dart index db49d73..9805767 100644 --- a/frontend/lib/blog/widgets/article_card.dart +++ b/frontend/lib/blog/widgets/article_card.dart @@ -11,15 +11,15 @@ class ArticleCard extends StatelessWidget { @override Widget build(BuildContext context) { final typography = FluentTheme.of(context).typography; - return SizedBox( - width: 300, - height: 250, - child: Card( - borderColor: Colors.grey.withOpacity(0.3), - borderRadius: BorderRadius.zero, - padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 24), - child: GestureDetector( - onTap: () => router.push('/posts/${article.id}'), + return GestureDetector( + onTap: () => router.push('/posts/${article.id}'), + child: SizedBox( + width: 300, + height: 250, + child: Card( + borderColor: Colors.grey.withOpacity(0.3), + borderRadius: BorderRadius.zero, + padding: const EdgeInsets.symmetric(horizontal: 24.0, vertical: 24), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ diff --git a/frontend/lib/data/providers/article_provider.dart b/frontend/lib/data/providers/article_provider.dart index 763f187..9d6e9d8 100644 --- a/frontend/lib/data/providers/article_provider.dart +++ b/frontend/lib/data/providers/article_provider.dart @@ -31,4 +31,17 @@ class ArticleProvider extends BaseProvider> { addEvent(ProviderEvent.success(data: [...articles, article])); } + + Future deleteArticle( + int articleId, + ) async { + final articles = lastEvent?.data ?? []; + await safeRun(() => apiSvc.deleteArticle( + articleId, + )); + + addEvent(ProviderEvent.success(data: [ + ...articles, + ])); + } } diff --git a/pubspec.lock b/pubspec.lock index f8183e2..a34bff2 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -927,16 +927,20 @@ packages: yaroo: dependency: "direct main" description: - path: "../yaroo-dev/yaroo" - relative: true - source: path + path: yaroo + ref: HEAD + resolved-ref: a83e6d8821a01fc6d45a37a5a3006977936d5c4e + url: "https://github.com/codekeyz/yaroo.git" + source: git version: "0.0.1" yaroorm: dependency: "direct main" description: - path: "../yaroo-dev/yaroorm" - relative: true - source: path + path: yaroorm + ref: HEAD + resolved-ref: a83e6d8821a01fc6d45a37a5a3006977936d5c4e + url: "https://github.com/codekeyz/yaroo.git" + source: git version: "1.0.0" sdks: dart: ">=3.2.2 <4.0.0"