-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor
WithUiId
handling to use hook useWithUiId()
Following up on #1554, create hook `useWithUiId()` to handle injecting UI id to objects. Any object `T` going will to the hook will come out as a `WithUiId<T>` object. Tables using the UI id have been adjusted to use the Constant `UI_UNIQUE_ID` as the `WithUiId<T>` table data `idProperty`. All uses of `WithUiId<T>` are now handled the same way. Signed-off-by: Scott J Dickerson <[email protected]>
- Loading branch information
Showing
8 changed files
with
103 additions
and
65 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
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { useMemo } from "react"; | ||
import { UI_UNIQUE_ID } from "@app/Constants"; | ||
import { WithUiId } from "@app/api/models"; | ||
|
||
/** | ||
* Make a shallow copy of `data` and insert a new `UI_UNIQUE_ID` field in each element | ||
* with the output of the `generator` function. This hook allows generating the needed | ||
* UI id field for any object that does not already have a unique id field so the object | ||
* can be used with our table selection handlers. | ||
* | ||
* @returns A shallow copy of `T` with an added `UI_UNIQUE_ID` field. | ||
*/ | ||
export const useWithUiId = <T>( | ||
/** Source data to modify. */ | ||
data: T[] | undefined, | ||
/** Generate the unique id for a specific `T`. */ | ||
generator: (item: T) => string | ||
): WithUiId<T>[] => { | ||
const result = useMemo(() => { | ||
if (!data || data.length === 0) { | ||
return []; | ||
} | ||
|
||
const dataWithUiId: WithUiId<T>[] = data.map((item) => ({ | ||
...item, | ||
[UI_UNIQUE_ID]: generator(item), | ||
})); | ||
|
||
return dataWithUiId; | ||
}, [data, generator]); | ||
|
||
return result; | ||
}; |