Last active
November 26, 2018 02:18
-
-
Save flipjs/b8bf9ba0197e0ea65518d0dcfddd5f51 to your computer and use it in GitHub Desktop.
Redux Ducks
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
// 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 | |
} | |
} | |
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
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 | |
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
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) | |
} | |
} | |
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
// 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