Skip to content

Commit

Permalink
Persist vegas stores (the house doesn't forget)
Browse files Browse the repository at this point in the history
  • Loading branch information
tswaters committed Feb 24, 2019
1 parent 1c8c1f0 commit 0022b67
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"react-dom": "^16.8.3",
"react-redux": "^5.1.1",
"redux-logger": "^3.0.6",
"redux-subscribe-reselect": "^1.0.2",
"redux-thunk": "^2.3.0",
"reselect": "^3.0.1"
}
Expand Down
30 changes: 30 additions & 0 deletions src/ts/lib/persist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { ScoreStore } from '../redux/score'
import { ScoringType } from '../redux/globals'

export function saveScore(score: ScoreStore): void {
try {
if (score.scoringType === ScoringType.vegas) {
localStorage.setItem('score', score.score.toString())
}
} catch (err) {
// ehh, that sucks
}
}

export function getSavedScore(): number {
try {
let score = localStorage.getItem('score')
if (score == null) {
return 0
}

let parsed = parseInt(score, 10)
if (Number.isNaN(parsed)) {
return 0
}

return parsed
} catch (err) {
return 0
}
}
8 changes: 6 additions & 2 deletions src/ts/redux/score.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { INITIALIZE, GlobalActions, ScoreType, ScoringType } from './globals'
import { undoable } from './undoable'
import { getSavedScore } from '../lib/persist'

const INCREMENT_SCORE = 'INCREMENT_SCORE'
type INCREMENT_SCORE = typeof INCREMENT_SCORE
Expand All @@ -20,15 +21,18 @@ export type ScoreStore = {

export type ScoreActions = IncrementAction

const initialState: ScoreStore = { score: 0, scoringType: ScoringType.regular }
const score = getSavedScore()
const initialState: ScoreStore = { score, scoringType: ScoringType.regular }

function scoreReducer(
state: ScoreStore = initialState,
action: ScoreActions | GlobalActions
): ScoreStore {
if (action.type === INITIALIZE) {
const oldScore = getSavedScore()

return {
score: action.scoringType === ScoringType.vegas ? state.score - 52 : 0,
score: action.scoringType === ScoringType.vegas ? oldScore - 52 : 0,
scoringType: action.scoringType
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/ts/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { createStore, applyMiddleware } from 'redux'
import thunk, { ThunkMiddleware } from 'redux-thunk'
import { createLogger } from 'redux-logger'
import reducer, { StoreState, StoreActions } from './redux'
import subscribe from 'redux-subscribe-reselect'
import { ScoreStore } from './redux/score'
import { getScore } from './redux/selectors'
import { saveScore } from './lib/persist'

export default function configStore(state?: object) {
const middleware = []
Expand All @@ -12,5 +16,7 @@ export default function configStore(state?: object) {
middleware.push(createLogger())
}

return createStore(reducer, state!, applyMiddleware(...middleware))
const store = createStore(reducer, state!, applyMiddleware(...middleware))
subscribe(store, getScore, (score: ScoreStore) => saveScore(score))
return store
}

0 comments on commit 0022b67

Please sign in to comment.