import { GetRequestPagination, IHttpResponse } from "../types/common"; import { GET_LIST_LOCATIONS_URI, GET_LIST_RECENT_LOCATIONS_RATING_URI, GET_LIST_TOP_LOCATIONS, GET_LOCATION_TAGS_URI, GET_LOCATION_URI, POST_CREATE_LOCATION } from "../constants/api"; import { client } from "./config"; import statusCode from "./status-code"; import { AxiosError } from "axios"; const initialState: any = { data: null, error: null } interface GetListLocationsArg extends GetRequestPagination { order_by?: number, region_type?: number } async function getListLocationsService({ page, page_size }: GetListLocationsArg) { const newState = { ...initialState }; const url = `${GET_LIST_LOCATIONS_URI}?page=${page}&page_size=${page_size}` try { const response = await client({ method: 'GET', url: url }) switch (response.request.status) { case statusCode.OK: newState.data = response.data; return newState; default: newState.error = response.data; return newState } } catch (error) { console.log(error) } } async function getListRecentLocationsRatingsService(page_size: Number) { const newState = { ...initialState }; const url = `${GET_LIST_RECENT_LOCATIONS_RATING_URI}?page_size=${page_size}` try { const response = await client({ method: 'GET', url: url }) switch (response.request.status) { case statusCode.OK: newState.data = response.data; return newState; default: newState.error = response.data; return newState } } catch (error) { console.log(error) } } async function getListTopLocationsService({ page, page_size, order_by, region_type }: GetListLocationsArg) { const newState = { ...initialState }; const url = `${GET_LIST_TOP_LOCATIONS}?page=${page}&page_size=${page_size}&order_by=${order_by}®ion_type=${region_type}` try { const response = await client({ method: 'GET', url: url }) switch (response.request.status) { case statusCode.OK: newState.data = response.data; return newState; default: newState.error = response.data; return newState } } catch (error) { console.log(error) } } async function getLocationService(id: Number) { const newState = { ...initialState }; const url = `${GET_LOCATION_URI}/${id}` try { const response = await client({ method: 'GET', url: url }) switch (response.request.status) { case statusCode.OK: newState.data = response.data; return newState; default: newState.error = response.data; return newState; } } catch (error) { throw(error) } } async function getLocationTagsService(id: Number) { const newState = { ...initialState }; const url = `${GET_LOCATION_TAGS_URI}/${id}` try { const response = await client({ method: 'GET', url: url }) switch (response.request.status) { case statusCode.OK: newState.data = response.data; return newState; default: newState.error = response.data; return newState; } } catch (error) { console.log(error) } } async function createLocationService(data: FormData): Promise { const newState: IHttpResponse = { data: null, error: null}; try { const response = await client({ method: 'POST', url: POST_CREATE_LOCATION, data: data, withCredentials: true}) newState.data = response.data; newState.status = response.status return newState; } catch (error) { let err = error as AxiosError; newState.error = err; newState.status = err.status; return newState; } } export { getListLocationsService, getListRecentLocationsRatingsService, getListTopLocationsService, getLocationTagsService, getLocationService, createLocationService, }