Last active
December 7, 2016 07:17
-
-
Save mariusandra/1b8eeb3f2f4e542188b915e27133c858 to your computer and use it in GitHub Desktop.
kea/logic example
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
// see the accompanying kea/saga example here: https://gist.github.com/mariusandra/e6091b393e153c9edf3ba451a9d91aeb | |
import { PropTypes } from 'react' | |
import Logic, { createMapping } from 'kea/logic' | |
import { initFromProps } from '~/scenes/index' | |
class InboxThreadLogic extends Logic { | |
// PATH | |
path = () => ['scenes', 'inboxThread', 'index'] | |
// ACTIONS | |
actions = ({ constants }) => ({ | |
editNewMessage: (id, message) => ({ id, message }), | |
makeBookingRequest: (message) => ({ message }), | |
makeBookingFailure: (error) => ({ error }), | |
sendMessageRequest: (message, supportWithThread) => ({ message, supportWithThread }), | |
sendMessageSuccess: (id, message) => ({ id, message }), | |
sendMessageFailure: (errors) => ({ errors }) | |
}) | |
// STRUCTURE | |
structure = ({ actions, constants }) => ({ | |
newMessages: [{}, PropTypes.object, { persist: true }, { // store in local storage | |
[actions.editNewMessage]: (state, payload) => Object.assign({}, state, { [payload.id]: payload.message }), | |
[actions.sendMessageSuccess]: (state, payload) => Object.assign({}, state, { [payload.id]: '' }) | |
}], | |
errors: [null, PropTypes.object, { | |
[actions.editNewMessage]: () => null, | |
[actions.makeBookingRequest]: () => null, | |
[actions.sendMessageRequest]: () => null, | |
[actions.sendMessageSuccess]: () => null, | |
[actions.sendMessageFailure]: (_, payload) => payload.errors | |
}], | |
threadId: [null, PropTypes.number, { | |
[initFromProps]: (_, payload) => payload.threadId | |
}], | |
messages: [[], PropTypes.array, { | |
[initFromProps]: (_, payload) => payload.messages, | |
[actions.sendMessageSuccess]: (state, payload) => [payload.message].concat(state) | |
}], | |
bookingSubmitting: [false, PropTypes.bool, { | |
[actions.makeBookingRequest]: () => true, | |
[actions.makeBookingFailure]: () => false | |
}], | |
messageSubmitting: [false, PropTypes.bool, { | |
[actions.sendMessageRequest]: () => true, | |
[actions.sendMessageSuccess]: () => false, | |
[actions.sendMessageFailure]: () => false | |
}] | |
}) | |
} | |
export default new InboxThreadLogic().init() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment