import { GET_LIST_LOCATIONS_URI, GET_LIST_RECENT_LOCATIONS_RATING_URI, GET_LIST_TOP_LOCATIONS, GET_LOCATION_TAGS_URI, GET_LOCATION_URI } from "../constants/api"; import { client } from "./config"; import statusCode from "./status-code"; 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) { console.log(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) } } export { getListLocationsService, getListRecentLocationsRatingsService, getListTopLocationsService, getLocationTagsService, getLocationService }