1
0
mirror of https://github.com/mgerb/mywebsite synced 2026-01-11 18:32:50 +00:00

handling sensors with redux

This commit is contained in:
2016-09-05 18:27:52 +00:00
parent be5e95b6cf
commit 0e8d2e5b2b
15 changed files with 200 additions and 69 deletions

View File

@@ -1,4 +1,4 @@
import * as types from "./constants";
import * as types from "../constants/app";
import marked from 'marked';
import 'whatwg-fetch';

View File

@@ -0,0 +1,83 @@
import * as types from "../constants/sensor";
import 'whatwg-fetch';
function loadSensorList(sensor_list){
return {
type: types.LOAD_SENSOR_LIST,
sensor_list
}
}
function loadSensorInfoYear(sensor_info){
return{
type: types.LOAD_SENSOR_INFO_YEAR,
sensor_info
}
}
function loadSensorInfoMonth(sensor_info){
return{
type: types.LOAD_SENSOR_INFO_MONTH,
sensor_info
}
}
function fetchingList(){
return {
type: types.FETCHING_LIST
}
}
function fetchingInfoYear(){
return {
type: types.FETCHING_INFO_YEAR
}
}
function fetchingInfoMonth(){
return {
type: types.FETCHING_INFO_MONTH
}
}
export function fetchSensorList(){
return (dispatch) => {
dispatch(fetchingList());
return fetch('/api/allsensors')
.then(response => response.json())
.then(json => {
dispatch(loadSensorList(json));
})
.catch(error => {
console.log(error);
});
}
}
export function fetchSensorInfoYear(location, year){
return (dispatch) => {
dispatch(fetchingInfoYear());
return fetch(`/api/sensor/${location}/${year}`)
.then(response => response.json())
.then(json => {
dispatch(loadSensorInfoYear(json));
})
.catch(error => {
console.log(error);
});
}
}
export function fetchSensorInfoMonth(location, year, month){
return (dispatch) => {
dispatch(fetchingInfoMonth());
return fetch(`/api/sensor/${location}/${year}/${month}`)
.then(response => response.json())
.then(json => {
dispatch(loadSensorInfoMonth(json));
})
.catch(error => {
console.log(error);
});
}
}

View File

@@ -0,0 +1,10 @@
//constants
export const LOAD_SENSOR_LIST = 'LOAD_SENSOR_LIST';
export const LOAD_SENSOR_INFO_YEAR = 'LOAD_SENSOR_INFO_YEAR';
export const LOAD_SENSOR_INFO_MONTH = 'LOAD_SENSOR_INFO_MONTH';
//fetching
export const FETCHING_LIST = 'FETCHING_LIST';
export const FETCHING_INFO_YEAR = 'FETCHING_INFO_YEAR';
export const FETCHING_INFO_MONTH = 'FETCHING_INFO_MONTH';

View File

@@ -1,9 +1,5 @@
//just using one reducer - use combineReducers from redux to modularize things
import {combineReducers} from 'redux';
import {routerReducer} from 'react-router-redux';
//import typs
import * as types from './constants';
import * as types from '../constants/app';
//defaults -
const defaultState = {
@@ -17,7 +13,7 @@ const defaultState = {
};
//default reducer
function reducer(state = defaultState, action) {
export default function app(state = defaultState, action) {
//every reducer gets called when an action is called - we check for the type to modify our state accordingly
switch (action.type) {
case types.INIT_PREVIEW:
@@ -46,10 +42,3 @@ function reducer(state = defaultState, action) {
//return present state if no actions get called
return state;
}
const allReducers = combineReducers({
reducer,
routing: routerReducer
});
export default allReducers;

View File

@@ -0,0 +1,14 @@
//just using one reducer - use combineReducers from redux to modularize things
import {combineReducers} from 'redux';
import {routerReducer} from 'react-router-redux';
import app from './app';
import sensor from './sensor';
const allReducers = combineReducers({
app,
sensor,
routing: routerReducer
});
export default allReducers;

View File

@@ -0,0 +1,59 @@
//import typs
import * as types from '../constants/sensor';
//defaults -
const defaultState = {
list : [],
infoMonth: [],
infoYear: [],
fetchingList: false,
fetchingInfoMonth: false,
fetchingInfoYear: false,
fetchedList: false,
fetchedInfoMonth: false,
fetchedInfoYear: false
};
//default reducer
export default function app(state = defaultState, action) {
switch(action.type){
case types.FETCHING_LIST:
return Object.assign({}, state, {
fetchingList: true,
fetchedList: false
});
case types.FETCHING_INFO_MONTH:
return Object.assign({}, state, {
fetchingInfoMonth: true,
fetchedInfoMonth: false
});
case types.FETCHING_INFO_YEAR:
return Object.assign({}, state, {
fetchingInfoYear: true,
fetchedInfoYear: false
});
case types.LOAD_SENSOR_LIST:
return Object.assign({}, state, {
list: action.sensor_list,
fetchingList: false,
fetchedList: true
});
case types.LOAD_SENSOR_INFO_MONTH:
return Object.assign({}, state, {
infoMonth: action.sensor_info,
fetchingInfoMonth: false,
fetchedInfoMonth: true
});
case types.LOAD_SENSOR_INFO_YEAR:
return Object.assign({}, state, {
infoYear: action.sensor_info,
fetchingInfoYear: false,
fetchedInfoYear: true
});
}
//return present state if no actions get called
return state;
}

View File

@@ -4,7 +4,7 @@ import {applyMiddleware, createStore} from 'redux';
import {browserHistory} from 'react-router';
import {syncHistoryWithStore} from 'react-router-redux';
import reducers from './reducers';
import reducers from './reducers/reducers';
const middleware = applyMiddleware(thunk, logger());