diff options
| author | Kamran Ahmed <kamranahmed.se@gmail.com> | 2018-08-19 21:24:13 +0400 |
|---|---|---|
| committer | Kamran Ahmed <kamranahmed.se@gmail.com> | 2018-08-19 21:24:13 +0400 |
| commit | 6ca7bd64e8253f04beb44d00644a537cfeeb74ff (patch) | |
| tree | 0e5548398fe03102aabb051f487a3175b48eaee3 /src | |
| parent | Restructure and add router (diff) | |
| download | githunt-6ca7bd64e8253f04beb44d00644a537cfeeb74ff.tar.gz | |
Add redux/redux-persist and option reducer
Diffstat (limited to 'src')
| -rw-r--r-- | src/app.js | 10 | ||||
| -rw-r--r-- | src/redux/preference/actions.js | 19 | ||||
| -rw-r--r-- | src/redux/preference/reducer.js | 30 | ||||
| -rw-r--r-- | src/redux/preference/types.js | 3 | ||||
| -rw-r--r-- | src/redux/reducers.js | 7 | ||||
| -rw-r--r-- | src/store.js | 25 |
6 files changed, 93 insertions, 1 deletions
@@ -1,10 +1,18 @@ import React, { Component } from 'react'; +import { Provider } from 'react-redux'; +import { PersistGate } from 'redux-persist/lib/integration/react'; + +import { persist, store } from './store'; import AppRoutes from './router'; class App extends Component { render() { return ( - <AppRoutes/> + <Provider store={ store }> + <PersistGate loading={ () => '<h1>Loading..</h1>' } persistor={ persist }> + <AppRoutes/> + </PersistGate> + </Provider> ); } } diff --git a/src/redux/preference/actions.js b/src/redux/preference/actions.js new file mode 100644 index 0000000..1045b6b --- /dev/null +++ b/src/redux/preference/actions.js @@ -0,0 +1,19 @@ +import { UPDATE_OPTIONS, UPDATE_VIEW_TYPE } from './types'; + +export const updateOptions = function (setting) { + return dispatch => { + dispatch({ + type: UPDATE_OPTIONS, + payload: setting, + }); + }; +}; + +export const updateViewType = function (viewType = 'grid') { + return dispatch => { + dispatch({ + type: UPDATE_VIEW_TYPE, + payload: viewType + }); + }; +}; diff --git a/src/redux/preference/reducer.js b/src/redux/preference/reducer.js new file mode 100644 index 0000000..a72c8b2 --- /dev/null +++ b/src/redux/preference/reducer.js @@ -0,0 +1,30 @@ +import { RESET_OPTIONS, UPDATE_OPTIONS, UPDATE_VIEW_TYPE } from './types'; + +const initialState = { + view: 'grid', + options: { + token: '', + }, +}; + +export default function reducer(state = initialState, action) { + switch (action.type) { + case UPDATE_OPTIONS: + return { + ...state, + options: action.payload + }; + case RESET_OPTIONS: + return { + ...state, + options: initialState.options + }; + case UPDATE_VIEW_TYPE: + return { + ...state, + view: action.payload + }; + default: + return state; + } +} diff --git a/src/redux/preference/types.js b/src/redux/preference/types.js new file mode 100644 index 0000000..ac613c6 --- /dev/null +++ b/src/redux/preference/types.js @@ -0,0 +1,3 @@ +export const UPDATE_OPTIONS = 'update_options'; +export const RESET_OPTIONS = 'reset_options'; +export const UPDATE_VIEW_TYPE = 'update_view_type'; diff --git a/src/redux/reducers.js b/src/redux/reducers.js new file mode 100644 index 0000000..7afbc67 --- /dev/null +++ b/src/redux/reducers.js @@ -0,0 +1,7 @@ +import { combineReducers } from 'redux'; + +import preference from './preference/reducer'; + +export default combineReducers({ + preference, +}); diff --git a/src/store.js b/src/store.js new file mode 100644 index 0000000..0b56933 --- /dev/null +++ b/src/store.js @@ -0,0 +1,25 @@ +import { applyMiddleware, compose, createStore } from 'redux'; +import { persistReducer, persistStore } from 'redux-persist'; +import thunk from 'redux-thunk'; +import storage from 'redux-persist/lib/storage'; +import autoMergeLevel2 from 'redux-persist/lib/stateReconciler/autoMergeLevel2'; +import rootReducer from './redux/reducers'; + +const persistedReducers = persistReducer( + { + key: 'githunt:root', + storage: storage, + stateReconciler: autoMergeLevel2, + }, + rootReducer, +); + +export const store = createStore( + persistedReducers, + compose( + applyMiddleware(thunk), + window.devToolsExtension ? window.devToolsExtension() : f => f + ) +); + +export const persist = persistStore(store); |
