refactor: setup admin
This commit is contained in:
@@ -73,8 +73,8 @@ const AdminAccount: FC<Props> = ({ serverName, nextStep }) => {
|
|||||||
const loggedIn = useAppSelector((store) => !!store.authData.token);
|
const loggedIn = useAppSelector((store) => !!store.authData.token);
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
|
|
||||||
const [createAdmin, { isLoading: isSigningUp, error: signUpError }] = useCreateAdminMutation();
|
const [createAdmin, { isLoading: isSigningUp, isError: signUpError, isSuccess: signUpOk }] = useCreateAdminMutation();
|
||||||
const [login, { isLoading: isLoggingIn, error: loginError }] = useLoginMutation();
|
const [login, { isLoading: isLoggingIn, isError: loginError, }] = useLoginMutation();
|
||||||
const { data: serverData } = useGetServerQuery();
|
const { data: serverData } = useGetServerQuery();
|
||||||
const [updateServer, { isLoading: isUpdatingServer, isSuccess: isUpdatedServer }] =
|
const [updateServer, { isLoading: isUpdatingServer, isSuccess: isUpdatedServer }] =
|
||||||
useUpdateServerMutation();
|
useUpdateServerMutation();
|
||||||
@@ -85,12 +85,23 @@ const AdminAccount: FC<Props> = ({ serverName, nextStep }) => {
|
|||||||
|
|
||||||
// Display error
|
// Display error
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (signUpError === undefined) return;
|
if (signUpError) {
|
||||||
toast.error(`Failed to sign up: ${signUpError.data}`);
|
toast.error(`Failed to sign up`);
|
||||||
|
}
|
||||||
}, [signUpError]);
|
}, [signUpError]);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (loginError === undefined) return;
|
if (signUpOk) {
|
||||||
toast.error(`Login failed: ${loginError.data}`);
|
login({
|
||||||
|
email,
|
||||||
|
password,
|
||||||
|
type: "password"
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, [signUpOk]);
|
||||||
|
useEffect(() => {
|
||||||
|
if (loginError) {
|
||||||
|
toast.error(`Login failed`);
|
||||||
|
}
|
||||||
}, [loginError]);
|
}, [loginError]);
|
||||||
|
|
||||||
// After logged in
|
// After logged in
|
||||||
@@ -150,17 +161,13 @@ const AdminAccount: FC<Props> = ({ serverName, nextStep }) => {
|
|||||||
toast.error("Two passwords do not match!");
|
toast.error("Two passwords do not match!");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await createAdmin({
|
createAdmin({
|
||||||
email,
|
email,
|
||||||
name: "Admin",
|
name: "Admin",
|
||||||
password,
|
password,
|
||||||
gender: 0
|
gender: 0
|
||||||
});
|
});
|
||||||
await login({
|
|
||||||
email,
|
|
||||||
password,
|
|
||||||
type: "password"
|
|
||||||
});
|
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{!(isSigningUp || isLoggingIn || isUpdatingServer) ? t("sign") : "..."}
|
{!(isSigningUp || isLoggingIn || isUpdatingServer) ? t("sign") : "..."}
|
||||||
|
|||||||
Reference in New Issue
Block a user