vehicleInfoExpiry.js 1.12 KB
const expiryKeys = [
	"compulsoryInsuranceExpiry",
	"annualInspectionExpiry",
	"commercialInsuranceExpiry",
];

function getLocalDate(value) {
	if (!value) {
		return null;
	}
	if (value instanceof Date) {
		return new Date(value.getFullYear(), value.getMonth(), value.getDate());
	}
	const matched = String(value).match(/^(\d{4})[-/](\d{1,2})[-/](\d{1,2})/);
	if (matched) {
		return new Date(
			Number(matched[1]),
			Number(matched[2]) - 1,
			Number(matched[3]),
		);
	}
	const date = new Date(value);
	if (Number.isNaN(date.getTime())) {
		return null;
	}
	return new Date(date.getFullYear(), date.getMonth(), date.getDate());
}

export function getExpiryTextStyle(item, row) {
	if (!item || !expiryKeys.includes(item.key) || !row) {
		return {};
	}
	const expiryDate = getLocalDate(row[item.key]);
	if (!expiryDate) {
		return {};
	}
	const now = new Date();
	const today = new Date(now.getFullYear(), now.getMonth(), now.getDate());
	const diffDays = Math.ceil((expiryDate.getTime() - today.getTime()) / 86400000);
	if (diffDays < 0) {
		return { color: "#EB4E4E" };
	}
	if (diffDays <= 30) {
		return { color: "#FF8D1A" };
	}
	return {};
}