daily pastebin goal
8%
SHARE
TWEET

Untitled

a guest Aug 13th, 2017 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import * as Action from '../actions/todo';
  2.  
  3. const STATUS = {
  4.   NEW: 'new',
  5.   DONE: 'done'
  6. }
  7.  
  8. interface Todo {
  9.   text: string;
  10.   status: string;
  11. }
  12.  
  13. export interface State {
  14.   total: number;
  15.   todos: Array<Todo>;
  16. }
  17.  
  18. export const initialState: State = {
  19.   total: 0,
  20.   todos: []
  21. };
  22.  
  23. export function reducer(state = initialState, action: Action.Actions): State {
  24.   switch (action.type) {
  25.     case Action.ADD: {
  26.       return {
  27.         ...state,
  28.         total: state.total + 1,
  29.         todos: [
  30.           ...state.todos,
  31.           {
  32.             text: action.text,
  33.             status: STATUS.NEW
  34.           }
  35.         ]
  36.       };
  37.     }
  38.  
  39.     case Action.REMOVE: {
  40.       return {
  41.         ...state,
  42.         total: state.total - 1,
  43.         todos: [
  44.           ...state.todos.slice(0, action.index),
  45.           ...state.todos.slice(action.index + 1)
  46.         ]
  47.       };
  48.     }
  49.  
  50.     case Action.DONE: {
  51.       const todo = {
  52.         text: state.todos[action.index].text,
  53.         status: STATUS.DONE
  54.       }
  55.  
  56.       return {
  57.         ...state,
  58.         todos: [
  59.           ...state.todos.slice(0, action.index),
  60.           todo,
  61.           ...state.todos.slice(action.index + 1)
  62.         ]
  63.       };
  64.     }
  65.  
  66.     default: {
  67.       return state;
  68.     }
  69.   }
  70. }
RAW Paste Data
Top