Skip to content

Commit e6ee863

Browse files
authored
Merge pull request #23 from dgmstuart/keyed-session-data
Separate session data per config
2 parents 0327e7e + 85b9f3a commit e6ee863

13 files changed

+93
-55
lines changed

src/App.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const App: React.FC = () => {
2828
{
2929
path: "qr_code",
3030
element: <QRCode />,
31-
loader: configLoader,
31+
loader: defaultConfigLoader,
3232
},
3333
],
3434
},

src/components/Card.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ describe("'New card' button", () => {
1313
test("changes the words on the card", () => {
1414
render(
1515
<BrowserRouter>
16-
<Card wordList={wordList} name={""} url={""} />
16+
<Card wordList={wordList} name={""} url={""} id={""} />
1717
</BrowserRouter>,
1818
);
1919
const initialCells = screen.queryAllByRole("gridcell");
@@ -30,7 +30,7 @@ describe("'New card' button", () => {
3030
test("clears any stamped cells", () => {
3131
render(
3232
<BrowserRouter>
33-
<Card wordList={wordList} name={""} url={""} />
33+
<Card wordList={wordList} name={""} url={""} id={""} />
3434
</BrowserRouter>,
3535
);
3636
const cells = screen.queryAllByRole("gridcell");
@@ -49,7 +49,7 @@ describe("'Clear' button", () => {
4949
test("clears any stamped cells", () => {
5050
render(
5151
<BrowserRouter>
52-
<Card wordList={wordList} name={""} url={""} />
52+
<Card wordList={wordList} name={""} url={""} id={""} />
5353
</BrowserRouter>,
5454
);
5555
const cells = screen.queryAllByRole("gridcell");
@@ -74,7 +74,7 @@ describe("'Share' button", () => {
7474
const user = userEvent.setup();
7575
render(
7676
<BrowserRouter>
77-
<Card wordList={wordList} name={""} url={""} />
77+
<Card wordList={wordList} name={""} url={""} id={""} />
7878
<textarea rows={5} />
7979
</BrowserRouter>,
8080
);

src/components/Card.tsx

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ import share from "../lib/share";
88
import type { CellProps } from "./Cell";
99
import type { ButtonClickHandler } from "../clickHandler";
1010

11-
const Card: React.FC<{ name: string; url: string; wordList: string[] }> = ({
12-
name,
13-
url,
14-
wordList,
15-
}) => {
16-
const [cellDataList, toggleStamped, setNewWords, clearAllCells] =
17-
useCard(wordList);
11+
const Card: React.FC<{
12+
id: string;
13+
name: string;
14+
url: string;
15+
wordList: string[];
16+
}> = ({ id, name, url, wordList }) => {
17+
const [cellDataList, toggleStamped, setNewWords, clearAllCells] = useCard(
18+
id,
19+
wordList,
20+
);
1821

1922
const cellPropsList: CellProps[] = cellDataList.map((cellData, index) => ({
2023
...cellData,

src/components/DynamicCard.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import React from "react";
22
import { useLoaderData } from "react-router-dom";
33
import Card from "./Card";
44
import flattenWordList from "../lib/flattenWordList";
5-
import type { Config } from "../data/config";
5+
import type { KeyedConfig } from "../loaders/configLoaders";
66

77
const DynamicCard: React.FC = () => {
8-
const { name = "", url, wordList } = useLoaderData() as Config;
8+
const { id, name = "", url, wordList } = useLoaderData() as KeyedConfig;
99

1010
if (wordList.length > 0) {
1111
// Card saves the word list to the session, so don't render a card if the
1212
// word list hasn't loaded yet
1313
const words = flattenWordList(wordList);
14-
return <Card wordList={words} name={name} url={url} />;
14+
return <Card wordList={words} name={name} url={url} id={id} />;
1515
}
1616
};
1717

src/hooks/useCard.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ type GetterSetters = [
1010
ButtonClickHandler,
1111
];
1212

13-
const useCard = (wordList: string[]): GetterSetters => {
13+
const useCard = (id: string, wordList: string[]): GetterSetters => {
1414
const newWords = (): string[] => shuffle(wordList).slice(0, 25);
1515

1616
const newCellDataList = function (): CellData[] {
@@ -19,8 +19,10 @@ const useCard = (wordList: string[]): GetterSetters => {
1919
});
2020
};
2121

22-
const [cellDataList, setCellDataList] =
23-
useSession<CellData[]>(newCellDataList);
22+
const [cellDataList, setCellDataList] = useSession<CellData[]>({
23+
keyName: `bingoSession-${id}`,
24+
initFunction: newCellDataList,
25+
});
2426

2527
const setStamped = (index: number, stamped: boolean): void => {
2628
setCellDataList(

src/hooks/useSession.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ import GuaranteedJsonSession from "../lib/GuaranteedJsonSession";
33

44
type GetterSetter<T> = [T, (data: T) => void];
55

6-
const useSession = function <T>(initialData: () => T): GetterSetter<T> {
6+
const useSession = function <T>({
7+
keyName,
8+
initFunction,
9+
}: {
10+
keyName: string;
11+
initFunction: () => T;
12+
}): GetterSetter<T> {
713
const session = useMemo(
8-
() => new GuaranteedJsonSession<T>(initialData),
9-
[initialData],
14+
() => new GuaranteedJsonSession<T>({ keyName, initFunction }),
15+
[initFunction],
1016
);
1117

1218
const [data, setData] = useState<T>(session.sessionData);

src/lib/GuaranteedJsonSession.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ beforeEach(() => {
77

88
test("can store an array of cell data in the session", () => {
99
const initFunction = () => [];
10-
const session = new GuaranteedJsonSession<CellData[]>(initFunction);
10+
const session = new GuaranteedJsonSession<CellData[]>({
11+
keyName: "key",
12+
initFunction,
13+
});
1114

1215
session.sessionData = [
1316
{ word: "Aardvark", stamped: false },
@@ -22,7 +25,10 @@ test("can store an array of cell data in the session", () => {
2225

2326
test("returns a new array if nothing is stored", () => {
2427
const initFunction = () => [{ word: "Camel", stamped: false }];
25-
const session = new GuaranteedJsonSession<CellData[]>(initFunction);
28+
const session = new GuaranteedJsonSession<CellData[]>({
29+
keyName: "key",
30+
initFunction,
31+
});
2632

2733
expect(session.sessionData).toEqual([{ word: "Camel", stamped: false }]);
2834
});

src/lib/GuaranteedJsonSession.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@ class GuaranteedJsonSession<T> {
77
#session: JsonSession<T>;
88
#initFunction: () => T;
99

10-
constructor(initFunction: () => T) {
11-
this.#session = new JsonSession<T>();
10+
constructor({
11+
keyName,
12+
initFunction,
13+
}: {
14+
keyName: string;
15+
initFunction: () => T;
16+
}) {
17+
this.#session = new JsonSession<T>(keyName);
1218
this.#initFunction = initFunction;
1319
}
1420

src/lib/JsonDataImporter.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

src/lib/JsonSession.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ beforeEach(() => {
66
});
77

88
test("can store an array of cell data in the session", () => {
9-
const session = new JsonSession<CellData[]>();
9+
const session = new JsonSession<CellData[]>("key");
1010

1111
session.sessionData = [
1212
{ word: "Aardvark", stamped: false },
@@ -20,7 +20,7 @@ test("can store an array of cell data in the session", () => {
2020
});
2121

2222
test("returns null if nothing is stored", () => {
23-
const session = new JsonSession();
23+
const session = new JsonSession("Key");
2424

2525
expect(session.sessionData).toEqual(null);
2626
});

0 commit comments

Comments
 (0)