Skip to content

Commit

Permalink
[B] Merge browser and DB answer storage
Browse files Browse the repository at this point in the history
  • Loading branch information
dananjohnson authored and blnkt committed Nov 1, 2023
1 parent 3385f09 commit 9e02331
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions contexts/StoredAnswersContext.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
"use client";

import { createContext, useCallback, useSyncExternalStore } from "react";
import {
createContext,
useCallback,
useRef,
useSyncExternalStore,
} from "react";
import { Query } from "@/gql/student-schema/graphql";
import { AnswerData, Answers, InvestigationId } from "@/types/answers";

Expand All @@ -27,6 +32,8 @@ function StoredAnswersProvider(props: {
children: React.ReactNode;
investigationId: InvestigationId;
}) {
const snapshotUpdateCount = useRef(0);

function subscribe(listener: () => void) {
document.addEventListener("storageEvent", listener);
return () => {
Expand All @@ -44,10 +51,20 @@ function StoredAnswersProvider(props: {
}, [JSON.stringify(props.answers)]); // eslint-disable-line react-hooks/exhaustive-deps

const getSnapshot = useCallback(() => {
return (
localStorage.getItem(`${props.investigationId}_answers`) ??
getServerSnapshot()
);
const browserAnswers = JSON.parse(
localStorage.getItem(`${props.investigationId}_answers`) ?? "{}"
) as Answers;

// initially merge answer sets with priority given to answers stored in DB
if (snapshotUpdateCount.current === 0) {
const serverAnswers = JSON.parse(getServerSnapshot()) as Answers;
const mergedAnswers = Object.assign({}, browserAnswers, serverAnswers);

return JSON.stringify(mergedAnswers);
}

// on subsequent updates, just use local storage
return JSON.stringify(browserAnswers);
}, [props.investigationId, getServerSnapshot]);

const storedAnswers = useSyncExternalStore(
Expand All @@ -68,6 +85,7 @@ function StoredAnswersProvider(props: {
...(answerId && { id: answerId }),
},
});
snapshotUpdateCount.current++;
setLocalStorage(props.investigationId, newAnswers);
},
[props.investigationId, getSnapshot]
Expand Down

0 comments on commit 9e02331

Please sign in to comment.