Skip to content

Commit

Permalink
fix: fix double call into population card
Browse files Browse the repository at this point in the history
  • Loading branch information
Mehdi-BOUYAHIA committed Apr 12, 2023
1 parent 8da18d3 commit e7591b4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react'
import React, { useState, useEffect, useRef } from 'react'

import { Button, IconButton, Chip, CircularProgress, Typography } from '@mui/material'

Expand All @@ -16,13 +16,13 @@ import { MeState } from 'state/me'

import { ScopeTreeRow } from 'types'
import { getSelectedScopes, filterScopeTree } from 'utils/scopeTree'
import servicesPerimeters from 'services/aphp/servicePerimeters'

import useStyles from './styles'

const PopulationCard: React.FC = () => {
const classes = useStyles()
const dispatch = useAppDispatch()
const isRendered = useRef<boolean>(false)

const {
request: { selectedPopulation = [], ...requestState },
Expand Down Expand Up @@ -56,9 +56,7 @@ const PopulationCard: React.FC = () => {
subItems: []
}))

const allowSearchIpp = await servicesPerimeters.allowSearchIpp(_selectedPopulations)

dispatch<any>(buildCohortCreation({ selectedPopulation: _selectedPopulations, allowSearchIpp: allowSearchIpp }))
dispatch<any>(buildCohortCreation({ selectedPopulation: _selectedPopulations }))
onChangeOpenDrawer(false)
}

Expand Down Expand Up @@ -87,14 +85,17 @@ const PopulationCard: React.FC = () => {

useEffect(() => {
if (
!isRendered.current &&
!openDrawer &&
scopesList &&
scopesList.length === 1 &&
requestState.requestId &&
(selectedPopulation === null || (selectedPopulation && selectedPopulation.length === 0))
scopesList?.length === 1 &&
requestState?.requestId &&
(selectedPopulation === null || selectedPopulation?.length === 0)
) {
const savedSelectedItems: ScopeTreeRow[] = getSelectedScopes(scopesList[0], [], scopesList)
submitPopulation(savedSelectedItems)
isRendered.current = true
} else {
isRendered.current = false
}
}, [scopesList, requestState])

Expand Down
8 changes: 3 additions & 5 deletions src/state/cohortCreation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ type BuildCohortReturn = {
}
type BuildCohortParams = {
selectedPopulation?: ScopeTreeRow[] | null
allowSearchIpp?: boolean
}
const buildCohortCreation = createAsyncThunk<BuildCohortReturn, BuildCohortParams, { state: RootState }>(
'cohortCreation/build',
Expand Down Expand Up @@ -287,10 +286,9 @@ const buildCohortCreation = createAsyncThunk<BuildCohortReturn, BuildCohortParam
)
}

let allowSearchIpp = false
if (_selectedPopulation) {
allowSearchIpp = await services.perimeters.allowSearchIpp(_selectedPopulation as ScopeTreeRow[])
}
const allowSearchIpp = _selectedPopulation
? await services.perimeters.allowSearchIpp(_selectedPopulation as ScopeTreeRow[])
: false

let _initTemporalConstraints

Expand Down

0 comments on commit e7591b4

Please sign in to comment.