From e8f0ec1f36de72e7be78705b414ad3e07c937b68 Mon Sep 17 00:00:00 2001 From: Fedor Katurov Date: Sat, 8 Jan 2022 18:37:16 +0700 Subject: [PATCH] using formik on profile settings --- .../profile/ProfileSettings/index.tsx | 94 ++++++------------- src/hooks/profile/usePatchProfile.ts | 17 ++-- src/hooks/profile/useProfileForm.ts | 62 ++++++++++++ src/redux/auth/types.ts | 4 +- src/utils/providers/ProfileProvider.tsx | 4 +- 5 files changed, 106 insertions(+), 75 deletions(-) create mode 100644 src/hooks/profile/useProfileForm.ts diff --git a/src/components/profile/ProfileSettings/index.tsx b/src/components/profile/ProfileSettings/index.tsx index 4f54e3bb..8ca11c68 100644 --- a/src/components/profile/ProfileSettings/index.tsx +++ b/src/components/profile/ProfileSettings/index.tsx @@ -1,4 +1,4 @@ -import React, { FC, useCallback, useEffect, useState } from 'react'; +import React, { FC } from 'react'; import styles from './styles.module.scss'; import { Textarea } from '~/components/input/Textarea'; import { Button } from '~/components/input/Button'; @@ -10,62 +10,22 @@ import { ProfileAccounts } from '~/components/profile/ProfileAccounts'; import classNames from 'classnames'; import { useUser } from '~/hooks/user/userUser'; import { useProfileContext } from '~/utils/providers/ProfileProvider'; -import { showErrorToast } from '~/utils/errors/showToast'; -import { getValidationErrors } from '~/utils/errors/getValidationErrors'; -import { showToastSuccess } from '~/utils/toast'; -import { getRandomPhrase } from '~/constants/phrases'; +import { useProfileForm } from '~/hooks/profile/useProfileForm'; +import { has } from 'ramda'; + +const getError = (error?: string) => (error && has(error, ERROR_LITERAL) ? error : undefined); const ProfileSettings: FC = () => { - const [errors, setErrors] = useState>({}); const user = useUser(); const { updateProfile } = useProfileContext(); - const [password, setPassword] = useState(''); - const [new_password, setNewPassword] = useState(''); - - const [data, setData] = useState(user); - - const setDescription = useCallback(description => setData({ ...data, description }), [ - data, - setData, - ]); - - const setEmail = useCallback(email => setData({ ...data, email }), [data, setData]); - const setUsername = useCallback(username => setData({ ...data, username }), [data, setData]); - const setFullname = useCallback(fullname => setData({ ...data, fullname }), [data, setData]); - - const onSubmit = useCallback( - async event => { - try { - event.preventDefault(); - - const fields = { - ...data, - password: password.length > 0 && password ? password : undefined, - new_password: new_password.length > 0 && new_password ? new_password : undefined, - }; - - await updateProfile(fields); - - showToastSuccess(getRandomPhrase('SUCCESS')); - } catch (error) { - showErrorToast(error); - - const validationErrors = getValidationErrors(error); - if (validationErrors) { - setErrors(validationErrors); - } - } - }, - [data, password, new_password, updateProfile] + const { handleSubmit, values, errors, handleChange } = useProfileForm( + { ...user, password: '', newPassword: '' }, + updateProfile ); - useEffect(() => { - setErrors({}); - }, [password, new_password, data]); - return ( -
+
@@ -73,13 +33,17 @@ const ProfileSettings: FC = () => {
-