Add redux, fix routing, add normalize
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
import {
|
||||
CalculatorActionTypes,
|
||||
SET_CLASS,
|
||||
ADD_POINT,
|
||||
REMOVE_POINT,
|
||||
SET_POINTS,
|
||||
Points,
|
||||
} from './types'
|
||||
|
||||
export const setClass = (classId: number, points?: Points): CalculatorActionTypes => ({
|
||||
type: SET_CLASS,
|
||||
classId,
|
||||
points
|
||||
})
|
||||
|
||||
export const addPoint = (talentId: number): CalculatorActionTypes => ({
|
||||
type: ADD_POINT,
|
||||
talentId
|
||||
})
|
||||
|
||||
export const removePoint = (talentId: number): CalculatorActionTypes => ({
|
||||
type: REMOVE_POINT,
|
||||
talentId
|
||||
})
|
||||
|
||||
export const setPoints = (points: Points): CalculatorActionTypes => ({
|
||||
type: SET_POINTS,
|
||||
points
|
||||
})
|
||||
@@ -0,0 +1,76 @@
|
||||
import { Map } from 'immutable'
|
||||
import {
|
||||
CalculatorState,
|
||||
CalculatorActionTypes,
|
||||
SET_CLASS,
|
||||
ADD_POINT,
|
||||
REMOVE_POINT,
|
||||
SET_POINTS
|
||||
} from './types'
|
||||
import { canLearnTalent, canUnlearnTalent, encodeKnownTalents } from '../../lib/tree'
|
||||
import { talentsById } from '../../data/talents'
|
||||
|
||||
const initialState: CalculatorState = {
|
||||
classId: null,
|
||||
points: Map<number, number>(),
|
||||
pointsEncoded: ''
|
||||
}
|
||||
|
||||
export default function(state = initialState, action: CalculatorActionTypes): CalculatorState {
|
||||
const { classId, points } = state
|
||||
|
||||
switch (action.type) {
|
||||
case SET_CLASS: {
|
||||
if (classId === action.classId) {
|
||||
return state
|
||||
}
|
||||
return {
|
||||
...state,
|
||||
classId: action.classId,
|
||||
points: action.points || Map(),
|
||||
pointsEncoded: ''
|
||||
}
|
||||
}
|
||||
|
||||
case ADD_POINT: {
|
||||
const { talentId } = action
|
||||
const talent = talentsById[talentId]
|
||||
if (!canLearnTalent(points, talent)) {
|
||||
return state
|
||||
}
|
||||
const nextPoints = points.set(talentId, points.get(talentId, 0) + 1)
|
||||
return {
|
||||
...state,
|
||||
points: nextPoints,
|
||||
pointsEncoded: encodeKnownTalents(nextPoints, classId)
|
||||
}
|
||||
}
|
||||
|
||||
case REMOVE_POINT: {
|
||||
const { talentId } = action
|
||||
const talent = talentsById[talentId]
|
||||
if (!canUnlearnTalent(points, talent)) {
|
||||
return state
|
||||
}
|
||||
const nextPoints = points.set(talentId, points.get(talentId, 1) - 1)
|
||||
return {
|
||||
...state,
|
||||
points: nextPoints,
|
||||
pointsEncoded: encodeKnownTalents(nextPoints, classId)
|
||||
}
|
||||
}
|
||||
|
||||
case SET_POINTS: {
|
||||
if (points.equals(action.points)) {
|
||||
return state
|
||||
}
|
||||
return {
|
||||
...state,
|
||||
points: action.points
|
||||
}
|
||||
}
|
||||
|
||||
default:
|
||||
return state
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import { Map } from 'immutable'
|
||||
|
||||
export type Points = Map<number, number>
|
||||
|
||||
export interface CalculatorState {
|
||||
classId: number
|
||||
points: Points
|
||||
pointsEncoded: string
|
||||
}
|
||||
|
||||
export const SET_CLASS = 'SET_CLASS'
|
||||
export const ADD_POINT = 'ADD_POINT'
|
||||
export const REMOVE_POINT = 'REMOVE_POINT'
|
||||
export const SET_POINTS = 'SET_POINTS'
|
||||
|
||||
interface SetClassAction {
|
||||
type: typeof SET_CLASS
|
||||
classId: number
|
||||
points?: Points
|
||||
}
|
||||
|
||||
interface AddPointAction {
|
||||
type: typeof ADD_POINT
|
||||
talentId: number
|
||||
}
|
||||
|
||||
interface RemovePointAction {
|
||||
type: typeof REMOVE_POINT
|
||||
talentId: number
|
||||
}
|
||||
|
||||
interface SetPointsAction {
|
||||
type: typeof SET_POINTS
|
||||
points: Points
|
||||
}
|
||||
|
||||
export type CalculatorActionTypes = SetClassAction | AddPointAction | RemovePointAction |
|
||||
SetPointsAction
|
||||
@@ -0,0 +1,17 @@
|
||||
import { createStore, combineReducers, compose } from 'redux'
|
||||
import calculator from './calculator/reducers'
|
||||
|
||||
const rootReducer = combineReducers({
|
||||
calculator,
|
||||
})
|
||||
|
||||
export type AppState = ReturnType<typeof rootReducer>
|
||||
|
||||
const store = createStore(
|
||||
rootReducer,
|
||||
compose(
|
||||
(window as any).__REDUX_DEVTOOLS_EXTENSION__ && (window as any).__REDUX_DEVTOOLS_EXTENSION__()
|
||||
),
|
||||
)
|
||||
|
||||
export default store
|
||||
Reference in New Issue
Block a user