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

added check fetching functionality

This commit is contained in:
2016-08-27 07:30:32 +00:00
parent 4bcb240e97
commit d173b06129
6 changed files with 33 additions and 8 deletions

View File

@@ -33,12 +33,16 @@ export default class Index extends React.Component {
} }
render() { render() {
const fetched = this.props.redux.fetched;
const fetching = this.props.redux.fetching;
return ( return (
<div> <div>
<Header /> <Header />
<div class="Main"> <div class="Main">
{typeof this.page === 'undefined' ? <Preview posts={this.props.redux.preview.posts} /> : ""} {typeof this.page === 'undefined' && !fetching ? <Preview posts={this.props.redux.preview.posts} /> : null}
{this.page === 'post' ? <Post content={this.props.redux.post}/> : ""} {this.page === 'post' && !fetching ? <Post content={this.props.redux.post}/> : null}
{fetching ? <div class="Content">Fetching</div> : null}
<Sidebar /> <Sidebar />
</div> </div>
<Footer /> <Footer />

View File

@@ -15,10 +15,17 @@ function loadPost(post){
} }
} }
function fetching(){
return{
type: types.FETCHING
}
}
//using redux-thunk we can modify actions before they get called //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 //in this case we can send the http request here rather in the react component
export function fetchPreview() { export function fetchPreview() {
return (dispatch) => { return (dispatch) => {
dispatch(fetching());
return fetch('/public/metadata.json') return fetch('/public/metadata.json')
.then(response => response.json()) .then(response => response.json())
.then(json => { .then(json => {
@@ -32,6 +39,7 @@ export function fetchPreview() {
export function fetchPost(category, post) { export function fetchPost(category, post) {
return (dispatch) => { return (dispatch) => {
dispatch(fetching());
return fetch(`/public/posts/${category}/${post}.md`) return fetch(`/public/posts/${category}/${post}.md`)
.then(response => response.text()) .then(response => response.text())
.then(response => { .then(response => {

View File

@@ -2,3 +2,4 @@
export const INIT_PREVIEW = 'INIT_PREVIEW'; export const INIT_PREVIEW = 'INIT_PREVIEW';
export const FILTER_PREVIEW = 'FILTER_PREVIEW'; export const FILTER_PREVIEW = 'FILTER_PREVIEW';
export const LOAD_POST = 'LOAD_POST'; export const LOAD_POST = 'LOAD_POST';
export const FETCHING = 'FETCHING';

View File

@@ -17,7 +17,9 @@ const defaultState = {
filteredPreview: { filteredPreview: {
posts: [] posts: []
}, },
post: "" post: "",
fetched: false,
fetching: false
}; };
//default reducer //default reducer
@@ -26,7 +28,9 @@ function reducer(state = defaultState, action) {
switch (action.type) { switch (action.type) {
case types.INIT_PREVIEW: case types.INIT_PREVIEW:
return Object.assign({}, state, { return Object.assign({}, state, {
preview: Object.assign({}, state.preview, action.posts) preview: Object.assign({}, state.preview, action.posts),
fetched: true,
fetching: false
}); });
case types.FILTER_PREVIEW: case types.FILTER_PREVIEW:
return Object.assign({}, state, { return Object.assign({}, state, {
@@ -34,9 +38,15 @@ function reducer(state = defaultState, action) {
}); });
case types.LOAD_POST: case types.LOAD_POST:
return Object.assign({}, state, { return Object.assign({}, state, {
post: action.post post: action.post,
}) fetched: true,
fetching: false
});
case types.FETCHING:
return Object.assign({}, state, {
fetched : false,
fetching: true
});
} }
//return present state if no actions get called //return present state if no actions get called

View File

@@ -2,10 +2,11 @@ import {applyMiddleware, createStore} from 'redux';
import {syncHistoryWithStore} from 'react-router-redux'; import {syncHistoryWithStore} from 'react-router-redux';
import {browserHistory} from 'react-router'; import {browserHistory} from 'react-router';
import thunk from 'redux-thunk'; import thunk from 'redux-thunk';
import logger from 'redux-logger';
import reducers from './reducers'; import reducers from './reducers';
const middleware = applyMiddleware(thunk); const middleware = applyMiddleware(thunk, logger());
//create the new store with default state as an empty object //create the new store with default state as an empty object
const store = createStore(reducers, {}, middleware); const store = createStore(reducers, {}, middleware);

View File

@@ -50,6 +50,7 @@
"react-router": "^2.6.1", "react-router": "^2.6.1",
"react-router-redux": "^4.0.5", "react-router-redux": "^4.0.5",
"redux": "^3.5.2", "redux": "^3.5.2",
"redux-logger": "^2.6.1",
"redux-thunk": "^2.1.0", "redux-thunk": "^2.1.0",
"sass-loader": "^4.0.0", "sass-loader": "^4.0.0",
"style-loader": "^0.13.1", "style-loader": "^0.13.1",