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

fix(web): common error handler #1071

Merged
merged 1 commit into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
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
2 changes: 0 additions & 2 deletions web/src/layouts/Header/UserSetting/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useTranslation } from "react-i18next";
import { Avatar, Menu, MenuButton, MenuItem, MenuList, useColorMode } from "@chakra-ui/react";

import { CHAKRA_UI_COLOR_MODE_KEY, COLOR_MODE } from "@/constants";
import i18n from "@/utils/i18n";

import SettingModal, { TabKeys } from "@/pages/app/setting";
Expand Down Expand Up @@ -65,7 +64,6 @@ export default function UserSetting(props: { name: string; avatar?: string; widt
<MenuItem
onClick={() => {
localStorage.removeItem("token");
localStorage.setItem(CHAKRA_UI_COLOR_MODE_KEY, COLOR_MODE.light);
window.location.href = "/login";
}}
>
Expand Down
32 changes: 8 additions & 24 deletions web/src/pages/app/database/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,14 @@ export const useEntryDataQuery = (params: any, onSuccess: (data: any) => void) =
};

export const useCreateDBMutation = (config?: { onSuccess: (data: any) => void }) => {
const globalStore = useGlobalStore();
const queryClient = useQueryClient();
return useMutation(
(values: any) => {
return CollectionControllerCreate(values);
},
{
onSuccess: async (data) => {
if (data.error) {
globalStore.showError(data.error);
} else {
if (!data.error) {
await queryClient.invalidateQueries(queryKeys.useCollectionListQuery);
config?.onSuccess && config.onSuccess(data);
}
Expand All @@ -98,9 +95,7 @@ export const useDeleteDBMutation = (config?: { onSuccess: (data: any) => void })
},
{
onSuccess: async (data) => {
if (data.error) {
globalStore.showError(data.error);
} else {
if (!data.error) {
store.setCurrentDB(undefined);
await queryClient.invalidateQueries(queryKeys.useCollectionListQuery);
globalStore.showSuccess(t("DeleteSuccess"));
Expand Down Expand Up @@ -131,8 +126,6 @@ export const useAddDataMutation = (config?: { onSuccess: (data: any) => void })
globalStore.showSuccess(t("AddSuccess"));
queryClient.invalidateQueries([queryKeys.useEntryDataQuery(currentDB?.name || "")]);
config && config.onSuccess(data);
} else {
globalStore.showError(data.error);
}
},
},
Expand Down Expand Up @@ -213,9 +206,7 @@ export const useCreatePolicyMutation = () => {
},
{
onSuccess: async (data) => {
if (data.error) {
globalStore.showError(data.error);
} else {
if (!data.error) {
globalStore.showSuccess(t("AddSuccess"));
await queryClient.invalidateQueries(queryKeys.usePolicyListQuery);
store.setCurrentPolicy(data.data);
Expand All @@ -234,11 +225,10 @@ export const useUpdatePolicyMutation = () => {
},
{
onSuccess(data) {
if (data.error) {
globalStore.showError(data.error);
} else {
if (!data.error) {
globalStore.showSuccess(t("UpdateSuccess"));
queryClient.invalidateQueries(queryKeys.usePolicyListQuery);
} else {
}
},
},
Expand All @@ -255,9 +245,7 @@ export const useDeletePolicyMutation = () => {
},
{
onSuccess: async (data) => {
if (data.error) {
globalStore.showError(data.error);
} else {
if (!data.error) {
store.setCurrentPolicy(undefined);
globalStore.showSuccess(t("DeleteSuccess"));
await queryClient.invalidateQueries(queryKeys.usePolicyListQuery);
Expand Down Expand Up @@ -310,9 +298,7 @@ export const useUpdateRulesMutation = (onSuccess?: (data: any) => void) => {
},
{
onSuccess(data) {
if (data.error) {
globalStore.showError(data.error);
} else {
if (!data.error) {
globalStore.showSuccess(t("UpdateSuccess"));
onSuccess && onSuccess(data.data);
}
Expand All @@ -330,9 +316,7 @@ export const useDeleteRuleMutation = (onSuccess?: () => void) => {
},
{
onSuccess(data) {
if (data.error) {
globalStore.showError(data.error);
} else {
if (!data.error) {
globalStore.showSuccess(t("DeleteSuccess"));
onSuccess && onSuccess();
}
Expand Down
22 changes: 5 additions & 17 deletions web/src/pages/app/functions/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
FunctionControllerUpdate,
} from "@/apis/v1/apps";
import useFunctionCache from "@/hooks/useFunctionCache";
import useGlobalStore from "@/pages/globalStore";

const queryKeys = {
useFunctionListQuery: ["useFunctionListQuery"],
Expand Down Expand Up @@ -44,17 +43,14 @@ export const useFunctionDetailQuery = (name: string, config: any) => {

export const useCreateFunctionMutation = () => {
const store = useFunctionStore();
const globalStore = useGlobalStore();
const queryClient = useQueryClient();
return useMutation(
(values: any) => {
return FunctionControllerCreate(values);
},
{
onSuccess(data) {
if (data.error) {
globalStore.showError(data.error);
} else {
if (!data.error) {
queryClient.invalidateQueries(queryKeys.useFunctionListQuery);
store.setCurrentFunction(data.data);
}
Expand All @@ -64,17 +60,14 @@ export const useCreateFunctionMutation = () => {
};

export const useUpdateFunctionMutation = () => {
const globalStore = useGlobalStore();
const queryClient = useQueryClient();
return useMutation(
(values: any) => {
return FunctionControllerUpdate(values);
},
{
onSuccess(data) {
if (data.error) {
globalStore.showError(data.error);
} else {
if (!data.error) {
queryClient.invalidateQueries(queryKeys.useFunctionListQuery);
}
},
Expand All @@ -83,7 +76,6 @@ export const useUpdateFunctionMutation = () => {
};

export const useDeleteFunctionMutation = () => {
const globalStore = useGlobalStore();
const store = useFunctionStore();
const functionCache = useFunctionCache();
const queryClient = useQueryClient();
Expand All @@ -93,9 +85,7 @@ export const useDeleteFunctionMutation = () => {
},
{
onSuccess(data) {
if (data.error) {
globalStore.showError(data.error);
} else {
if (!data.error) {
queryClient.invalidateQueries(queryKeys.useFunctionListQuery);
store.setCurrentFunction({});
functionCache.removeCache(data?.data?.id);
Expand All @@ -106,18 +96,16 @@ export const useDeleteFunctionMutation = () => {
};

export const useCompileMutation = () => {
const globalStore = useGlobalStore();
const queryClient = useQueryClient();
return useMutation({
mutationKey: ["compileMutation"],
mutationFn: (values: { code: string; name: string }) => {
return FunctionControllerCompile(values);
},
onSuccess(data) {
if (data.error) {
globalStore.showError(data.error);
if (!data.error) {
queryClient.setQueryData(["compileMutation"], data);
}
queryClient.setQueryData(["compileMutation"], data);
},
});
};
30 changes: 6 additions & 24 deletions web/src/pages/app/storages/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export const useBucketListQuery = (config?: { onSuccess: (data: any) => void })
};

export const useBucketCreateMutation = (config?: { onSuccess: (data: any) => void }) => {
const globalStore = useGlobalStore();
const queryClient = useQueryClient();
const store = useStorageStore();
return useMutation(
Expand All @@ -58,9 +57,7 @@ export const useBucketCreateMutation = (config?: { onSuccess: (data: any) => voi
},
{
onSuccess: async (data) => {
if (data.error) {
globalStore.showError(data.error);
} else {
if (!data.error) {
store.setCurrentStorage(data.data);
await queryClient.invalidateQueries(queryKeys.useBucketListQuery);
}
Expand All @@ -70,19 +67,15 @@ export const useBucketCreateMutation = (config?: { onSuccess: (data: any) => voi
};

export const useBucketUpdateMutation = (config?: { onSuccess: (data: any) => void }) => {
const globalStore = useGlobalStore();
const queryClient = useQueryClient();
return useMutation(
(values: any) => {
return BucketControllerUpdate(values);
},
{
onSuccess: async (data) => {
if (data.error) {
globalStore.showError(data.error);
} else {
if (!data.error) {
await queryClient.invalidateQueries(queryKeys.useBucketListQuery);

config?.onSuccess && config.onSuccess(data);
}
},
Expand All @@ -100,9 +93,7 @@ export const useBucketDeleteMutation = () => {
},
{
onSuccess: async (data) => {
if (data.error) {
globalStore.showError(data.error);
} else {
if (!data.error) {
globalStore.showSuccess("delete success");
store.setCurrentStorage(undefined);
await queryClient.invalidateQueries(queryKeys.useBucketListQuery);
Expand All @@ -129,17 +120,14 @@ export const useWebsiteListQuery = (config?: { onSuccess: (data: any) => void })
};

export const useWebsiteCreateMutation = (config?: { onSuccess: (data: any) => void }) => {
const globalStore = useGlobalStore();
const queryClient = useQueryClient();
return useMutation(
(values: any) => {
return WebsiteControllerCreate(values);
},
{
onSuccess: async (data) => {
if (data.error) {
globalStore.showError(data.error);
} else {
if (!data.error) {
await queryClient.invalidateQueries(queryKeys.useBucketListQuery);
}
},
Expand All @@ -156,9 +144,7 @@ export const useWebsiteDeleteMutation = () => {
},
{
onSuccess: async (data) => {
if (data.error) {
globalStore.showError(data.error);
} else {
if (!data.error) {
globalStore.showSuccess("delete success");
await queryClient.invalidateQueries(queryKeys.useBucketListQuery);
}
Expand All @@ -168,19 +154,15 @@ export const useWebsiteDeleteMutation = () => {
};

export const useWebSiteUpdateMutation = (config?: { onSuccess: (data: any) => void }) => {
const globalStore = useGlobalStore();
const queryClient = useQueryClient();
return useMutation(
(values: any) => {
return WebsiteControllerBindDomain(values);
},
{
onSuccess: async (data) => {
if (data.error) {
globalStore.showError(data.error);
} else {
if (!data.error) {
await queryClient.invalidateQueries(queryKeys.useBucketListQuery);

config?.onSuccess && config.onSuccess(data);
}
},
Expand Down
16 changes: 3 additions & 13 deletions web/src/pages/auth/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
UserPasswordControllerSignin,
UserPasswordControllerSignup,
} from "@/apis/v1/auth";
import useGlobalStore from "@/pages/globalStore";

const queryKeys = {
useSigninByPasswordMutation: ["useSigninByPasswordMutation"],
Expand All @@ -20,16 +19,13 @@ const queryKeys = {
};

export const useSigninByPasswordMutation = (config?: { onSuccess: (result: any) => void }) => {
const globalStore = useGlobalStore();
return useMutation(
(values: any) => {
return UserPasswordControllerSignin(values);
},
{
onSuccess: async (result) => {
if (result.error) {
globalStore.showError(result.error);
} else {
if (!result.error) {
localStorage.setItem("token", result?.data);
config?.onSuccess(result);
}
Expand All @@ -39,16 +35,13 @@ export const useSigninByPasswordMutation = (config?: { onSuccess: (result: any)
};

export const useSigninBySmsCodeMutation = (config?: { onSuccess: (result: any) => void }) => {
const globalStore = useGlobalStore();
return useMutation(
(values: any) => {
return PhoneControllerSignin(values);
},
{
onSuccess: async (result) => {
if (result.error) {
globalStore.showError(result.error);
} else {
if (!result.error) {
localStorage.setItem("token", result?.data);
config?.onSuccess(result);
}
Expand All @@ -58,16 +51,13 @@ export const useSigninBySmsCodeMutation = (config?: { onSuccess: (result: any) =
};

export const useSignupMutation = (config?: { onSuccess: (result: any) => void }) => {
const globalStore = useGlobalStore();
return useMutation(
(values: any) => {
return UserPasswordControllerSignup(values);
},
{
onSuccess: async (result) => {
if (result.error) {
globalStore.showError(result.error);
} else {
if (!result.error) {
config?.onSuccess(result);
}
},
Expand Down
3 changes: 2 additions & 1 deletion web/src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { createStandaloneToast } from "@chakra-ui/react";
import axios, { AxiosRequestHeaders, AxiosResponse } from "axios";

import { VITE_SERVER_BASE_URL } from "../constants";
import { CHAKRA_UI_COLOR_MODE_KEY, VITE_SERVER_BASE_URL } from "../constants";

const { toast } = createStandaloneToast();

Expand Down Expand Up @@ -45,6 +45,7 @@ request.interceptors.response.use(
toast({
title: data?.error,
position: "top",
variant: localStorage.getItem(CHAKRA_UI_COLOR_MODE_KEY) ? "subtle" : "solid",
status: "error",
duration: 1500,
});
Expand Down