You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
1.5 KiB
79 lines
1.5 KiB
6 years ago
|
import { createAction } from 'redux-actions';
|
||
|
import requestAction from 'Utilities/requestAction';
|
||
|
import updateSectionState from 'Utilities/State/updateSectionState';
|
||
|
import { createThunk, handleThunks } from 'Store/thunks';
|
||
|
import createHandleActions from './Creators/createHandleActions';
|
||
|
import { set } from './baseActions';
|
||
|
|
||
|
//
|
||
|
// Variables
|
||
|
|
||
5 years ago
|
export const section = 'providerOptions';
|
||
6 years ago
|
|
||
|
//
|
||
|
// State
|
||
|
|
||
|
export const defaultState = {
|
||
|
items: [],
|
||
|
isFetching: false,
|
||
|
isPopulated: false,
|
||
|
error: false
|
||
|
};
|
||
|
|
||
|
//
|
||
|
// Actions Types
|
||
|
|
||
5 years ago
|
export const FETCH_OPTIONS = 'devices/fetchOptions';
|
||
|
export const CLEAR_OPTIONS = 'devices/clearOptions';
|
||
6 years ago
|
|
||
|
//
|
||
|
// Action Creators
|
||
|
|
||
5 years ago
|
export const fetchOptions = createThunk(FETCH_OPTIONS);
|
||
|
export const clearOptions = createAction(CLEAR_OPTIONS);
|
||
6 years ago
|
|
||
|
//
|
||
|
// Action Handlers
|
||
|
|
||
|
export const actionHandlers = handleThunks({
|
||
|
|
||
5 years ago
|
[FETCH_OPTIONS]: function(getState, payload, dispatch) {
|
||
6 years ago
|
dispatch(set({
|
||
|
section,
|
||
|
isFetching: true
|
||
|
}));
|
||
|
|
||
5 years ago
|
const promise = requestAction(payload);
|
||
6 years ago
|
|
||
|
promise.done((data) => {
|
||
|
dispatch(set({
|
||
|
section,
|
||
|
isFetching: false,
|
||
|
isPopulated: true,
|
||
|
error: null,
|
||
5 years ago
|
items: data.options || []
|
||
6 years ago
|
}));
|
||
|
});
|
||
|
|
||
|
promise.fail((xhr) => {
|
||
|
dispatch(set({
|
||
|
section,
|
||
|
isFetching: false,
|
||
|
isPopulated: false,
|
||
|
error: xhr
|
||
|
}));
|
||
|
});
|
||
|
}
|
||
|
});
|
||
|
|
||
|
//
|
||
|
// Reducers
|
||
|
|
||
|
export const reducers = createHandleActions({
|
||
|
|
||
5 years ago
|
[CLEAR_OPTIONS]: function(state) {
|
||
6 years ago
|
return updateSectionState(state, section, defaultState);
|
||
|
}
|
||
|
|
||
|
}, defaultState, section);
|