import {
  Actions,
  ActionType,
} from './list.action';

import {
  State,
  initialState,
} from './list.state';

export function reducer(state = initialState, action: Actions): State {
  switch (action.type) {
    case ActionType.ReadAll: {
      return {
        ...state,
        error: null,
        pending: true,
      };
    }

    case ActionType.ReadAllSuccess: {
      return {
        ...state,
        error: null,
        pending: false,
        inputs: action.payload
      };
    }

    case ActionType.ReadAllFailure: {
      return {
        ...state,
        error: action.payload,
        pending: false,
        inputs: null,
      };
    }

    default: {
      return state;
    }
  }
}
