Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cherry-pick #6879 feat: support hide custom type toast in operator #6880

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions config-ui/src/utils/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ export type OperateConfig = {
formatMessage?: () => string;
formatReason?: (err: unknown) => string;
hideToast?: boolean;
hideSuccessToast?: boolean;
hideErrorToast?: boolean;
};

/**
Expand All @@ -32,6 +34,9 @@ export type OperateConfig = {
* @param config.setOperating -> Control the status of the request
* @param config.formatMessage -> Show the message for the success
* @param config.formatReason -> Show the reason for the failure
* @param config.hideToast -> Hide all the toast
* @param config.hideSuccessToast -> Hide the success toast
* @param config.hideErrorToast -> Hide the error toast
* @returns
*/
export const operator = async <T>(request: () => Promise<T>, config?: OperateConfig): Promise<[boolean, any?]> => {
Expand All @@ -41,14 +46,14 @@ export const operator = async <T>(request: () => Promise<T>, config?: OperateCon
setOperating?.(true);
const res = await request();
const content = formatMessage?.() ?? 'Operation successfully completed';
if (!config?.hideToast) {
if (!config?.hideToast || !config?.hideSuccessToast) {
message.success(content);
}
return [true, res];
} catch (err) {
console.error('Operation failed.', err);
const reason = formatReason?.(err) ?? (err as any).response?.data?.message ?? 'Operation failed.';
if (!config?.hideToast) {
if (!config?.hideToast || !config?.hideErrorToast) {
message.error(reason);
}

Expand Down
Loading