1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-09-28 17:55:15 +02:00
unleash.unleash/frontend/src/component/user/ResetPassword/ResetPassword.tsx
Nuno Góis 4167a60588
feat: biome lint frontend (#4903)
Follows up on https://github.com/Unleash/unleash/pull/4853 to add Biome
to the frontend as well.


![image](https://github.com/Unleash/unleash/assets/14320932/1906faf1-fc29-4172-a4d4-b2716d72cd65)

Added a few `biome-ignore` to speed up the process but we may want to
check and fix them in the future.
2023-10-02 13:25:46 +01:00

80 lines
3.0 KiB
TypeScript

import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { OK } from 'constants/statusCodes';
import useLoading from 'hooks/useLoading';
import { styled, Typography } from '@mui/material';
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
import InvalidToken from '../common/InvalidToken/InvalidToken';
import useResetPassword from 'hooks/api/getters/useResetPassword/useResetPassword';
import StandaloneLayout from '../common/StandaloneLayout';
import ResetPasswordForm from '../common/ResetPasswordForm/ResetPasswordForm';
import ResetPasswordError from '../common/ResetPasswordError/ResetPasswordError';
import { useAuthResetPasswordApi } from 'hooks/api/actions/useAuthResetPasswordApi/useAuthResetPasswordApi';
import { useAuthDetails } from 'hooks/api/getters/useAuth/useAuthDetails';
const StyledDiv = styled('div')(({ theme }) => ({
width: '350px',
maxWidth: '350px',
[theme.breakpoints.down('sm')]: {
width: '100%',
},
}));
const StyledTypography = styled(Typography)(({ theme }) => ({
fontWeight: 'bold',
fontSize: theme.spacing(2.5),
marginBottom: theme.spacing(2),
}));
const ResetPassword = () => {
const { token, loading, isValidToken } = useResetPassword();
const { resetPassword, loading: actionLoading } = useAuthResetPasswordApi();
const { authDetails } = useAuthDetails();
const ref = useLoading(loading || actionLoading);
const navigate = useNavigate();
const [hasApiError, setHasApiError] = useState(false);
const passwordDisabled = authDetails?.defaultHidden === true;
const onSubmit = async (password: string) => {
try {
const res = await resetPassword({ token, password });
if (res.status === OK) {
navigate('/login?reset=true');
setHasApiError(false);
} else {
setHasApiError(true);
}
} catch (e) {
setHasApiError(true);
}
};
return (
<div ref={ref}>
<StandaloneLayout>
<StyledDiv>
<ConditionallyRender
condition={!isValidToken || passwordDisabled}
show={<InvalidToken />}
elseShow={
<>
<StyledTypography variant='h2' data-loading>
Reset password
</StyledTypography>
<ConditionallyRender
condition={hasApiError}
show={<ResetPasswordError />}
/>
<ResetPasswordForm onSubmit={onSubmit} />
</>
}
/>
</StyledDiv>
</StandaloneLayout>
</div>
);
};
export default ResetPassword;