Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. /**
  2. * Factory used to create reducer and actions to perfom fetch functions.
  3. * @module fetch.factory
  4. * @member redux/factories
  5. */
  6.  
  7. export const fetchActionConstants = (reducerId) => ({
  8. FETCH_REQUEST: `${reducerId}::FETCH_REQUEST`,
  9. FETCH_SUCCESS: `${reducerId}::FETCH_SUCCESS`,
  10. FETCH_FAILURE: `${reducerId}::FETCH_FAILURE`,
  11. FETCH_RESET: `${reducerId}::FETCH_RESET`,
  12. })
  13.  
  14. const DEFAULT_STATE = {
  15. isFetching: false,
  16. error: undefined,
  17. lastUpdated: undefined,
  18. data: undefined,
  19. }
  20.  
  21. // ======================================
  22. // Reducer factory
  23. // ======================================
  24. const defaultDataFormatter = payload => payload.data
  25.  
  26. /**
  27. * @param {string} reducerId - a unique name to identify the reducer
  28. * @param {Object} options - Reducer configuration.
  29. {
  30. iniData?: undefined, // initial value for data field of state
  31. dataFormatter?: payload => payload.data, // Function called after succes fetch GET to assign
  32. // a new value
  33. }
  34. * to data field of state.
  35. * @returns {Function} reducer for fetch with GET method
  36. */
  37. export default (
  38. reducerId,
  39. {
  40. iniData = undefined,
  41. dataFormatter = defaultDataFormatter,
  42. } = {}
  43. ) => {
  44. // Action constants
  45. const {
  46. FETCH_REQUEST, FETCH_SUCCESS, FETCH_FAILURE, FETCH_RESET,
  47. } = fetchActionConstants(reducerId)
  48.  
  49. // Initial state
  50. const initialState = {
  51. ...DEFAULT_STATE,
  52. data: iniData,
  53. }
  54.  
  55. // Return the fetch reducer
  56. return (state = initialState, action) => {
  57. const { type, payload } = action
  58. switch (type) {
  59. case FETCH_REQUEST:
  60. return {
  61. ...state,
  62. isFetching: true,
  63. error: undefined,
  64. }
  65. case FETCH_SUCCESS:
  66. return {
  67. ...state,
  68. isFetching: false,
  69. error: undefined,
  70. lastUpdated: payload.receivedAt,
  71. data: dataFormatter(payload),
  72. }
  73. case FETCH_FAILURE:
  74. return {
  75. ...state,
  76. isFetching: false,
  77. error: payload,
  78. }
  79. case FETCH_RESET:
  80. return initialState
  81. default:
  82. return state
  83. }
  84. }
  85. }
  86.  
  87. // ======================================
  88. // Action creators
  89. // ======================================
  90. export const fetchRequest = (type) => ({ type })
  91.  
  92. export const fetchSuccess = (type, response: { data, headers?}) => ({
  93. type,
  94. payload: {
  95. data: response.data,
  96. headers: response.headers,
  97. receivedAt: new Date(),
  98. },
  99. })
  100.  
  101. export const fetchFailure = (type, error) => ({
  102. type,
  103. payload: error,
  104. error: true,
  105. })
  106.  
  107. export const resetFetch = (type) => ({ type })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement