import {
  LOGIN_USER_REQUEST,
  LOGIN_USER_SUCCESS,
  LOGIN_USER_FAILURE,
  GET_DATA_USER_LOGIN_AUTH,
  VALIDATE_USER_AUTH
} from "../../../../constants/auth/login/Login";

interface AuthState {
  rex_user: any;
  rex_validate_user: boolean;
  loading: boolean;
  error: string | null;
}

const INIT_STATE: AuthState = {
  rex_user: null,
  rex_validate_user: false,
  loading: false,
  error: null
};

export default (state = INIT_STATE, action: any): AuthState => {
  switch (action.type) {
    case LOGIN_USER_REQUEST:
      return {
        ...state,
        loading: true,
        error: null
      };

    case LOGIN_USER_SUCCESS:
      return {
        ...state,
        loading: false
      };

    case LOGIN_USER_FAILURE:
      return {
        ...state,
        loading: false,
        error: action.payload
      };

    case GET_DATA_USER_LOGIN_AUTH:
      return {
        ...state,
        rex_user: action.payload,
        rex_validate_user: true
      };

    case VALIDATE_USER_AUTH:
      return {
        ...state,
        rex_validate_user: action.payload,
      };

    default:
      return state;
  }
};