Skip to content

Commit

Permalink
Merge pull request #38 from zacharyLYH/zac-implement-localStorage
Browse files Browse the repository at this point in the history
Zac implement local storage
  • Loading branch information
ES-Legacy authored Dec 3, 2023
2 parents 131872e + a12d8ca commit 23e8661
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 16 deletions.
9 changes: 6 additions & 3 deletions client-side-queries/rq-queries/code-submit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@ export function getCodeSubmitCount() {
return resp;
}

interface UsePostSubmitCountProps {
interface UseCodeSubmissionProps {
code: string;
dateTime: string;
email: string;
challenge: string;
}

export async function postSubmitCount({
export async function codeSubmission({
code,
dateTime,
email,
challenge,
}: UsePostSubmitCountProps) {
}: UseCodeSubmissionProps) {
if (!code || !dateTime || !email || !challenge) {
throw new Error("Something wasn't passed into codeSubmission.");
}
const resp = await axios.post("/api/code/submit", {
code: code,
dateTime: dateTime,
Expand Down
14 changes: 10 additions & 4 deletions components/core/code-area-actions/submit-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import {
AlertDialogTitle,
} from "@/components/ui/alert-dialog";
import { useResetFeState } from "@/lib/reset-fe-state";
import { postSubmitCount } from "@/client-side-queries/rq-queries/code-submit";
import { codeSubmission } from "@/client-side-queries/rq-queries/code-submit";
import useSessionStore from "@/data-store/session-store";
import { useStepperStore } from "@/data-store/stepper-store";
import { loadFromLocalStorage } from "@/lib/localStorage";

const smallProps: ConfettiProps = {
force: 0.6,
Expand Down Expand Up @@ -63,11 +64,16 @@ const SubmitButton = () => {
const handleSubmitButtonClick = async () => {
try {
const dateTime = getCurrentDateTime();
await postSubmitCount({

const submitEmail = email ? email : loadFromLocalStorage("email");
const submitChallenge = challenge
? challenge
: loadFromLocalStorage("challenge");
await codeSubmission({
code,
dateTime,
email,
challenge,
email: submitEmail,
challenge: submitChallenge,
});
setSubmitClicked(true);
} catch (error: any) {
Expand Down
12 changes: 11 additions & 1 deletion components/core/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Button } from "../ui/button";
import StaticPrompt from "./target-image";
import LandingPageChallengeCode from "../landing/test-challenges/challenge-code";
import { useStepperStore } from "@/data-store/stepper-store";
import { loadFromLocalStorage, saveToLocalStorage } from "@/lib/localStorage";

const Editor = () => {
const { code, setCode } = useSessionStore();
Expand Down Expand Up @@ -39,10 +40,19 @@ const Editor = () => {
};

loadAce();
const localStorageCode = loadFromLocalStorage("code");
if (code.length === 0 && localStorageCode.length > 0) {
setCode(localStorageCode);
}
}, []);

if (!AceEditor) return <GlobalLoadingUI />;

const onType = (newCode: string) => {
setCode(newCode);
saveToLocalStorage("code", code);
};

return (
<div className='flex h-screen flex-col'>
<div className='flex flex-row'>
Expand Down Expand Up @@ -71,7 +81,7 @@ const Editor = () => {
theme={aceEditorTheme}
name='editor'
height='100%'
onChange={(newCode: string) => setCode(newCode)}
onChange={(newCode: string) => onType(newCode)}
fontSize={fontSize}
showPrintMargin={true}
showGutter={true}
Expand Down
6 changes: 4 additions & 2 deletions components/ui/useCode/StepTwo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useRouter } from "next/navigation";
import { ChallengeFormField } from "./Challenge-FormField";
import { Loader2 } from "lucide-react";
import { challengeEnum } from "@/data-store/challenge-store";
import { saveToLocalStorage } from "@/lib/localStorage";

const formStepTwoSchema = z.object({
challenge: z.nativeEnum(challengeEnum, {
Expand All @@ -31,7 +32,7 @@ const formStepTwoSchema = z.object({
});

export function StepTwo() {
const { progress, setProgress, step, setStep, setChallenge } =
const { progress, setProgress, step, setStep, setChallenge, email } =
useStepperStore();
const { setCode } = useSessionStore();

Expand All @@ -47,7 +48,8 @@ export function StepTwo() {
setCode(LandingPageCode());
setChallenge(selection);
setProgress(progress + progressIncrements);

saveToLocalStorage("email", email);
saveToLocalStorage("challenge", selection);
router.push("/code-area");
}
}
Expand Down
26 changes: 26 additions & 0 deletions lib/localStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export const saveToLocalStorage = (key: string, value: string) => {
try {
localStorage.setItem(key, value);
} catch (error) {
console.error("Error saving to localStorage", error);
}
};

export const loadFromLocalStorage = (key: string) => {
try {
const value = localStorage.getItem(key);
if (value === null) {
return "";
}
return value;
} catch (error) {
console.error("Error reading from localStorage", error);
return "";
}
};

export const removeItemFromLocalStorage = (key: string) => {
if (typeof window !== "undefined") {
localStorage.removeItem(key);
}
};
4 changes: 4 additions & 0 deletions lib/reset-fe-state.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import useCodeAreaStore from "@/data-store/code-area-store";
import useSessionStore from "@/data-store/session-store";
import { removeItemFromLocalStorage } from "./localStorage";

/*
A custom hook that resets the coding area state. Clears the code from coding area and resets certain settings.
Expand All @@ -11,5 +12,8 @@ export function useResetFeState() {
return function performReset() {
reset();
codeAreaReset();
removeItemFromLocalStorage("email");
removeItemFromLocalStorage("code");
removeItemFromLocalStorage("challenge");
};
}
21 changes: 15 additions & 6 deletions lib/validate-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Currently, it doesn't do anything, however it allows for more scalability if there are multiple different validations neccecary to authorize a user. */

import { maxProgress } from "@/data-store/stepper-store";
import { loadFromLocalStorage } from "./localStorage";

export default function validateUser(
validationType: string,
Expand All @@ -12,13 +13,21 @@ export default function validateUser(
) {
validationType = validationType.toLowerCase();

/*
Email is only in local storage if you went through the proper initialization steps. If a user refreshes the /code-area, their STATE gets lost, but local storage doesn't. So, if the user refreshes their page, we'll just allow the STATE to get lost, but since this user has email in local storage, we know for a fact this user got to the /code-area via the proper path.
*/
if (validationType === "code") {
return (
email === "" ||
check === false ||
progress !== maxProgress ||
challenge === ""
);
const localStorageEmail = loadFromLocalStorage("email");
if (
(email === "" ||
check === false ||
progress !== maxProgress ||
challenge === "") &&
localStorageEmail?.length === 0
) {
return true;
}
return false;
} else {
throw new Error("Invalid Validation Type");
}
Expand Down

0 comments on commit 23e8661

Please sign in to comment.