import {StatusCode, HttpRequest, RestConstant} from 'constants';

const initialState = {
  data: undefined,
  error: undefined,
};

export const getProducts = async payload => {
  const {page_id, page_size} = payload;
  const newState = {...initialState};
  const url = `${RestConstant.products}?page_id=${page_id}&page_size=${page_size}`;
  try {
    const response = await HttpRequest.get(url);
    switch (response.request.status) {
      case StatusCode.OK:
        newState.data = response.data;
        return newState;
      case StatusCode.UNAUTHORIZED:
        newState.error = response.response.data;
        return newState;
      case StatusCode.SERVER_ERROR:
        newState.error = response.response.data;
        return newState;
    }
  } catch (error) {
    console.log(error);
  }
};
