-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #762 from Health-Informatics-UoN/fix/761/use-state
Fix Mapper stops responding when add/delete concept quickly
- Loading branch information
Showing
10 changed files
with
199 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
app/next-client-app/components/concepts/ConceptDataTable.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
"use client"; | ||
|
||
import { DataTable } from "@/components/data-table"; | ||
import { DataTableFilter } from "@/components/data-table/DataTableFilter"; | ||
import { addConceptsToResults } from "@/lib/concept-utils"; | ||
import { useEffect, useState } from "react"; | ||
|
||
interface CustomDataTableProps<T> { | ||
scanReportsData: T[]; | ||
scanReportsConcepts: ScanReportConcept[]; | ||
conceptsFilter: Concept[]; | ||
permissions: PermissionsResponse; | ||
count: number; | ||
defaultPageSize: 10 | 20 | 30 | 40 | 50; | ||
columns: ( | ||
addConcept: (newConcept: ScanReportConcept, newConFilter: Concept) => void, | ||
deleteConcept: (id: number) => void | ||
) => any; | ||
clickable?: boolean; | ||
filterCol: string; | ||
filterText: string; | ||
linkPrefix?: string; | ||
} | ||
|
||
export function ConceptDataTable< | ||
T extends { id: number; concepts?: Concept[]; permissions: Permission[] } | ||
>({ | ||
scanReportsData, | ||
scanReportsConcepts, | ||
conceptsFilter, | ||
permissions, | ||
count, | ||
defaultPageSize, | ||
columns, | ||
clickable, | ||
filterCol, | ||
filterText, | ||
linkPrefix, | ||
}: CustomDataTableProps<T>) { | ||
const filter = <DataTableFilter filter={filterCol} filterText={filterText} />; | ||
|
||
// Set the needed Concepts and Concepts filter in state, so we can mutate them individually. | ||
const [neededConcepts, setNeededConcepts] = useState(scanReportsConcepts); | ||
const [neededConceptFilter, setNeededConceptFilter] = | ||
useState(conceptsFilter); | ||
|
||
// Necessary for pagination | ||
useEffect(() => { | ||
setNeededConcepts(scanReportsConcepts); | ||
}, [scanReportsConcepts]); | ||
|
||
useEffect(() => { | ||
setNeededConceptFilter(conceptsFilter); | ||
}, [conceptsFilter]); | ||
|
||
const deleteConcept = (id: number) => { | ||
// filter it out. | ||
const updatedConcepts = neededConcepts.filter( | ||
(concept) => concept.id !== id | ||
); | ||
setNeededConcepts(updatedConcepts); | ||
}; | ||
|
||
const addConcept = (newConcept: ScanReportConcept, newConFilter: Concept) => { | ||
// merge it. | ||
|
||
const updatedConcepts = [...neededConcepts, newConcept]; | ||
setNeededConcepts(updatedConcepts); | ||
|
||
const updatedConceptFilters = [...neededConceptFilter, newConFilter]; | ||
setNeededConceptFilter(updatedConceptFilters); | ||
}; | ||
|
||
const scanReportsResult = addConceptsToResults( | ||
scanReportsData, | ||
neededConcepts, | ||
neededConceptFilter, | ||
permissions | ||
); | ||
|
||
return ( | ||
<div> | ||
<DataTable | ||
columns={columns(addConcept, deleteConcept)} | ||
data={scanReportsResult} | ||
count={count} | ||
Filter={filter} | ||
clickableRow={clickable} | ||
defaultPageSize={defaultPageSize} | ||
linkPrefix={linkPrefix} | ||
/> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.