Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: replace formik with react hook form #24

Merged
merged 7 commits into from
May 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: create checkbox input component
  • Loading branch information
Saba-Var committed May 28, 2023
commit 5d8566ecdebc11b2da0983a306b8ef02e8115a54
16 changes: 7 additions & 9 deletions components/log-In/LogInForm/LogInForm.tsx
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import { FormProvider } from 'react-hook-form'
import { useLogInForm } from './useLogInForm'
import { useTranslate } from 'hooks'
import {
CheckboxInputField,
ForgetPassword,
SubmitButton,
InputField,
RememberMe,
} from 'components'

const LogInForm = () => {
const {
setRememberCheckbox,
submitHandler,
handleSubmit,
authorizing,
form,
} = useLogInForm()
const { submitHandler, handleSubmit, authorizing, form } = useLogInForm()

return (
<FormProvider {...form}>
Expand All @@ -25,7 +20,10 @@ const LogInForm = () => {
</div>

<div className='flex mb-4 mt-4 items-center justify-between'>
<RememberMe setRememberCheckbox={setRememberCheckbox} />
<CheckboxInputField
text={useTranslate('auth:remember-me')}
name='rememberMe'
/>
<ForgetPassword />
</div>

Expand Down
15 changes: 7 additions & 8 deletions components/log-In/LogInForm/useLogInForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,9 @@ import { authorization } from 'services'
import { setAccessToken } from 'slices'
import { useRouter } from 'next/router'
import { logInSchema } from 'schemas'
import { useState } from 'react'
import Cookies from 'js-cookie'

export const useLogInForm = () => {
const [rememberCheckbox, setRememberCheckbox] = useState(false)

const { mutate: submitForm, isLoading: authorizing } =
useMutation(authorization)

Expand All @@ -22,6 +19,7 @@ export const useLogInForm = () => {
const form = useForm({
resolver: yupResolver(logInSchema),
defaultValues: {
rememberMe: false,
password: '',
email: '',
},
Expand All @@ -32,17 +30,19 @@ export const useLogInForm = () => {

const submitHandler: SubmitHandler<SignInformValues> = (formValues) => {
submitForm(formValues, {
onSuccess: (response) => {
onSuccess: (response, variables: SignInformValues) => {
dispatch(setAccessToken(response?.data?.accessToken))

const { rememberMe } = variables

Cookies.set('id', response?.data?.id, {
expires: rememberCheckbox ? 30 : undefined,
expires: rememberMe ? 30 : undefined,
sameSite: 'Strict',
secure: true,
})

Cookies.set('remember-me', rememberCheckbox ? 'true' : 'false', {
expires: rememberCheckbox ? 30 : undefined,
Cookies.set('remember-me', rememberMe ? 'true' : 'false', {
expires: rememberMe ? 30 : undefined,
sameSite: 'Strict',
secure: true,
})
Expand Down Expand Up @@ -72,7 +72,6 @@ export const useLogInForm = () => {
}

return {
setRememberCheckbox,
submitHandler,
handleSubmit,
authorizing,
Expand Down
22 changes: 0 additions & 22 deletions components/log-In/RememberMe/RememberMe.tsx

This file was deleted.

1 change: 0 additions & 1 deletion components/log-In/RememberMe/index.ts

This file was deleted.

5 changes: 0 additions & 5 deletions components/log-In/RememberMe/types.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion components/log-In/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
export * from './ForgetPassword'
export * from './RememberMe'
export * from './LogInForm'
20 changes: 20 additions & 0 deletions components/shared/CheckboxInputField/CheckboxInputField.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import useCheckboxInputField from './useCheckboxInputField'
import { CheckboxInputFieldProps } from './types'

const CheckboxInputField: React.FC<CheckboxInputFieldProps> = (props) => {
const { register } = useCheckboxInputField()

return (
<label className='text-sm lg:text-base text-gray-900 flex gap-2 items-center cursor-pointer'>
<input
{...register(props.name)}
className='h-4 w-4 rounded border-gray-300 text-green-600 focus:ring-transparent'
name='remember-me'
type='checkbox'
/>
<span className='select-none'>{props.text}</span>
</label>
)
}

export default CheckboxInputField
1 change: 1 addition & 0 deletions components/shared/CheckboxInputField/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as CheckboxInputField } from './CheckboxInputField'
4 changes: 4 additions & 0 deletions components/shared/CheckboxInputField/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type CheckboxInputFieldProps = {
name: string
text: string
}
9 changes: 9 additions & 0 deletions components/shared/CheckboxInputField/useCheckboxInputField.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useFormContext } from 'react-hook-form'

const useCheckboxInputField = () => {
const { register } = useFormContext()

return { register }
}

export default useCheckboxInputField
1 change: 1 addition & 0 deletions components/shared/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './CheckboxInputField'
export * from './InputErrorMessage'
export * from './LanguageSelector'
export * from './LottieAnimation'
Expand Down
1 change: 1 addition & 0 deletions types/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type SignUpFormValues = {
}

export type SignInformValues = {
rememberMe: boolean
password: string
email: string
}
Expand Down