Skip to content

Commit

Permalink
fix(j-s): Memoize casefiles so they don't move when after reordering …
Browse files Browse the repository at this point in the history
…while renaming
  • Loading branch information
unakb committed Sep 25, 2024
1 parent 5d3444a commit 8bd58b2
Showing 1 changed file with 24 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useContext, useState } from 'react'
import { useCallback, useContext, useMemo, useState } from 'react'
import { useIntl } from 'react-intl'
import { LayoutGroup } from 'framer-motion'
import router from 'next/router'
Expand All @@ -23,6 +23,15 @@ import { caseFile as m } from './CaseFile.strings'
const CaseFile = () => {
const { workingCase, isLoadingWorkingCase, caseNotFound } =
useContext(FormContext)

const caseFiles = useMemo(() => {
return (
workingCase.caseFiles?.filter(
(caseFile) => caseFile.category === CaseFileCategory.CASE_FILE_RECORD,
) ?? []
)
}, [workingCase.caseFiles])

const { formatMessage } = useIntl()
const [editCount, setEditCount] = useState<number>(0)

Expand Down Expand Up @@ -57,24 +66,20 @@ const CaseFile = () => {
<Box marginBottom={5}>
<LayoutGroup>
<Accordion singleExpand>
{workingCase.policeCaseNumbers?.map((policeCaseNumber, index) => (
<IndictmentsCaseFilesAccordionItem
key={index}
caseId={workingCase.id}
policeCaseNumber={policeCaseNumber}
shouldStartExpanded={index === 0}
caseFiles={
workingCase.caseFiles?.filter(
(caseFile) =>
caseFile.policeCaseNumber === policeCaseNumber &&
caseFile.category === CaseFileCategory.CASE_FILE_RECORD,
) ?? []
}
subtypes={workingCase.indictmentSubtypes}
crimeScenes={workingCase.crimeScenes}
setEditCount={setEditCount}
/>
))}
{workingCase.policeCaseNumbers?.map((policeCaseNumber, index) => {
return (
<IndictmentsCaseFilesAccordionItem
key={index}
caseId={workingCase.id}
policeCaseNumber={policeCaseNumber}
shouldStartExpanded={index === 0}
caseFiles={caseFiles}
subtypes={workingCase.indictmentSubtypes}
crimeScenes={workingCase.crimeScenes}
setEditCount={setEditCount}
/>
)
})}
</Accordion>
</LayoutGroup>
</Box>
Expand Down

0 comments on commit 8bd58b2

Please sign in to comment.