240 lines
6.4 KiB
JavaScript
240 lines
6.4 KiB
JavaScript
import { toast } from "react-toastify";
|
|
import specialties from "@/data/specialties.json";
|
|
import Cookies from "js-cookie";
|
|
import moment from "moment-jalaali";
|
|
|
|
export const numberToArStyle = (num) => num?.toLocaleString("ar-AE") || "";
|
|
|
|
export const isActiveURL = (link, name) => link === name;
|
|
|
|
export const convertToJalali = (date) => {
|
|
const currentDate = date;
|
|
|
|
const jalaliDate = moment(currentDate).format("jYYYY/jM/jD");
|
|
const jalaliYear = moment(jalaliDate, "jYYYY/jM/jD").jYear();
|
|
const jalaliMonth = moment(jalaliDate, "jYYYY/jM/jD").jMonth() + 1;
|
|
const jalaliDay = moment(jalaliDate, "jYYYY/jM/jD").jDate();
|
|
|
|
return `${jalaliYear}/${jalaliMonth}/${jalaliDay}`;
|
|
};
|
|
|
|
export const handleTwoLength = (value) =>
|
|
String(value).length === 1 ? `0${value}` : value;
|
|
|
|
export const checkAllValues = (data) => {
|
|
for (let key in data) {
|
|
if (typeof data[key] === "object" && data[key] !== null) {
|
|
if ("value" in data[key]) {
|
|
if (!data[key].value) {
|
|
return false;
|
|
}
|
|
} else {
|
|
if (!checkAllValues(data[key])) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return true;
|
|
};
|
|
|
|
export const checkTimes = (arr) => {
|
|
return arr.map((day) => {
|
|
const hasMorningTime = day.morning_time.some((time) => time !== "");
|
|
const hasEveningTime = day.evening_time.some((time) => time !== "");
|
|
return hasMorningTime && hasEveningTime;
|
|
});
|
|
};
|
|
|
|
export const uploadImage = async (event) => {
|
|
let obj = {
|
|
file: "",
|
|
blob: "",
|
|
};
|
|
|
|
const file = event.target.files[0];
|
|
if (file) {
|
|
obj.file = file;
|
|
const readFileAsDataURL = (file) => {
|
|
return new Promise((resolve, reject) => {
|
|
const reader = new FileReader();
|
|
|
|
reader.onload = () => resolve(reader.result);
|
|
|
|
reader.onerror = (error) => reject(error);
|
|
|
|
reader.readAsDataURL(file);
|
|
});
|
|
};
|
|
try {
|
|
obj.blob = await readFileAsDataURL(file);
|
|
return obj;
|
|
} catch (error) {
|
|
console.error("Error reading file: ", error);
|
|
throw error;
|
|
}
|
|
} else {
|
|
throw new Error("No file selected");
|
|
}
|
|
};
|
|
|
|
export const alert = (type, text, prop) => {
|
|
toast[type](
|
|
<div className="h-full w-full flex flex-col items-start justify-center gap-[13px]">
|
|
<p className="text-[14px] font-medium">
|
|
پیغام{" "}
|
|
{type === "error"
|
|
? "خطا"
|
|
: type === "warning"
|
|
? "هشدار"
|
|
: type === "info"
|
|
? "اعلام"
|
|
: "موفقیت"}
|
|
</p>
|
|
<p className="text-[14px] font-normal">{text}</p>
|
|
</div>,
|
|
{ prop }
|
|
);
|
|
};
|
|
|
|
export const addLabelToList = (list) =>
|
|
list.map((item) => {
|
|
return {
|
|
...item,
|
|
label: item.name,
|
|
};
|
|
});
|
|
|
|
export const formatPhoneNumber = (input) => {
|
|
if (typeof input !== "string") input = String(input);
|
|
|
|
const normalized = input
|
|
.replace(/[\u06F0-\u06F9]/g, (d) => String(d.charCodeAt(0) - 0x06f0))
|
|
.replace(/[\u0660-\u0669]/g, (d) => String(d.charCodeAt(0) - 0x0660));
|
|
|
|
const digits = normalized.replace(/\D/g, "");
|
|
|
|
const trimmed = digits.substring(0, 9);
|
|
|
|
const match = trimmed.match(/^(\d{0,2})(\d{0,3})(\d{0,4})$/);
|
|
if (!match) return trimmed;
|
|
|
|
return [match[1], match[2], match[3]].filter(Boolean).join(" ");
|
|
};
|
|
|
|
export function validatePhoneNumber(input) {
|
|
if (!input) {
|
|
return { valid: false, text: "شماره موبایل نمیتواند خالی باشد." };
|
|
}
|
|
|
|
if (input.length !== 11) {
|
|
return { valid: false, text: "شماره موبایل باید دقیقا 11 رقم باشد." };
|
|
}
|
|
|
|
const isValid = /^09\d{9}$/.test(input);
|
|
if (!isValid) {
|
|
return { valid: false, text: "شماره موبایل نامعتبر است." };
|
|
}
|
|
|
|
return { valid: true, text: null };
|
|
}
|
|
|
|
export const searchOnList = (list, value, name) => {
|
|
let newList = list;
|
|
newList = list.filter((item) => item[name]?.toLowerCase()?.includes(value));
|
|
return newList;
|
|
};
|
|
|
|
export function clearFilterParams(router, listRemove) {
|
|
const currentParams = new URLSearchParams(window.location.search);
|
|
const keysToRemove = listRemove;
|
|
keysToRemove.forEach((key) => currentParams.delete(key));
|
|
const queryString = currentParams.toString();
|
|
const newUrl = queryString ? `/doctors?${queryString}` : "/doctors";
|
|
|
|
router.push(newUrl);
|
|
}
|
|
|
|
export const filterList = (data) => {
|
|
const parentList = specialties.filter((item) => !item.parent);
|
|
const childrenList = specialties.filter((item) => item.parent);
|
|
|
|
const filteredChildrenList =
|
|
data && data.category
|
|
? childrenList.filter((item) => item.parent === data.category.id)
|
|
: [];
|
|
|
|
return {
|
|
parentList: parentList,
|
|
childrenList: filteredChildrenList,
|
|
};
|
|
};
|
|
|
|
export function getCleanNumberValue(value, defaultValue = 0) {
|
|
if (typeof value !== "string") value = String(value);
|
|
|
|
const persianArabicToEnglish = value
|
|
.replace(/[\u06F0-\u06F9]/g, (d) => String(d.charCodeAt(0) - 0x06f0))
|
|
.replace(/[\u0660-\u0669]/g, (d) => String(d.charCodeAt(0) - 0x0660));
|
|
|
|
const cleaned = persianArabicToEnglish.replace(/[^0-9.]/g, "");
|
|
|
|
const number = parseFloat(cleaned);
|
|
return isNaN(number) ? defaultValue : number;
|
|
}
|
|
|
|
export function hasDataChanged(previousData, newData) {
|
|
const oldDataString = JSON.stringify(
|
|
previousData,
|
|
Object.keys(previousData).sort()
|
|
);
|
|
const newDataString = JSON.stringify(newData, Object.keys(newData).sort());
|
|
return oldDataString !== newDataString;
|
|
}
|
|
|
|
export const getParsedUserInfo = () => {
|
|
const user = Cookies.get("userInfo");
|
|
const parsedUserInfo = user && JSON.parse(user);
|
|
return parsedUserInfo;
|
|
};
|
|
|
|
export const handleTimeExpiresToken = (expires_in) => {
|
|
const accessTokenExpires = new Date();
|
|
accessTokenExpires.setSeconds(accessTokenExpires.getSeconds() + expires_in);
|
|
const refreshTokenExpires = new Date();
|
|
refreshTokenExpires.setDate(refreshTokenExpires.getDate() + 30);
|
|
|
|
return {
|
|
accessTokenExpires,
|
|
refreshTokenExpires,
|
|
};
|
|
};
|
|
|
|
export const removeAdditionalKeysDashboard = (information) => {
|
|
const {
|
|
national_code,
|
|
changed,
|
|
created,
|
|
id,
|
|
status,
|
|
prev_data,
|
|
uuid,
|
|
...usedKays
|
|
} = information;
|
|
|
|
return usedKays;
|
|
};
|
|
|
|
export const changeDateType = (data, toTimeStamp) => {
|
|
const newData = { ...data };
|
|
const birthday = newData.birthday;
|
|
|
|
if (toTimeStamp) {
|
|
const m = moment(birthday, "jYYYY/jMM/jDD");
|
|
newData.birthday = m.unix();
|
|
} else {
|
|
newData.birthday = moment.unix(birthday).format("jYYYY/jMM/jDD");
|
|
}
|
|
return newData;
|
|
};
|