UNPKG

@25sprout/react-starter

Version:

25sprout web starter with React

50 lines (40 loc) 989 B
import { createAction, handleActions } from 'redux-actions'; import { useRedux } from 'util/hook/redux'; import { wrapFetch } from 'util/api'; export const getMembers = createAction('GET_MEMBERS', async () => { try { const data = await wrapFetch('avatar/apo/25sproutMember.php'); return data; } catch (error) { return { staffs: '' }; } }); export const cleanMembers = createAction('CLEAN_MEMBERS'); export const defaultState = { loading: false, staffs: {}, }; export const reducer = { members: handleActions( { GET_MEMBERS_PENDING: state => ({ ...state, loading: true, }), GET_MEMBERS_FULFILLED: (state, action) => ({ ...state, staffs: action.payload.staffs, loading: false, }), CLEAN_MEMBERS: state => ({ ...state, staffs: {}, }), }, defaultState, ), }; const mapHooksToState = state => ({ members: state.members.staffs, }); export const useMember = () => useRedux(mapHooksToState, { getMembers, cleanMembers });