Skip to content

Commit

Permalink
added delete post (#14)
Browse files Browse the repository at this point in the history
Co-authored-by: Strange-Philip <[email protected]>
  • Loading branch information
Strange-Philip and Strange-Philip authored Jan 6, 2024
1 parent 17afdf1 commit e2ada16
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 17 deletions.
52 changes: 50 additions & 2 deletions frontend/lib/blog/blog_detail.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -20,6 +21,8 @@ class BlogDetail extends StatelessWidget {
articleId: int.tryParse(articleId),
child: (detail, layout) {
final currentUser = context.read<AuthProvider>().user;
final articleProv = context.read<ArticleProvider>();

final article = detail.article;
final owner = detail.owner;

Expand Down Expand Up @@ -49,14 +52,58 @@ 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,
),
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(
Expand All @@ -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)),
]
],
),
Expand Down
18 changes: 9 additions & 9 deletions frontend/lib/blog/widgets/article_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down
13 changes: 13 additions & 0 deletions frontend/lib/data/providers/article_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,17 @@ class ArticleProvider extends BaseProvider<List<Article>> {

addEvent(ProviderEvent.success(data: [...articles, article]));
}

Future<void> deleteArticle(
int articleId,
) async {
final articles = lastEvent?.data ?? [];
await safeRun(() => apiSvc.deleteArticle(
articleId,
));

addEvent(ProviderEvent.success(data: [
...articles,
]));
}
}
16 changes: 10 additions & 6 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit e2ada16

Please sign in to comment.