Skip to content

Commit

Permalink
Use different cookies for different problem types and useEffect loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Elscrux committed Nov 9, 2023
1 parent 98bc3c2 commit 297877a
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/components/solvers/ProgressHandler.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export interface ProgressHandlerProps<T> {
export const ProgressHandler = <T extends {}>(
props: ProgressHandlerProps<T>
) => {
const historyCookieName: string = `problemStates-${props.problemTypes}`;

const [wasClicked, setClicked] = useState<boolean>(false);
const [finished, setFinished] = useState<boolean>(false);
const [solutions, setSolutions] = useState<Solution[]>();
Expand All @@ -45,15 +47,18 @@ export const ProgressHandler = <T extends {}>(
requestContent: props.problemInput,
requestedSubSolveRequests: {},
});
const [cookies, setCookie, removeCookie] = useCookies(["problemStates"]);
const [cookies, setCookies] = useCookies([historyCookieName]);

useEffect(() => {
// Handle problem states cookies
if (problemStates.length == 0 && cookies.problemStates?.length > 0) {
setProblemStates(cookies.problemStates);
if (problemStates.length == 0 && cookies[historyCookieName]?.length > 0) {
setProblemStates(cookies[historyCookieName]);
}
setCookie("problemStates", problemStates);
}, [problemStates, cookies, setCookie]);
}, [problemStates, cookies, historyCookieName]);

useEffect(() => {
setCookies(historyCookieName, problemStates);
}, [historyCookieName, problemStates, setCookies]);

async function getSolution() {
setClicked(true);
Expand Down

0 comments on commit 297877a

Please sign in to comment.