Skip to content

Commit

Permalink
fixed server cookies provider bad code
Browse files Browse the repository at this point in the history
  • Loading branch information
Andcool-Systems committed Jan 3, 2025
1 parent f4a7b6d commit 49c5ee0
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 11 deletions.
13 changes: 3 additions & 10 deletions src/app/modules/utils/CookiesProvider/CookieProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,18 @@ interface CookiesContextProps {
get: (name: string) => string | undefined;
}

type CookieType = [string, { name: string, value: string }];

const CookiesContext = createContext<CookiesContextProps | undefined>(undefined);

export const useCookiesServer = (): CookiesContextProps => {
const context = useContext(CookiesContext);
if (!context) {
if (!context)
throw new Error("CookieProvider not mounted");
}
return context;
};


export const CookiesContextProvider = ({ children, value }: { children: ReactNode, value: any }) => {
const get = (name: string) => {
const cookie: CookieType = value.find((cookie: CookieType) => cookie[0] === name);
if (!cookie) return undefined;
return cookie[1].value;
}
export const CookiesContextProvider = ({ children, value }: { children: ReactNode, value: { name: string, value: string }[] }) => {
const get = (name: string) => value.find(cookie => cookie.name === name)?.value;

return (
<CookiesContext.Provider value={{ get }}>
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/utils/CookiesProvider/CookiesComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const CookieProvider = async ({ children }: { children: React.ReactNode }
const _cookies = await cookies();

return (
<CookiesContextProvider value={_cookies}>
<CookiesContextProvider value={_cookies.getAll()}>
{children}
</CookiesContextProvider>
);
Expand Down

0 comments on commit 49c5ee0

Please sign in to comment.