Skip to content

Commit

Permalink
fix: fix store when revieve response from socket - Ref gestion-de-pro…
Browse files Browse the repository at this point in the history
…jet#2469
  • Loading branch information
Mehdi-BOUYAHIA committed Jul 4, 2024
1 parent 3946210 commit 1dd5a6a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 39 deletions.
63 changes: 25 additions & 38 deletions src/components/CreationCohort/ControlPanel/ControlPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import ShareIcon from '@mui/icons-material/Share'
import SupervisedUserCircleIcon from '@mui/icons-material/SupervisedUserCircle'
import UpdateSharpIcon from '@mui/icons-material/UpdateSharp'

import { WebSocketJobStatus } from 'types'

import ModalCohortTitle from '../Modals/ModalCohortTitle/ModalCohortTitle'
import ModalShareRequest from 'components/Requests/Modals/ModalShareRequest/ModalShareRequest'

Expand All @@ -35,7 +37,8 @@ import {
deleteCriteriaGroup,
buildCohortCreation,
unbuildCohortCreation,
addActionToNavHistory
addActionToNavHistory,
updateCount
} from 'state/cohortCreation'

import {
Expand All @@ -46,15 +49,13 @@ import {
RequestType,
SimpleStatus,
Snapshot,
WebSocketJobName,
WebSocketMessage
} from 'types'

import useStyle from './styles'

import displayDigit from 'utils/displayDigit'
import { ODD_FEASABILITY_REPORT, SHORT_COHORT_LIMIT } from '../../../constants'
import { WebSocketJobStatus } from 'types'
import services from 'services/aphp'
import ValidationDialog from 'components/ui/ValidationDialog'
import { JToolComponentEggWrapper } from 'components/Impersonation/JTool'
Expand Down Expand Up @@ -101,10 +102,6 @@ const ControlPanel: React.FC<{
} = useAppSelector((state) => state.cohortCreation.request || {})
const { uuid, includePatient, status, jobFailMsg } = count

const [patientCount, setPatientCount] = useState(includePatient)
const [countError, setCountError] = useState<string | undefined>(jobFailMsg)
const [countStatus, setCountStatus] = useState<string | undefined>(status)

const [requestShare, setRequestShare] = useState<RequestType | null>({
currentSnapshot,
requestId,
Expand Down Expand Up @@ -222,30 +219,20 @@ const ControlPanel: React.FC<{
setCountLoading(LoadingStatus.FETCHING)
dispatch(countCohortCreation({ uuid: uuid }))
}
setCountStatus(status)
setCountError(jobFailMsg)
}, [dispatch, status, uuid, jobFailMsg])

useEffect(() => {
setPatientCount(includePatient)
}, [includePatient])
}, [dispatch, status, uuid, jobFailMsg, includePatient])

