-
Notifications
You must be signed in to change notification settings - Fork 1
Add newPassword page #230
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
base: master
Are you sure you want to change the base?
Add newPassword page #230
Changes from all commits
e2ad94f
c01f3a9
8d5a744
e9065e6
82a9e79
e1d8d4c
247c1dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { MutationHookOptions, MutationTuple, MutationResult } from '@apollo/client'; | ||
|
||
import UpdatePasswordMutation from 'graphql/mutations/updatePassword.graphql'; | ||
|
||
import type { UpdatePasswordVariables, UpdatePasswordData } from '../../types/user/updatePasswordApiType'; | ||
import useMutation from '../../hooks/useMutationHook'; | ||
|
||
type UpdatePasswordResponseData = { | ||
updatePassword: UpdatePasswordData; | ||
}; | ||
|
||
type UpdatePasswordRequestVariables = { | ||
input: UpdatePasswordVariables; | ||
}; | ||
|
||
type UpdatePasswordMutationOptions = MutationHookOptions<UpdatePasswordResponseData, UpdatePasswordRequestVariables>; | ||
|
||
type UpdatePasswordMutationTuple = MutationTuple<UpdatePasswordResponseData, UpdatePasswordRequestVariables>; | ||
|
||
export type UpdatePasswordMutationResult = MutationResult<UpdatePasswordResponseData>; | ||
|
||
const useUpdatePasswordMutation = (options: UpdatePasswordMutationOptions): UpdatePasswordMutationTuple => { | ||
return useMutation<UpdatePasswordResponseData, UpdatePasswordRequestVariables>(UpdatePasswordMutation, options); | ||
}; | ||
|
||
export default useUpdatePasswordMutation; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export type UpdatePasswordData = { | ||
accessToken: string; | ||
}; | ||
|
||
export type UpdatePasswordVariables = { | ||
password: string; | ||
resetToken: string | string[] | undefined; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React from 'react'; | ||
import Router from 'next/router'; | ||
|
||
import { withApolloClient } from 'lib/withApolloClient'; | ||
import WithAuth from 'lib/auth/withAuth'; | ||
|
||
import { HOME } from 'config/routes'; | ||
import { NotifierProvider } from 'contexts/NotifierContext'; | ||
|
||
import DefaultTemplate from 'components/shared/templates/DefaultTemplate'; | ||
import Notifier from 'components/shared/atoms/Notifier'; | ||
|
||
import NewPasswordForm from './components/NewPasswordForm'; | ||
|
||
import { PageContentWrapper } from './styled'; | ||
|
||
const NewPasswordPage = ({ query }) => { | ||
return ( | ||
<NotifierProvider> | ||
<DefaultTemplate testId="new-password-page"> | ||
<PageContentWrapper> | ||
<NewPasswordForm query={query} /> | ||
</PageContentWrapper> | ||
<Notifier /> | ||
</DefaultTemplate> | ||
</NotifierProvider> | ||
); | ||
}; | ||
|
||
NewPasswordPage.getInitialProps = ({ res, accessTokenManager }) => { | ||
if (accessTokenManager.accessToken) { | ||
if (res) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't know why, but in other files we use the following conditions There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now for all pages we check only res, so I think we can leave it |
||
res.redirect(302, HOME); | ||
} else { | ||
Router.push(HOME); | ||
} | ||
} | ||
return {}; | ||
}; | ||
|
||
export default withApolloClient(WithAuth(NewPasswordPage)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why don't you use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we can leave as is, cause almost all pages do not use this HOC. I'll take a look at this in next prs |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import React from 'react'; | ||
import { Form, Formik, FormikProps } from 'formik'; | ||
import * as Yup from 'yup'; | ||
import { useRouter } from 'next/router'; | ||
|
||
import useUpdatePassword from 'lib/apollo/hooks/actions/useUpdatePassword'; | ||
|
||
import Button from 'components/shared/atoms/Button'; | ||
import FormFieldInput from 'components/shared/atoms/FormField'; | ||
import Loader from 'components/shared/atoms/Loader'; | ||
import { FormFieldType } from 'types/formsType'; | ||
import passwordRegExp from 'config/passwordRegExp'; | ||
|
||
import { FieldWrapper, FormContentWrapper, SubmitButtonWrapper } from './styled'; | ||
|
||
const SignInValidationSchema = Yup.object().shape({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SignIn |
||
password: Yup.string() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. better create file with yup validation |
||
.required('This field is required') | ||
.trim() | ||
.min(6, 'The minimum password length is 6 characters') | ||
.matches(passwordRegExp, 'Password must contain upper and lower case characters and numbers'), | ||
passwordConfirmation: Yup.string() | ||
.oneOf([Yup.ref('password')], 'Password does not match') | ||
.required('This field is required'), | ||
}); | ||
|
||
type FormValues = { | ||
password: string; | ||
passwordConfirmation: string; | ||
}; | ||
|
||
const initialValues: FormValues = { | ||
password: '', | ||
passwordConfirmation: '', | ||
}; | ||
|
||
const SignInFormContent = ({ isSubmitting }: FormikProps<FormValues>) => ( | ||
<FormContentWrapper> | ||
<Form> | ||
<FieldWrapper> | ||
<FormFieldInput name="password" type={FormFieldType.password} label="New Password" /> | ||
</FieldWrapper> | ||
<FieldWrapper> | ||
<FormFieldInput name="passwordConfirmation" type={FormFieldType.password} label="New Password Confirmation" /> | ||
</FieldWrapper> | ||
<SubmitButtonWrapper> | ||
<Button type={FormFieldType.submit} testID="submit-button" disabled={isSubmitting}> | ||
Submit | ||
</Button> | ||
</SubmitButtonWrapper> | ||
</Form> | ||
</FormContentWrapper> | ||
); | ||
|
||
const NewPasswordForm = () => { | ||
const { query } = useRouter(); | ||
const { reset_token: resetToken } = query; | ||
const [updatePassword, updatePasswordState] = useUpdatePassword(); | ||
|
||
const onSubmit = async ({ password }: { password: string }) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. password: Omit<TData, 'resetToken'> There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TData = UpdatePasswordVariables |
||
await updatePassword({ password, resetToken }); | ||
}; | ||
|
||
return ( | ||
<> | ||
<Formik | ||
initialValues={initialValues} | ||
onSubmit={onSubmit} | ||
component={SignInFormContent} | ||
validationSchema={SignInValidationSchema} | ||
/> | ||
{updatePasswordState.loading && <Loader testId="form-loader">Loading...</Loader>} | ||
</> | ||
); | ||
}; | ||
|
||
export default NewPasswordForm; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './NewPasswordForm'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import styled from 'styled-components'; | ||
|
||
export const FormContentWrapper = styled.div` | ||
width: 40rem; | ||
`; | ||
|
||
export const FieldWrapper = styled.div` | ||
margin-top: 1rem; | ||
`; | ||
|
||
export const SubmitButtonWrapper = styled.div` | ||
margin-top: 2rem; | ||
`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from './NewPasswordPage'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import styled from 'styled-components'; | ||
|
||
export const PageContentWrapper = styled.div` | ||
display: flex; | ||
justify-content: center; | ||
`; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export default /^(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])([0-9A-Za-z#$@&!?.*^{}<>;,)(~'"=_%+-]+)$/; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
mutation UpdatePassword($input: UpdatePasswordInput!) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oyyy I didn't know what type for $input existed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm sorry, but I don't understand your point. I think we can leave as is for now |
||
updatePassword(input: $input) { | ||
accessToken | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. refreshToken |
||
refreshToken | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { useRouter } from 'next/router'; | ||
|
||
import { SIGNIN } from 'config/routes'; | ||
import { useNotifier } from 'contexts/NotifierContext'; | ||
|
||
import type { UpdatePasswordVariables } from 'api/types/user/updatePasswordApiType'; | ||
import type { UpdatePasswordMutationResult } from 'api/mutations/update/useUpdatePasswordMutation'; | ||
import useUpdatePasswordMutation from 'api/mutations/update/useUpdatePasswordMutation'; | ||
|
||
const useUpdatePassword = (): [(variables: UpdatePasswordVariables) => Promise<void>, UpdatePasswordMutationResult] => { | ||
const { setError, setSuccess } = useNotifier(); | ||
const router = useRouter(); | ||
|
||
const onCompleted = () => { | ||
setSuccess('Пароль успешно изменен'); | ||
setTimeout(() => router.push(SIGNIN), 1000); | ||
}; | ||
|
||
const [mutation, mutationResult] = useUpdatePasswordMutation({ | ||
onCompleted, | ||
}); | ||
|
||
const mutate = async ({ password, resetToken }: UpdatePasswordVariables) => { | ||
const updatePasswordInput = { password, resetToken }; | ||
|
||
try { | ||
await mutation({ variables: { input: updatePasswordInput } }); | ||
} catch (error) { | ||
if (setError) setError(error); | ||
} | ||
}; | ||
|
||
return [mutate, mutationResult]; | ||
}; | ||
|
||
export default useUpdatePassword; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { default } from 'components/pages/newPassword'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be the absolute path