Skip to content

Commit

Permalink
Use URL to determine whether or not page should be compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
hstandaert committed Apr 15, 2024
1 parent d20e9ff commit 8492954
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 48 deletions.
1 change: 0 additions & 1 deletion src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
export { default as useLocalStorage } from "./useLocalStorage";
export { default as useForm } from "./useForm";
43 changes: 0 additions & 43 deletions src/hooks/useLocalStorage.ts

This file was deleted.

29 changes: 25 additions & 4 deletions src/providers/AppStateProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { useLocalStorage } from "@/hooks";

Check failure on line 1 in src/providers/AppStateProvider.tsx

View workflow job for this annotation

GitHub Actions / deploy

'useLocalStorage' is declared but its value is never read.

Check failure on line 1 in src/providers/AppStateProvider.tsx

View workflow job for this annotation

GitHub Actions / deploy

Module '"@/hooks"' has no exported member 'useLocalStorage'.
import { type PropsWithChildren, createContext, useContext } from "react";
import {
type PropsWithChildren,
createContext,
useContext,
useEffect,
useState,
} from "react";

export type AppStateContextProps = PropsWithChildren & {
defaultValue?: AppStateContext;
Expand All @@ -20,11 +26,26 @@ const AppStateContext = createContext<AppStateContext>(initialState);
export const useAppState = () => useContext(AppStateContext);

function AppStateProvider({ children, defaultValue }: AppStateContextProps) {
const [isCompliant, setIsCompliant] = useLocalStorage(
"isCompliant",
defaultValue?.isCompliant || initialState.isCompliant,
const params = new URLSearchParams(window.location.search);
const compliantParam = params.get("compliant") === "true";

const [isCompliant, setIsCompliant] = useState(
defaultValue?.isCompliant || compliantParam,
);

useEffect(() => {
if (isCompliant) {
params.set("compliant", "true");
} else {
params.delete("compliant");
}

const newUrl = `${window.location.pathname}${
params.size > 0 ? `?${params.toString()}` : ""
}`;
window.history.replaceState({}, "", newUrl);
}, [isCompliant, params.delete, params.set, params.size, params.toString]);

const toggleCompliancy = (newValue?: boolean) => {
setIsCompliant((prev) => (newValue !== undefined ? newValue : !prev));
};
Expand Down

0 comments on commit 8492954

Please sign in to comment.