useEffect(() => {
const listener = (message: WebSocketMessage) => {
if (
message.job_name === WebSocketJobName.COUNT &&
message.status === WebSocketJobStatus.finished &&
message.extra_info?.measure !== null
) {
setPatientCount(message.extra_info?.measure)
setCountLoading(LoadingStatus.SUCCESS)
}
if (message.job_name === WebSocketJobName.COUNT && message.status === WebSocketJobStatus.failed) {
setCountStatus('failed')
setPatientCount(message.extra_info?.measure)
setCountError(message.extra_info?.request_job_fail_msg)
let response = {}
if (message.status !== WebSocketJobStatus.pending) {
setCountLoading(LoadingStatus.SUCCESS)
response = {
includePatient: message.extra_info?.measure,
status: message.status,
jobFailMsg: message.extra_info?.request_job_fail_msg
}
}
dispatch(updateCount(response))
}

webSocketContext?.addListener(listener)
Expand All @@ -262,7 +249,7 @@ const ControlPanel: React.FC<{
typeof onExecute !== 'function' ||
maintenanceIsActive ||
count_outdated ||
patientCount === 0
includePatient === 0
}
onClick={() => onSetOpenModal('executeCohortConfirmation')}
className={classes.requestExecution}
Expand Down Expand Up @@ -378,19 +365,19 @@ const ControlPanel: React.FC<{
) : (
<Grid container alignItems="center" style={{ width: 'fit-content' }}>
<Typography className={cx(classes.boldText, classes.patientTypo, classes.blueText)}>
{displayDigit(patientCount)}
{displayDigit(includePatient)}
{oldCount !== null && !!oldCount.includePatient
? (patientCount ?? 0) - oldCount.includePatient > 0
? ` (+${(patientCount ?? 0) - oldCount.includePatient})`
: ` (${(patientCount ?? 0) - oldCount.includePatient})`
? (includePatient ?? 0) - oldCount.includePatient > 0
? ` (+${(includePatient ?? 0) - oldCount.includePatient})`
: ` (${(includePatient ?? 0) - oldCount.includePatient})`
: ''}
</Typography>
{oldCount !== null && !!oldCount.includePatient && (
<Tooltip
title={`Le delta ${
(patientCount ?? 0) - oldCount.includePatient > 0
? ` (+${(patientCount ?? 0) - oldCount.includePatient})`
: ` (${(patientCount ?? 0) - oldCount.includePatient})`
(includePatient ?? 0) - oldCount.includePatient > 0
? ` (+${(includePatient ?? 0) - oldCount.includePatient})`
: ` (${(includePatient ?? 0) - oldCount.includePatient})`
} est la différence de patient entre le ${lastUpdatedOldCount?.format(
'DD/MM/YYYY'
)} et la date du jour.`}
Expand All @@ -402,7 +389,7 @@ const ControlPanel: React.FC<{
)}
</Grid>
</Grid>
{!status && !patientCount && (
{!status && !includePatient && (
<Alert className={classes.errorAlert} severity="info">
Votre requête ne contient pas de nombre de patient.
<br />
Expand All @@ -418,11 +405,11 @@ const ControlPanel: React.FC<{
</Button>
</Alert>
)}
{(countStatus === 'failed' || countStatus === 'error') && (
{(status === 'failed' || status === 'error') && (
<Alert className={classes.errorAlert} severity="error">
Une erreur est survenue lors du calcul du nombre de patients de votre requête.
<br />
<Typography style={{ wordBreak: 'break-all' }}>{countError}</Typography>
<Typography style={{ wordBreak: 'break-all' }}>{jobFailMsg}</Typography>
<br />
<Button
onClick={() => _relaunchCount(false)}
Expand Down Expand Up @@ -510,7 +497,7 @@ const ControlPanel: React.FC<{
<ModalCohortTitle
onExecute={onExecute}
onClose={() => onSetOpenModal(null)}
longCohort={patientCount ? patientCount > cohortLimit : false}
longCohort={includePatient ? includePatient > cohortLimit : false}
cohortLimit={cohortLimit}
/>
)}
Expand Down
12 changes: 11 additions & 1 deletion src/state/cohortCreation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
QuerySnapshotInfo,
CurrentSnapshot,
CohortJobStatus,
CriteriaGroupType
CriteriaGroupType,
WebSocketMessage
} from 'types'

import { buildRequest, unbuildRequest, joinRequest, checkNominativeCriteria } from 'utils/cohortCreation'
Expand Down Expand Up @@ -704,6 +705,14 @@ const cohortCreationSlice = createSlice({
status: CohortJobStatus.PENDING
}
},
updateCount: (state: CohortCreationState, action: PayloadAction<CohortCreationCounterType>) => {
state.count = {
...state.count,
status: action.payload.status,
includePatient: action.payload.includePatient,
jobFailMsg: action.payload.jobFailMsg
}
},
addActionToNavHistory: (state: CohortCreationState, action: PayloadAction<CurrentSnapshot>) => {
let navHistory = state.navHistory
const newSnapshot = action.payload
Expand Down Expand Up @@ -801,5 +810,6 @@ export const {
deleteTemporalConstraint,
suspendCount,
unsuspendCount,
updateCount,
addActionToNavHistory
} = cohortCreationSlice.actions

0 comments on commit 1dd5a6a

Please sign in to comment.