diff --git a/components/forms/PasswordEditForm.js b/components/forms/PasswordEditForm.js deleted file mode 100644 index f4480393..00000000 --- a/components/forms/PasswordEditForm.js +++ /dev/null @@ -1,68 +0,0 @@ -import { useRef } from 'react'; -import { Form, FormInput, FormButtons, PasswordField, Button } from 'react-basics'; -import { useApi } from 'next-basics'; -import { useMutation } from '@tanstack/react-query'; -import { getAuthToken } from 'lib/client'; -import styles from './Form.module.css'; - -export default function PasswordEditForm({ onSave, onClose }) { - const { post } = useApi(getAuthToken()); - const { mutate, error, isLoading } = useMutation(data => post('/account/change-password', data)); - const ref = useRef(null); - - const handleSubmit = async data => { - mutate(data, { - onSuccess: async () => { - onSave(); - }, - }); - }; - - const samePassword = value => { - if (value !== ref?.current?.getValues('new_password')) { - return "Passwords don't match"; - } - return true; - }; - - return ( - <> -
- > - ); -} diff --git a/components/forms/PasswordResetForm.js b/components/forms/PasswordResetForm.js deleted file mode 100644 index ac5df2f2..00000000 --- a/components/forms/PasswordResetForm.js +++ /dev/null @@ -1,52 +0,0 @@ -import { useRef } from 'react'; -import { Form, FormInput, FormButtons, PasswordField, Button } from 'react-basics'; -import { useApi } from 'next-basics'; -import { useMutation } from '@tanstack/react-query'; -import { getAuthToken } from 'lib/client'; -import styles from './Form.module.css'; - -export default function PasswordResetForm({ token, onSave }) { - const { post } = useApi(getAuthToken()); - const { mutate, error, isLoading } = useMutation(data => - post('/account/reset-password', { ...data, token }), - ); - const ref = useRef(null); - - const handleSubmit = async data => { - mutate(data, { - onSuccess: async () => { - onSave(); - }, - }); - }; - - const samePassword = value => { - if (value !== ref?.current?.getValues('new_password')) { - return "Passwords don't match"; - } - return true; - }; - - return ( - <> - - > - ); -} diff --git a/components/forms/ProfilePasswordForm.js b/components/forms/ProfilePasswordForm.js deleted file mode 100644 index a713342f..00000000 --- a/components/forms/ProfilePasswordForm.js +++ /dev/null @@ -1,69 +0,0 @@ -import { useMutation } from '@tanstack/react-query'; -import { getAuthToken } from 'lib/client'; -import { useApi } from 'next-basics'; -import { useRef } from 'react'; -import { SubmitButton, Form, FormButtons, FormInput, PasswordField } from 'react-basics'; -import styles from './UserForm.module.css'; -import useUser from 'hooks/useUser'; - -export default function UserPasswordForm({ onSave }) { - const { - user: { id }, - } = useUser(); - const { post } = useApi(getAuthToken()); - const { mutate, error } = useMutation(data => post(`/users/${id}/password`, data)); - const ref = useRef(null); - - const handleSubmit = async data => { - mutate(data, { - onSuccess: async () => { - onSave(); - }, - }); - }; - - const samePassword = value => { - if (value !== ref?.current?.getValues('new_password')) { - return "Passwords don't match"; - } - return true; - }; - - return ( - <> - - > - ); -} diff --git a/components/forms/UserPasswordForm.js b/components/forms/UserPasswordForm.js index b8fe4d1f..a5543352 100644 --- a/components/forms/UserPasswordForm.js +++ b/components/forms/UserPasswordForm.js @@ -1,25 +1,35 @@ +import { useRef } from 'react'; +import { Form, FormInput, FormButtons, PasswordField, Button } from 'react-basics'; +import { useApi } from 'next-basics'; import { useMutation } from '@tanstack/react-query'; import { getAuthToken } from 'lib/client'; -import { useApi } from 'next-basics'; -import { useRef } from 'react'; -import { SubmitButton, Form, FormButtons, FormInput, PasswordField } from 'react-basics'; import styles from './UserForm.module.css'; +import useUser from 'hooks/useUser'; -export default function UserPasswordForm({ data, onSave }) { - const { id } = data; +export default function UserPasswordForm({ onSave, userId }) { + const { + user: { id }, + } = useUser(); + + const isCurrentUser = !userId || id === userId; + const url = isCurrentUser ? `/users/${id}/password` : `/users/${id}`; const { post } = useApi(getAuthToken()); - const { mutate, error } = useMutation(({ password }) => post(`/users/${id}`, { password })); + const { mutate, error, isLoading } = useMutation(data => post(url, data)); const ref = useRef(null); const handleSubmit = async data => { - mutate( - { password: data.new_password }, - { - onSuccess: async () => { - onSave(); - }, + const payload = isCurrentUser + ? data + : { + password: data.new_password, + }; + + mutate(payload, { + onSuccess: async () => { + onSave(); + ref.current.reset(); }, - ); + }); }; const samePassword = value => { @@ -30,33 +40,42 @@ export default function UserPasswordForm({ data, onSave }) { }; return ( - <> - - > + )} +