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

Integrate form library #28

Merged
merged 5 commits into from
Feb 6, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@
"@material-ui/core": "^4.11.2",
"@material-ui/icons": "^4.11.2",
"@material-ui/lab": "^4.0.0-alpha.57",
"formik": "^2.2.6",
"i18next": "^19.8.4",
"mobx": "^6.0.4",
"mobx-react": "^7.0.5",
"next": "^10.0.5",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-i18next": "^11.8.5",
"sass": "^1.32.4"
"sass": "^1.32.4",
"yup": "^0.32.8"
},
"devDependencies": {
"@types/node": "^14.14.21",
"@types/react": "^17.0.0",
"@types/yup": "^0.29.11",
"@typescript-eslint/eslint-plugin": "^4.13.0",
"@typescript-eslint/parser": "^4.13.0",
"eslint": "^7.18.0",
Expand Down
6 changes: 6 additions & 0 deletions public/locales/bg/auth.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,11 @@
"forgotten-password": {
"instructions": "За да смените паролата си, моля въведете вашия email и ще ви изпратим потвърждение с инареукции."
}
},
"validation":{
"email": "Невалиден email",
"required": "Задължително поле",
"password-min": "Паролата трябва да бъде поне {{count}} символа",
"password-match": "Паролата не съвпада"
}
}
6 changes: 6 additions & 0 deletions public/locales/en/auth.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,11 @@
"forgotten-password": {
"instructions": "To reset your password, please type your email address below. We will then send you an email with instructions to follow."
}
},
"validation":{
"email": "Invalid email",
"required": "Required field",
"password-min": "Password should be at least {{count}} characters",
"password-match": "Password doesn't match"
}
}
48 changes: 37 additions & 11 deletions src/components/changePassword/ChangePasswordPage.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,42 @@
import React, { useState } from 'react'
import React from 'react'
import { useTranslation } from 'react-i18next'
import { Container, Grid, TextField, Button } from '@material-ui/core'
import { useFormik } from 'formik'
import * as yup from 'yup'

import Layout from 'components/layout/Layout'

