-
-
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.
wip: migrated from mockup counter store to questions store, update co…
…mponents and layout accordingly
- Loading branch information
Showing
8 changed files
with
90 additions
and
121 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,24 @@ | ||
import "../globals.css"; | ||
import "@repo/design-system/styles"; | ||
import "@repo/design-system/themes/theme-default"; | ||
import { CounterStoreProvider } from "./providers/counterStoreProvider"; | ||
import { StoreProvider } from "./providers/storeProvider"; | ||
import UrlUpdater from "./utils/urlUpdater"; | ||
|
||
const collection = ["Collection 1", "Collection 2", "Collection 3"]; | ||
|
||
export default async function RootLayout({ | ||
children, | ||
}: { | ||
children: React.ReactNode; | ||
}) { | ||
console.log(collection); | ||
return ( | ||
<html lang="en"> | ||
<CounterStoreProvider> | ||
<StoreProvider collection={collection}> | ||
<UrlUpdater> | ||
<body>{children}</body> | ||
</UrlUpdater> | ||
</CounterStoreProvider> | ||
</StoreProvider> | ||
</html> | ||
); | ||
} |
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 was deleted.
Oops, something went wrong.
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,57 @@ | ||
"use client"; | ||
|
||
import { type ReactNode, createContext, useRef, useContext } from "react"; | ||
import { type StoreApi, createStore, useStore } from "zustand"; | ||
|
||
type Store = { | ||
count: number; | ||
questions: string[]; | ||
currentQuestion: number; | ||
collection: string[]; | ||
decrementCount: () => void; | ||
incrementCount: () => void; | ||
prevQuestion: () => void; | ||
skipQuestion: () => void; | ||
}; | ||
|
||
export const StoreContext = createContext<StoreApi<Store> | undefined>( | ||
undefined, | ||
); | ||
|
||
// update to props with children | ||
export interface StoreProviderProps { | ||
children: ReactNode; | ||
collection: string[]; | ||
} | ||
|
||
export const StoreProvider = ({ children, collection }: StoreProviderProps) => { | ||
const storeRef = useRef<StoreApi<Store> | undefined>(); | ||
if (!storeRef.current) { | ||
storeRef.current = createStore<Store>((set) => ({ | ||
collection, | ||
count: 1, | ||
questions: ["Item 1", "Item 2", "Item 3"], | ||
currentQuestion: 1, | ||
decrementCount: () => set((state) => ({ count: state.count - 1 })), | ||
incrementCount: () => set((state) => ({ count: state.count + 1 })), | ||
prevQuestion: () => | ||
set((state) => ({ currentQuestion: state.currentQuestion - 1 })), | ||
skipQuestion: () => | ||
set((state) => ({ currentQuestion: state.currentQuestion + 1 })), | ||
})); | ||
} | ||
|
||
return ( | ||
<StoreContext.Provider value={storeRef.current}> | ||
{children} | ||
</StoreContext.Provider> | ||
); | ||
}; | ||
|
||
export function useQuestionsStore<U>(selector: (state: Store) => U): U { | ||
const store = useContext(StoreContext); | ||
if (!store) { | ||
throw new Error("Missing StoreProvider"); | ||
} | ||
return useStore(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