Toasts working
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
|
||||
import { useCommonHooks } from 'hooks';
|
||||
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}`;
|
||||
|
||||
@@ -32,32 +32,8 @@ 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';
|
||||
} else if (endpoint === 'hourly') {
|
||||
url = 'https://api.weather.gov/gridpoints/HGX/55,96/forecast/hourly';
|
||||
}
|
||||
const callEndpoint = async () => {
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) {
|
||||
throw new Error(`${response.status} ${response.statusText}`);
|
||||
}
|
||||
return await response.json();
|
||||
};
|
||||
|
||||
return useQuery({
|
||||
queryKey: ['GET', url],
|
||||
queryFn: () => callEndpoint(),
|
||||
});
|
||||
}
|
||||
|
||||
export function useMutationAPI() {
|
||||
const { notify } = useCommonHooks();
|
||||
const callEndpoint = async (
|
||||
method: string,
|
||||
endpoint: string,
|
||||
@@ -76,9 +52,10 @@ export function useMutationAPI() {
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
};
|
||||
|
||||
notify('Sending...');
|
||||
const response = await fetch(`${url}/${endpoint}`, options);
|
||||
if (!response.ok) {
|
||||
notify('Error!');
|
||||
const responseData = await response.json();
|
||||
if (typeof responseData.detail !== 'string') {
|
||||
const errorResponse = JSON.stringify({
|
||||
@@ -114,6 +91,7 @@ export function useMutationAPI() {
|
||||
|
||||
throw new Error(errorResponse);
|
||||
}
|
||||
notify('Success!');
|
||||
return await response.json();
|
||||
};
|
||||
|
||||
@@ -137,6 +115,31 @@ export function useMutationAPI() {
|
||||
});
|
||||
}
|
||||
|
||||
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';
|
||||
} else if (endpoint === 'hourly') {
|
||||
url = 'https://api.weather.gov/gridpoints/HGX/55,96/forecast/hourly';
|
||||
}
|
||||
const callEndpoint = async () => {
|
||||
const response = await fetch(url);
|
||||
if (!response.ok) {
|
||||
throw new Error(`${response.status} ${response.statusText}`);
|
||||
}
|
||||
return await response.json();
|
||||
};
|
||||
|
||||
return useQuery({
|
||||
queryKey: ['GET', url],
|
||||
queryFn: () => callEndpoint(),
|
||||
});
|
||||
}
|
||||
|
||||
export function useFileUploadAPI() {
|
||||
const callEndpoint = async (
|
||||
method: string,
|
||||
@@ -319,78 +322,3 @@ export function useFetchAllRecordsAPI({ endpoint }: { endpoint: string }) {
|
||||
invalidate,
|
||||
};
|
||||
}
|
||||
|
||||
interface DownloadFileOptions {
|
||||
endpoint: string;
|
||||
file_name: string;
|
||||
}
|
||||
|
||||
export function useDownloadFile() {
|
||||
const downloadFile = useCallback(async ({ file_name }: DownloadFileOptions) => {
|
||||
const options: any = {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Headers': '*',
|
||||
'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
|
||||
'Access-Control-Allow-Credentials': true,
|
||||
'Sec-Fetch-Mode': 'no-cors',
|
||||
'Sec-Fetch-Site': 'same-site',
|
||||
},
|
||||
};
|
||||
const toastID = toast.loading('Processing...');
|
||||
try {
|
||||
const response = await fetch(url, options);
|
||||
if (!response.ok) {
|
||||
throw new Error('Network response was not ok');
|
||||
}
|
||||
|
||||
const blob = await response.blob();
|
||||
if (blob.type === 'application/json') {
|
||||
const blob_text = await blob.text();
|
||||
const cleaned_text = blob_text.replace(/"/g, '');
|
||||
toast.update(toastID, {
|
||||
render: cleaned_text,
|
||||
type: 'info',
|
||||
isLoading: false,
|
||||
autoClose: 5000,
|
||||
closeButton: null,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
const blobURL = window.URL.createObjectURL(blob);
|
||||
const a = document.createElement('a');
|
||||
|
||||
a.style.display = 'none';
|
||||
a.href = blobURL;
|
||||
a.download = file_name;
|
||||
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
window.URL.revokeObjectURL(blobURL);
|
||||
document.body.removeChild(a);
|
||||
toast.update(toastID, {
|
||||
render: `${file_name} Downloaded`,
|
||||
type: 'success',
|
||||
isLoading: false,
|
||||
autoClose: 5000,
|
||||
closeButton: null,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Download failed:', error);
|
||||
toast.update(toastID, {
|
||||
render: `Error Downloading ${file_name}. Please try again.`,
|
||||
type: 'error',
|
||||
isLoading: false,
|
||||
autoClose: 5000,
|
||||
closeButton: null,
|
||||
});
|
||||
}
|
||||
}, []);
|
||||
|
||||
return downloadFile;
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { useLocalStorage } from '@mantine/hooks';
|
||||
import { appTheme } from 'contexts';
|
||||
import { useAtom } from 'jotai';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { toast } from 'react-toastify';
|
||||
import { useQueryAPI, useWeatherAPI } from './useAPI';
|
||||
|
||||
export function useCommonHooks() {
|
||||
@@ -95,6 +96,8 @@ export function useCommonHooks() {
|
||||
|
||||
const [applicationData, setApplicationData] = useState<any>([]);
|
||||
|
||||
const notify = (notification: any) => toast(notification, { position: 'bottom-right' });
|
||||
|
||||
useEffect(() => {
|
||||
if (apps && !isAppsFetching) {
|
||||
setApplicationData(apps);
|
||||
@@ -200,5 +203,6 @@ export function useCommonHooks() {
|
||||
personalIsFetched,
|
||||
personalIsFetching,
|
||||
personalIsLoading,
|
||||
notify,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user