Files
ecoconnect/client/src/utilities.ts
2024-06-28 09:21:03 +08:00

42 lines
1.0 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { AxiosError } from "axios";
import toast from "react-hot-toast";
export function getTimeOfDay(): number {
const currentHour = new Date().getHours();
if (currentHour >= 6 && currentHour < 12) {
return 0; // Morning
} else if (currentHour >= 12 && currentHour < 18) {
return 1; // Afternoon
} else {
return 2; // Evening
}
}
export const popToast = (message: string, type: number) => {
const words = message.split(" ");
const duration = Math.max(4, words.length * 1);
toast(message, {
duration: duration * 1000, // Convert to milliseconds
position: "top-center",
// Custom Icon
icon: type === 0 ? "" : type === 1 ? "✅" : type === 2 ? "❌" : undefined,
// Aria
ariaProps: {
role: "status",
"aria-live": "polite",
},
});
};
export const popErrorToast = (error: any) => {
try {
popToast(((error as AxiosError).response?.data as any).message, 2);
} catch {
popToast("An unexpected error occured!\nPlease try again later.", 2);
}
};