Skip to content

Instantly share code, notes, and snippets.

@mariusandra
Last active December 5, 2016 18: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 mariusandra/e6091b393e153c9edf3ba451a9d91aeb to your computer and use it in GitHub Desktop.
Save mariusandra/e6091b393e153c9edf3ba451a9d91aeb to your computer and use it in GitHub Desktop.
kea/saga example
// 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