2022-12-27 01:57:59 +01:00
|
|
|
import { useRef } from 'react';
|
2023-01-06 07:56:36 +01:00
|
|
|
import { Form, FormRow, FormInput, FormButtons, PasswordField, Button } from 'react-basics';
|
2022-12-28 05:20:44 +01:00
|
|
|
import useApi from 'hooks/useApi';
|
2022-12-27 01:57:59 +01:00
|
|
|
import useUser from 'hooks/useUser';
|
|
|
|
|
2022-12-28 21:37:09 +01:00
|
|
|
export default function UserPasswordForm({ onSave, onClose, userId }) {
|
2022-12-29 00:43:22 +01:00
|
|
|
const user = useUser();
|
2022-12-27 01:57:59 +01:00
|
|
|
|
2022-12-29 00:43:22 +01:00
|
|
|
const isCurrentUser = !userId || user?.id === userId;
|
|
|
|
const url = isCurrentUser ? `/users/${user?.id}/password` : `/users/${user?.id}`;
|
2022-12-29 00:49:28 +01:00
|
|
|
const { post, useMutation } = useApi();
|
2022-12-27 01:57:59 +01:00
|
|
|
const { mutate, error, isLoading } = useMutation(data => post(url, data));
|
|
|
|
const ref = useRef(null);
|
|
|
|
|
|
|
|
const handleSubmit = async data => {
|
|
|
|
const payload = isCurrentUser
|
|
|
|
? data
|
|
|
|
: {
|
2022-12-27 02:36:48 +01:00
|
|
|
password: data.newPassword,
|
2022-12-27 01:57:59 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
mutate(payload, {
|
|
|
|
onSuccess: async () => {
|
|
|
|
onSave();
|
|
|
|
ref.current.reset();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const samePassword = value => {
|
2022-12-27 02:36:48 +01:00
|
|
|
if (value !== ref?.current?.getValues('newPassword')) {
|
2022-12-27 01:57:59 +01:00
|
|
|
return "Passwords don't match";
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2023-01-11 17:33:43 +01:00
|
|
|
<Form ref={ref} onSubmit={handleSubmit} error={error}>
|
2022-12-27 01:57:59 +01:00
|
|
|
{isCurrentUser && (
|
2023-01-06 07:56:36 +01:00
|
|
|
<FormRow label="Current password">
|
|
|
|
<FormInput name="currentPassword" rules={{ required: 'Required' }}>
|
|
|
|
<PasswordField autoComplete="off" />
|
|
|
|
</FormInput>
|
|
|
|
</FormRow>
|
|
|
|
)}
|
|
|
|
<FormRow label="New password">
|
|
|
|
<FormInput
|
|
|
|
name="newPassword"
|
|
|
|
rules={{
|
|
|
|
required: 'Required',
|
|
|
|
minLength: { value: 8, message: 'Minimum length 8 characters' },
|
|
|
|
}}
|
|
|
|
>
|
2022-12-27 01:57:59 +01:00
|
|
|
<PasswordField autoComplete="off" />
|
|
|
|
</FormInput>
|
2023-01-06 07:56:36 +01:00
|
|
|
</FormRow>
|
|
|
|
<FormRow label="Confirm password">
|
|
|
|
<FormInput
|
|
|
|
name="confirmPassword"
|
|
|
|
rules={{
|
|
|
|
required: 'Required',
|
|
|
|
minLength: { value: 8, message: 'Minimum length 8 characters' },
|
|
|
|
validate: samePassword,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<PasswordField autoComplete="off" />
|
|
|
|
</FormInput>
|
|
|
|
</FormRow>
|
2022-12-27 01:57:59 +01:00
|
|
|
<FormButtons flex>
|
2022-12-28 21:37:09 +01:00
|
|
|
<Button type="submit" variant="primary" disabled={isLoading}>
|
2022-12-27 01:57:59 +01:00
|
|
|
Save
|
|
|
|
</Button>
|
2022-12-28 21:37:09 +01:00
|
|
|
<Button onClick={onClose}>Close</Button>
|
2022-12-27 01:57:59 +01:00
|
|
|
</FormButtons>
|
|
|
|
</Form>
|
|
|
|
);
|
|
|
|
}
|