Skip to content

Commit 5ebbf69

Browse files
authored
Merge pull request #23 from sohebmahmood-madetech/persist-radar
Persist radar choices
2 parents 44fb9c5 + febe3c0 commit 5ebbf69

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/components/RadarProvider.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { roles } from "../utils/data/roles";
44

55
const RadarContext = React.createContext();
66

7+
const lsKeyData = "youData";
8+
79
function radarReducer(state, action) {
810
switch (action.type) {
911
case "upload": {
@@ -23,8 +25,25 @@ function radarReducer(state, action) {
2325
return { ...state, data };
2426
}
2527
case "update": {
28+
localStorage.setItem(lsKeyData, JSON.stringify(state.data.datasets[state.data.you].data));
2629
return { ...state, data: state.data };
2730
}
31+
case "loadFromLocalStorage": {
32+
const storageData = JSON.parse(localStorage.getItem(lsKeyData));
33+
if (!storageData || !Array.isArray(storageData)) {
34+
return state;
35+
}
36+
37+
const updatedDatasets = state.data.datasets.map((dataset, index) =>
38+
index === state.data.you ? { ...dataset, data: storageData } : dataset
39+
);
40+
41+
return {
42+
...state,
43+
data: { ...state.data, datasets: updatedDatasets}
44+
};
45+
}
46+
2847
default: {
2948
throw new Error(`Unhandled action type: ${action.type}`);
3049
}

src/components/SkillsSelector.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,21 @@ import FormControl from "@mui/material/FormControl";
33
import InputLabel from "@mui/material/InputLabel";
44
import MenuItem from "@mui/material/MenuItem";
55
import Select from "@mui/material/Select";
6-
import { RadarConsumer } from "./RadarProvider";
6+
import { RadarConsumer, useData } from "./RadarProvider";
7+
import { useEffect } from "react";
78

89
const SkillsSelector = () => {
10+
11+
const ctx = useData();
12+
13+
useEffect(() => {
14+
ctx.dispatch({type: "loadFromLocalStorage"});
15+
},
16+
// Intentionally disabling the warning here because we want to load data only once
17+
// when the component mounts
18+
// eslint-disable-next-line
19+
[]);
20+
921
return (
1022
<RadarConsumer>
1123
{({ state, dispatch }) => (

0 commit comments

Comments
 (0)