Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { createReducer, createActions } from 'reduxsauce'
- import Immutable from 'seamless-immutable'
- import DeviceInfo from 'react-native-device-info'
- /* ------------- Types and Action Creators ------------- */
- const { Types, Creators } = createActions({
- beaconFetchEntriesRequest: null,
- beaconFetchEntriesSuccess: ['entries'],
- beaconFetchSuccess: ['entries'],
- beaconFetchEntriesFail: null,
- beaconSelectDescription: ['descriptionIdx'],
- beaconAddDescription: ['description'],
- beaconUpdateBeaconInfo: ['beaconInfo'],
- beaconSaveEntry: ['entry'],
- beaconSelectBeacon: ['beaconUuid'],
- beaconRequest: ['data'],
- beaconSuccess: ['payload'],
- beaconFailure: null
- })
- export const BeaconTypes = Types
- export default Creators
- /* ------------- Initial State ------------- */
- export const INITIAL_STATE = Immutable({
- fetchingEntries: null,
- fetchingEntriesError: null,
- selectedDescription: null,
- descriptions: [],
- beaconInfo: [],
- entries: [],
- deviceModel: DeviceInfo.getModel(),
- selectedBeacon: null,
- data: null,
- fetching: null,
- payload: null,
- error: null
- })
- /* ------------- Reducers ------------- */
- export const fetchEntriesRequest = (state) => state.merge({ fetchingEntries: true })
- export const fetchSuccess = (state, { entries }) => {
- console.log('reduced: ', entries)
- }
- export const fetchEntriesSuccess = (state, { entries }) => {
- console.log(entries)
- // state.merge({ fetchingEntries: false, fetchingEntriesError: null, entries: []})
- }
- // export const fetchEntriesSuccess = (state, { data }) => state.merge({ fetchingEntries: false, fetchingEntriesError: null, entries: data })
- export const fetchEntriesFail = (state) => state.merge({ fetchingEntries: false, fetchingEntriesError: true, entries: [] })
- export const selectDescription = (state, { descriptionIdx }) =>
- state.merge({ selectedDescription: state.descriptions[descriptionIdx] })
- export const addDescription = (state, { description }) =>
- state.merge({ descriptions: [...state.descriptions, description] })
- export const updateBeaconInfo = (state, { beaconInfo }) =>
- state.merge({ beaconInfo: [...state.beaconInfo, beaconInfo] })
- export const saveEntry = (state, { entry }) =>
- state.merge({ entries: [entry, ...state.entries] })
- export const selectBeacon = (state, { beaconUuid }) =>
- state.merge({ selectedBeacon: beaconUuid })
- // request the data from an api
- export const request = (state, { data }) =>
- state.merge({ fetching: true, data, payload: null })
- // successful api lookup
- export const success = (state, action) => {
- const { payload } = action
- return state.merge({ fetching: false, error: null, payload })
- }
- // Something went wrong somewhere.
- export const failure = state =>
- state.merge({ fetching: false, error: true, payload: null })
- /* ------------- Hookup Reducers To Types ------------- */
- export const reducer = createReducer(INITIAL_STATE, {
- [Types.BEACON_FETCH_ENTRIES_REQUEST]: fetchEntriesRequest,
- [Types.BEACON_FETCH_SUCCESS]: fetchSuccess,
- [Types.BEACON_FETCH_ENTRIES_SUCCESS]: fetchEntriesSuccess,
- [Types.BEACON_FETCH_ENTRIES_FAIL]: fetchEntriesFail,
- [Types.BEACON_SELECT_DESCRIPTION]: selectDescription,
- [Types.BEACON_ADD_DESCRIPTION]: addDescription,
- [Types.BEACON_UPDATE_BEACON_INFO]: updateBeaconInfo,
- [Types.BEACON_SAVE_ENTRY]: saveEntry,
- [Types.BEACON_SELECT_BEACON]: selectBeacon,
- [Types.BEACON_REQUEST]: request,
- [Types.BEACON_SUCCESS]: success,
- [Types.BEACON_FAILURE]: failure
- })
Add Comment
Please, Sign In to add comment