Skip to content

Commit

Permalink
♻️ manage one entry on localstorage per user
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin Burg committed Apr 9, 2024
1 parent 6f95aef commit 5b1c78d
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions context/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,8 @@ type action =
};

const saveState = (state: tezosState) => {
const storage = JSON.parse(localStorage.getItem("app_state")!);
localStorage.setItem(
"app_state",
`app_state:${state.address}`,
JSON.stringify({
contracts: state.contracts,
aliases: state.aliases,
Expand Down Expand Up @@ -296,8 +295,13 @@ function reducer(state: tezosState, action: action): tezosState {
};
}
case "login": {
const rawStorage = window!.localStorage.getItem(
`app_state:${action.address}`
)!;
const storage: storage = JSON.parse(rawStorage);
return {
...state,
...storage,
balance: action.balance,
accountInfo: action.accountInfo,
address: action.address,
Expand Down Expand Up @@ -369,14 +373,9 @@ function reducer(state: tezosState, action: action): tezosState {
}
}
function init(): tezosState {
let rawStorage = window!.localStorage.getItem("app_state")!;
let storage: storage = JSON.parse(rawStorage);

return {
...emptyState(),
...storage,
};
return emptyState();
}

let AppStateContext: Context<tezosState | null> =
createContext<tezosState | null>(null);
let AppDispatchContext: Context<Dispatch<action> | null> =
Expand Down

0 comments on commit 5b1c78d

Please sign in to comment.