0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-20 21:32:31 -05:00

refactor: use hook for toastError in order to get type support

This commit is contained in:
Charles Zhao 2022-10-19 17:50:30 +08:00
parent 103b80a84e
commit af2cc58172
No known key found for this signature in database
GPG key ID: 4858774754C92DF2

View file

@ -1,7 +1,6 @@
import { useLogto } from '@logto/react';
import { RequestErrorBody } from '@logto/schemas';
import { managementResource } from '@logto/schemas/lib/seeds';
import { t } from 'i18next';
import ky from 'ky';
import { useMemo } from 'react';
import { toast } from 'react-hot-toast';
@ -20,15 +19,21 @@ export class RequestError extends Error {
}
}
const toastError = async (response: Response) => {
const fallbackErrorMessage = t('admin_console.errors.unknown_server_error');
const useToastError = () => {
const { t } = useTranslation(undefined, { keyPrefix: 'admin_console' });
try {
const data = await response.json<RequestErrorBody>();
toast.error([data.message, data.details].join('\n') || fallbackErrorMessage);
} catch {
toast.error(fallbackErrorMessage);
}
const toastError = async (response: Response) => {
const fallbackErrorMessage = t('errors.unknown_server_error');
try {
const data = await response.json<RequestErrorBody>();
toast.error([data.message, data.details].join('\n') || fallbackErrorMessage);
} catch {
toast.error(fallbackErrorMessage);
}
};
return toastError;
};
type Props = {
@ -38,6 +43,7 @@ type Props = {
const useApi = ({ hideErrorToast }: Props = {}) => {
const { isAuthenticated, getAccessToken } = useLogto();
const { i18n } = useTranslation();
const toastError = useToastError();
const api = useMemo(
() =>
@ -64,7 +70,7 @@ const useApi = ({ hideErrorToast }: Props = {}) => {
],
},
}),
[hideErrorToast, isAuthenticated, getAccessToken, i18n.language]
[hideErrorToast, toastError, isAuthenticated, getAccessToken, i18n.language]
);
return api;