Last active
April 26, 2018 11:24
-
-
Save fasetto/dc1a4be1d3384dcb45c17322c27e634e to your computer and use it in GitHub Desktop.
Revisions
-
SERKAN BIRCAN revised this gist
Apr 26, 2018 . 5 changed files with 48 additions and 17 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,4 @@ import { Action, ActionWithPayload } from './types'; export function createAction<T extends string>(type: T): Action<T>; export function createAction<T extends string, P>(type: T, payload: P): ActionWithPayload<T, P>; This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -7,10 +7,10 @@ export enum ActionTypes { RELOAD_URL = '[core] reload page' } export const Actions = { setAge: (age: number) => createAction(ActionTypes.SET_AGE, age), setName: (name: string) => createAction(ActionTypes.SET_NAME, name), reloadUrl: () => createAction(ActionTypes.RELOAD_URL) }; export type Actions = ActionsUnion<typeof Actions>; This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ import { Actions, ActionTypes } from './actions'; export interface State { user: { age: number, name: string } | {}; @@ -10,33 +10,32 @@ export const initialState: State = { reloadPage: false }; export const reducer = (state = initialState, action: Actions): State => { switch (action.type) { case ActionTypes.SET_AGE: { const { payload: newAge } = action; const newUser = { ...state.user, age: newAge }; const newState = { ...state, user: newUser }; return newState; } case ActionTypes.SET_NAME: { const { payload: newName } = action; const newUser = { ...state.user, name: newName }; const newState = { ...state, user: newUser }; return newState; } case ActionTypes.RELOAD_URL: { return { ...state, reloadPage: true }; } default: return state; } }; This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,25 @@ import { createStore, combineReducers, Reducer } from 'redux'; import * as fromRoot from '../containers/App/reducer'; // const composeEnhancers = ( // process.env.NODE_ENV === 'development' && // (window as any).__REDUX_DEVTOOLS_EXTENSION__ && // (window as any).__REDUX_DEVTOOLS_EXTENSION__() // ) || compose; // const enhancer = composeEnhancers(); /* Middlewares etc. */ // [P]roperty in keyof [S]tate type ReducersMapObject<S> = { [P in keyof S]: Reducer<S[P]> }; export interface State { root: fromRoot.State; } const reducers: ReducersMapObject<State> = { root: fromRoot.reducer }; const initialState = {} as State; export default createStore(combineReducers(reducers), initialState); This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,19 @@ export interface Action<T extends string> { type: T; } export interface ActionWithPayload<T extends string, P> extends Action<T> { payload: P; } type FunctionType = (...args: any[]) => any; type ActionCreatorsMapObject = { [actionCreator: string]: FunctionType }; export type ActionsUnion<A extends ActionCreatorsMapObject> = ReturnType<A[keyof A]>; export type ActionsOfType<ActionUnion, ActionType extends string> = ActionUnion extends Action< ActionType > ? ActionUnion : never; -
SERKAN BIRCAN revised this gist
Apr 25, 2018 . No changes.There are no files selected for viewing
-
SERKAN BIRCAN revised this gist
Apr 25, 2018 . 3 changed files with 35 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,14 @@ export interface Action<T extends string> { type: T; } export interface ActionWithPayload<T extends string, P> extends Action<T> { payload: P; } export function createAction<T extends string>(type: T): Action<T>; export function createAction<T extends string, P>(type: T, payload: P): ActionWithPayload<T, P>; export function createAction<T extends string, P>(type: T, payload?: P) { return payload === undefined ? { type } : { type, payload }; } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ import { ActionsUnion } from './types'; import { createAction } from './action-helpers'; export enum ActionTypes { SET_AGE = '[core] set age', SET_NAME = '[core] set name', RELOAD_URL = '[core] reload page' } export const actions = { setAge: (age: number) => createAction(ActionTypes.SET_AGE, age), setName: (name: string) => createAction(ActionTypes.SET_NAME, name), reloadUrl: () => createAction(ActionTypes.RELOAD_URL) }; export type Actions = ActionsUnion<typeof actions>; This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,5 @@ type FunctionType = (...args: any[]) => any; type ActionCreatorsMapObject = { [actionCreator: string]: FunctionType }; export type ActionsUnion<A extends ActionCreatorsMapObject> = ReturnType<A[keyof A]>; -
SERKAN BIRCAN revised this gist
Apr 25, 2018 . No changes.There are no files selected for viewing
-
SERKAN BIRCAN created this gist
Apr 25, 2018 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,42 @@ import * as fromActions from './actions'; export interface State { user: { age: number, name: string } | {}; reloadPage: boolean; } export const initialState: State = { user: {}, reloadPage: false }; export const reducer = (state = initialState, action: fromActions.Actions): State => { switch (action.type) { case fromActions.ActionTypes.SET_AGE: { const { payload: newAge } = action; const newUser = { ...state.user, age: newAge }; const newState = { ...state, user: newUser }; return newState; } case fromActions.ActionTypes.SET_NAME: { const { payload: newName } = action; const newUser = { ...state.user, name: newName }; const newState = { ...state, user: newUser }; return newState; } case fromActions.ActionTypes.RELOAD_URL: { return { ...state, reloadPage: true }; } default: { return state; } } };