Skip to content

Instantly share code, notes, and snippets.

@matthew-gerstman
Last active August 23, 2018 15:48
Show Gist options
  • Save matthew-gerstman/7447b9ae50d93c4f6123a970bcb170b3 to your computer and use it in GitHub Desktop.
Save matthew-gerstman/7447b9ae50d93c4f6123a970bcb170b3 to your computer and use it in GitHub Desktop.
// data/wizards/reducer.ts
import { WizardAction, WizardActionTypes, WizardNamespaceShape } from "./types";
const defaultState: WizardNamespaceShape = {
harryPotter: {
name: "Harry Potter",
parentsAlive: false,
spells: []
}
};
export default function reducer(
state: WizardNamespaceShape,
action: WizardAction
) {
switch (action.type) {
case WizardActionTypes.KillParents: {
const { id } = action.payload;
return {
...state,
[id]: {
...state[id],
parentsAlive: false
}
};
}
case WizardActionTypes.LearnSpell: {
const { id, spell } = action.payload;
return {
...state,
[id]: {
...state[id],
spells: [...state[id].spells, spell]
}
};
}
default: {
return state;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment