Skip to content

Instantly share code, notes, and snippets.

@jquense
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jquense/67ba7b7ca247b1626980 to your computer and use it in GitHub Desktop.
Save jquense/67ba7b7ca247b1626980 to your computer and use it in GitHub Desktop.
function fetchSiteStatus(){
//pretend this is being fetched from a server
return {
type: 'FETCH_SITE_STATUS'
payload: { currentStatus: 'awesome', currentUser: 'John', lastLogin: new Date() }
}
}
function fetchStatuses(){
//also from the server
return {
type: 'FETCH_STATUSES'
payload: [
{ status: 'initial' },
{ status: 'awesome' }
]
}
}
function addStatus(status, location){
return {
type: 'ADD_STATUS'
payload: { status, location }
}
}
var siteState = {
currentStatus: null,
currentuser: 'guest',
lastLogin: null
}
function siteStatusReducer(state = siteState, action){
if ( action.type === 'ADD_STATUS'){
// I need to ONLY update currentStatus if the status was added at the end of the status stack
// not if it was inserted somewhere in the middle
// tradition flux i'd just use a waitFor() here
return { ...state, currentStatus: ??? }
}
if (action.type === 'FETCH_SITE_STATUS')
return action.payload // { currentStatus: 'awesome', ... }
return state
}
function statusesReducer(state = [], action){
var { status, location } = action.payload; //just using indexes in lieu of dates, for simplicity
if ( action.type === 'ADD_STATUS')
return insertAtIndex(state, location)
if (action.type === 'FETCH_STATUSES')
return action.payload // [Statuses]
return state
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment