Skip to content

Instantly share code, notes, and snippets.

@donabrams
Last active September 6, 2016 14:27
Show Gist options
  • Save donabrams/8d533b466d38f63ca6b7a78b80aae5a2 to your computer and use it in GitHub Desktop.
Save donabrams/8d533b466d38f63ca6b7a78b80aae5a2 to your computer and use it in GitHub Desktop.
Javascript state machine example
import { ACTION_PUBLISH, ACTION_UNPUBLISH } from "./actions"
export const STATUS_DRAFT = "STATUS_DRAFT"
export const STATUS_PUBLISHED = "STATUS_PUBLISHED"
export const STATUS_UNPUBLISHED = "STATUS_UNPUBLISHED"
const initialState = {
status: STATUS_DRAFT
}
export default function postReducer(state = initialState, action) {
switch(action.type) {
case ACTION_PUBLISH: {
return {
status: STATUS_PUBLISHED
}
}
case ACTION_UNPUBLISH: {
return {
status: STATUS_UNPUBLISHED
}
}
default: {
return state;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment