1
0
mirror of https://github.com/mgerb/mywebsite synced 2026-01-10 09:52:51 +00:00

fixed end point - fixed posts in metadata

This commit is contained in:
2016-09-13 04:37:56 +00:00
parent e558fe5da2
commit e5d340df5d
10 changed files with 102 additions and 18 deletions

View File

@@ -38,7 +38,7 @@ ReactDOM.render((
<Router history={history}> <Router history={history}>
<Route path="/" component={App}> <Route path="/" component={App}>
<IndexRoute component={Preview}/> <IndexRoute component={Preview}/>
<Route path="post/:category/:post" component={Post}/> <Route path="post(/:category)/:post" component={Post}/>
<Route path="sensor/:location" component={SensorInfo}/> <Route path="sensor/:location" component={SensorInfo}/>
</Route> </Route>
</Router> </Router>

View File

@@ -21,7 +21,7 @@ export default class Post extends React.Component {
componentDidMount() { componentDidMount() {
const params = this.props.params; const params = this.props.params;
this.props.appActions.fetchPost(params.category, params.post); this.props.appActions.fetchPost(params.post, params.category);
} }
render() { render() {

View File

@@ -1,11 +1,31 @@
import React from 'react'; import React from 'react';
let location, sensor, actions, uniqueDates;
export default class SensorInfo extends React.Component{ export default class SensorInfo extends React.Component{
componentDidMount(){ componentDidMount(){
location = this.props.params.location;
actions = this.props.sensorActions;
sensor = this.props.sensor;
actions.fetchUniqueDates(location);
/*
this.props.sensorActions.fetchSensorInfoYear('Grand Meadow', '2016'); this.props.sensorActions.fetchSensorInfoYear('Grand Meadow', '2016');
this.props.sensorActions.fetchSensorInfoMonth('Grand Meadow', '2016', 'May'); this.props.sensorActions.fetchSensorInfoMonth('Grand Meadow', '2016', 'May');
this.props.sensorActions.fetchUniqueDates('Grand Meadow');
*/
} }
componentWillReceiveProps(){
if(sensor.fetchedUniqueDates){
uniqueDates = sensor.uniqueDates;
//!sensor.fetchedInfoMonth ? actions.fetchSensorInfoMonth(location, )
}
}
render(){ render(){
return( return(
<div class="Content">Test123</div> <div class="Content">Test123</div>

View File

@@ -44,10 +44,14 @@ export function fetchPreview() {
} }
} }
export function fetchPost(category, post) { //adjust url according to parameters
//mainly used to load md files in the parent directory within /posts
export function fetchPost(post, category = null) {
let url;
return (dispatch) => { return (dispatch) => {
dispatch(fetching()); dispatch(fetching());
return fetch(`/public/posts/${category}/${post}.md`) url = category !== null || typeof category === 'undefined' ? `/public/posts/${category}/${post}.md` : `/public/posts/${post}.md`;
return fetch(url)
.then(response => response.text()) .then(response => response.text())
.then(response => { .then(response => {
dispatch(loadPost(response)); dispatch(loadPost(response));

View File

@@ -22,6 +22,13 @@ function loadSensorInfoMonth(sensor_info){
} }
} }
function loadUniqueDates(dates){
return{
type: types.LOAD_UNIQUE_DATES,
dates
}
}
function fetchingList(){ function fetchingList(){
return { return {
type: types.FETCHING_LIST type: types.FETCHING_LIST
@@ -40,6 +47,12 @@ function fetchingInfoMonth(){
} }
} }
function fetchingUniqueDates(){
return {
type: types.FETCHING_UNIQUE_DATES
}
}
export function fetchSensorList(){ export function fetchSensorList(){
return (dispatch) => { return (dispatch) => {
dispatch(fetchingList()); dispatch(fetchingList());
@@ -80,4 +93,18 @@ export function fetchSensorInfoMonth(location, year, month){
console.log(error); console.log(error);
}); });
} }
}
export function fetchUniqueDates(location){
return (dispatch) => {
dispatch(fetchingUniqueDates());
return fetch(`/api/uniquedates/${location}`)
.then(response => response.json())
.then(json => {
dispatch(loadUniqueDates(json));
})
.catch(error => {
console.log(error);
});
}
} }

View File

@@ -2,9 +2,10 @@
export const LOAD_SENSOR_LIST = 'LOAD_SENSOR_LIST'; export const LOAD_SENSOR_LIST = 'LOAD_SENSOR_LIST';
export const LOAD_SENSOR_INFO_YEAR = 'LOAD_SENSOR_INFO_YEAR'; export const LOAD_SENSOR_INFO_YEAR = 'LOAD_SENSOR_INFO_YEAR';
export const LOAD_SENSOR_INFO_MONTH = 'LOAD_SENSOR_INFO_MONTH'; export const LOAD_SENSOR_INFO_MONTH = 'LOAD_SENSOR_INFO_MONTH';
export const LOAD_UNIQUE_DATES = 'LOAD_UNIQUE_DATES';
//fetching //fetching
export const FETCHING_LIST = 'FETCHING_LIST'; export const FETCHING_LIST = 'FETCHING_LIST';
export const FETCHING_INFO_YEAR = 'FETCHING_INFO_YEAR'; export const FETCHING_INFO_YEAR = 'FETCHING_INFO_YEAR';
export const FETCHING_INFO_MONTH = 'FETCHING_INFO_MONTH'; export const FETCHING_INFO_MONTH = 'FETCHING_INFO_MONTH';
export const FETCHING_UNIQUE_DATES = 'FETCHING_UNIQUE_DATES';

View File

@@ -6,19 +6,23 @@ const defaultState = {
list : [], list : [],
infoMonth: [], infoMonth: [],
infoYear: [], infoYear: [],
uniqueDates: {},
fetchingList: false, fetchingList: false,
fetchingInfoMonth: false, fetchingInfoMonth: false,
fetchingInfoYear: false, fetchingInfoYear: false,
fetchingUniqueDates: false,
fetchedList: false, fetchedList: false,
fetchedInfoMonth: false, fetchedInfoMonth: false,
fetchedInfoYear: false fetchedInfoYear: false,
fetchedUniqueDates: false
}; };
//default reducer //default reducer
export default function app(state = defaultState, action) { export default function app(state = defaultState, action) {
switch(action.type){ switch(action.type){
//fetching functions - we use a fetching state to display loading images
case types.FETCHING_LIST: case types.FETCHING_LIST:
return Object.assign({}, state, { return Object.assign({}, state, {
fetchingList: true, fetchingList: true,
@@ -34,7 +38,13 @@ export default function app(state = defaultState, action) {
fetchingInfoYear: true, fetchingInfoYear: true,
fetchedInfoYear: false fetchedInfoYear: false
}); });
case types:FETCHING_UNIQUE_DATES:
return Object.assign({}, state, {
fetchingUniqueDates: true,
fetchedUniqueDates: false
});
//other functions
case types.LOAD_SENSOR_LIST: case types.LOAD_SENSOR_LIST:
return Object.assign({}, state, { return Object.assign({}, state, {
list: action.sensor_list, list: action.sensor_list,
@@ -53,6 +63,12 @@ export default function app(state = defaultState, action) {
fetchingInfoYear: false, fetchingInfoYear: false,
fetchedInfoYear: true fetchedInfoYear: true
}); });
case types.LOAD_UNIQUE_DATES:
return Object.assign({}, state, {
uniqueDates: action.dates,
fetchingUniqueDates: false,
fetchedUniqueDates: true
});
} }
//return present state if no actions get called //return present state if no actions get called
return state; return state;

View File

@@ -19,13 +19,15 @@ marked.setOptions({
} }
}); });
const dir = './posts/'; const rootDirectory = './posts/';
const json = { const json = {
posts: [] posts: []
}; };
//do everything synchronously to keep posts ordered //do everything synchronously to keep posts ordered
function parse_dir(dir, folder_name){ //we are not worried about execution time since this script only runs once when building
//ignores files that are not in a directory
function parse_dir(dir, folder_name = null){
const posts = fs.readdirSync(dir); const posts = fs.readdirSync(dir);
for(let post of posts){ for(let post of posts){
@@ -33,7 +35,7 @@ function parse_dir(dir, folder_name){
if(stats.isDirectory()){ if(stats.isDirectory()){
parse_dir(dir + post + '/', post); parse_dir(dir + post + '/', post);
} else { } else if(folder_name !== null){
const file = fs.readFileSync(dir+post, 'utf8'); const file = fs.readFileSync(dir+post, 'utf8');
const tokens = marked.lexer(file, null); const tokens = marked.lexer(file, null);
const temp = { const temp = {
@@ -49,7 +51,8 @@ function parse_dir(dir, folder_name){
} }
//recursively parse posts directory for all markdown files //recursively parse posts directory for all markdown files
parse_dir(dir, 'posts'); //folder defaults to null and immediate child files are not added to json
parse_dir(rootDirectory);
//sort posts by date //sort posts by date
json.posts.sort((a, b) => { json.posts.sort((a, b) => {

View File

@@ -11,7 +11,8 @@
"dev": "webpack-dev-server --content-base public --inline --hot --history-api-fallback", "dev": "webpack-dev-server --content-base public --inline --hot --history-api-fallback",
"get_dependencies": "go get ./server && npm install", "get_dependencies": "go get ./server && npm install",
"prod": "export NODE_ENV=production && webpack -p && babel-node metadata.js && go build ./server/mywebsite.go", "prod": "export NODE_ENV=production && webpack -p && babel-node metadata.js && go build ./server/mywebsite.go",
"prod-win": "set NODE_ENV=production && webpack -p && babel-node metadata.js && go build ./server/mywebsite.go" "prod-win": "set NODE_ENV=production && webpack -p && babel-node metadata.js && go build ./server/mywebsite.go",
"watch": "webpack --watch --colors --progress"
}, },
"repository": { "repository": {
"type": "git", "type": "git",

View File

@@ -200,13 +200,17 @@ func GetAllSensorInfoByMonth(sensor_location string, year int, monthname string)
} }
} }
//I need to find a better implementation of this
//MongoDB $addToSet creates an array of objecta
//this ends up being a different type than the Data struct above
//it would be nice to make all MongoDB queries load directly into a Data struct
type UniqueDates struct{ type UniqueDates struct{
Dates []Data `json:"dates" bson:"dates"` Dates []Data `json:"dates" bson:"dates"`
} }
func GetUniqueSensorDates(sensor_location string) ([]Data, error){ func GetUniqueSensorDates(sensor_location string) ([]Data, error){
d := []Data{} d := []Data{}
temp := UniqueDates{}; //temp := UniqueDates{};
if db.Mongo.Connected() == true { if db.Mongo.Connected() == true {
@@ -214,12 +218,20 @@ func GetUniqueSensorDates(sensor_location string) ([]Data, error){
defer session.Close() defer session.Close()
c := session.DB(db.Mongo.Info.Database).C(collection) c := session.DB(db.Mongo.Info.Database).C(collection)
err := c.Pipe([]bson.M{{"$match": bson.M{"location": sensor_location}},
{"$group": bson.M{"_id": "null", "dates": bson.M{"$addToSet": bson.M{"month": "$month", "monthname": "$monthname", "year": "$year"}}}},
}).One(&temp)
d = temp.Dates; err := c.Pipe([]bson.M{bson.M{"$match": bson.M{"location": sensor_location}},
{"$group": bson.M{"_id": "null", "dates": bson.M{"$addToSet": bson.M{"month": "$month", "monthname": "$monthname", "year": "$year"}}}},
{"$unwind": "$dates"},
{"$project": bson.M{"_id": 0, "month": "$dates.month", "monthname": "$dates.monthname", "year": "$dates.year"}},
{"$sort": bson.M{"year": 1, "month": 1}},
}).All(&d)
//d = temp.Dates;
test12, _ := json.MarshalIndent(d, "", " ")
log.Println(string(test12))
if err != nil { if err != nil {
log.Println(err) log.Println(err)