Skip to content

Commit

Permalink
Merge branch 'refactor/register-form' of https://github.com/BesLogic/…
Browse files Browse the repository at this point in the history
…releaf-canopeum into refactor/register-form
  • Loading branch information
Samuel-Therrien-Beslogic committed Dec 17, 2024
2 parents 3446d7d + db8f205 commit c02d395
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
4 changes: 0 additions & 4 deletions canopeum_backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ dev = [
"types-jsonschema",
]

[tool.pyright]
venvPath = "."
venv = ".venv"

# https://mypy.readthedocs.io/en/stable/config_file.html
[tool.mypy]
show_column_numbers = true
Expand Down
5 changes: 5 additions & 0 deletions canopeum_frontend/src/constants/style.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const invalidFieldClass = 'is-invalid'

export const formClasses = {
invalidFieldClass,
}
28 changes: 14 additions & 14 deletions canopeum_frontend/src/pages/Register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import { Link, useSearchParams } from 'react-router-dom'
import AuthPageLayout from '@components/auth/AuthPageLayout'
import { AuthenticationContext } from '@components/context/AuthenticationContext'
import { appRoutes } from '@constants/routes.constant'
import { formClasses } from '@constants/style'
import useApiClient from '@hooks/ApiClientHook'
import type { UserInvitation } from '@services/api'
import { RegisterUser } from '@services/api'
import { storeToken } from '@utils/auth.utils'
import { passwordRegex } from '@utils/validators'
import { emailRegex, passwordRegex } from '@utils/validators'

type RegisterInputs = {
type RegisterFormInputs = {
username: string,
email: string,
password: string,
Expand All @@ -37,15 +38,15 @@ const Register = () => {
handleSubmit,
formState: { errors, touchedFields },
setValue,
} = useForm<RegisterInputs>({ mode: 'onTouched' })
const onSubmit: SubmitHandler<RegisterInputs> = async data => {
} = useForm<RegisterFormInputs>({ mode: 'onTouched' })
const onSubmit: SubmitHandler<RegisterFormInputs> = async formData => {
try {
const response = await getApiClient().authenticationClient.register(
new RegisterUser({
email: data.email.trim(),
username: data.username.trim(),
password: data.password,
passwordConfirmation: data.confirmPassword,
email: formData.email.trim(),
username: formData.username.trim(),
password: formData.password,
passwordConfirmation: formData.confirmPassword,
code: userInvitation?.code,
}),
)
Expand All @@ -60,8 +61,6 @@ const Register = () => {
}
}

const invalidFieldClass = 'is-invalid'

const fetchUserInvitation = useCallback(
async (code: string) => {
try {
Expand Down Expand Up @@ -111,7 +110,7 @@ const Register = () => {
aria-describedby='emailHelp'
className={`form-control ${
touchedFields.username && errors.username
? invalidFieldClass
? formClasses.invalidFieldClass
: ''
}`}
{...register('username', {
Expand All @@ -130,14 +129,15 @@ const Register = () => {
aria-describedby='email'
className={`form-control ${
touchedFields.email && errors.email
? invalidFieldClass
? formClasses.invalidFieldClass
: ''
}`}
disabled={!!userInvitation}
id='email'
type='email'
{...register('email', {
required: { value: true, message: translate('auth.email-error-required') },
pattern: { value: emailRegex, message: translate('auth.email-error-format') },
})}
/>
{errors.email && (
Expand All @@ -151,7 +151,7 @@ const Register = () => {
<input
className={`form-control ${
touchedFields.password && errors.password
? invalidFieldClass
? formClasses.invalidFieldClass
: ''
}`}
id='password-input'
Expand All @@ -174,7 +174,7 @@ const Register = () => {
<input
className={`form-control ${
touchedFields.confirmPassword && errors.confirmPassword
? invalidFieldClass
? formClasses.invalidFieldClass
: ''
}`}
id='confirmation-password-input'
Expand Down
4 changes: 2 additions & 2 deletions canopeum_frontend/src/utils/validators.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export const passwordRegex = /^(?=.*[A-Za-z])(?=.*\d)[\d!#$%&*.?@A-Za-z]{8,}$/u
export const passwordRegex = /^(?=.*[A-Za-z])(?=.*\d)(?=.{8}).+$/u

export const isValidPassword = (input: string) => new RegExp(passwordRegex).test(input)

const emailRegex = /^[^@]+@[^@][^.@]*\.[^@]+$/u
export const emailRegex = /^[^@]+@[^@][^.@]*\.[^@]+$/u

export const isValidEmail = (input: string) => new RegExp(emailRegex).test(input)

Expand Down

0 comments on commit c02d395

Please sign in to comment.