mirror of
https://github.com/wekan/wekan.git
synced 2026-11-24 09:59:47 -05:00
Root cause of #6000 was diff(date, now, 'days') with an invalid unit comparing raw milliseconds, so future due dates were mis-coloured; replaced with a precise hours-based comparison (past=overdue/red, <=48h=due-soon/amber, >48h=not-due/grey). For #5965 the colour-decision logic is unified into one shared dueDateClass() helper used by both the minicard and card-detail badges, and the card-detail status colours now win the cascade, so overdue shows red in both. Unit tested. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
45 lines
1.5 KiB
JavaScript
45 lines
1.5 KiB
JavaScript
/**
|
|
* Pure helper that decides the CSS status class for a card "due date" badge.
|
|
*
|
|
* This logic is shared between the card-detail badge (`cardDueDate`) and the
|
|
* minicard badge (`minicardDueDate`) so that both views always agree on the
|
|
* colour of a due date (see issues #6000 and #5965).
|
|
*
|
|
* Rules (no Blaze / no DOM dependencies, easy to unit-test):
|
|
* - If the card has an end date:
|
|
* - end before due => 'completed-early'
|
|
* - otherwise => 'completed'
|
|
* - Otherwise, compare the due date against "now":
|
|
* - due in the past => 'overdue' (red)
|
|
* - due within 48 hours => 'due-soon' (amber)
|
|
* - due more than 48h in future => 'not-due' (grey)
|
|
*
|
|
* @param {Date|string|number} dueDate - The card due date.
|
|
* @param {Date|string|number} now - The current time.
|
|
* @param {Date|string|number} [endDate] - The card end date, if set.
|
|
* @returns {string} One of 'overdue' | 'due-soon' | 'not-due' |
|
|
* 'completed-early' | 'completed'.
|
|
*/
|
|
export function dueDateClass(dueDate, now, endDate) {
|
|
const due = new Date(dueDate);
|
|
const nowVal = new Date(now);
|
|
|
|
if (endDate) {
|
|
const end = new Date(endDate);
|
|
if (end.getTime() < due.getTime()) {
|
|
return 'completed-early';
|
|
}
|
|
return 'completed';
|
|
}
|
|
|
|
const diffMs = due.getTime() - nowVal.getTime();
|
|
const hoursDiff = diffMs / (1000 * 60 * 60);
|
|
|
|
if (hoursDiff < 0) {
|
|
return 'overdue';
|
|
} else if (hoursDiff <= 48) {
|
|
return 'due-soon';
|
|
}
|
|
return 'not-due';
|
|
}
|