vehicleInfoExpiry.js
1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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 {};
}