export default function ChangePasswordPage() {
const { t } = useTranslation()
const [password, setPassword] = useState('')
const [confirmPassword, setConfirmPassword] = useState('')

function handleSubmit(event: React.FormEvent) {
event.preventDefault()
}
const ChangePasswordSchema = yup.object().shape({
password: yup
.string()
.min(6, t('auth:validation.password-min', { count: 6 }))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's gonna be better to handle the translation at the last step right before displaying the error.

Otherwise we risk to receive mixed translations as the messages are memoized.

We can handle similar validations in a central location as it's shown at https://github.com/jquense/yup#using-a-custom-locale-dictionary

This will also help us decompose the validation schema definition in a separate file, away from the actual form component implementation.

Not super important at this stage but in future can be confusing.

Peek.2021-02-06.12-05.mp4

.required(t('auth:validation.required')),
confirmPassword: yup
.string()
.required(t('auth:validation.required'))
.oneOf([yup.ref('password'), null], t('auth:validation.password-match')),
})

const formik = useFormik({
initialValues: {
password: '',
confirmPassword: '',
},
validationSchema: ChangePasswordSchema,
validateOnChange: false,
validateOnBlur: false,
onSubmit: (values) => {
return
},
})

return (
<Layout title={t('nav.changePassword')}>
<Container maxWidth="xs">
<form onSubmit={handleSubmit}>
<form onSubmit={formik.handleSubmit}>
<Grid container spacing={3}>
<Grid item xs={12}>
<TextField
Expand All @@ -26,8 +46,11 @@ export default function ChangePasswordPage() {
name="password"
size="small"
variant="outlined"
value={password}
onChange={(event) => setPassword(event.target.value)}
error={!!formik.errors && !!formik.errors.password}
helperText={formik.errors && formik.errors.password}
value={formik.values.password}
onBlur={formik.handleBlur}
onChange={formik.handleChange}
/>
</Grid>
<Grid item xs={12}>
Expand All @@ -38,8 +61,11 @@ export default function ChangePasswordPage() {
name="confirmPassword"
size="small"
variant="outlined"
value={confirmPassword}
onChange={(event) => setConfirmPassword(event.target.value)}
error={!!formik.errors && !!formik.errors.confirmPassword}
helperText={formik.errors && formik.errors.confirmPassword}
value={formik.values.confirmPassword}
onBlur={formik.handleBlur}
onChange={formik.handleChange}
/>
</Grid>
<Grid item xs={12}>
Expand Down
32 changes: 24 additions & 8 deletions src/components/forgottenPassword/ForgottenPasswordPage.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
import React, { useState } from 'react'
import React from 'react'
import { useTranslation } from 'react-i18next'
import { Typography, Container, Grid, TextField, Button } from '@material-ui/core'
import { useFormik } from 'formik'
import * as yup from 'yup'

import Layout from 'components/layout/Layout'

export default function ForgottenPasswordPage() {
const { t } = useTranslation()
const [email, setEmail] = useState('')

function handleSubmit(event: React.FormEvent) {
event.preventDefault()
}
const ForgottenPasswordSchema = yup.object().shape({
email: yup.string().email(t('auth:validation.email')).required(t('auth:validation.required')),
})

const formik = useFormik({
initialValues: {
email: '',
},
validationSchema: ForgottenPasswordSchema,
validateOnChange: false,
validateOnBlur: false,
onSubmit: (values) => {
return
},
})

return (
<Layout title={t('nav.forgottenPassword')}>
<Container maxWidth="xs">
<form onSubmit={handleSubmit}>
<form onSubmit={formik.handleSubmit}>
<Typography variant="body1" paragraph>
{t('auth:pages.forgotten-password.instructions')}
</Typography>
Expand All @@ -28,8 +41,11 @@ export default function ForgottenPasswordPage() {
name="email"
size="small"
variant="outlined"
value={email}
onChange={(event) => setEmail(event.target.value)}
error={!!formik.errors && !!formik.errors.email}
helperText={formik.errors && formik.errors.email}
value={formik.values.email}
onBlur={formik.handleBlur}
onChange={formik.handleChange}
/>
</Grid>
<Grid item xs={12}>
Expand Down
48 changes: 36 additions & 12 deletions src/components/login/LoginPage.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
import React, { useState } from 'react'
import React from 'react'
import { useTranslation } from 'react-i18next'
import { Container, Grid, TextField, Button, Box } from '@material-ui/core'
import { useFormik } from 'formik'
import * as yup from 'yup'

import { routes } from 'common/routes'
import Layout from 'components/layout/Layout'
import Link from 'components/shared/Link'

export default function LoginPage() {
const { t } = useTranslation()
const [email, setEmail] = useState('')
const [password, setPassword] = useState('')
const test = useTranslation()
console.log(test)
const t = test.t
const LoginSchema = yup.object().shape({
email: yup.string().email(t('auth:validation.email')).required(t('auth:validation.required')),
password: yup
.string()
.min(6, t('auth:validation.password-min', { count: 6 }))
.required(t('auth:validation.required')),
})

function handleSubmit(event: React.FormEvent) {
event.preventDefault()
}
const formik = useFormik({
initialValues: {
email: '',
password: '',
},
validationSchema: LoginSchema,
validateOnChange: false,
validateOnBlur: false,
onSubmit: (values) => {
return
},
})

return (
<Layout title={t('nav.login')}>
<Container maxWidth="xs">
<form onSubmit={handleSubmit}>
<form onSubmit={formik.handleSubmit}>
<Grid container spacing={3}>
<Grid item xs={12}>
<TextField
Expand All @@ -29,8 +47,11 @@ export default function LoginPage() {
size="small"
variant="outlined"
autoFocus
value={email}
onChange={(event) => setEmail(event.target.value)}
error={!!formik.errors && !!formik.errors.email}
helperText={formik.errors && formik.errors.email}
value={formik.values.email}
onBlur={formik.handleBlur}
onChange={formik.handleChange}
/>
</Grid>
<Grid item xs={12}>
Expand All @@ -41,8 +62,11 @@ export default function LoginPage() {
name="password"
size="small"
variant="outlined"
value={password}
onChange={(event) => setPassword(event.target.value)}
error={!!formik.errors && !!formik.errors.password}
helperText={formik.errors && formik.errors.password}
value={formik.values.password}
onBlur={formik.handleBlur}
onChange={formik.handleChange}
/>
</Grid>
<Grid item xs={12}>
Expand Down
65 changes: 48 additions & 17 deletions src/components/register/RegisterPage.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
import React, { useState } from 'react'
import React from 'react'
import { useTranslation } from 'react-i18next'
import { Container, Grid, TextField, Button } from '@material-ui/core'
import { useFormik } from 'formik'
import * as yup from 'yup'

import Layout from 'components/layout/Layout'

export default function RegisterPage() {
const { t } = useTranslation()
const [firstName, setFirstName] = useState('')
const [lastName, setLastName] = useState('')
const [email, setEmail] = useState('')
const [password, setPassword] = useState('')

function handleSubmit(event: React.FormEvent) {
event.preventDefault()
}
const RegisterSchema = yup.object().shape({
firstName: yup.string().required(t('auth:validation.required')),
lastName: yup.string().required(t('auth:validation.required')),
email: yup.string().email(t('auth:validation.email')).required(t('auth:validation.required')),
password: yup
.string()
.min(6, t('auth:validation.password-min', { count: 6 }))
.required(t('auth:validation.required')),
})

const formik = useFormik({
initialValues: {
firstName: '',
lastName: '',
email: '',
password: '',
},
validationSchema: RegisterSchema,
validateOnChange: false,
validateOnBlur: false,
onSubmit: (values) => {
return
},
})

return (
<Layout title={t('nav.register')}>
<Container maxWidth="xs">
<form onSubmit={handleSubmit}>
<form onSubmit={formik.handleSubmit}>
<Grid container spacing={3}>
<Grid item xs={12} sm={6}>
<TextField
Expand All @@ -29,8 +48,11 @@ export default function RegisterPage() {
size="small"
variant="outlined"
autoFocus
value={firstName}
onChange={(event) => setFirstName(event.target.value)}
error={!!formik.errors && !!formik.errors.firstName}
helperText={formik.errors && formik.errors.firstName}
value={formik.values.firstName}
onBlur={formik.handleBlur}
onChange={formik.handleChange}
/>
</Grid>
<Grid item xs={12} sm={6}>
Expand All @@ -41,8 +63,11 @@ export default function RegisterPage() {
name="lastName"
size="small"
variant="outlined"
value={lastName}
onChange={(event) => setLastName(event.target.value)}
error={!!formik.errors && !!formik.errors.lastName}
helperText={formik.errors && formik.errors.lastName}
value={formik.values.lastName}
onBlur={formik.handleBlur}
onChange={formik.handleChange}
/>
</Grid>
<Grid item xs={12}>
Expand All @@ -53,8 +78,11 @@ export default function RegisterPage() {
name="email"
size="small"
variant="outlined"
value={email}
onChange={(event) => setEmail(event.target.value)}
error={!!formik.errors && !!formik.errors.email}
helperText={formik.errors && formik.errors.email}
value={formik.values.email}
onBlur={formik.handleBlur}
onChange={formik.handleChange}
/>
</Grid>
<Grid item xs={12}>
Expand All @@ -65,8 +93,11 @@ export default function RegisterPage() {
name="password"
size="small"
variant="outlined"
value={password}
onChange={(event) => setPassword(event.target.value)}
error={!!formik.errors && !!formik.errors.password}
helperText={formik.errors && formik.errors.password}
value={formik.values.password}
onBlur={formik.handleBlur}
onChange={formik.handleChange}
/>
</Grid>
<Grid item xs={12}>
Expand Down
Loading