mirror of
https://github.com/muerwre/vault-frontend.git
synced 2025-04-24 20:36:40 +07:00
fixed error handling
This commit is contained in:
parent
f2e9095cd1
commit
35fbff0813
3 changed files with 28 additions and 18 deletions
|
@ -2,7 +2,12 @@ import { useCallback, useMemo } from 'react';
|
||||||
|
|
||||||
import useSWR from 'swr';
|
import useSWR from 'swr';
|
||||||
|
|
||||||
import { apiAttachSocial, apiDropSocial, apiGetSocials, apiLoginWithSocial } from '~/api/auth';
|
import {
|
||||||
|
apiAttachSocial,
|
||||||
|
apiDropSocial,
|
||||||
|
apiGetSocials,
|
||||||
|
apiLoginWithSocial,
|
||||||
|
} from '~/api/auth';
|
||||||
import { API } from '~/constants/api';
|
import { API } from '~/constants/api';
|
||||||
import { Dialog } from '~/constants/modal';
|
import { Dialog } from '~/constants/modal';
|
||||||
import { useAuth } from '~/hooks/auth/useAuth';
|
import { useAuth } from '~/hooks/auth/useAuth';
|
||||||
|
@ -15,13 +20,14 @@ export const useOAuth = () => {
|
||||||
const { isUser, setToken } = useAuth();
|
const { isUser, setToken } = useAuth();
|
||||||
const { showModal, hideModal } = useModal();
|
const { showModal, hideModal } = useModal();
|
||||||
|
|
||||||
const { data, isValidating: isLoading, mutate } = useSWR(
|
const {
|
||||||
isUser ? API.USER.GET_SOCIALS : null,
|
data,
|
||||||
async () => {
|
isValidating: isLoading,
|
||||||
const result = await apiGetSocials();
|
mutate,
|
||||||
return result.accounts;
|
} = useSWR(isUser ? API.USER.GET_SOCIALS : null, async () => {
|
||||||
}
|
const result = await apiGetSocials();
|
||||||
);
|
return result.accounts;
|
||||||
|
});
|
||||||
|
|
||||||
const openOauthWindow = useCallback((provider: OAuthProvider) => {
|
const openOauthWindow = useCallback((provider: OAuthProvider) => {
|
||||||
window.open(API.USER.OAUTH_WINDOW(provider), '', 'width=600,height=400');
|
window.open(API.USER.OAUTH_WINDOW(provider), '', 'width=600,height=400');
|
||||||
|
@ -37,10 +43,9 @@ export const useOAuth = () => {
|
||||||
setToken(result.token);
|
setToken(result.token);
|
||||||
hideModal();
|
hideModal();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const needsRegister: string | undefined = path(
|
console.log(path(['response'], error));
|
||||||
['response', 'data', 'needs_register'],
|
|
||||||
error
|
const needsRegister = path(['response', 'status'], error) === 428;
|
||||||
);
|
|
||||||
|
|
||||||
if (needsRegister && token) {
|
if (needsRegister && token) {
|
||||||
showModal(Dialog.LoginSocialRegister, { token });
|
showModal(Dialog.LoginSocialRegister, { token });
|
||||||
|
@ -50,7 +55,7 @@ export const useOAuth = () => {
|
||||||
showErrorToast(error);
|
showErrorToast(error);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[showModal, hideModal, setToken]
|
[showModal, hideModal, setToken],
|
||||||
);
|
);
|
||||||
|
|
||||||
const loginWithSocial = useCallback(
|
const loginWithSocial = useCallback(
|
||||||
|
@ -62,7 +67,7 @@ export const useOAuth = () => {
|
||||||
setToken(token);
|
setToken(token);
|
||||||
hideModal();
|
hideModal();
|
||||||
},
|
},
|
||||||
[setToken, hideModal]
|
[setToken, hideModal],
|
||||||
);
|
);
|
||||||
|
|
||||||
const attachAccount = useCallback(
|
const attachAccount = useCallback(
|
||||||
|
@ -76,7 +81,7 @@ export const useOAuth = () => {
|
||||||
showErrorToast(error);
|
showErrorToast(error);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[mutate]
|
[mutate],
|
||||||
);
|
);
|
||||||
|
|
||||||
const dropAccount = useCallback(
|
const dropAccount = useCallback(
|
||||||
|
@ -88,7 +93,7 @@ export const useOAuth = () => {
|
||||||
showErrorToast(error);
|
showErrorToast(error);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[mutate]
|
[mutate],
|
||||||
);
|
);
|
||||||
|
|
||||||
const accounts = useMemo(() => data || [], [data]);
|
const accounts = useMemo(() => data || [], [data]);
|
||||||
|
|
|
@ -6,6 +6,10 @@ export const getErrorMessage = (error: unknown): string | undefined => {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (path(['response', 'data', 'message'], error)) {
|
||||||
|
return path(['response', 'data', 'message'], error) as string;
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof error === 'string' && has(error, ERROR_LITERAL)) {
|
if (typeof error === 'string' && has(error, ERROR_LITERAL)) {
|
||||||
return ERROR_LITERAL[error];
|
return ERROR_LITERAL[error];
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,13 @@
|
||||||
import React from 'react';
|
import { CSSProperties } from 'react';
|
||||||
|
|
||||||
import { Toaster } from 'react-hot-toast';
|
import { Toaster } from 'react-hot-toast';
|
||||||
|
|
||||||
const containerStyle = {
|
const containerStyle: CSSProperties = {
|
||||||
top: 10,
|
top: 10,
|
||||||
left: 10,
|
left: 10,
|
||||||
bottom: 10,
|
bottom: 10,
|
||||||
right: 10,
|
right: 10,
|
||||||
|
textAlign: 'center',
|
||||||
};
|
};
|
||||||
|
|
||||||
export const ToastProvider = () => <Toaster containerStyle={containerStyle} />;
|
export const ToastProvider = () => <Toaster containerStyle={containerStyle} />;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue