2020-12-15 12:38:59 +02:00
|
|
|
import { fromJS } from 'immutable';
|
|
|
|
import { STATUS_REQUEST, STATUS_SUCCESS, STATUS_FAILURE, StatusAction } from '../actions/status';
|
|
|
|
import { StaticallyTypedRecord } from '../types/immutable';
|
2020-06-03 12:44:03 +03:00
|
|
|
|
2020-12-15 12:38:59 +02:00
|
|
|
export type Status = StaticallyTypedRecord<{
|
|
|
|
isFetching: boolean;
|
|
|
|
status: StaticallyTypedRecord<{
|
|
|
|
auth: StaticallyTypedRecord<{ status: boolean }>;
|
|
|
|
api: StaticallyTypedRecord<{ status: boolean; statusPage: string }>;
|
|
|
|
}>;
|
|
|
|
error: Error | undefined;
|
|
|
|
}>;
|
2020-06-03 12:44:03 +03:00
|
|
|
|
2020-12-15 12:38:59 +02:00
|
|
|
const defaultState = fromJS({
|
|
|
|
isFetching: false,
|
|
|
|
status: {
|
|
|
|
auth: { status: true },
|
|
|
|
api: { status: true, statusPage: '' },
|
|
|
|
},
|
|
|
|
error: undefined,
|
|
|
|
}) as Status;
|
|
|
|
|
|
|
|
const status = (state = defaultState, action: StatusAction) => {
|
2020-06-03 12:44:03 +03:00
|
|
|
switch (action.type) {
|
|
|
|
case STATUS_REQUEST:
|
|
|
|
return state.set('isFetching', true);
|
|
|
|
case STATUS_SUCCESS:
|
|
|
|
return state.withMutations(map => {
|
|
|
|
map.set('isFetching', false);
|
|
|
|
map.set('status', fromJS(action.payload.status));
|
|
|
|
});
|
|
|
|
case STATUS_FAILURE:
|
|
|
|
return state.withMutations(map => {
|
|
|
|
map.set('isFetching', false);
|
|
|
|
map.set('error', action.payload.error);
|
|
|
|
});
|
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const selectStatus = (status: Status) => {
|
2020-12-15 12:38:59 +02:00
|
|
|
return status.get('status').toJS();
|
2020-06-03 12:44:03 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
export default status;
|