1
0
mirror of https://github.com/alice-lg/alice-lg.git synced 2024-05-11 05:55:03 +00:00

140 lines
3.0 KiB
React
Raw Normal View History

2017-05-16 13:34:00 +02:00
// Routeserver Reducer
import {LOAD_ROUTESERVERS_REQUEST,
LOAD_ROUTESERVERS_SUCCESS,
2018-08-05 18:39:11 +02:00
2017-05-16 13:34:00 +02:00
LOAD_ROUTESERVER_STATUS_SUCCESS,
2018-08-05 18:39:11 +02:00
LOAD_ROUTESERVER_STATUS_ERROR,
2017-05-16 13:34:00 +02:00
LOAD_ROUTESERVER_PROTOCOL_REQUEST,
2018-09-16 19:56:22 +02:00
LOAD_ROUTESERVER_PROTOCOL_SUCCESS}
2017-05-16 13:34:00 +02:00
from './actions'
2018-10-03 17:17:18 +02:00
import {LOAD_CONFIG_SUCCESS} from 'components/config/actions'
2017-05-23 16:08:43 +02:00
2017-05-16 13:34:00 +02:00
const initialState = {
2018-08-05 18:39:11 +02:00
2017-05-16 13:34:00 +02:00
all: [],
2018-08-05 18:39:11 +02:00
2017-05-16 13:34:00 +02:00
details: {},
protocols: {},
2018-08-05 18:54:19 +02:00
statusErrors: {},
2017-05-16 13:34:00 +02:00
2018-10-03 17:17:18 +02:00
rejectReasons: {},
noexportReasons: {},
2017-05-23 16:08:43 +02:00
2018-10-03 17:17:18 +02:00
rejectCandidates: {
communities: {}
},
2017-05-16 13:34:00 +02:00
isLoading: false,
protocolsAreLoading: false
};
2018-10-03 17:17:18 +02:00
// == Handlers ==
const _importConfig = function(state, payload) {
// Get reject and filter reasons from config
2018-10-04 14:56:12 +02:00
const rejectReasons = payload.reject_reasons;
2018-10-03 17:17:18 +02:00
const noexportReasons = payload.noexport_reasons;
// Get reject candidates from config
const rejectCandidates = payload.reject_candidates;
return Object.assign({}, state, {
2018-10-04 14:56:12 +02:00
rejectReasons: rejectReasons,
rejectCandidates: rejectCandidates,
2018-10-03 17:17:18 +02:00
2018-10-04 14:56:12 +02:00
noexportReasons: noexportReasons
2018-10-03 17:17:18 +02:00
});
};
const _updateStatus = function(state, payload) {
const details = Object.assign({}, state.details, {
[payload.routeserverId]: payload.status
});
const errors = Object.assign({}, state.statusErrors, {
[payload.routeserverId]: null,
});
return Object.assign({}, state, {
details: details,
2018-10-04 14:56:12 +02:00
statusErrors: errors
2018-10-03 17:17:18 +02:00
});
}
const _updateStatusError = function(state, payload) {
var info = {
code: 42,
tag: "UNKNOWN_ERROR",
message: "Unknown error"
};
if (payload.error &&
2018-10-04 14:56:12 +02:00
payload.error.response &&
2018-10-03 17:17:18 +02:00
payload.error.response.data &&
payload.error.response.data.code) {
info = payload.error.response.data;
}
2018-10-04 14:56:12 +02:00
2018-10-03 17:17:18 +02:00
var errors = Object.assign({}, state.statusErrors, {
[payload.routeserverId]: info
});
return Object.assign({}, state, {
2018-10-04 14:56:12 +02:00
statusErrors: errors
2018-10-03 17:17:18 +02:00
});
}
const _updateProtocol = function(state, payload) {
var protocols = Object.assign({}, state.protocols, {
[payload.routeserverId]: payload.protocol
});
return Object.assign({}, state, {
protocols: protocols,
protocolsAreLoading: false
});
}
2017-05-16 13:34:00 +02:00
export default function reducer(state = initialState, action) {
switch(action.type) {
case LOAD_ROUTESERVERS_REQUEST:
return Object.assign({}, state, {
isLoading: true
});
case LOAD_ROUTESERVERS_SUCCESS:
return Object.assign({}, state, {
all: action.payload.routeservers,
isLoading: false
});
case LOAD_ROUTESERVER_PROTOCOL_REQUEST:
return Object.assign({}, state, {
protocolsAreLoading: true
})
case LOAD_ROUTESERVER_PROTOCOL_SUCCESS:
2018-10-03 17:17:18 +02:00
return _updateProtocol(state, action.payload);
2017-05-16 13:34:00 +02:00
2018-10-03 17:17:18 +02:00
case LOAD_CONFIG_SUCCESS:
return _importConfig(state, action.payload);
2017-05-23 16:08:43 +02:00
2017-05-16 13:34:00 +02:00
case LOAD_ROUTESERVER_STATUS_SUCCESS:
2018-10-03 17:17:18 +02:00
return _updateStatus(state, action.payload);
2017-05-16 13:34:00 +02:00
2018-08-05 18:39:11 +02:00
case LOAD_ROUTESERVER_STATUS_ERROR:
2018-10-03 17:17:18 +02:00
return _updateStatusError(state, action.payload);
2017-05-16 13:34:00 +02:00
}
return state;
}