Skip to content

Commit

Permalink
[#415] Ligretto: fix username validation (#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
margo-yunanova authored Aug 28, 2023
1 parent a66103d commit ba4b330
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
12 changes: 8 additions & 4 deletions apps/auth-front/src/pages/profile/ProfilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { AvatarDropzone } from '../../components/AvatarDropzone'
import type { ProfileFormValues } from './ProfilePage.types'
import { useCasServices } from '../../modules/cas-services'
import { useProfileRequest } from './useProfileRequest'
import { useProfileValidation } from './useProfileValidation'

interface ProfilePageProps {
onLoginSucceeded: ({ token }: { token: string }) => void
Expand Down Expand Up @@ -54,13 +55,16 @@ export const ProfilePage = memo<ProfilePageProps>(({ onLoginSucceeded }) => {
[profile, avatar, onLoginSucceeded, updateUserProfileService],
)

const validate = useProfileValidation()

return !isProfileLoading ? (
<Container component="main" maxWidth="xs">
<Header />
<Form<ProfileFormValues>
onSubmit={handleSubmit}
validate={validate}
initialValues={initialValues}
render={({ handleSubmit, submitError }) => (
render={({ handleSubmit, submitError, hasValidationErrors }) => (
<form onSubmit={handleSubmit}>
<Paper>
<AvatarDropzone avatarUrl={getAbsoluteUrl(profile?.avatarUrl || '')} onChange={setAvatar} />
Expand All @@ -82,14 +86,14 @@ export const ProfilePage = memo<ProfilePageProps>(({ onLoginSucceeded }) => {
id="username"
label={t.profile.username}
name="username"
error={!meta.modifiedSinceLastSubmit && Boolean(meta.error || meta.submitError)}
helperText={!meta.modifiedSinceLastSubmit && meta.submitError}
error={meta.error || (!meta.modifiedSinceLastSubmit && meta.submitError)}
helperText={meta.error || (!meta.modifiedSinceLastSubmit && meta.submitError)}
/>
)}
/>
{submitError}
<br />
<Button type="submit" fullWidth variant="contained" color="primary" size="large">
<Button type="submit" fullWidth variant="contained" color="primary" size="large" disabled={hasValidationErrors}>
{t.profile.save}
</Button>
</Paper>
Expand Down
4 changes: 4 additions & 0 deletions apps/auth-front/src/pages/profile/ProfilePage.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ export type ProfileFormSubmissionError = Partial<{
username: string
[FORM_ERROR]: string
}>

export type ProfileFormValidationErrors = Partial<{
username: string
}>
19 changes: 19 additions & 0 deletions apps/auth-front/src/pages/profile/useProfileValidation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { ProfileFormValidationErrors, ProfileFormValues } from './ProfilePage.types'
import { useCallback } from 'react'
import { t } from '../../utils/i18n'
import { stringLengthInRange } from '../../utils/stringLengthInRange'

const USERNAME_MIN_LENGTH = 2
const USERNAME_MAX_LENGTH = 20

export const useProfileValidation = () =>
useCallback((values: ProfileFormValues): ProfileFormValidationErrors => {
const errors: ProfileFormValidationErrors = {}
const isValidUsername = stringLengthInRange(values.username, { minLength: USERNAME_MIN_LENGTH, maxLength: USERNAME_MAX_LENGTH })

if (values.username && !isValidUsername) {
errors.username = t.validation.username(USERNAME_MIN_LENGTH, USERNAME_MAX_LENGTH)
}

return errors
}, [])

1 comment on commit ba4b330

@github-actions
Copy link

Choose a reason for hiding this comment

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

Coverage report for apps/ligretto-gameplay-backend

St.
Category Percentage Covered / Total
🔴 Statements 48.63% 373/767
🔴 Branches 24.53% 26/106
🔴 Functions 26.07% 55/211
🔴 Lines 46.27% 310/670

Test suite run success

12 tests passing in 1 suite.

Report generated by 🧪jest coverage report action from ba4b330

Please sign in to comment.