summaryrefslogtreecommitdiffhomepage
path: root/gui/src/shared
diff options
context:
space:
mode:
authorHank <hank@mullvad.net>2022-09-28 13:12:36 +0200
committerHank <hank@mullvad.net>2022-09-30 15:40:18 +0200
commit12e28adc9ee88ad911861d91d845e0f711ecae80 (patch)
tree6a9bcbfd499b4209f84cde0556d4c06be94e69eb /gui/src/shared
parent0be74230db7f4f805edee572de8b8cb5ea5f2424 (diff)
downloadmullvadvpn-12e28adc9ee88ad911861d91d845e0f711ecae80.tar.xz
mullvadvpn-12e28adc9ee88ad911861d91d845e0f711ecae80.zip
Update display of time left. Add unit and e2e tests
Diffstat (limited to 'gui/src/shared')
-rw-r--r--gui/src/shared/account-expiry.ts4
-rw-r--r--gui/src/shared/date-helper.ts24
2 files changed, 26 insertions, 2 deletions
diff --git a/gui/src/shared/account-expiry.ts b/gui/src/shared/account-expiry.ts
index c6fa0c6d07..85d0d78315 100644
--- a/gui/src/shared/account-expiry.ts
+++ b/gui/src/shared/account-expiry.ts
@@ -1,4 +1,4 @@
-import { dateByAddingComponent, DateComponent, DateType, formatRelativeDate } from './date-helper';
+import { dateByAddingComponent, DateComponent, DateType, formatTimeLeft } from './date-helper';
import { capitalize } from './string-helpers';
export function hasExpired(expiry: DateType): boolean {
@@ -22,6 +22,6 @@ export function formatRemainingTime(
expiry: DateType,
shouldCapitalizeFirstLetter?: boolean,
): string {
- const remaining = formatRelativeDate(new Date(), expiry, true);
+ const remaining = formatTimeLeft(new Date(), expiry);
return shouldCapitalizeFirstLetter ? capitalize(remaining) : remaining;
}
diff --git a/gui/src/shared/date-helper.ts b/gui/src/shared/date-helper.ts
index 7f232552b3..aeffb9a7e5 100644
--- a/gui/src/shared/date-helper.ts
+++ b/gui/src/shared/date-helper.ts
@@ -120,3 +120,27 @@ export function formatRelativeDate(
}
}
}
+
+/**
+ * If a user has more than 2 years (730 days) left of time it should be displayed in whole years rounded down
+ * If a user has less than 2 years left (e.g. 729 days) then this should be displayed in days.
+ *
+ * @param fromDate
+ * @param toDate
+ */
+export const formatTimeLeft = (fromDate: DateType, toDate: DateType): string => {
+ const diff = new DateDiff(fromDate, toDate);
+ const years = Math.abs(diff.years);
+ const days = Math.abs(diff.days);
+
+ let suffix = 'left';
+ if (diff.milliseconds < 0) {
+ suffix = 'ago';
+ }
+
+ if (days < 730) {
+ return sprintf(messages.ngettext(`1 day ${suffix}`, `%d days ${suffix}`, days), days);
+ }
+
+ return sprintf(messages.ngettext(`1 year ${suffix}`, `%d years ${suffix}`, years), years);
+};