-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JNG-5386 add view data context and hooks (#282)
- Loading branch information
Showing
8 changed files
with
81 additions
and
16 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
38 changes: 38 additions & 0 deletions
38
judo-ui-react/src/main/resources/actor/src/hooks/ViewDataContext.tsx.hbs
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,38 @@ | ||
import type { FC, ReactNode } from 'react'; | ||
import { createContext, useContext, useState } from 'react'; | ||
import type { JudoIdentifiable } from '~/services/data-api/common'; | ||
|
||
interface DataContextProps { | ||
children: ReactNode; | ||
} | ||
|
||
interface ViewContextContextValue { | ||
setLatestViewData: (newData: JudoIdentifiable<any> | null) => void; | ||
getLatestViewData: () => JudoIdentifiable<any> | null; | ||
} | ||
|
||
const ViewDataContext = createContext<ViewContextContextValue | undefined>(undefined); | ||
|
||
export const ViewDataProvider: FC<DataContextProps> = ({ children }) => { | ||
const [data, setData] = useState<JudoIdentifiable<any> | null>(null); | ||
|
||
const useData = (): ViewContextContextValue => { | ||
const setLatestViewData = (newData: JudoIdentifiable<any> | null): void => { | ||
setData(newData); | ||
}; | ||
const getLatestViewData = (): JudoIdentifiable<any> | null => data; | ||
return { setLatestViewData, getLatestViewData }; | ||
}; | ||
|
||
return <ViewDataContext.Provider value={useData()}>{children}</ViewDataContext.Provider>; | ||
}; | ||
|
||
export const useViewData = (): ViewContextContextValue => { | ||
const context = useContext(ViewDataContext); | ||
|
||
if (!context) { | ||
throw new Error('useViewData must be used within a ViewDataProvider'); | ||
} | ||
|
||
return context; | ||
}; |
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