eduedix

Untitled

Jun 27th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. import { createReducer, createActions } from 'reduxsauce'
  2. import Immutable from 'seamless-immutable'
  3. import DeviceInfo from 'react-native-device-info'
  4.  
  5. /* ------------- Types and Action Creators ------------- */
  6.  
  7. const { Types, Creators } = createActions({
  8. beaconFetchEntriesRequest: null,
  9. beaconFetchEntriesSuccess: ['entries'],
  10. beaconFetchSuccess: ['entries'],
  11. beaconFetchEntriesFail: null,
  12. beaconSelectDescription: ['descriptionIdx'],
  13. beaconAddDescription: ['description'],
  14. beaconUpdateBeaconInfo: ['beaconInfo'],
  15. beaconSaveEntry: ['entry'],
  16. beaconSelectBeacon: ['beaconUuid'],
  17. beaconRequest: ['data'],
  18. beaconSuccess: ['payload'],
  19. beaconFailure: null
  20. })
  21.  
  22. export const BeaconTypes = Types
  23. export default Creators
  24.  
  25. /* ------------- Initial State ------------- */
  26.  
  27. export const INITIAL_STATE = Immutable({
  28. fetchingEntries: null,
  29. fetchingEntriesError: null,
  30. selectedDescription: null,
  31. descriptions: [],
  32. beaconInfo: [],
  33. entries: [],
  34. deviceModel: DeviceInfo.getModel(),
  35. selectedBeacon: null,
  36.  
  37. data: null,
  38. fetching: null,
  39. payload: null,
  40. error: null
  41. })
  42.  
  43. /* ------------- Reducers ------------- */
  44.  
  45. export const fetchEntriesRequest = (state) => state.merge({ fetchingEntries: true })
  46.  
  47. export const fetchSuccess = (state, { entries }) => {
  48. console.log('reduced: ', entries)
  49. }
  50.  
  51. export const fetchEntriesSuccess = (state, { entries }) => {
  52. console.log(entries)
  53. // state.merge({ fetchingEntries: false, fetchingEntriesError: null, entries: []})
  54. }
  55. // export const fetchEntriesSuccess = (state, { data }) => state.merge({ fetchingEntries: false, fetchingEntriesError: null, entries: data })
  56. export const fetchEntriesFail = (state) => state.merge({ fetchingEntries: false, fetchingEntriesError: true, entries: [] })
  57.  
  58. export const selectDescription = (state, { descriptionIdx }) =>
  59. state.merge({ selectedDescription: state.descriptions[descriptionIdx] })
  60.  
  61. export const addDescription = (state, { description }) =>
  62. state.merge({ descriptions: [...state.descriptions, description] })
  63.  
  64. export const updateBeaconInfo = (state, { beaconInfo }) =>
  65. state.merge({ beaconInfo: [...state.beaconInfo, beaconInfo] })
  66.  
  67. export const saveEntry = (state, { entry }) =>
  68. state.merge({ entries: [entry, ...state.entries] })
  69.  
  70. export const selectBeacon = (state, { beaconUuid }) =>
  71. state.merge({ selectedBeacon: beaconUuid })
  72.  
  73. // request the data from an api
  74. export const request = (state, { data }) =>
  75. state.merge({ fetching: true, data, payload: null })
  76.  
  77. // successful api lookup
  78. export const success = (state, action) => {
  79. const { payload } = action
  80. return state.merge({ fetching: false, error: null, payload })
  81. }
  82.  
  83. // Something went wrong somewhere.
  84. export const failure = state =>
  85. state.merge({ fetching: false, error: true, payload: null })
  86.  
  87. /* ------------- Hookup Reducers To Types ------------- */
  88.  
  89. export const reducer = createReducer(INITIAL_STATE, {
  90. [Types.BEACON_FETCH_ENTRIES_REQUEST]: fetchEntriesRequest,
  91. [Types.BEACON_FETCH_SUCCESS]: fetchSuccess,
  92. [Types.BEACON_FETCH_ENTRIES_SUCCESS]: fetchEntriesSuccess,
  93. [Types.BEACON_FETCH_ENTRIES_FAIL]: fetchEntriesFail,
  94. [Types.BEACON_SELECT_DESCRIPTION]: selectDescription,
  95. [Types.BEACON_ADD_DESCRIPTION]: addDescription,
  96. [Types.BEACON_UPDATE_BEACON_INFO]: updateBeaconInfo,
  97. [Types.BEACON_SAVE_ENTRY]: saveEntry,
  98. [Types.BEACON_SELECT_BEACON]: selectBeacon,
  99. [Types.BEACON_REQUEST]: request,
  100. [Types.BEACON_SUCCESS]: success,
  101. [Types.BEACON_FAILURE]: failure
  102. })
Add Comment
Please, Sign In to add comment