File tree Expand file tree Collapse file tree 2 files changed +32
-1
lines changed Expand file tree Collapse file tree 2 files changed +32
-1
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,8 @@ import { roles } from "../utils/data/roles";
4
4
5
5
const RadarContext = React . createContext ( ) ;
6
6
7
+ const lsKeyData = "youData" ;
8
+
7
9
function radarReducer ( state , action ) {
8
10
switch ( action . type ) {
9
11
case "upload" : {
@@ -23,8 +25,25 @@ function radarReducer(state, action) {
23
25
return { ...state , data } ;
24
26
}
25
27
case "update" : {
28
+ localStorage . setItem ( lsKeyData , JSON . stringify ( state . data . datasets [ state . data . you ] . data ) ) ;
26
29
return { ...state , data : state . data } ;
27
30
}
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
+
28
47
default : {
29
48
throw new Error ( `Unhandled action type: ${ action . type } ` ) ;
30
49
}
Original file line number Diff line number Diff line change @@ -3,9 +3,21 @@ import FormControl from "@mui/material/FormControl";
3
3
import InputLabel from "@mui/material/InputLabel" ;
4
4
import MenuItem from "@mui/material/MenuItem" ;
5
5
import Select from "@mui/material/Select" ;
6
- import { RadarConsumer } from "./RadarProvider" ;
6
+ import { RadarConsumer , useData } from "./RadarProvider" ;
7
+ import { useEffect } from "react" ;
7
8
8
9
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
+
9
21
return (
10
22
< RadarConsumer >
11
23
{ ( { state, dispatch } ) => (
You can’t perform that action at this time.
0 commit comments