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

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

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

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

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

    default: {
      return state;
    }
  }
}