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

132 lines
2.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
2017-06-26 15:35:54 +02:00
import {LOAD_RESULTS_REQUEST,
LOAD_RESULTS_SUCCESS,
2018-09-17 15:35:46 +02:00
LOAD_RESULTS_ERROR,
2018-09-17 16:26:42 +02:00
SET_LOOKUP_QUERY_VALUE,
RESET}
2017-06-26 15:35:54 +02:00
from './actions'
2017-05-16 13:34:00 +02:00
2018-09-17 15:35:46 +02:00
const LOCATION_CHANGE = '@@router/LOCATION_CHANGE'
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
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-09-17 15:35:46 +02:00
/*
* Restore lookup query state from location paramenters
*/
const _restoreQueryState = function(state, payload) {
const params = payload.query;
const query = params["q"] || "";
return Object.assign({}, state, {
query: query,
queryValue: query
});
}
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
cachedAt: api.cache_status.cached_at, // I don't like this style.
cacheTtl: api.ttl,
// Routes
routesImported: imported.routes,
routesFiltered: filtered.routes,
// 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-09-25 22:32:31 +02:00
totalPagesImported: imported.pagination.total_pages,
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:
return _restoreQueryState(state, action.payload);
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:
return Object.assign({}, state, initialState, {
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,
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