Skip to content

Instantly share code, notes, and snippets.

@flipjs
Last active November 26, 2018 02:18
Show Gist options
  • Save flipjs/b8bf9ba0197e0ea65518d0dcfddd5f51 to your computer and use it in GitHub Desktop.
Save flipjs/b8bf9ba0197e0ea65518d0dcfddd5f51 to your computer and use it in GitHub Desktop.
Redux Ducks
// Reducer Actions
import keyMirror from 'key-mirror'
export const MESSAGES = keyMirror({
GET_ALL_ADVISORIES_REQUESTED: null,
GET_ALL_ADVISORIES_RESOLVED: null,
GET_ALL_ADVISORIES_REJECTED: null
})
export function requestGetAllAdvisories () {
return {
type: MESSAGES.GET_ALL_ADVISORIES_REQUESTED
}
}
export function resolveGetAllAdvisories (data) {
return {
type: MESSAGES.GET_ALL_ADVISORIES_RESOLVED,
payload: data
}
}
export function rejectGetAllAdvisories (error) {
return {
type: MESSAGES.GET_ALL_ADVISORIES_REJECTED,
payload: error
}
}
import { MESSAGES } from './advisories-messages'
export const initialState = {
isFetching: false,
isStale: false,
records: [],
pagination: {},
error: null
}
function advisoriesReducer (state = initialState, action) {
const {type, payload} = action
switch (type) {
case MESSAGES.GET_ALL_ADVISORIES_REQUESTED:
return {
...state,
isFetching: true,
isStale: false,
error: null
}
case MESSAGES.GET_ALL_ADVISORIES_RESOLVED:
return {
...state,
...payload,
isFetching: false,
error: null
}
case MESSAGES.GET_ALL_ADVISORIES_REJECTED:
return {
...state,
...payload,
isFetching: false
}
default:
return state
}
}
export default advisoriesReducer
import { takeLatest } from 'redux-saga'
import { put, call } from 'redux-saga/effects'
import { apiGetAllAdvisories } from 'api'
import { SIGNALS } from './advisories-signals'
import * as messages from './advisories-messages'
function * processGetAllAdvisories () {
yield put(messages.requestGetAllAdvisories())
try {
const response = yield call(apiGetAllAdvisories)
yield put(messages.resolveGetAllAdvisories(response.data))
} catch (response) {
yield put(messages.rejectGetAllAdvisories(response.data))
}
}
export function * watchGetAllAdvisories () {
while (true) {
yield takeLatest(SIGNALS.GET_ALL_ADVISORIES, processGetAllAdvisories)
}
}
// UI/System Actions
import keyMirror from 'key-mirror'
export const SIGNALS = keyMirror({
GET_ALL_ADVISORIES: null
})
export function getAllAdvisories () {
return {
type: SIGNALS.GET_ALL_ADVISORIES
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment