diff options
| author | Oskar Nyberg <oskar@mullvad.net> | 2020-06-15 20:11:24 +0200 |
|---|---|---|
| committer | Oskar Nyberg <oskar@mullvad.net> | 2020-06-24 11:23:07 +0200 |
| commit | 1f54942ca8da86efeb64b174dcceecdd9e48a85c (patch) | |
| tree | f1187dd93e4e6cb1f6ee1c91e87e101a7031694b /gui/src/shared | |
| parent | 63193938c1c5719e06eae51f8d213af55125a2de (diff) | |
| download | mullvadvpn-1f54942ca8da86efeb64b174dcceecdd9e48a85c.tar.xz mullvadvpn-1f54942ca8da86efeb64b174dcceecdd9e48a85c.zip | |
Refactor AccountExpiry
Diffstat (limited to 'gui/src/shared')
| -rw-r--r-- | gui/src/shared/account-expiry.ts | 81 | ||||
| -rw-r--r-- | gui/src/shared/notifications/accountExpiry.ts | 17 |
2 files changed, 48 insertions, 50 deletions
diff --git a/gui/src/shared/account-expiry.ts b/gui/src/shared/account-expiry.ts index d6acb0de91..4511797f3c 100644 --- a/gui/src/shared/account-expiry.ts +++ b/gui/src/shared/account-expiry.ts @@ -2,57 +2,52 @@ import moment from 'moment'; import { sprintf } from 'sprintf-js'; import { messages } from './gettext'; -export default class AccountExpiry { - private expiry: moment.Moment; +type DateArgument = string | Date | moment.Moment; - constructor(isoString: string, locale: string) { - this.expiry = moment(isoString).locale(locale); - } - - public hasExpired(): boolean { - return this.willHaveExpiredAt(new Date()); - } - - public willHaveExpiredAt(date: Date): boolean { - return this.expiry.isSameOrBefore(date); - } +export function hasExpired(expiry: DateArgument): boolean { + return moment(expiry).isSameOrBefore(new Date()); +} - public formattedDate(): string { - return this.expiry.format('lll'); - } +export function formatDate(date: DateArgument, locale: string): string { + return moment(date).locale(locale).format('lll'); +} - public durationUntilExpiry(): string { - const daysDiff = this.expiry.diff(new Date(), 'days'); +export function formatDurationUntilExpiry(expiry: DateArgument, locale: string): string { + const expiryMoment = moment(expiry).locale(locale); + const daysDiff = expiryMoment.diff(new Date(), 'days'); - // Below three months we want to show the duration in days. Moments fromNow() method starts - // measuring duration in months from 26 days and up. - // https://momentjs.com/docs/#/displaying/fromnow/ - if (daysDiff >= 26 && daysDiff <= 90) { - return sprintf( - // TRANSLATORS: The remaining time left on the account measured in days. - // TRANSLATORS: Available placeholders: - // TRANSLATORS: %(duration)s - The remaining time measured in days. - messages.pgettext('account-expiry', '%(duration)s days'), - { duration: daysDiff }, - ); - } else { - return this.expiry.fromNow(true); - } + // Below three months we want to show the duration in days. Moments fromNow() method starts + // measuring duration in months from 26 days and up. + // https://momentjs.com/docs/#/displaying/fromnow/ + if (daysDiff >= 26 && daysDiff <= 90) { + return sprintf( + // TRANSLATORS: The remaining time left on the account measured in days. + // TRANSLATORS: Available placeholders: + // TRANSLATORS: %(duration)s - The remaining time measured in days. + messages.pgettext('account-expiry', '%(duration)s days'), + { duration: daysDiff }, + ); + } else { + return expiryMoment.fromNow(true); } +} - public remainingTime(shouldCapitalizeFirstLetter?: boolean): string { - const duration = this.durationUntilExpiry(); +export function formatRemainingTime( + expiry: DateArgument, + locale: string, + shouldCapitalizeFirstLetter?: boolean, +): string { + const duration = formatDurationUntilExpiry(expiry, locale); - const remaining = sprintf( - // TRANSLATORS: The remaining time left on the account displayed across the app. - // TRANSLATORS: Available placeholders: - // TRANSLATORS: %(duration)s - a localized remaining time (in minutes, hours, or days) until the account expiry - messages.pgettext('account-expiry', '%(duration)s left'), - { duration }, - ); + const remaining = sprintf( + // TRANSLATORS: The remaining time left on the account displayed across the app. + // TRANSLATORS: Available placeholders: + // TRANSLATORS: %(duration)s - a localized remaining time (in minutes, hours, or days) until the account expiry + messages.pgettext('account-expiry', '%(duration)s left'), + { duration }, + ); - return shouldCapitalizeFirstLetter ? capitalizeFirstLetter(remaining) : remaining; - } + return shouldCapitalizeFirstLetter ? capitalizeFirstLetter(remaining) : remaining; } function capitalizeFirstLetter(inputString: string): string { diff --git a/gui/src/shared/notifications/accountExpiry.ts b/gui/src/shared/notifications/accountExpiry.ts index b749fb55c1..db536c7405 100644 --- a/gui/src/shared/notifications/accountExpiry.ts +++ b/gui/src/shared/notifications/accountExpiry.ts @@ -2,7 +2,7 @@ import moment from 'moment'; import { sprintf } from 'sprintf-js'; import { links } from '../../config.json'; import { messages } from '../../shared/gettext'; -import AccountExpiry from '../account-expiry'; +import { hasExpired, formatRemainingTime } from '../account-expiry'; import { InAppNotification, InAppNotificationProvider, @@ -11,7 +11,8 @@ import { } from './notification'; interface AccountExpiryContext { - accountExpiry: AccountExpiry; + accountExpiry: string; + locale: string; tooSoon?: boolean; } @@ -20,10 +21,12 @@ export class AccountExpiryNotificationProvider public constructor(private context: AccountExpiryContext) {} public mayDisplay() { + const willHaveExpiredInThreeDays = moment(this.context.accountExpiry).isSameOrBefore( + moment().add(3, 'days'), + ); + return ( - !this.context.accountExpiry.hasExpired() && - this.context.accountExpiry.willHaveExpiredAt(moment().add(3, 'days').toDate()) && - !this.context.tooSoon + !hasExpired(this.context.accountExpiry) && willHaveExpiredInThreeDays && !this.context.tooSoon ); } @@ -34,7 +37,7 @@ export class AccountExpiryNotificationProvider // TRANSLATORS: %(duration)s - remaining time, e.g. "2 days" messages.pgettext('notifications', 'Account credit expires in %(duration)s'), { - duration: this.context.accountExpiry.remainingTime(), + duration: formatRemainingTime(this.context.accountExpiry, this.context.locale), }, ); @@ -49,7 +52,7 @@ export class AccountExpiryNotificationProvider return { indicator: 'warning', title: messages.pgettext('in-app-notifications', 'ACCOUNT CREDIT EXPIRES SOON'), - subtitle: this.context.accountExpiry.remainingTime(true), + subtitle: formatRemainingTime(this.context.accountExpiry, this.context.locale, true), action: { type: 'open-url', url: links.purchase, withAuth: true }, }; } |
