Skip to content

Commit

Permalink
Replace cookie approach with local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Elscrux committed Nov 14, 2023
1 parent 7b723a0 commit 4e254b8
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions src/components/solvers/ProgressHandler.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Box, Center, HStack } from "@chakra-ui/react";
import React, { useEffect, useState } from "react";
import { useCookies } from "react-cookie";
import { History } from "./History";
import { GoButton } from "./buttons/GoButton";
import { fetchSolution, postProblem } from "../../api/ToolboxAPI";
Expand Down Expand Up @@ -37,7 +36,7 @@ export interface ProgressHandlerProps<T> {
export const ProgressHandler = <T extends {}>(
props: ProgressHandlerProps<T>
) => {
const historyCookieName: string = `problemStates-${props.problemTypes}`;
const historyItemName: string = `problemStates-${props.problemTypes}`;

const [wasClicked, setClicked] = useState<boolean>(false);
const [finished, setFinished] = useState<boolean>(false);
Expand All @@ -47,18 +46,23 @@ export const ProgressHandler = <T extends {}>(
requestContent: props.problemInput,
requestedSubSolveRequests: {},
});
const [cookies, setCookies] = useCookies([historyCookieName]);

useEffect(() => {
// Handle problem states cookies
if (problemStates.length == 0 && cookies[historyCookieName]?.length > 0) {
setProblemStates(cookies[historyCookieName]);
// Handle problem states from local storage
if (problemStates.length == 0) {
let historyItem = localStorage.getItem(historyItemName);
if (historyItem == null) return;

let storageStates = JSON.parse(historyItem);
if (storageStates.length > 0) {
setProblemStates(storageStates);
}
}
}, [problemStates, cookies, historyCookieName]);
}, [problemStates, historyItemName]);

useEffect(() => {
setCookies(historyCookieName, problemStates);
}, [historyCookieName, problemStates, setCookies]);
localStorage.setItem(historyItemName, JSON.stringify(problemStates));
}, [historyItemName, problemStates]);

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

0 comments on commit 4e254b8

Please sign in to comment.