Skip to content

Instantly share code, notes, and snippets.

@joepie91

joepie91/.js Secret

Created February 11, 2019 21:47
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 joepie91/d67c4f5a9a8f3ad8f16f3a72ff7ea1b4 to your computer and use it in GitHub Desktop.
Save joepie91/d67c4f5a9a8f3ad8f16f3a72ff7ea1b4 to your computer and use it in GitHub Desktop.
"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