Skip to content

Instantly share code, notes, and snippets.

@fasetto
Last active April 26, 2018 11:24
Show Gist options
  • Save fasetto/dc1a4be1d3384dcb45c17322c27e634e to your computer and use it in GitHub Desktop.
Save fasetto/dc1a4be1d3384dcb45c17322c27e634e to your computer and use it in GitHub Desktop.

Revisions

  1. SERKAN BIRCAN revised this gist Apr 26, 2018. 5 changed files with 48 additions and 17 deletions.
    9 changes: 1 addition & 8 deletions action-helpers.ts
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,4 @@

    export interface Action<T extends string> {
    type: T;
    }

    export interface ActionWithPayload<T extends string, P> extends Action<T> {
    payload: P;
    }
    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>;
    4 changes: 2 additions & 2 deletions actions.ts
    Original file line number Diff line number Diff line change
    @@ -7,10 +7,10 @@ export enum ActionTypes {
    RELOAD_URL = '[core] reload page'
    }

    export const actions = {
    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>;
    export type Actions = ActionsUnion<typeof Actions>;
    13 changes: 6 additions & 7 deletions reducer.ts
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    import * as fromActions from './actions';
    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: fromActions.Actions): State => {
    export const reducer = (state = initialState, action: Actions): State => {
    switch (action.type) {
    case fromActions.ActionTypes.SET_AGE: {
    case 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: {
    case 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: {
    case ActionTypes.RELOAD_URL: {
    return {
    ...state,
    reloadPage: true
    };
    }

    default: {
    default:
    return state;
    }
    }
    };
    25 changes: 25 additions & 0 deletions store.ts
    Original 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);
    14 changes: 14 additions & 0 deletions types.ts
    Original 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;
  2. SERKAN BIRCAN revised this gist Apr 25, 2018. No changes.
  3. SERKAN BIRCAN revised this gist Apr 25, 2018. 3 changed files with 35 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions action-helpers.ts
    Original 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 };
    }
    16 changes: 16 additions & 0 deletions actions.ts
    Original 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>;
    5 changes: 5 additions & 0 deletions types.ts
    Original 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]>;
  4. SERKAN BIRCAN revised this gist Apr 25, 2018. No changes.
  5. SERKAN BIRCAN created this gist Apr 25, 2018.
    42 changes: 42 additions & 0 deletions reducer.ts
    Original 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;
    }
    }
    };