Commit 5b5bf09c authored by Jens's avatar Jens
Browse files

wip redux

parent 0977e751
......@@ -32,6 +32,7 @@
"react-dom": "^16.2.0",
"react-render-image": "^1.1.1",
"react-router-dom": "^4.2.2",
"redux": "^3.7.2",
"typescript": "^2.6.2",
"whatwg-fetch": "^2.0.3"
},
......
......@@ -8,8 +8,10 @@ import ApolloClient from "apollo-client";
import { InMemoryCache } from "apollo-cache-inmemory";
import { AsyncComponentProvider, createAsyncContext } from 'react-async-component'
import asyncBootstrapper from 'react-async-bootstrapper'
import { Provider } from 'react-redux'
import Routes from '../common/routes'
import { store } from '../common/state'
declare let window: {
__APOLLO_STATE__: any,
......@@ -32,11 +34,13 @@ const reactRoot: HTMLElement = document.getElementById('react-root');
const render = (Routings: React.ComponentClass) => {
return (
<AsyncComponentProvider rehydrateState={window.__ASYNC_STATE__} >
<Provider store={store}>
<ApolloProvider client={apolloClient}>
<Router>
<Routings />
<Routings/>
</Router>
</ApolloProvider>
</Provider>
</AsyncComponentProvider>
)
}
......
import {combineReducers} from 'redux'
import {createStore} from "redux";
import {reducer as counterReducer} from './reducers/counter'
const reducers = combineReducers({
counter: counterReducer
});
export const store = createStore(reducers);
\ No newline at end of file
import { Action } from "redux";
import {createAction, getType} from 'typesafe-actions'
interface State {
count:number
}
const initialState: State = {
count: 0
}
interface AddAction extends Action {
type: 'ADD',
payload: {
amount: number
}
}
interface SubAction extends Action {
type: 'SUB',
payload: {
amount: number
}
}
export const countersAction = {
increment: createAction('INCREMENT'),
add: createAction('ADD',(amount: number):AddAction => ({
type: 'ADD',
payload: {
amount
}
})),
sub: createAction('SUB',(amount: number):SubAction => ({
type: 'SUB',
payload: {
amount
}
}))
}
export const reducer = (state: State = initialState, action: Action):State => {
switch (action.type){
case getType(countersAction.increment):
return {
...state,
count: state.count +1
}
case getType(countersAction.add):
const addAction = action as AddAction;
return {
...state,
count: state.count + addAction.payload.amount
}
case getType(countersAction.sub):
const subAction = action as SubAction;
return {
...state,
count: state.count - subAction.payload.amount
}
default:
return state;
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment