-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
271 additions
and
143 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import React, { createContext, useContext } from 'react'; | ||
|
||
import { create } from 'zustand'; | ||
|
||
export interface DataLoading { | ||
dataElements: Record<string, boolean>; | ||
isDone: () => boolean; | ||
setDataElements: (dataElements: Record<string, boolean>) => void; | ||
} | ||
|
||
export const createDataLoadingStore = () => | ||
create<DataLoading>((set, state) => ({ | ||
dataElements: {}, | ||
isDone() { | ||
return Object.values(state().dataElements).every((v) => v === true); | ||
}, | ||
setDataElements: (newDataElements: Record<string, boolean>) => { | ||
set((state) => ({ | ||
dataElements: { | ||
...state.dataElements, | ||
...newDataElements, | ||
}, | ||
})); | ||
}, | ||
})); | ||
|
||
const StoreContext = createContext<ReturnType<typeof createDataLoadingStore> | null>(null); | ||
|
||
export function DataLoadingProvider({ children }: React.PropsWithChildren) { | ||
const store = createDataLoadingStore(); | ||
|
||
return <StoreContext.Provider value={store}>{children}</StoreContext.Provider>; | ||
} | ||
|
||
export const useDataLoadingStore = <T,>(selector: (state: DataLoading) => T) => { | ||
const store = useContext(StoreContext); | ||
if (!store) { | ||
throw new Error('useDataLoadingStore must be used within a DataLoadingProvider'); | ||
} | ||
|
||
return store(selector); | ||
}; |
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
Oops, something went wrong.