mirror of
https://github.com/mgerb/mywebsite
synced 2026-01-12 02:42:48 +00:00
added redux
This commit is contained in:
23
client/js/redux/actions.js
Normal file
23
client/js/redux/actions.js
Normal file
@@ -0,0 +1,23 @@
|
||||
import * as types from "./constants";
|
||||
|
||||
function initPreview(posts){
|
||||
return{
|
||||
type: types.INIT_PREVIEW,
|
||||
posts
|
||||
}
|
||||
}
|
||||
|
||||
//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) => {
|
||||
return fetch('/metadata.json')
|
||||
.then(response => response.json())
|
||||
.then(json => {
|
||||
dispatch(initPreview(json));
|
||||
})
|
||||
.catch(error => {
|
||||
console.log(error);
|
||||
});
|
||||
}
|
||||
}
|
||||
3
client/js/redux/constants.js
Normal file
3
client/js/redux/constants.js
Normal file
@@ -0,0 +1,3 @@
|
||||
//constants
|
||||
export const INIT_PREVIEW = 'INIT_PREVIEW';
|
||||
export const FILTER_PREVIEW = 'FILTER_PREVIEW';
|
||||
37
client/js/redux/reducers.js
Normal file
37
client/js/redux/reducers.js
Normal file
@@ -0,0 +1,37 @@
|
||||
//just using one reducer - use combineReducers from redux to modularize things
|
||||
import {combineReducers} from 'redux';
|
||||
import {routerReducer} from 'react-router-redux';
|
||||
|
||||
//import typs
|
||||
import * as types from './constants';
|
||||
|
||||
//defaults -
|
||||
const defaultState = {
|
||||
preview: {posts: []},
|
||||
filteredPreview: {posts: []}
|
||||
};
|
||||
|
||||
//default reducer
|
||||
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){
|
||||
case types.INIT_PREVIEW:
|
||||
return Object.assign({}, state, {
|
||||
preview: Object.assign({}, state.preview, action.posts)
|
||||
});
|
||||
case types.FILTER_PREVIEW:
|
||||
return Object.assign({}, state, {
|
||||
filteredPreview: Object.assign({}, state.filteredPreview, action.posts)
|
||||
})
|
||||
}
|
||||
|
||||
//return present state if no actions get called
|
||||
return state;
|
||||
}
|
||||
|
||||
const allReducers = combineReducers({
|
||||
reducer,
|
||||
routing: routerReducer
|
||||
});
|
||||
|
||||
export default allReducers;
|
||||
15
client/js/redux/store.js
Normal file
15
client/js/redux/store.js
Normal file
@@ -0,0 +1,15 @@
|
||||
import {applyMiddleware, createStore} from 'redux';
|
||||
import {syncHistoryWithStore} from 'react-router-redux';
|
||||
import {browserHistory} from 'react-router';
|
||||
import thunk from 'redux-thunk';
|
||||
|
||||
import reducers from './reducers';
|
||||
|
||||
const middleware = applyMiddleware(thunk);
|
||||
|
||||
//create the new store with default state as an empty object
|
||||
const store = createStore(reducers, {}, middleware);
|
||||
|
||||
export const history = syncHistoryWithStore(browserHistory, store);
|
||||
|
||||
export default store;
|
||||
Reference in New Issue
Block a user