Skip to content

Commit

Permalink
fix: fixed biology search by code value - Ref gestion-de-projet#2216
Browse files Browse the repository at this point in the history
  • Loading branch information
ManelleG committed Aug 22, 2023
1 parent e7fe5bf commit 4f906dc
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import {
SearchByTypes,
SelectedCriteriaType,
CriteriaItemType,
CalendarRequestLabel
CalendarRequestLabel,
Comparators
} from 'types'

import docTypes from 'assets/docTypes.json'
Expand Down Expand Up @@ -806,12 +807,12 @@ const CriteriaCardContent: React.FC<CriteriaCardContentProps> = ({ currentCriter
_currentCriteria &&
_currentCriteria.isLeaf &&
_currentCriteria?.valueComparator &&
(_currentCriteria?.valueMin || _currentCriteria?.valueMax) && (
(typeof _currentCriteria?.valueMin === 'number' || typeof _currentCriteria?.valueMax === 'number') && (
<Chip
className={classes.criteriaChip}
label={
<Typography>
{_currentCriteria?.valueComparator === '<x>'
{_currentCriteria?.valueComparator === Comparators.BETWEEN
? `Valeur comprise entre ${_currentCriteria.valueMin} et ${_currentCriteria.valueMax}`
: `Valeur ${_currentCriteria.valueComparator} ${_currentCriteria.valueMin}`}
</Typography>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ import KeyboardBackspaceIcon from '@mui/icons-material/KeyboardBackspace'
import useStyles from './styles'
import { useAppDispatch, useAppSelector } from 'state'
import { fetchBiology } from 'state/biology'
import { CriteriaName, HierarchyTree } from 'types'
import { Comparators, CriteriaName, HierarchyTree, ObservationDataType } from 'types'
import OccurrencesNumberInputs from '../../../AdvancedInputs/OccurrencesInputs/OccurrenceNumberInputs'
import AdvancedInputs from '../../../AdvancedInputs/AdvancedInputs'

type BiologyFormProps = {
isOpen: boolean
isEdition: boolean
criteria: any
selectedCriteria: any
selectedCriteria: ObservationDataType
onChangeValue: (key: string, value: any) => void
goBack: (data: any) => void
onChangeSelectedCriteria: (data: any) => void
Expand All @@ -45,7 +45,9 @@ const BiologyForm: React.FC<BiologyFormProps> = (props) => {
const initialState: HierarchyTree | null = useAppSelector((state) => state.syncHierarchyTable)
const currentState = { ...selectedCriteria, ...initialState }
const [multiFields, setMultiFields] = useState<string | null>(localStorage.getItem('multiple_fields'))
const [allowSearchByValue, setAllowSearchByValue] = useState(false)
const [allowSearchByValue, setAllowSearchByValue] = useState(
typeof currentState.valueMin === 'number' || typeof currentState.valueMax === 'number'
)

const _onSubmit = () => {
onChangeSelectedCriteria(currentState)
Expand All @@ -67,7 +69,7 @@ const BiologyForm: React.FC<BiologyFormProps> = (props) => {
useEffect(() => {
const checkChildren = async () => {
try {
const getChildrenResp = await criteria.fetch.fetchBiologyHierarchy(currentState.code[0].id)
const getChildrenResp = await criteria.fetch.fetchBiologyHierarchy(currentState.code?.[0].id)

if (getChildrenResp.length > 0) {
if (currentState.isLeaf !== false) {
Expand All @@ -83,7 +85,7 @@ const BiologyForm: React.FC<BiologyFormProps> = (props) => {
}
}

if (currentState?.code.length === 1 && currentState?.code[0].id !== '*') {
if (currentState?.code?.length === 1 && currentState?.code[0].id !== '*') {
checkChildren()
} else {
if (currentState.isLeaf !== false) {
Expand Down Expand Up @@ -207,30 +209,36 @@ const BiologyForm: React.FC<BiologyFormProps> = (props) => {
<Grid
style={{
display: 'grid',
gridTemplateColumns: currentState.valueComparator === '<x>' ? '50px 1fr 1fr 1fr' : '50px 1fr 1fr',
gridTemplateColumns:
currentState.valueComparator === Comparators.BETWEEN ? '50px 1fr 1fr 1fr' : '50px 1fr 1fr',
alignItems: 'center',
marginTop: '1em'
}}
>
<Checkbox
checked={allowSearchByValue}
onClick={() => setAllowSearchByValue(!allowSearchByValue)}
onClick={() => {
if (allowSearchByValue) {
onChangeValue('valueMin', undefined)
onChangeValue('valueMin', undefined)
}
setAllowSearchByValue(!allowSearchByValue)
}}
disabled={!currentState.isLeaf}
/>

<Select
style={{ marginRight: '1em' }}
id="biology-value-comparator-select"
value={currentState.valueComparator}
onChange={(event) => onChangeValue('valueComparator', event.target.value as string)}
value={currentState.valueComparator ?? Comparators.GREATER_OR_EQUAL}
onChange={(event) => onChangeValue('valueComparator', event.target.value)}
disabled={!allowSearchByValue}
>
<MenuItem value={'<='}>{'<='}</MenuItem>
<MenuItem value={'<'}>{'<'}</MenuItem>
<MenuItem value={'='}>{'='}</MenuItem>
<MenuItem value={'>'}>{'>'}</MenuItem>
<MenuItem value={'>='}>{'>='}</MenuItem>
<MenuItem value={'<x>'}>{'< X >'}</MenuItem>
{(Object.keys(Comparators) as (keyof typeof Comparators)[]).map((key, index) => (
<MenuItem key={index} value={Comparators[key]}>
{Comparators[key]}
</MenuItem>
))}
</Select>

<TextField
Expand All @@ -242,11 +250,11 @@ const BiologyForm: React.FC<BiologyFormProps> = (props) => {
id="criteria-value"
variant="outlined"
value={currentState.valueMin}
onChange={(e) => onChangeValue('valueMin', e.target.value)}
placeholder={currentState.valueComparator === '<x>' ? 'Valeur minimale' : ''}
onChange={(e) => onChangeValue('valueMin', parseInt(e.target.value))}
placeholder={currentState.valueComparator === Comparators.BETWEEN ? 'Valeur minimale' : '0'}
disabled={!allowSearchByValue}
/>
{currentState.valueComparator === '<x>' && (
{currentState.valueComparator === Comparators.BETWEEN && (
<TextField
required
inputProps={{
Expand All @@ -256,7 +264,7 @@ const BiologyForm: React.FC<BiologyFormProps> = (props) => {
id="criteria-value"
variant="outlined"
value={currentState.valueMax}
onChange={(e) => onChangeValue('valueMax', e.target.value)}
onChange={(e) => onChangeValue('valueMax', parseInt(e.target.value))}
placeholder="Valeur maximale"
disabled={!allowSearchByValue}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import { initSyncHierarchyTableEffect, OBSERVATION, syncOnChangeFormValue } from
import { PmsiListType } from 'state/pmsi'
import { fetchBiology } from 'state/biology'
import { useAppDispatch, useAppSelector } from 'state'
import { Comparators } from 'types'

export const defaultBiology = {
type: OBSERVATION,
title: 'Critères de biologie',
code: [],
isLeaf: false,
valueMin: 0,
valueMax: 0,
valueComparator: '>=',
valueComparator: Comparators.GREATER_OR_EQUAL,
occurrence: 1,
occurrenceComparator: '>=',
startOccurrence: '',
Expand Down
15 changes: 12 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,15 @@ export enum CalendarRequestLabel {
DAY = 'jour(s)'
}

export enum Comparators {
LESS_OR_EQUAL = '<=',
LESS = '<',
EQUAL = '=',
GREATER = '>',
GREATER_OR_EQUAL = '>=',
BETWEEN = '<x>'
}

export type EncounterDataType = {
type: 'Encounter'
title: string
Expand Down Expand Up @@ -593,9 +602,9 @@ export type ObservationDataType = {
type: 'Observation'
code: { id: string; label: string }[] | null
isLeaf: boolean
valueMin: number
valueMax: number
valueComparator: '<=' | '<' | '=' | '>' | '>=' | '<x>'
valueMin?: number
valueMax?: number
valueComparator: Comparators
occurrence: number
occurrenceComparator: '<=' | '<' | '=' | '>' | '>='
startOccurrence: Date | null
Expand Down
47 changes: 23 additions & 24 deletions src/utils/cohortCreation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
SearchByTypes,
Calendar,
CalendarRequestLabel,
CalendarLabel
CalendarLabel,
Comparators
} from 'types'

import docTypes from 'assets/docTypes.json'
Expand Down Expand Up @@ -479,19 +480,19 @@ const constructFilterFhir = (criterion: SelectedCriteriaType): string => {
let valueComparatorFilter = ''
if (criterion.valueComparator) {
switch (criterion.valueComparator) {
case '<':
case Comparators.LESS:
valueComparatorFilter = 'lt'
break
case '<=':
case Comparators.LESS_OR_EQUAL:
valueComparatorFilter = 'le'
break
case '=':
case Comparators.EQUAL:
valueComparatorFilter = ''
break
case '>':
case Comparators.GREATER:
valueComparatorFilter = 'gt'
break
case '>=':
case Comparators.GREATER_OR_EQUAL:
valueComparatorFilter = 'ge'
break
default:
Expand Down Expand Up @@ -523,8 +524,8 @@ const constructFilterFhir = (criterion: SelectedCriteriaType): string => {
criterion.code &&
criterion.code.length === 1 &&
criterion.valueComparator &&
(criterion.valueMin || criterion.valueMax)
? criterion.valueComparator === '<x>' && criterion.valueMax
(typeof criterion.valueMin === 'number' || typeof criterion.valueMax === 'number')
? criterion.valueComparator === Comparators.BETWEEN && criterion.valueMax
? `${OBSERVATION_VALUE}=le${criterion.valueMin}&${OBSERVATION_VALUE}=ge${criterion.valueMax}`
: `${OBSERVATION_VALUE}=${valueComparatorFilter}${criterion.valueMin}`
: ''
Expand Down Expand Up @@ -1398,9 +1399,6 @@ export async function unbuildRequest(_json: string): Promise<any> {
currentCriterion.title = 'Critère de biologie'
currentCriterion.code = currentCriterion.code ? currentCriterion.code : []
currentCriterion.isLeaf = currentCriterion.isLeaf ? currentCriterion.isLeaf : false
currentCriterion.valueMin = currentCriterion.valueMin ? currentCriterion.valueMin : 0
currentCriterion.valueMax = currentCriterion.valueMax ? currentCriterion.valueMax : 0
currentCriterion.valueComparator = currentCriterion.valueComparator ? currentCriterion.valueComparator : '>='
currentCriterion.occurrence = currentCriterion.occurrence ? currentCriterion.occurrence : null
currentCriterion.startOccurrence = currentCriterion.startOccurrence ? currentCriterion.startOccurrence : null
currentCriterion.endOccurrence = currentCriterion.endOccurrence ? currentCriterion.endOccurrence : null
Expand Down Expand Up @@ -1461,28 +1459,29 @@ export async function unbuildRequest(_json: string): Promise<any> {

case OBSERVATION_VALUE: {
let valueComparator = ''
let valueMin = 0
let valueMax = 0
let valueMin
let valueMax

if (value?.search('le') === 0) {
valueComparator = '<='
valueMin = parseInt(value?.replace('le', '')) ?? 0
valueComparator = Comparators.LESS_OR_EQUAL
valueMin = parseInt(value?.replace('le', ''))
} else if (value?.search('lt') === 0) {
valueComparator = '<'
valueMin = parseInt(value?.replace('lt', '')) ?? 0
valueComparator = Comparators.LESS
valueMin = parseInt(value?.replace('lt', ''))
} else if (value?.search('ge') === 0) {
if (nbValueComparators === 2) {
valueComparator = '<x>'
valueMax = parseInt(value?.replace('ge', '')) ?? 0
valueComparator = Comparators.BETWEEN
valueMax = parseInt(value?.replace('ge', ''))
} else {
valueComparator = '>='
valueMin = parseInt(value?.replace('ge', '')) ?? 0
valueComparator = Comparators.GREATER_OR_EQUAL
valueMin = parseInt(value?.replace('ge', ''))
console.log('valueMin', valueMin)
}
} else if (value?.search('gt') === 0) {
valueComparator = '>'
valueMin = parseInt(value?.replace('gt', '')) ?? 0
valueComparator = Comparators.GREATER
valueMin = parseInt(value?.replace('gt', ''))
} else {
valueComparator = '='
valueComparator = Comparators.EQUAL
valueMin = parseInt(value ?? '0')
}

Expand Down

0 comments on commit 4f906dc

Please sign in to comment.