Skip to content

Commit

Permalink
feat: refactor consent provider
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpastor committed Jun 26, 2023
1 parent befc94f commit 2a0862c
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/configs/providers/ad-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ReactElement } from "react";

import { Consent } from "./consent-provider/consent";
import { useConsent } from "./consent-provider/consent-context";
import { useConsent } from "./consent-provider/context";

export const AdProvider = (): ReactElement | null => {
const { consent } = useConsent();
Expand Down

This file was deleted.

18 changes: 0 additions & 18 deletions src/configs/providers/consent-provider/consent-context.tsx

This file was deleted.

35 changes: 35 additions & 0 deletions src/configs/providers/consent-provider/context.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { createContext, PropsWithChildren, ReactElement, useContext, useMemo } from "react";

import { useLocalStorage } from "@utils/hooks/use-local-storage";

import { Consent } from "./consent";

interface IConsentContext {
consent: Consent;
}
const ConsentContext = createContext<IConsentContext | undefined>(undefined);
ConsentContext.displayName = "ConsentContext";

export const ConsentContextProvider = ({ children }: PropsWithChildren<unknown>): ReactElement => {
const { value: consent } = useLocalStorage("consent", Consent.Pending);

const value = useMemo((): IConsentContext => ({
consent
}), [consent]);

return (
<ConsentContext.Provider value={value}>
{children}
</ConsentContext.Provider>
);
};

export const useConsent = (): IConsentContext => {
const context = useContext(ConsentContext);

if (context === undefined) {
throw new Error("useConsent must be used within a ConsentProvider.");
}

return context;
};
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import { ComponentType, ReactElement, Suspense } from "react";
import { useLocalStorage } from "@utils/hooks/use-local-storage";

import { Consent } from "./consent";
import type { ConsentSnackbarProps } from "./consent-snackbar";
import type { ConsentSnackbarProps } from "./snackbar";

const LazyConsentSnackbar = dynamic(async (): Promise<{ default: ComponentType<ConsentSnackbarProps> }> => ({
default: (await import("./consent-snackbar")).ConsentSnackbar
default: (await import("./snackbar")).ConsentSnackbar
}));

export const ConsentForm = (): ReactElement | null => {
Expand Down
4 changes: 2 additions & 2 deletions src/configs/providers/consent-provider/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PropsWithChildren, ReactElement } from "react";

import { ConsentContextProvider } from "./consent-context-provider";
import { ConsentForm } from "./consent-form";
import { ConsentContextProvider } from "./context";
import { ConsentForm } from "./form";

export const ConsentProvider = ({ children }: PropsWithChildren<unknown>): ReactElement => (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useTracking, Options } from "react-tracking";

import { TrackingData } from "./tracking-data";
import { Consent } from "../consent-provider/consent";
import { useConsent } from "../consent-provider/consent-context";
import { useConsent } from "../consent-provider/context";

const getAmplitudeApiKey = (): string => {
if (!process.env["NEXT_PUBLIC_AMPLITUDE_API_KEY"]) {
Expand Down

0 comments on commit 2a0862c

Please sign in to comment.