Skip to content

Commit

Permalink
Make control unit resource type its name if no name is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
ivangabriele committed Oct 9, 2023
1 parent 41241d9 commit 9847389
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@ import {
IconButton,
THEME,
getOptionsFromIdAndName,
getOptionsFromLabelledEnum,
useKey
} from '@mtes-mct/monitor-ui'
import { Formik } from 'formik'
import { useCallback } from 'react'
import styled from 'styled-components'

import { CONTROL_UNIT_RESOURCE_FORM_SCHEMA } from './constants'
import { CONTROL_UNIT_RESOURCE_FORM_SCHEMA, CONTROL_UNIT_RESOURCE_TYPES_AS_OPTIONS } from './constants'
import { useGetBasesQuery } from '../../../../../api/basesAPI'
import { ControlUnit } from '../../../../../domain/entities/controlUnit'
import { useAppDispatch } from '../../../../../hooks/useAppDispatch'
import { mainWindowActions } from '../../../../MainWindow/slice'
import { MainWindowConfirmationModalActionType } from '../../../../MainWindow/types'
Expand All @@ -37,13 +35,6 @@ export function Form({ initialValues, isNew, onCancel, onSubmit }: FormProps) {
const { data: bases } = useGetBasesQuery()

const basesAsOptions = getOptionsFromIdAndName(bases)?.filter(baseAsOption => baseAsOption.value !== 0)
const controlUnitResourceTypesAsOptions = getOptionsFromLabelledEnum(
ControlUnit.ControlUnitResourceType,
true
).filter(
controlUnitResourceTypeAsOption =>
controlUnitResourceTypeAsOption.label !== ControlUnit.ControlUnitResourceType.UNKNOWN
)

const askForDeletionConfirmation = useCallback(async () => {
if (!initialValues.id) {
Expand Down Expand Up @@ -80,8 +71,8 @@ export function Form({ initialValues, isNew, onCancel, onSubmit }: FormProps) {
<>
<Title>Ajouter un moyen</Title>
<StyledForm onSubmit={handleSubmit}>
<FormikSelect isLight label="Type de moyen" name="type" options={CONTROL_UNIT_RESOURCE_TYPES_AS_OPTIONS} />
<FormikTextInput isLight label="Nom du moyen" name="name" />
<FormikSelect isLight label="Type de moyen" name="type" options={controlUnitResourceTypesAsOptions} />
<FormikSelect isLight label="Base du moyen" name="baseId" options={basesAsOptions} />
<FormikTextarea isLight label="Commentaire" name="note" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export function Item({ controlUnitResource, onEdit }: ItemProps) {
<InfoBox>
<InfoBoxHeader>
<div>
<Name>{ControlUnit.ControlUnitResourceType[controlUnitResource.type]}</Name>
<Name>
{ControlUnit.ControlUnitResourceType[controlUnitResource.type]}{controlUnitResource.name}
</Name>
<p>{controlUnitResource.base.name}</p>
</div>
<div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import { getOptionsFromLabelledEnum } from '@mtes-mct/monitor-ui'
import { object, string } from 'yup'

import { ControlUnit } from '../../../../../domain/entities/controlUnit'

import type { ControlUnitResourceFormValues } from './types'

export const CONTROL_UNIT_RESOURCE_FORM_SCHEMA = object().shape({
baseId: string().required('Veuillez choisir une base.'),
name: string().required('Veuillez entrer un nom.'),
type: string().required('Veuillez choisir un type.')
})

export const CONTROL_UNIT_RESOURCE_TYPES_AS_OPTIONS = getOptionsFromLabelledEnum(
ControlUnit.ControlUnitResourceType,
true
).filter(
controlUnitResourceTypeAsOption =>
controlUnitResourceTypeAsOption.label !== ControlUnit.ControlUnitResourceType.UNKNOWN
)

export const INITIAL_CONTROL_UNIT_RESOURCE_FORM_VALUES: ControlUnitResourceFormValues = {
baseId: undefined,
controlUnitId: undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,18 @@ export function ControlUnitResourceList({ controlUnit }: ControlUnitResourceList

const createOrUpdateControlUnitResource = useCallback(
async (controlUnitResourceFormValues: ControlUnitResourceFormValues) => {
const controlledControlUnitResourceFormValues = {
...controlUnitResourceFormValues,
// We set the resource type as the resource name if no name has been provided by the user
name: controlUnitResourceFormValues.name || controlUnitResourceFormValues.type
}

if (isNewControlUnitResourceFormOpen) {
await createControlUnitResource(controlUnitResourceFormValues as ControlUnit.NewControlUnitResourceData)
await createControlUnitResource(
controlledControlUnitResourceFormValues as ControlUnit.NewControlUnitResourceData
)
} else {
await updateControlUnitResource(controlUnitResourceFormValues as ControlUnit.ControlUnitResourceData)
await updateControlUnitResource(controlledControlUnitResourceFormValues as ControlUnit.ControlUnitResourceData)
}

closeForm()
Expand Down

0 comments on commit 9847389

Please sign in to comment.