feat: show unpublish error

This commit is contained in:
Lucas.Xu
2024-07-03 11:05:15 +08:00
parent 59b36f67c8
commit 4ece2ff959
4 changed files with 51 additions and 16 deletions

View File

@ -13,6 +13,7 @@ import 'package:freezed_annotation/freezed_annotation.dart';
part 'document_share_bloc.freezed.dart'; part 'document_share_bloc.freezed.dart';
// todo: replace with beta
const _url = 'https://test.appflowy.com'; const _url = 'https://test.appflowy.com';
class DocumentShareBloc extends Bloc<DocumentShareEvent, DocumentShareState> { class DocumentShareBloc extends Bloc<DocumentShareEvent, DocumentShareState> {
@ -80,6 +81,7 @@ class DocumentShareBloc extends Bloc<DocumentShareEvent, DocumentShareState> {
state.copyWith( state.copyWith(
isPublished: true, isPublished: true,
publishResult: FlowySuccess(null), publishResult: FlowySuccess(null),
unpublishResult: null,
url: '$_url/${result.namespace}/$publishName', url: '$_url/${result.namespace}/$publishName',
), ),
); );
@ -92,12 +94,20 @@ class DocumentShareBloc extends Bloc<DocumentShareEvent, DocumentShareState> {
publishResult: FlowyResult.failure( publishResult: FlowyResult.failure(
FlowyError(msg: 'publish error: $e'), FlowyError(msg: 'publish error: $e'),
), ),
unpublishResult: null,
url: '', url: '',
), ),
); );
} }
}, },
unPublish: () async { unPublish: () async {
emit(
state.copyWith(
publishResult: null,
unpublishResult: null,
),
);
final result = await ViewBackendService.unpublish(view); final result = await ViewBackendService.unpublish(view);
final isPublished = !result.isSuccess; final isPublished = !result.isSuccess;
result.onFailure((f) { result.onFailure((f) {
@ -108,7 +118,8 @@ class DocumentShareBloc extends Bloc<DocumentShareEvent, DocumentShareState> {
state.copyWith( state.copyWith(
isPublished: isPublished, isPublished: isPublished,
publishResult: null, publishResult: null,
url: '', unpublishResult: result,
url: result.fold((_) => '', (_) => state.url),
), ),
); );
}, },
@ -213,6 +224,7 @@ class DocumentShareState with _$DocumentShareState {
FlowyResult<ExportDataPB, FlowyError>? exportResult, FlowyResult<ExportDataPB, FlowyError>? exportResult,
required bool isPublished, required bool isPublished,
FlowyResult<void, FlowyError>? publishResult, FlowyResult<void, FlowyError>? publishResult,
FlowyResult<void, FlowyError>? unpublishResult,
required String url, required String url,
required String viewName, required String viewName,
}) = _DocumentShareState; }) = _DocumentShareState;

View File

@ -22,19 +22,7 @@ class PublishTab extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocConsumer<DocumentShareBloc, DocumentShareState>( return BlocConsumer<DocumentShareBloc, DocumentShareState>(
listener: (context, state) { listener: (context, state) {
if (state.publishResult != null) { _showToast(context, state);
state.publishResult!.fold(
(value) => showToastNotification(
context,
message: LocaleKeys.publish_publishSuccessfully.tr(),
),
(error) => showToastNotification(
context,
message:
'${LocaleKeys.publish_publishFailed.tr()}: ${error.code}',
),
);
}
}, },
builder: (context, state) { builder: (context, state) {
return state.isPublished return state.isPublished
@ -64,6 +52,33 @@ class PublishTab extends StatelessWidget {
}, },
); );
} }
void _showToast(BuildContext context, DocumentShareState state) {
if (state.publishResult != null) {
state.publishResult!.fold(
(value) => showToastNotification(
context,
message: LocaleKeys.publish_publishSuccessfully.tr(),
),
(error) => showToastNotification(
context,
message: '${LocaleKeys.publish_publishFailed.tr()}: ${error.code}',
),
);
} else if (state.unpublishResult != null) {
state.unpublishResult!.fold(
(value) => showToastNotification(
context,
message: LocaleKeys.publish_unpublishSuccessfully.tr(),
),
(error) => showToastNotification(
context,
message: LocaleKeys.publish_unpublishFailed.tr(),
description: error.msg,
),
);
}
}
} }
class _PublishedWidget extends StatefulWidget { class _PublishedWidget extends StatefulWidget {

View File

@ -295,7 +295,14 @@ void showToastNotification(
type: ToastificationType.success, type: ToastificationType.success,
style: ToastificationStyle.flat, style: ToastificationStyle.flat,
title: FlowyText(message), title: FlowyText(message),
description: description != null ? FlowyText(description) : null, description: description != null
? FlowyText.regular(
description,
fontSize: 12,
lineHeight: 1.2,
maxLines: 3,
)
: null,
alignment: Alignment.bottomCenter, alignment: Alignment.bottomCenter,
autoCloseDuration: const Duration(seconds: 4), autoCloseDuration: const Duration(seconds: 4),
showProgressBar: false, showProgressBar: false,

View File

@ -2054,6 +2054,7 @@
"containsPublishedPage": "This page contains one or more published page, it will be unpublished if you continue.", "containsPublishedPage": "This page contains one or more published page, it will be unpublished if you continue.",
"publishSuccessfully": "Published successfully", "publishSuccessfully": "Published successfully",
"unpublishSuccessfully": "Unpublished successfully", "unpublishSuccessfully": "Unpublished successfully",
"publishFailed": "Failed to publish" "publishFailed": "Failed to publish",
"unpublishFailed": "Failed to unpublish"
} }
} }