-
-
Save joepie91/d67c4f5a9a8f3ad8f16f3a72ff7ea1b4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
const types = require("../plumbing/types"); | |
module.exports = { | |
name: "uiAddFriend", | |
state: { | |
uiAddFriend: types.UiFormState() | |
}, | |
stateTransformations: { | |
processing: (state, action) => { | |
return state.updateIn(["uiAddFriend"], (formState) => { | |
return formState.merge({ | |
processing: true, | |
errorMessage: null | |
}); | |
}); | |
}, | |
error: (state, action) => { | |
let {errorMessage} = action; | |
return state.updateIn(["uiAddFriend"], (formState) => { | |
return formState.merge({ | |
processing: false, | |
errorMessage: errorMessage | |
}); | |
}); | |
}, | |
complete: (state, action) => { | |
return state.setIn(["uiAddFriend", "processing"], false); | |
}, | |
}, | |
logicHandlers: { | |
submit: { | |
errorType: "error", | |
validate: ({getState, action}, allow, reject) => { | |
if (action.username.length === 0) { | |
reject({ | |
errorType: "noUsernameSpecified", | |
errorMessage: "You must specify a username." | |
}); | |
} else if (getState().friends.has(action.username)) { | |
reject({ | |
errorType: "friendAlreadyInList", | |
errorMessage: "There's already a user with that username in your friends list." | |
}); | |
} else { | |
allow(); | |
} | |
}, | |
process: ({action, api}, dispatch) => { | |
dispatch("processing"); | |
return api.addFriend({ | |
username: action.username | |
}); | |
}, | |
success: (result) => { | |
let friend = types.Friend(result.friend); | |
return [{ | |
type: "complete", | |
friend: friend | |
}, { | |
type: "@friendAdded", | |
friend: friend | |
}, { | |
type: "@friendAddedDbStored", | |
username: friend.username | |
}]; | |
}, | |
failure: (error) => { | |
/* FIXME: Error filtering to distinguish bugs from end-user errors? */ | |
return { | |
errorMessage: error.message | |
}; | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment