Skip to content

Commit

Permalink
fix: fixed bug in cohort creation title errors - Ref gestion-de-proje…
Browse files Browse the repository at this point in the history
…t#2268

* fix: fixed bug in cohort creation title errors - Ref gestion-de-projet#2268

* refactor: refacto of error gestion in cohort creation - Ref gestion-de-projet#2268

* refactor: refacto of error gestion in cohort creation - Ref gestion-de-projet#2268
  • Loading branch information
ManelleG authored Sep 19, 2023
1 parent 0b9f0a1 commit 1644655
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ import {
} from '@mui/material'

import useStyles from './styles'
import { CohortCreationError } from 'types'

const ERROR_TITLE = 'error_title'
const ERROR_DESCRIPTION = 'error_description'
const BLANK_REGEX = /^\s*$/
const ERROR_REGEX = 'error_regex'

const ModalCohortTitle: React.FC<{
onExecute?: (cohortName: string, cohortDescription: string, globalCount: boolean) => void
Expand All @@ -32,28 +30,33 @@ const ModalCohortTitle: React.FC<{
const [title, onChangeTitle] = useState('')
const [description, onChangeDescription] = useState('')
const [globalCount, onCheckGlobalCount] = useState(false)
const [error, setError] = useState<'error_title' | 'error_description' | 'error_regex' | null>(null)
const [error, setError] = useState<CohortCreationError | null>(null)
const [loading, setLoading] = useState(false)

const handleClose = () => onClose()

const handleConfirm = () => {
setLoading(true)
if (!title || (title && title.length > 255)) {
setLoading(false)
return setError(ERROR_TITLE)
}

if (title && BLANK_REGEX.test(title)) {
setLoading(false)
return setError(ERROR_REGEX)
if (error !== null) {
return
}
setLoading(true)

if (onExecute) {
onExecute(title, description, globalCount)
}
}

const handleTitleChange = (title: string) => {
onChangeTitle(title)
if (!title || title.length > 255) {
setError(CohortCreationError.ERROR_TITLE)
} else if (title && BLANK_REGEX.test(title)) {
setError(CohortCreationError.ERROR_REGEX)
} else {
setError(null)
}
}

return (
<Dialog fullWidth maxWidth="sm" open onClose={handleClose} aria-labelledby="form-dialog-title">
<DialogTitle>Création de la cohorte</DialogTitle>
Expand All @@ -63,18 +66,18 @@ const ModalCohortTitle: React.FC<{
<TextField
placeholder="Nom de la cohorte"
value={title}
onChange={(e) => onChangeTitle(e.target.value)}
onChange={(e) => handleTitleChange(e.target.value)}
autoFocus
id="title"
margin="normal"
fullWidth
error={error === ERROR_TITLE || error === ERROR_REGEX}
error={error !== null}
helperText={
error === ERROR_TITLE
error === CohortCreationError.ERROR_TITLE
? title.length === 0
? 'Le nom de la cohorte doit comporter au moins un caractère.'
: 'Le nom est trop long (255 caractères max.)'
: error === ERROR_REGEX
: error === CohortCreationError.ERROR_REGEX
? "Le nom de la cohorte ne peut pas être composé uniquement d'espaces."
: ''
}
Expand All @@ -93,7 +96,6 @@ const ModalCohortTitle: React.FC<{
multiline
minRows={5}
maxRows={8}
error={error === ERROR_DESCRIPTION}
/>
</Grid>

Expand Down Expand Up @@ -127,7 +129,7 @@ const ModalCohortTitle: React.FC<{
<Button disabled={loading} onClick={handleClose} color="secondary">
Annuler
</Button>
<Button disabled={loading} onClick={handleConfirm}>
<Button disabled={loading || error !== null} onClick={handleConfirm}>
Créer
</Button>
</DialogActions>
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ export enum CompositionStatusKind {
_enteredInError = 'entered-in-error'
}

export enum CohortCreationError {
ERROR_TITLE = 'error_title',
ERROR_REGEX = 'error_regex'
}

export type FHIR_API_Response<T extends FhirResource> = Bundle<T> | OperationOutcome

export type Back_API_Response<T> = {
Expand Down

0 comments on commit 1644655

Please sign in to comment.