Skip to content

Commit

Permalink
correction bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
aetchego committed Jan 8, 2025
1 parent 34d1979 commit 374f361
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 27 deletions.
1 change: 0 additions & 1 deletion src/components/SearchValueSet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ const SearchValueSet = ({ references, selectedNodes, onSelect }: SearchValueSetP
<Input
value={searchInput}
placeholder="Rechercher un code"
disabled={loadingStatus.search === LoadingStatus.FETCHING}
fullWidth
onChange={(event) => onChangeSearchInput(event.target.value)}
endAdornment={
Expand Down
25 changes: 16 additions & 9 deletions src/hooks/hierarchy/useHierarchy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,22 @@ export const useHierarchy = <T>(
}

const search = async (fetchSearch: () => Promise<Back_API_Response<Hierarchy<T>>>) => {
const { results: endCodes, count } = await fetchSearch()
const bySystem = groupBySystem(endCodes)
const newCodes = await getMissingCodesWithSystems(trees, bySystem, codes, fetchHandler)
const newTrees = buildMultipleTrees(trees, bySystem, newCodes, selectedCodes, Mode.SEARCH)
setCodes(newCodes)
setTrees(newTrees)
return { display: getDisplayFromTrees(endCodes, newTrees), count }
setLoadingStatus({ ...loadingStatus, search: LoadingStatus.FETCHING })
try {
const { results: endCodes, count } = await fetchSearch()
const bySystem = groupBySystem(endCodes)
const newCodes = await getMissingCodesWithSystems(trees, bySystem, codes, fetchHandler)
const newTrees = buildMultipleTrees(trees, bySystem, newCodes, selectedCodes, Mode.SEARCH)
setCodes(newCodes)
setTrees(newTrees)
setLoadingStatus({ ...loadingStatus, search: LoadingStatus.SUCCESS })
return { display: getDisplayFromTrees(endCodes, newTrees), count }
} catch (e) {
return {
display: [],
count: 0
}
}
}

const fetchMore = async (
Expand All @@ -96,13 +105,11 @@ export const useHierarchy = <T>(
mode: SearchMode,
id?: string
) => {
setLoadingStatus({ ...loadingStatus, search: LoadingStatus.FETCHING })
const { display, count } = await search(fetchSearch)
if (mode == SearchMode.EXPLORATION && id) {
const currentHierarchy = hierarchies.get(id) || DEFAULT_HIERARCHY_INFO
setHierarchies(replaceInMap(id, { ...currentHierarchy, tree: display, page }, hierarchies))
} else setSearchResults({ tree: display, count, page, system: '' })
setLoadingStatus({ ...loadingStatus, search: LoadingStatus.SUCCESS })
}

const select = (nodes: Hierarchy<T>[], toAdd: boolean, mode: SearchMode.EXPLORATION | SearchMode.RESEARCH) => {
Expand Down
15 changes: 13 additions & 2 deletions src/hooks/valueSet/useSearchValueSet.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useEffect, useMemo, useState } from 'react'
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { FhirItem, Reference } from 'types/valueSet'
import { LIMIT_PER_PAGE, SearchParameters, useSearchParameters } from '../search/useSearchParameters'
import { useHierarchy } from 'hooks/hierarchy/useHierarchy'
Expand All @@ -13,6 +13,8 @@ import { Codes, Hierarchy, SearchMode } from 'types/hierarchy'
import { saveValueSets, selectValueSetCodes } from 'state/valueSets'
import { useAppDispatch, useAppSelector } from 'state'
import { DEFAULT_HIERARCHY_INFO, getItemSelectedStatus, mapCodesToCache } from 'utils/hierarchy'
import { LoadingStatus } from 'types'
import { cancelPendingRequest } from 'utils/abortController'

export const useSearchValueSet = (references: Reference[], selectedNodes: Hierarchy<FhirItem, string>[]) => {
const researchParameters = useSearchParameters()
Expand All @@ -21,6 +23,7 @@ export const useSearchValueSet = (references: Reference[], selectedNodes: Hierar
const [mode, setMode] = useState(SearchMode.EXPLORATION)
const [initialized, setInitialized] = useState({ exploration: false, research: false })
const dispatch = useAppDispatch()
const controllerRef = useRef<AbortController | null>(null)

const fetchChildren = useCallback(
async (ids: string, system: string) => (await getChildrenFromCodes(system, ids.split(','))).results,
Expand Down Expand Up @@ -73,7 +76,15 @@ export const useSearchValueSet = (references: Reference[], selectedNodes: Hierar

const fetchSearch = async (searchInput: string, page: number, references: string[]) => {
if (references.length) {
return await searchInValueSets(references, searchInput, page * LIMIT_PER_PAGE, LIMIT_PER_PAGE)
if (loadingStatus.search === LoadingStatus.FETCHING)
controllerRef.current = cancelPendingRequest(controllerRef.current)
return await searchInValueSets(
references,
searchInput,
page * LIMIT_PER_PAGE,
LIMIT_PER_PAGE,
controllerRef.current?.signal
)
} else return { results: [], count: 0 }
}

Expand Down
18 changes: 8 additions & 10 deletions src/services/aphp/serviceValueSets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { getConfig } from 'config'
import { LOW_TOLERANCE_TAG } from './callApi'
import { sortArray } from 'utils/arrays'
import { FhirItem } from 'types/valueSet'
import axios from 'axios'

export const UNKOWN_HIERARCHY_CHAPTER = 'UNKNOWN'
export const HIERARCHY_ROOT = '*'
Expand Down Expand Up @@ -180,7 +181,8 @@ export const searchInValueSets = async (
const response = formatValuesetExpansion(getApiResponseResourceOrThrow(res).expansion)
response.results = mapAbandonedChildren(response.results)
return response
} catch (e) {
} catch (error) {
if (axios.isCancel(error)) throw "Cancelled request."

Check failure on line 185 in src/services/aphp/serviceValueSets.ts

View workflow job for this annotation

GitHub Actions / test

Replace `"Cancelled·request."` with `'Cancelled·request.'`
return {
count: 0,
results: []
Expand Down Expand Up @@ -238,23 +240,19 @@ export const getHierarchyRoots = async (
const chapters = codeList
.filter((code) => filterRoots(code))
.map((e) => mapFhirHierarchyToHierarchyWithLabelAndSystem(e))
const toBeAdoptedCodes = codeList
.filter((code) => !filterRoots(code))
.map((e) => mapFhirHierarchyToHierarchyWithLabelAndSystem(e))
const toBeAdoptedCodes = codeList.filter((code) => !filterRoots(code))
const childrenIds = chapters.map((code) => code.id)
let subItems: Hierarchy<FhirItem>[] | undefined = undefined
if (toBeAdoptedCodes.length) {
const unknownChildren = toBeAdoptedCodes.map((child) => ({
...child,
above_levels_ids: `${HIERARCHY_ROOT},${UNKOWN_HIERARCHY_CHAPTER}`
}))
const unknownChaptersIds = toBeAdoptedCodes.map((code) => code.id)
const unknownChapters = (await getChildrenFromCodes(codeSystem, unknownChaptersIds)).results
const unknownChapter: Hierarchy<FhirItem> = {
id: UNKOWN_HIERARCHY_CHAPTER,
label: `${UNKOWN_HIERARCHY_CHAPTER}`,
system: codeSystem,
above_levels_ids: HIERARCHY_ROOT,
inferior_levels_ids: unknownChildren.map((code) => code.id).join(','),
subItems: unknownChildren
inferior_levels_ids: unknownChapters.map((code) => code.id).join(','),
subItems: unknownChapters
}
const chaptersEntities = (await getChildrenFromCodes(codeSystem, childrenIds)).results
childrenIds.push(UNKOWN_HIERARCHY_CHAPTER)
Expand Down
24 changes: 19 additions & 5 deletions src/utils/hierarchy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ export const getDisplayFromTree = <T>(toDisplay: Hierarchy<T>[], tree: Hierarchy
let branches: Hierarchy<T, string>[] = []
if (toDisplay.length && tree.length)
branches = toDisplay.map((item) => {
const path = item.above_levels_ids ? [...getAboveLevelsWithRights(item, tree), ...[item.id]] : [item.id]
let path = item.above_levels_ids ? [...getAboveLevelsWithRights(item, tree), ...[item.id]] : [item.id]
path = updateUnknownChapter(tree, path)
return findBranch(path, tree)
})
return branches
Expand All @@ -260,7 +261,10 @@ export const getDisplayFromTrees = <T>(
}

const findBranch = <T>(path: string[], tree: Hierarchy<T, string>[]): Hierarchy<T, string> => {
let branch: Hierarchy<T, string> = { id: `${path}-empty`, label: `${path}---------------` } as Hierarchy<T, string>
let branch: Hierarchy<T, string> = { id: `${path}-empty`, label: `ERROR | ${path}---------------` } as Hierarchy<
T,
string
>
const key = path[0]
const index = tree.findIndex((item) => item.id === key)
const next = tree[index]
Expand All @@ -270,10 +274,19 @@ const findBranch = <T>(path: string[], tree: Hierarchy<T, string>[]): Hierarchy<
return branch
}

const updateUnknownChapter = <T>(baseTree: Hierarchy<T, string>[], path: string[]) => {
const unknownChapterFound = baseTree[0]?.subItems?.find((chapter) => chapter.id === UNKOWN_HIERARCHY_CHAPTER)
const childrenIds = unknownChapterFound?.inferior_levels_ids.split(',') || []
if (path?.[1] !== UNKOWN_HIERARCHY_CHAPTER && childrenIds.includes(path?.[1]))
path.splice(1, 0, UNKOWN_HIERARCHY_CHAPTER)
return path
}

const getPaths = <T>(baseTree: Hierarchy<T, string>[], endCodes: Hierarchy<T, string>[]) => {
const paths = endCodes.map((item) =>
let paths = endCodes.map((item) =>
item.above_levels_ids ? [...getAboveLevelsWithRights(item, baseTree), ...[item.id]] : [item.id]
)
paths = paths.map((path) => updateUnknownChapter(baseTree, path))
return paths
}

Expand Down Expand Up @@ -319,8 +332,9 @@ export const getItemSelectedStatus = <T, S>(item: Hierarchy<T, S>): SelectedStat

export const getSelectedCodesFromTree = <T>(tree: Hierarchy<T, string>[]) => {
const get = <T>(node: Hierarchy<T, string>, selectedCodes: Hierarchy<T>[]) => {
if (node.status === SelectedStatus.INDETERMINATE) node.subItems?.forEach((subItem) => get(subItem, selectedCodes))
if (node.status === SelectedStatus.SELECTED) selectedCodes.push(node)
if (node.status === SelectedStatus.INDETERMINATE || node.id === UNKOWN_HIERARCHY_CHAPTER)
node.subItems?.forEach((subItem) => get(subItem, selectedCodes))
if (node.status === SelectedStatus.SELECTED && node.id !== UNKOWN_HIERARCHY_CHAPTER) selectedCodes.push(node)
return selectedCodes
}
const selectedCodes = mapHierarchyToMap(tree.flatMap((hierarchy) => get(hierarchy, [])))
Expand Down

0 comments on commit 374f361

Please sign in to comment.