refactor: 注册成功后重定向

pull/13/head
moonrailgun 4 years ago
parent 56cf913675
commit 6409aa012b

@ -0,0 +1,31 @@
import { useEffect, useState } from 'react';
const getValue = (search: string, param: string) =>
new URLSearchParams(search).get(param);
export type UseQueryParam = (param: string) => string | null;
export const useSearchParam: UseQueryParam = (param) => {
const location = window.location;
const [value, setValue] = useState<string | null>(() =>
getValue(location.search, param)
);
useEffect(() => {
const onChange = () => {
setValue(getValue(location.search, param));
};
window.addEventListener('popstate', onChange);
window.addEventListener('pushstate', onChange);
window.addEventListener('replacestate', onChange);
return () => {
window.removeEventListener('popstate', onChange);
window.removeEventListener('pushstate', onChange);
window.removeEventListener('replacestate', onChange);
};
}, []);
return value;
};

@ -6,6 +6,8 @@ import { Icon } from '@iconify/react';
import { useHistory } from 'react-router';
import { setUserJWT } from '../../utils/jwt-helper';
import { setGlobalUserLoginInfo } from '../../utils/user-helper';
import { useSearchParam } from '@/hooks/useSearchParam';
import { isValidStr } from '../../../../shared/utils/string-helper';
/**
*
@ -14,6 +16,7 @@ export const RegisterView: React.FC = React.memo(() => {
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
const history = useHistory();
const navRedirect = useSearchParam('redirect');
const [{ loading, error }, handleRegister] = useAsyncFn(async () => {
await string()
@ -30,8 +33,13 @@ export const RegisterView: React.FC = React.memo(() => {
setGlobalUserLoginInfo(data);
await setUserJWT(data.token);
history.push('/main');
}, [email, password]);
if (isValidStr(navRedirect)) {
history.push(decodeURIComponent(navRedirect));
} else {
history.push('/main');
}
}, [email, password, navRedirect]);
const toLoginView = useCallback(() => {
history.push('/entry/login');

Loading…
Cancel
Save