-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
6,185 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,75 @@ | ||
import { createStore } from 'redux'; | ||
import { createStore, applyMiddleware, Reducer, Store, AnyAction} from "redux"; | ||
import { composeWithDevTools } from 'redux-devtools-extension'; | ||
import thunk from 'redux-thunk' | ||
|
||
interface StoreState { | ||
total: number; | ||
current: number; | ||
loading: boolean; | ||
} | ||
|
||
// SET-UP | ||
const reducer = (state = {total: 0, current: 0}, action: {type: string, payload?: any}) => { | ||
const reducer: Reducer<StoreState> = (state = { total: 0, current: 0, loading: false }, action: AnyAction) => { | ||
console.log(action); | ||
|
||
switch(action.type) { | ||
case 'SET_CURRENT': | ||
return {...state, current: action.payload.value}; | ||
case 'ADD': | ||
return {...state, total: state.total + action.payload.value}; | ||
case 'SUBTRACT': | ||
return {...state, total: state.total - action.payload.value}; | ||
case 'RESET': | ||
return {...state, total: 0}; | ||
switch (action.type) { | ||
case "SET_CURRENT": | ||
return { ...state, current: action.payload.value }; | ||
case "SET_TOTAL": | ||
return { ...state, total: action.payload.value }; | ||
case "SUBTRACT": | ||
return { ...state, total: state.total - action.payload.value }; | ||
case "RESET": | ||
return { ...state, total: 0 }; | ||
case "START_LOADER": | ||
return { ...state, loading: true }; | ||
case "STOP_LOADER": | ||
return { ...state, loading: false }; | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
const enhancer = ((window as any).__REDUX_DEVTOOLS_EXTENSION__ && (window as any).__REDUX_DEVTOOLS_EXTENSION__()) | ||
|
||
// ACTION-CREATORS | ||
export const add = (value: number) => ({ | ||
type: 'ADD', | ||
payload: { value }, | ||
}); | ||
export const add = (value: number) => async (dispatch, getState) => { | ||
dispatch(startLoader()); | ||
const result = await makeComputation(value, getState().total); | ||
dispatch(setTotal(result)); | ||
dispatch(stopLoader()) | ||
}; | ||
|
||
export const subtract = (value: number) => ({ | ||
type: 'SUBTRACT', | ||
type: "SUBTRACT", | ||
payload: { value }, | ||
}); | ||
|
||
export const setCurrent = (value: number) => ({ | ||
type: 'SET_CURRENT', | ||
payload: { value }, | ||
}); | ||
type: "SET_CURRENT", | ||
payload: { value }, | ||
}); | ||
|
||
export const setTotal = (value: number) => ({ | ||
type: "SET_TOTAL", | ||
payload: { value }, | ||
}); | ||
|
||
export const startLoader = () => ({ | ||
type: "START_LOADER" | ||
}) | ||
|
||
export const stopLoader = () => ({ | ||
type: "STOP_LOADER" | ||
}) | ||
|
||
export const reset = () => ({ type: "RESET" }); | ||
|
||
|
||
export const store = createStore(reducer, composeWithDevTools(applyMiddleware(thunk))); | ||
|
||
export const reset = () => ({ type: 'RESET' }); | ||
|
||
export const store = createStore(reducer, enhancer); | ||
//HELPERS | ||
function makeComputation(value, currentValue): Promise<number> { | ||
return new Promise((resolve, reject) => { | ||
setTimeout(() => resolve(value + currentValue), 2000) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.