Skip to content

Commit

Permalink
fix: save state first time if not present
Browse files Browse the repository at this point in the history
  • Loading branch information
sohrab- committed Jul 26, 2022
1 parent 6afed43 commit d4a0b4e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 28 deletions.
30 changes: 17 additions & 13 deletions src/hooks/useOptionsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@ import produce from "immer";
import create from "zustand";
import { DEFAULT_OPTIONS_STATE, OptionsState } from "../models/state";

const LOCAL_STORAGE_KEY = "bscolOptionsState";
const LOCAL_STORAGE_KEY = "bcsolOptionsState";

// excludes functions
const saveState = ({ transactionOptions, appOptions }: OptionsState) => {
localStorage.setItem(
LOCAL_STORAGE_KEY,
JSON.stringify({
transactionOptions,
appOptions,
})
);
};

export const useOptionsStore = create<OptionsState>((set) => {
// retrieve local storage
Expand All @@ -11,8 +22,10 @@ export const useOptionsStore = create<OptionsState>((set) => {
? JSON.parse(existingStateString)
: DEFAULT_OPTIONS_STATE;

// TODO capture that it's the first time and display something helpful
// TODO save options if first time
if (!existingStateString) {
// TODO display a welcome
saveState(state);
}

return {
...state,
Expand All @@ -23,14 +36,5 @@ export const useOptionsStore = create<OptionsState>((set) => {
});

useOptionsStore.subscribe((state) => {
// exclude functions
const { transactionOptions, appOptions } = state;

localStorage.setItem(
LOCAL_STORAGE_KEY,
JSON.stringify({
transactionOptions,
appOptions,
})
);
saveState(state);
});
36 changes: 21 additions & 15 deletions src/hooks/useTransactionStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,22 @@ import {
TransactionState,
} from "../models/state";

const LOCAL_STORAGE_KEY = "bscolTransactionState";
const LOCAL_STORAGE_KEY = "bcsolTransactionState";

// exclude functions
const saveState = ({ transaction, results, uiState }: TransactionState) => {
// in progress should not survive page reloads
const { inProgress: _inProgress, ...restOfResults } = results;

localStorage.setItem(
LOCAL_STORAGE_KEY,
JSON.stringify({
transaction,
results: restOfResults,
uiState,
})
);
};

export const useTransactionStore = create<TransactionState>((set) => {
// retrieve local storage
Expand All @@ -16,6 +31,10 @@ export const useTransactionStore = create<TransactionState>((set) => {
? JSON.parse(existingStateString)
: DEFAULT_TRANSACTION_STATE;

if (!existingStateString) {
saveState(state);
}

return {
...state,
set: (fn) => {
Expand Down Expand Up @@ -43,18 +62,5 @@ export const useTransactionStore = create<TransactionState>((set) => {
});

useTransactionStore.subscribe((state) => {
// exclude functions
const { transaction, results, uiState } = state;

// in progress should not survive page reloads
const { inProgress: _inProgress, ...restOfResults } = results;

localStorage.setItem(
LOCAL_STORAGE_KEY,
JSON.stringify({
transaction,
results: restOfResults,
uiState,
})
);
saveState(state);
});

0 comments on commit d4a0b4e

Please sign in to comment.