Last active
December 5, 2016 18:26
-
-
Save mariusandra/e6091b393e153c9edf3ba451a9d91aeb to your computer and use it in GitHub Desktop.
kea/saga 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/logic example here: https://gist.github.com/mariusandra/1b8eeb3f2f4e542188b915e27133c858 | |
import Saga from 'kea/saga' | |
import sceneLogic from './logic' | |
export default class InboxThreadSaga extends Saga { | |
// pick actions from logic stores | |
actions = () => ([ | |
sceneLogic, [ | |
'makeBookingRequest', | |
'makeBookingFailure', | |
'sendMessageRequest', | |
'sendMessageSuccess', | |
'sendMessageFailure' | |
] | |
]) | |
// delegates to the redux-saga takeEvery function | |
// also available: `takeLatest` | |
takeEvery = ({actions}) => ({ | |
[actions.makeBookingRequest]: this.bookingWorker, | |
[actions.sendMessageRequest]: this.messageWorker | |
}) | |
// called when saga is run | |
run = function * () { | |
cosnole.log('Running saga') | |
console.log(this) | |
console.log(this.actions) | |
} | |
// called when saga is cancelled | |
canceled = function * () { | |
console.log('Saga got cancelled') | |
} | |
// on every makeBookingRequest | |
bookingWorker = function * (action) { | |
const { makeBookingFailure } = this.actions | |
const { message } = action.payload | |
const courseId = yield sceneLogic.get('courseId') | |
yield put(makeBookingFailure(courseId)) | |
} | |
// on every sendMessageRequest | |
messageWorker = function * (action) { | |
const { sendMessageSuccess, sendMessageFailure } = this.actions | |
const { message, supportWithThread } = action.payload | |
if (!message.trim()) { | |
yield put(sendMessageFailure({ message: 'The message is empty!' })) | |
return | |
} | |
// use a selector to get the thread id | |
const threadId = yield sceneLogic.get('threadId') | |
const response = yield apiCall(threadId, message) | |
if (response.success) { | |
yield put(sendMessageSuccess(threadId, response.message)) | |
messg.success("Hurray", 2500) | |
} else { | |
messg.error("Damn", 2500) | |
yield put(sendMessageFailure({ message: response.error })) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment