From 02a9f0dfbbe1c25eaca63d91b0cd7c84d06724f8 Mon Sep 17 00:00:00 2001 From: "Lucas.Xu" Date: Fri, 21 Jun 2024 21:08:48 +0800 Subject: [PATCH] feat: integrate unpublish api --- .../application/document_share_bloc.dart | 29 ++++++++--- .../presentation/share/publish_tab.dart | 50 +++++++++++++------ 2 files changed, 57 insertions(+), 22 deletions(-) diff --git a/frontend/appflowy_flutter/lib/plugins/document/application/document_share_bloc.dart b/frontend/appflowy_flutter/lib/plugins/document/application/document_share_bloc.dart index a9b4a0d84f..4a90cb8fb8 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/application/document_share_bloc.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/application/document_share_bloc.dart @@ -42,20 +42,33 @@ class DocumentShareBloc extends Bloc { final name = '${view.name}-${uuid()}'; // set space name + + FlowyResult? result; try { await ViewBackendService.setPublishNameSpace(spaceName) .getOrThrow(); - await ViewBackendService.publish(view, name: name).getOrThrow(); + result = await ViewBackendService.publish(view, name: name); + + emit( + state.copyWith( + isPublished: true, + publishResult: result, + url: 'https://test.appflowy.io/$spaceName/$name', + ), + ); } catch (e) { Log.error('publish error: $e'); - } - emit( - state.copyWith( - isPublished: true, - url: 'https://test.appflowy.io/$spaceName/$name', - ), - ); + emit( + state.copyWith( + isPublished: false, + publishResult: FlowyResult.failure( + FlowyError(msg: 'publish error: $e'), + ), + url: '', + ), + ); + } }, unPublish: () async { await ViewBackendService.unpublish(view); diff --git a/frontend/appflowy_flutter/lib/plugins/document/presentation/share/publish_tab.dart b/frontend/appflowy_flutter/lib/plugins/document/presentation/share/publish_tab.dart index 981276856b..96e5b3e6a0 100644 --- a/frontend/appflowy_flutter/lib/plugins/document/presentation/share/publish_tab.dart +++ b/frontend/appflowy_flutter/lib/plugins/document/presentation/share/publish_tab.dart @@ -1,6 +1,7 @@ import 'package:appflowy/generated/flowy_svgs.g.dart'; import 'package:appflowy/generated/locale_keys.g.dart'; import 'package:appflowy/plugins/document/application/document_share_bloc.dart'; +import 'package:appflowy/workspace/presentation/home/toast.dart'; import 'package:appflowy_editor/appflowy_editor.dart'; import 'package:easy_localization/easy_localization.dart'; import 'package:flowy_infra_ui/flowy_infra_ui.dart'; @@ -13,29 +14,45 @@ class PublishTab extends StatelessWidget { @override Widget build(BuildContext context) { - return context.watch().state.isPublished - ? _PublishedWidget( - onUnPublish: () { - context - .read() - .add(const DocumentShareEvent.unPublish()); - }, - onVisitSite: () {}, - ) - : _UnPublishWidget( - onPublish: () => context - .read() - .add(const DocumentShareEvent.publish('')), + return BlocConsumer( + listener: (context, state) { + if (state.publishResult != null) { + state.publishResult!.fold( + (value) => showSnackBarMessage(context, 'Published successfully'), + (error) => + showSnackBarMessage(context, 'Failed to publish: $error'), ); + } + }, + builder: (context, state) { + return state.isPublished + ? _PublishedWidget( + url: state.url, + onVisitSite: () {}, + onUnPublish: () { + context + .read() + .add(const DocumentShareEvent.unPublish()); + }, + ) + : _UnPublishWidget( + onPublish: () => context + .read() + .add(const DocumentShareEvent.publish('')), + ); + }, + ); } } class _PublishedWidget extends StatefulWidget { const _PublishedWidget({ + required this.url, required this.onVisitSite, required this.onUnPublish, }); + final String url; final VoidCallback onVisitSite; final VoidCallback onUnPublish; @@ -46,6 +63,12 @@ class _PublishedWidget extends StatefulWidget { class _PublishedWidgetState extends State<_PublishedWidget> { final controller = TextEditingController(); + @override + void initState() { + super.initState(); + controller.text = widget.url; + } + @override void dispose() { controller.dispose(); @@ -195,7 +218,6 @@ class _PublishUrl extends StatelessWidget { child: FlowyTextField( autoFocus: false, controller: controller, - text: 'http:/appflowy.com/vinh/open-positions', suffixIcon: _buildCopyLinkIcon(), ), );