-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(modelTransfer): simplify and fix refresh list crash
- Loading branch information
Showing
4 changed files
with
173 additions
and
119 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
src/components/metadataFormControls/ModelTransfer/BaseModelTransfer.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,67 @@ | ||
import { Transfer, TransferProps } from '@dhis2/ui' | ||
import React, { useCallback, useMemo } from 'react' | ||
import { DisplayableModel } from '../../../types/models' | ||
|
||
const toDisplayOption = (model: DisplayableModel) => ({ | ||
value: model.id, | ||
label: model.displayName, | ||
}) | ||
|
||
type OwnProps<TModel> = { | ||
selected: TModel[] | ||
available: TModel[] | ||
onChange: ({ selected }: { selected: TModel[] }) => void | ||
} | ||
|
||
export type BaseModelTransferProps<TModel> = Omit< | ||
TransferProps, | ||
keyof OwnProps<TModel> | 'options' | 'selected' | ||
> & | ||
OwnProps<TModel> | ||
|
||
/* Simple wrapper component handle generic models with Transfer-component. */ | ||
export const BaseModelTransfer = <TModel extends DisplayableModel>({ | ||
available, | ||
selected, | ||
onChange, | ||
...transferProps | ||
}: BaseModelTransferProps<TModel>) => { | ||
const { allModelsMap, allTransferOptions } = useMemo(() => { | ||
const allModels = selected.concat(available) | ||
const allModelsMap = new Map(allModels.map((o) => [o.id, o])) | ||
const allTransferOptions = allModels.map(toDisplayOption) | ||
return { | ||
allModelsMap, | ||
allTransferOptions, | ||
} | ||
}, [available, selected]) | ||
|
||
const selectedTransferValues = useMemo( | ||
() => selected.map((s) => s.id), | ||
[selected] | ||
) | ||
|
||
const handleOnChange = useCallback( | ||
({ selected }: { selected: string[] }) => { | ||
// map the selected ids to the full model | ||
// loop through selected to keep order | ||
const selectedModels = selected | ||
.map((id) => allModelsMap.get(id)) | ||
.filter((model) => !!model) | ||
|
||
onChange({ | ||
selected: selectedModels, | ||
}) | ||
}, | ||
[onChange, allModelsMap] | ||
) | ||
|
||
return ( | ||
<Transfer | ||
{...transferProps} | ||
selected={selectedTransferValues} | ||
options={allTransferOptions} | ||
onChange={handleOnChange} | ||
/> | ||
) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,8 @@ | ||
// generated by https://github.com/Birkbjo/dhis2-open-api-ts | ||
export type * from './generated' | ||
export * from './systemSettings' | ||
|
||
export type DisplayableModel = { | ||
id: string | ||
displayName: string | ||
} |