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

205 lines
4.9 KiB
React
Raw Normal View History

2017-06-26 15:35:54 +02:00
/*
* Prefix Lookup Reducer
*/
2017-05-16 13:34:00 +02:00
2018-10-18 17:13:39 +02:00
import {
LOAD_RESULTS_REQUEST,
LOAD_RESULTS_SUCCESS,
LOAD_RESULTS_ERROR,
SET_LOOKUP_QUERY_VALUE,
RESET,
} from './actions'
import {
FILTER_GROUP_SOURCES,
FILTER_GROUP_ASNS,
FILTER_GROUP_COMMUNITIES,
FILTER_GROUP_EXT_COMMUNITIES,
FILTER_GROUP_LARGE_COMMUNITIES,
2018-10-23 19:40:36 +02:00
} from 'components/filters/groups'
import {
decodeFiltersSources,
decodeFiltersAsns,
decodeFiltersCommunities,
decodeFiltersExtCommunities,
decodeFiltersLargeCommunities,
2018-10-23 19:40:36 +02:00
} from 'components/filters/encoding'
2018-10-19 09:57:24 +02:00
import {
cloneFilters
2018-10-23 19:37:30 +02:00
} from 'components/filters/state'
2018-09-17 15:35:46 +02:00
const LOCATION_CHANGE = '@@router/LOCATION_CHANGE'
2018-10-18 15:02:34 +02:00
const initialFilterState = [
2018-10-19 09:57:24 +02:00
{"key": "sources", "filters": []},
{"key": "asns", "filters": []},
{"key": "communities", "filters": []},
{"key": "ext_communities", "filters": []},
{"key": "large_communities", "filters": []},
2018-10-18 15:02:34 +02:00
];
2017-05-16 13:34:00 +02:00
const initialState = {
2018-09-17 15:35:46 +02:00
query: "",
queryValue: "",
2017-05-16 13:34:00 +02:00
2018-10-01 19:10:46 +02:00
anchor: "",
2018-10-18 15:02:34 +02:00
filtersAvailable: initialFilterState,
filtersApplied: initialFilterState,
routesImported: [],
routesFiltered: [],
2017-06-26 15:35:54 +02:00
error: null,
2017-06-28 14:02:54 +02:00
2017-06-26 15:35:54 +02:00
queryDurationMs: 0.0,
2017-05-16 13:34:00 +02:00
cachedAt: false,
cacheTtl: false,
pageImported: 0,
pageFiltered: 0,
2018-09-25 23:07:11 +02:00
pageSizeImported: 0,
pageSizeFiltered: 0,
2018-09-25 22:32:31 +02:00
totalPagesImported: 0,
totalPagesFiltered: 0,
totalRoutesImported: 0,
totalRoutesFiltered: 0,
2017-06-28 14:02:54 +02:00
2017-06-26 15:35:54 +02:00
isLoading: false
2017-05-16 13:34:00 +02:00
}
2018-10-01 19:10:46 +02:00
/*
* Helper: Get scroll anchor from hash
*/
const getScrollAnchor = function(hash) {
return hash.substr(hash.indexOf('-')+1);
}
/*
* Decode filters applied from params
*/
const _decodeFiltersApplied = function(params) {
2018-10-19 09:57:24 +02:00
let groups = cloneFilters(initialFilterState);
groups[FILTER_GROUP_SOURCES].filters = decodeFiltersSources(params);
groups[FILTER_GROUP_ASNS].filters = decodeFiltersAsns(params);
groups[FILTER_GROUP_COMMUNITIES].filters = decodeFiltersCommunities(params);
groups[FILTER_GROUP_EXT_COMMUNITIES].filters = decodeFiltersExtCommunities(params);
groups[FILTER_GROUP_LARGE_COMMUNITIES].filters = decodeFiltersLargeCommunities(params);
return groups;
}
2018-10-01 19:10:46 +02:00
2018-09-17 15:35:46 +02:00
/*
* Restore lookup query state from location paramenters
*/
2018-10-01 19:10:46 +02:00
const _handleLocationChange = function(state, payload) {
2018-09-17 15:35:46 +02:00
const params = payload.query;
const query = params["q"] || "";
2018-10-01 19:10:46 +02:00
const pageFiltered = parseInt(params["pf"] || 0, 10);
const pageReceived = parseInt(params["pr"] || 0, 10);
const anchor = getScrollAnchor(payload.hash);
2018-09-17 15:35:46 +02:00
// Restore filters applied from location
const filtersApplied = _decodeFiltersApplied(params);
2018-09-17 15:35:46 +02:00
return Object.assign({}, state, {
2018-10-01 19:10:46 +02:00
anchor: anchor,
2018-09-17 15:35:46 +02:00
query: query,
2018-10-01 19:10:46 +02:00
queryValue: query,
pageImported: pageReceived,
pageFiltered: pageFiltered,
filtersApplied: filtersApplied,
2018-09-17 15:35:46 +02:00
});
}
2018-10-01 19:10:46 +02:00
/*
* Receive query results
*/
const _loadQueryResult = function(state, payload) {
const results = payload.results;
const imported = results.imported;
const filtered = results.filtered;
const api = results.api;
return Object.assign({}, state, {
isLoading: false,
// Cache Status
2018-10-19 09:57:24 +02:00
cachedAt: api.cache_status.cached_at, // I don't like this style.
cacheTtl: api.ttl,
// Routes
routesImported: imported.routes,
routesFiltered: filtered.routes,
2018-10-18 15:02:34 +02:00
// Filters available
filtersAvailable: results.filters_available,
// Pagination
pageImported: imported.pagination.page,
pageFiltered: filtered.pagination.page,
2018-09-25 23:07:11 +02:00
pageSizeImported: imported.pagination.page_size,
pageSizeFiltered: filtered.pagination.page_size,
2018-10-19 09:57:24 +02:00
totalPagesImported: imported.pagination.total_pages,
2018-09-25 22:32:31 +02:00
totalPagesFiltered: filtered.pagination.total_pages,
totalRoutesImported: imported.pagination.total_results,
totalRoutesFiltered: filtered.pagination.total_results,
2018-09-25 22:32:31 +02:00
// Statistics
2018-09-25 22:32:31 +02:00
queryDurationMs: results.request_duration_ms,
totalRoutes: imported.pagination.total_results + filtered.pagination.total_results
});
}
2018-09-17 15:35:46 +02:00
2017-05-16 13:34:00 +02:00
export default function reducer(state=initialState, action) {
2017-06-26 15:35:54 +02:00
switch(action.type) {
2018-09-17 15:35:46 +02:00
case LOCATION_CHANGE:
2018-10-01 19:10:46 +02:00
return _handleLocationChange(state, action.payload);
2018-10-19 09:57:24 +02:00
2018-09-17 15:35:46 +02:00
case SET_LOOKUP_QUERY_VALUE:
return Object.assign({}, state, {
queryValue: action.payload.value,
});
2017-06-26 15:35:54 +02:00
case LOAD_RESULTS_REQUEST:
2018-10-01 19:10:46 +02:00
return Object.assign({}, state, {
2017-06-26 22:48:02 +02:00
query: action.payload.query,
2018-09-17 16:26:42 +02:00
queryValue: action.payload.query,
2017-06-26 22:48:02 +02:00
isLoading: true
2017-06-26 15:35:54 +02:00
});
case LOAD_RESULTS_SUCCESS:
2017-07-04 12:07:03 +02:00
if (state.query != action.payload.query) {
return state;
}
return _loadQueryResult(state, action.payload);
2017-07-04 12:07:03 +02:00
2017-06-26 15:35:54 +02:00
case LOAD_RESULTS_ERROR:
2017-07-04 12:07:03 +02:00
if (state.query != action.payload.query) {
return state;
}
2017-06-26 15:35:54 +02:00
return Object.assign({}, state, initialState, {
2017-06-26 22:48:02 +02:00
query: action.payload.query,
2018-10-01 12:19:34 +02:00
queryValue: action.payload.query,
2017-06-26 22:48:02 +02:00
error: action.payload.error
2017-06-26 15:35:54 +02:00
});
2018-09-17 16:26:42 +02:00
case RESET:
return Object.assign({}, state, initialState);
2017-06-26 15:35:54 +02:00
}
return state;
2017-05-16 13:34:00 +02:00
}
2017-06-26 15:35:54 +02:00