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

95 lines
2.0 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
2017-06-26 15:35:54 +02:00
results: [],
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
2017-06-28 14:02:54 +02:00
limit: 100,
offset: 0,
totalRoutes: 0,
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
});
}
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;
}
2017-06-26 15:35:54 +02:00
return Object.assign({}, state, {
isLoading: false,
2017-06-26 22:48:02 +02:00
query: action.payload.query,
2017-06-26 15:35:54 +02:00
queryDurationMs: action.payload.results.query_duration_ms,
results: action.payload.results.routes,
2017-06-28 14:02:54 +02:00
limit: action.payload.results.limit,
offset: action.payload.results.offset,
totalRoutes: action.payload.results.total_routes,
error: null
2017-06-26 15:35:54 +02:00
});
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