2025-04-30 17:28:58 +01:00

103 lines
3.0 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

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 {notifications} from "@mantine/notifications";
import type {BaseUserData, LoginUserInfo} from "~/utils/models";
import {UserType} from "~/utils/hms_enums";
export function showErrorMessage(msg: string, title: string = "Error", autoClose: boolean | number = 10000) {
console.log("ErrorMessage:", title, msg)
notifications.show({
title: title,
message: msg,
color: 'red',
autoClose: autoClose
})
}
export function showWarningMessage(msg: string, title: string = "注意", autoClose: boolean | number = 10000) {
console.log("WarningMessage:", title, msg)
notifications.show({
title: title,
message: msg,
color: 'yellow',
autoClose: autoClose,
})
}
export function showInfoMessage(msg: string, title: string = "Info", autoClose: boolean | number = 10000) {
notifications.show({
title: title,
message: msg,
color: 'blue',
autoClose: autoClose,
})
}
export function getBaseUserInfo(loginUserInfo?: LoginUserInfo): BaseUserData | undefined {
if (!loginUserInfo) {
return undefined
}
if (loginUserInfo.user_type == UserType.PATIENT) {
return loginUserInfo.patient_data
}
else if (loginUserInfo.user_type == UserType.DOCTOR) {
return loginUserInfo.doctor_data
}
else {
return undefined
}
}
export function getUserDisplayFullName(loginUserInfo?: LoginUserInfo | null): string {
if (!loginUserInfo) {
return ""
}
if (loginUserInfo.user_type == UserType.PATIENT) {
return `${loginUserInfo.patient_data?.title} ${loginUserInfo.patient_data?.name} (Patient)`
}
else if (loginUserInfo.user_type == UserType.DOCTOR) {
return `${loginUserInfo.doctor_data?.title} ${loginUserInfo.doctor_data?.name} (Staff)`
}
else {
return ""
}
}
export function dateToTimestamp(date: Date): number {
const utcDate = new Date(date.getTime() + date.getTimezoneOffset() * 60 * 1000);
return Math.floor(utcDate.getTime() / 1000)
}
export function timestampToDate(timestamp: number): Date {
const date = new Date(timestamp * 1000);
return new Date(date.getTime() - date.getTimezoneOffset() * 60 * 1000)
}
export function parseDmyToDate(input: string): Date {
const match = input.match(
/^(?<day>\d{1,2})\/(?<month>\d{1,2})\/(?<year>\d{4})$/
);
if (!match || !match.groups) return new Date(2000, 1, 1); // Invalid date;
const day = Number(match.groups.day);
const month = Number(match.groups.month); // 112
const year = Number(match.groups.year);
const date = new Date(year, month - 1, day);
if (
date.getFullYear() !== year ||
date.getMonth() !== month - 1 ||
date.getDate() !== day
) {
return new Date(2000, 1, 1); // Invalid date
}
return date;
}
export function getEnumKeyByValue<T extends Record<string, string>>(
enumObj: T,
value: string
): keyof T | undefined {
return (Object.keys(enumObj) as Array<keyof T>)
.find(k => enumObj[k] === value);
}