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:
@@ -38,7 +38,7 @@ ReactDOM.render((
|
||||
<Router history={history}>
|
||||
<Route path="/" component={App}>
|
||||
<IndexRoute component={Preview}/>
|
||||
<Route path="post/:category/:post" component={Post}/>
|
||||
<Route path="post(/:category)/:post" component={Post}/>
|
||||
<Route path="sensor/:location" component={SensorInfo}/>
|
||||
</Route>
|
||||
</Router>
|
||||
|
||||
@@ -21,7 +21,7 @@ export default class Post extends React.Component {
|
||||
|
||||
componentDidMount() {
|
||||
const params = this.props.params;
|
||||
this.props.appActions.fetchPost(params.category, params.post);
|
||||
this.props.appActions.fetchPost(params.post, params.category);
|
||||
}
|
||||
|
||||
render() {
|
||||
|
||||
@@ -1,11 +1,31 @@
|
||||
import React from 'react';
|
||||
|
||||
let location, sensor, actions, uniqueDates;
|
||||
|
||||
export default class SensorInfo extends React.Component{
|
||||
|
||||
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.fetchSensorInfoMonth('Grand Meadow', '2016', 'May');
|
||||
this.props.sensorActions.fetchUniqueDates('Grand Meadow');
|
||||
*/
|
||||
}
|
||||
|
||||
componentWillReceiveProps(){
|
||||
if(sensor.fetchedUniqueDates){
|
||||
uniqueDates = sensor.uniqueDates;
|
||||
|
||||
//!sensor.fetchedInfoMonth ? actions.fetchSensorInfoMonth(location, )
|
||||
}
|
||||
}
|
||||
|
||||
render(){
|
||||
return(
|
||||
<div class="Content">Test123</div>
|
||||
|
||||
@@ -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) => {
|
||||
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 => {
|
||||
dispatch(loadPost(response));
|
||||
|
||||
@@ -22,6 +22,13 @@ function loadSensorInfoMonth(sensor_info){
|
||||
}
|
||||
}
|
||||
|
||||
function loadUniqueDates(dates){
|
||||
return{
|
||||
type: types.LOAD_UNIQUE_DATES,
|
||||
dates
|
||||
}
|
||||
}
|
||||
|
||||
function fetchingList(){
|
||||
return {
|
||||
type: types.FETCHING_LIST
|
||||
@@ -40,6 +47,12 @@ function fetchingInfoMonth(){
|
||||
}
|
||||
}
|
||||
|
||||
function fetchingUniqueDates(){
|
||||
return {
|
||||
type: types.FETCHING_UNIQUE_DATES
|
||||
}
|
||||
}
|
||||
|
||||
export function fetchSensorList(){
|
||||
return (dispatch) => {
|
||||
dispatch(fetchingList());
|
||||
@@ -81,3 +94,17 @@ export function fetchSensorInfoMonth(location, year, month){
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -2,9 +2,10 @@
|
||||
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';
|
||||
|
||||
export const LOAD_UNIQUE_DATES = 'LOAD_UNIQUE_DATES';
|
||||
|
||||
//fetching
|
||||
export const FETCHING_LIST = 'FETCHING_LIST';
|
||||
export const FETCHING_INFO_YEAR = 'FETCHING_INFO_YEAR';
|
||||
export const FETCHING_INFO_MONTH = 'FETCHING_INFO_MONTH';
|
||||
export const FETCHING_UNIQUE_DATES = 'FETCHING_UNIQUE_DATES';
|
||||
@@ -6,19 +6,23 @@ const defaultState = {
|
||||
list : [],
|
||||
infoMonth: [],
|
||||
infoYear: [],
|
||||
uniqueDates: {},
|
||||
|
||||
fetchingList: false,
|
||||
fetchingInfoMonth: false,
|
||||
fetchingInfoYear: false,
|
||||
fetchingUniqueDates: false,
|
||||
|
||||
fetchedList: false,
|
||||
fetchedInfoMonth: false,
|
||||
fetchedInfoYear: false
|
||||
fetchedInfoYear: false,
|
||||
fetchedUniqueDates: false
|
||||
};
|
||||
|
||||
//default reducer
|
||||
export default function app(state = defaultState, action) {
|
||||
switch(action.type){
|
||||
//fetching functions - we use a fetching state to display loading images
|
||||
case types.FETCHING_LIST:
|
||||
return Object.assign({}, state, {
|
||||
fetchingList: true,
|
||||
@@ -34,7 +38,13 @@ export default function app(state = defaultState, action) {
|
||||
fetchingInfoYear: true,
|
||||
fetchedInfoYear: false
|
||||
});
|
||||
case types:FETCHING_UNIQUE_DATES:
|
||||
return Object.assign({}, state, {
|
||||
fetchingUniqueDates: true,
|
||||
fetchedUniqueDates: false
|
||||
});
|
||||
|
||||
//other functions
|
||||
case types.LOAD_SENSOR_LIST:
|
||||
return Object.assign({}, state, {
|
||||
list: action.sensor_list,
|
||||
@@ -53,6 +63,12 @@ export default function app(state = defaultState, action) {
|
||||
fetchingInfoYear: false,
|
||||
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 state;
|
||||
|
||||
11
metadata.js
11
metadata.js
@@ -19,13 +19,15 @@ marked.setOptions({
|
||||
}
|
||||
});
|
||||
|
||||
const dir = './posts/';
|
||||
const rootDirectory = './posts/';
|
||||
const json = {
|
||||
posts: []
|
||||
};
|
||||
|
||||
//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);
|
||||
|
||||
for(let post of posts){
|
||||
@@ -33,7 +35,7 @@ function parse_dir(dir, folder_name){
|
||||
|
||||
if(stats.isDirectory()){
|
||||
parse_dir(dir + post + '/', post);
|
||||
} else {
|
||||
} else if(folder_name !== null){
|
||||
const file = fs.readFileSync(dir+post, 'utf8');
|
||||
const tokens = marked.lexer(file, null);
|
||||
const temp = {
|
||||
@@ -49,7 +51,8 @@ function parse_dir(dir, folder_name){
|
||||
}
|
||||
|
||||
//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
|
||||
json.posts.sort((a, b) => {
|
||||
|
||||
@@ -11,7 +11,8 @@
|
||||
"dev": "webpack-dev-server --content-base public --inline --hot --history-api-fallback",
|
||||
"get_dependencies": "go get ./server && npm install",
|
||||
"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": {
|
||||
"type": "git",
|
||||
|
||||
@@ -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{
|
||||
Dates []Data `json:"dates" bson:"dates"`
|
||||
}
|
||||
|
||||
func GetUniqueSensorDates(sensor_location string) ([]Data, error){
|
||||
d := []Data{}
|
||||
temp := UniqueDates{};
|
||||
//temp := UniqueDates{};
|
||||
|
||||
if db.Mongo.Connected() == true {
|
||||
|
||||
@@ -215,11 +219,19 @@ func GetUniqueSensorDates(sensor_location string) ([]Data, error){
|
||||
|
||||
c := session.DB(db.Mongo.Info.Database).C(collection)
|
||||
|
||||
err := c.Pipe([]bson.M{{"$match": bson.M{"location": sensor_location}},
|
||||
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"}}}},
|
||||
}).One(&temp)
|
||||
{"$unwind": "$dates"},
|
||||
{"$project": bson.M{"_id": 0, "month": "$dates.month", "monthname": "$dates.monthname", "year": "$dates.year"}},
|
||||
{"$sort": bson.M{"year": 1, "month": 1}},
|
||||
|
||||
d = temp.Dates;
|
||||
}).All(&d)
|
||||
|
||||
//d = temp.Dates;
|
||||
|
||||
test12, _ := json.MarshalIndent(d, "", " ")
|
||||
|
||||
log.Println(string(test12))
|
||||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
|
||||
Reference in New Issue
Block a user