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

@@ -0,0 +1,59 @@
import * as types from "../constants/app";
import marked from 'marked';
import 'whatwg-fetch';
export function increasePostLimit() {
return {
type: types.INCREASE_POST_LIMIT
}
}
function initPreview(posts) {
return {
type: types.INIT_PREVIEW,
posts
}
}
function loadPost(post) {
return {
type: types.LOAD_POST,
post
}
}
function fetching() {
return {
type: types.FETCHING
}
}
//using redux-thunk we can modify actions before they get called
//in this case we can send the http request here rather in the react component
export function fetchPreview() {
return (dispatch) => {
dispatch(fetching());
return fetch('/public/metadata.json')
.then(response => response.json())
.then(json => {
dispatch(initPreview(json));
})
.catch(error => {
console.log(error);
});
}
}
export function fetchPost(category, post) {
return (dispatch) => {
dispatch(fetching());
return fetch(`/public/posts/${category}/${post}.md`)
.then(response => response.text())
.then(response => {
dispatch(loadPost(response));
})
.catch(error => {
console.log(error);
});
}
}

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);
});
}
}