1
0
mirror of https://github.com/mgerb/mywebsite synced 2026-01-11 02:12:53 +00:00

too many changes - load posts now

This commit is contained in:
2016-08-24 00:29:00 -05:00
parent 24ef17ac5b
commit 7b48c79180
26 changed files with 354 additions and 272 deletions

View File

@@ -1,20 +1,41 @@
import * as types from "./constants";
import marked from 'marked';
function initPreview(posts){
return{
function initPreview(posts) {
return {
type: types.INIT_PREVIEW,
posts
}
}
function loadPost(post){
return {
type: types.LOAD_POST,
post
}
}
//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(){
export function fetchPreview() {
return (dispatch) => {
return fetch('/public/metadata.json')
.then(response => response.json())
.then(json => {
dispatch(initPreview(json));
dispatch(initPreview(json));
})
.catch(error => {
console.log(error);
});
}
}
export function fetchPost(category, post) {
return (dispatch) => {
return fetch(`/public/posts/${category}/${post}.md`)
.then(response => response.text())
.then(response => {
dispatch(loadPost(response));
})
.catch(error => {
console.log(error);

View File

@@ -1,3 +1,4 @@
//constants
export const INIT_PREVIEW = 'INIT_PREVIEW';
export const FILTER_PREVIEW = 'FILTER_PREVIEW';
export const FILTER_PREVIEW = 'FILTER_PREVIEW';
export const LOAD_POST = 'LOAD_POST';

View File

@@ -1,20 +1,29 @@
//just using one reducer - use combineReducers from redux to modularize things
import {combineReducers} from 'redux';
import {routerReducer} from 'react-router-redux';
import {
combineReducers
} from 'redux';
import {
routerReducer
} from 'react-router-redux';
//import typs
import * as types from './constants';
//defaults -
//defaults -
const defaultState = {
preview: {posts: []},
filteredPreview: {posts: []}
preview: {
posts: []
},
filteredPreview: {
posts: []
},
post: ""
};
//default reducer
function reducer(state = defaultState, action){
function reducer(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){
switch (action.type) {
case types.INIT_PREVIEW:
return Object.assign({}, state, {
preview: Object.assign({}, state.preview, action.posts)
@@ -22,9 +31,14 @@ function reducer(state = defaultState, action){
case types.FILTER_PREVIEW:
return Object.assign({}, state, {
filteredPreview: Object.assign({}, state.filteredPreview, action.posts)
});
case types.LOAD_POST:
return Object.assign({}, state, {
post: action.post
})
}
//return present state if no actions get called
return state;
}
@@ -34,4 +48,4 @@ const allReducers = combineReducers({
routing: routerReducer
});
export default allReducers;
export default allReducers;