You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.6 KiB
JavaScript

import _ from 'lodash';
import { combineReducers } from 'redux';
import { routeReducer } from 'react-router-redux';
import * as actions from '../actions/action-types';
const sensorReducerFactory = function (sensor) {
return function (state = {fetching: false, fetched: false, data: null}, action) {
let s = sensor.toUpperCase();
switch (action.type) {
case actions[`REQUEST_SENSOR_DATA_${s}`]:
console.log(`REQUEST_SENSOR_DATA_${s}`);
state = _.cloneDeep(state);
state.fetching = true;
break;
case actions[`RECEIVE_SENSOR_DATA_${s}`]:
console.log(`RECEIVE_SENSOR_DATA_${s}`);
state = _.cloneDeep(state);
state.data = action.data;
state.fetching = false;
state.fetched = true;
break;
}
return state;
};
};
const sensorUv = sensorReducerFactory('uv');
const sensorLuminosity = sensorReducerFactory('luminosity');
const sensorPressure = sensorReducerFactory('pressure');
const sensorHumidity = sensorReducerFactory('humidity');
const sensorTemperature = sensorReducerFactory('temperature');
const sensorPm10 = sensorReducerFactory('pm10');
const sensorPm25 = sensorReducerFactory('pm25');
const sensorWindDir = sensorReducerFactory('windDir');
const sensorWindSpeed = sensorReducerFactory('windSpeed');
const sensorRain = sensorReducerFactory('rain');
export default combineReducers({
routing: routeReducer,
sensorUv,
sensorLuminosity,
sensorPressure,
sensorHumidity,
sensorTemperature,
sensorPm10,
sensorPm25,
sensorWindDir,
sensorWindSpeed,
sensorRain,
});