Removed hardcoded urls

This commit is contained in:
Sean Edmunds
2024-12-04 13:44:35 -06:00
parent 85e9c9134a
commit 1fca2b7688
16 changed files with 262 additions and 178 deletions

View File

@@ -7,9 +7,10 @@ import {
import { useCallback } from 'react';
import { toast } from 'react-toastify';
const url = `${import.meta.env.prod ? import.meta.env.VITE_DOMAIN_PROD : import.meta.env.VITE_DOMAIN_DEV}/${import.meta.env.VITE_BASE_URL}`;
export function useQueryAPI({ endpoint }: { endpoint: string }) {
const callEndpoint = async (endpoint: string) => {
const url = `http://localhost:5000/trigger/${endpoint}`;
const options: object = {
method: 'GET',
headers: {
@@ -23,7 +24,7 @@ export function useQueryAPI({ endpoint }: { endpoint: string }) {
},
};
const response = await fetch(url, options);
const response = await fetch(`${url}/${endpoint}`, options);
if (!response.ok) {
throw new Error(`${response.status} ${response.statusText}`);
}
@@ -38,13 +39,13 @@ export function useQueryAPI({ endpoint }: { endpoint: string }) {
export function useWeatherAPI({ endpoint }: { endpoint: string }) {
let url = '';
if (endpoint === 'baseline') {
url = 'https://api.weather.gov/gridpoints/HGX/55,96';
} else if (endpoint === 'current') {
url = 'https://api.weather.gov/stations/KSGR/observations';
} else if (endpoint === 'forecast') {
url = 'https://api.weather.gov/gridpoints/HGX/55,96/forecast';
}
if (endpoint === 'baseline') {
url = 'https://api.weather.gov/gridpoints/HGX/55,96';
} else if (endpoint === 'current') {
url = 'https://api.weather.gov/stations/KSGR/observations';
} else if (endpoint === 'forecast') {
url = 'https://api.weather.gov/gridpoints/HGX/55,96/forecast';
}
const callEndpoint = async (endpoint: string) => {
const options: object = {
method: 'GET',
@@ -77,7 +78,6 @@ export function useMutationAPI() {
endpoint: string,
body: object | undefined | null,
) => {
const url = `http://localhost:5000/trigger/${endpoint}`;
const options: any = {
method: method,
headers: {
@@ -158,7 +158,6 @@ export function useFileUploadAPI() {
endpoint: string,
body?: object | FormData | undefined | null,
) => {
const url = `http://localhost:5000/trigger/${endpoint}`;
const options = {
method: method,
@@ -231,19 +230,19 @@ export function useFetchAllRecordsAPI({ endpoint }: { endpoint: string }) {
const fetchPage = async (page: number, retryCount = 0): Promise<any> => {
// Start building the URL
const url = new URL(`http://localhost:5000/trigger/${endpoint}`);
const urlObject = new URL(`${url}/${endpoint}`);
// Check and append custom query parameters from the endpoint
const customParams = new URLSearchParams(endpoint.split('?')[1]);
for (const [key, value] of customParams) {
if (!url.searchParams.has(key)) {
url.searchParams.append(key, value);
if (!urlObject.searchParams.has(key)) {
urlObject.searchParams.append(key, value);
}
}
// Add 'page' and 'size' parameters
url.searchParams.set('page', page.toString()); // Use 'set' to ensure uniqueness
url.searchParams.set('size', pageSize.toString());
urlObject.searchParams.set('page', page.toString()); // Use 'set' to ensure uniqueness
urlObject.searchParams.set('size', pageSize.toString());
const options: any = {
method: 'GET',
@@ -345,7 +344,6 @@ interface DownloadFileOptions {
export function useDownloadFile() {
const downloadFile = useCallback(
async ({ endpoint, file_name }: DownloadFileOptions) => {
const url = `http://localhost:5000/trigger/${endpoint}`;
const options: any = {
method: 'GET',
headers: {
@@ -426,7 +424,6 @@ export const useFetchInfiniteQuery = ({
const queryKey = ['GET', endpoint];
const fetchInfiniteQuery = async (endpoint: string) => {
const url = `http://localhost:5000/trigger/${endpoint}`;
const requestOptions: any = {
method: 'GET',
headers: {