-
Notifications
You must be signed in to change notification settings - Fork 3
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
1 parent
917dadf
commit 49a05a5
Showing
6 changed files
with
104 additions
and
29 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,43 @@ | ||
"use client"; | ||
|
||
import { | ||
createContext, | ||
Dispatch, | ||
ReactNode, | ||
SetStateAction, | ||
useContext, | ||
useState, | ||
} from "react"; | ||
import type { GetHeroResponse } from "@/api/proto-http/frontend"; | ||
|
||
const HeroContext = createContext< | ||
GetHeroResponse & { | ||
selectedCurrency: string; | ||
setSelectedCurrency: Dispatch<SetStateAction<string>>; | ||
} | ||
>({ | ||
hero: undefined, | ||
dictionary: undefined, | ||
rates: undefined, | ||
selectedCurrency: "", | ||
setSelectedCurrency: () => {}, | ||
}); | ||
|
||
export function HeroContextProvider({ | ||
children, | ||
...props | ||
}: GetHeroResponse & { children: ReactNode }) { | ||
const [selectedCurrency, setSelectedCurrency] = useState( | ||
props.dictionary?.baseCurrency || "", | ||
); | ||
|
||
return ( | ||
<HeroContext.Provider | ||
value={{ ...props, selectedCurrency, setSelectedCurrency }} | ||
> | ||
{children} | ||
</HeroContext.Provider> | ||
); | ||
} | ||
|
||
export const useHeroContext = () => useContext(HeroContext); |
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 @@ | ||
"use client"; | ||
|
||
import { useHeroContext } from "@/components/contexts/HeroContext"; | ||
import GenericPopover from "@/components/ui/Popover"; | ||
import { cn } from "@/lib/utils"; | ||
|
||
export default function Component() { | ||
const { rates, selectedCurrency, setSelectedCurrency } = useHeroContext(); | ||
|
||
if (!rates?.currencies) return null; | ||
|
||
return ( | ||
<GenericPopover | ||
title="currency" | ||
openElement={ | ||
<span className="bg-textColor px-2 py-1 text-buttonTextColor"> | ||
{`Currency: ${selectedCurrency}`} | ||
</span> | ||
} | ||
> | ||
<div className="space-y-2 px-12 pb-7"> | ||
{Object.entries(rates.currencies).map(([k, v]) => ( | ||
<button | ||
key={k} | ||
onClick={() => { | ||
setSelectedCurrency(k); | ||
}} | ||
className={cn("flex items-center gap-1 text-sm", { | ||
underline: k === selectedCurrency, | ||
})} | ||
> | ||
{v.description} {JSON.stringify(v.rate)} | ||
</button> | ||
))} | ||
</div> | ||
</GenericPopover> | ||
); | ||
} |
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,12 @@ | ||
import { serviceClient } from "@/lib/api"; | ||
import { HeroContextProvider } from "../contexts/HeroContext"; | ||
|
||
type Props = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
export default async function Layout({ children }: Props) { | ||
const heroData = await serviceClient.GetHero({}); | ||
|
||
return <HeroContextProvider {...heroData}>{children}</HeroContextProvider>; | ||
} |
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