From 2c25cb44dcf7ae85c3b19a50dbebd1a12e8d665c Mon Sep 17 00:00:00 2001 From: Hamzah Ullah Date: Thu, 22 Aug 2024 13:07:19 -0400 Subject: [PATCH] feat: adds end timestamp to maintenance banner (#1165) --- .env.development | 1 + .env.development-stage | 1 + src/components/app/Layout.test.jsx | 25 ++++++++++++++++++++++++- src/components/app/data/utils.js | 26 ++++++++++++++++++-------- src/index.jsx | 1 + src/utils/common.js | 7 +++++++ 6 files changed, 52 insertions(+), 9 deletions(-) diff --git a/.env.development b/.env.development index 7a7331ca46..4947024009 100644 --- a/.env.development +++ b/.env.development @@ -36,6 +36,7 @@ FEATURE_LANGUAGE_FACET='true' IS_MAINTENANCE_ALERT_ENABLED='' MAINTENANCE_ALERT_MESSAGE='' MAINTENANCE_ALERT_START_TIMESTAMP='' +MAINTENANCE_ALERT_END_TIMESTAMP='' HOTJAR_APP_ID='' HOTJAR_VERSION=6 HOTJAR_DEBUG='' diff --git a/.env.development-stage b/.env.development-stage index a969863e4e..48678b67a9 100644 --- a/.env.development-stage +++ b/.env.development-stage @@ -36,6 +36,7 @@ FEATURE_LANGUAGE_FACET='true' IS_MAINTENANCE_ALERT_ENABLED='' MAINTENANCE_ALERT_MESSAGE='' MAINTENANCE_ALERT_START_TIMESTAMP='' +MAINTENANCE_ALERT_END_TIMESTAMP='' HOTJAR_APP_ID='' HOTJAR_VERSION=6 HOTJAR_DEBUG='' diff --git a/src/components/app/Layout.test.jsx b/src/components/app/Layout.test.jsx index e22102202f..56c6d7ba7a 100644 --- a/src/components/app/Layout.test.jsx +++ b/src/components/app/Layout.test.jsx @@ -80,26 +80,43 @@ describe('Layout', () => { isSystemMaintenanceAlertOpen: false, maintenanceMessage: undefined, maintenanceStartTimestamp: undefined, + maintenanceEndTimestamp: undefined, }, { isSystemMaintenanceAlertOpen: true, maintenanceMessage: 'Hello World!', maintenanceStartTimestamp: undefined, + maintenanceEndTimestamp: undefined, }, { isSystemMaintenanceAlertOpen: true, maintenanceMessage: 'Hello World!', maintenanceStartTimestamp: dayjs().subtract(1, 'm').toISOString(), + maintenanceEndTimestamp: dayjs().add(2, 'm').toISOString(), }, { isSystemMaintenanceAlertOpen: false, maintenanceMessage: 'Hello World!', - maintenanceStartTimestamp: dayjs().add(1, 'm').toISOString(), + maintenanceStartTimestamp: dayjs().subtract(1, 'm').toISOString(), + maintenanceEndTimestamp: dayjs().add(2, 'm').toISOString(), + }, + { + isSystemMaintenanceAlertOpen: true, + maintenanceMessage: 'Hello World!', + maintenanceStartTimestamp: undefined, + maintenanceEndTimestamp: dayjs().add(2, 'm').toISOString(), + }, + { + isSystemMaintenanceAlertOpen: true, + maintenanceMessage: 'Hello World!', + maintenanceStartTimestamp: dayjs().subtract(1, 'm').toISOString(), + maintenanceEndTimestamp: undefined, }, ])('renders with enterprise customer (%s)', ({ isSystemMaintenanceAlertOpen, maintenanceMessage, maintenanceStartTimestamp, + maintenanceEndTimestamp, }) => { if (maintenanceMessage) { mergeConfig({ @@ -113,6 +130,12 @@ describe('Layout', () => { }); } + if (maintenanceEndTimestamp) { + mergeConfig({ + MAINTENANCE_ALERT_END_TIMESTAMP: maintenanceEndTimestamp ?? '', + }); + } + renderWithRouterProvider({ path: '/:enterpriseSlug', element: , diff --git a/src/components/app/data/utils.js b/src/components/app/data/utils.js index 12ae8b9568..4eb8145c4d 100644 --- a/src/components/app/data/utils.js +++ b/src/components/app/data/utils.js @@ -3,7 +3,12 @@ import { logError } from '@edx/frontend-platform/logging'; import { POLICY_TYPES } from '../../enterprise-user-subsidy/enterprise-offers/data/constants'; import { LICENSE_STATUS } from '../../enterprise-user-subsidy/data/constants'; -import { getBrandColorsFromCSSVariables, isDefinedAndNotNull, isTodayWithinDateThreshold } from '../../../utils/common'; +import { + getBrandColorsFromCSSVariables, + isDefinedAndNotNull, + isTodayBetweenDates, + isTodayWithinDateThreshold, +} from '../../../utils/common'; import { COURSE_STATUSES, SUBSIDY_TYPE } from '../../../constants'; import { LATE_ENROLLMENTS_BUFFER_DAYS } from '../../../config/constants'; import { @@ -34,15 +39,20 @@ export function isSystemMaintenanceAlertOpen(config) { return false; } const startTimestamp = config.MAINTENANCE_ALERT_START_TIMESTAMP; - - // Given no start timestamp, the system maintenance alert should be open, as - // it's enabled and has a message. - if (!startTimestamp) { - return true; + const endTimestamp = config.MAINTENANCE_ALERT_END_TIMESTAMP; + if (startTimestamp && endTimestamp) { + return isTodayBetweenDates({ startDate: startTimestamp, endDate: endTimestamp }); + } + if (startTimestamp) { + return dayjs().isAfter(dayjs(startTimestamp)); + } + if (endTimestamp) { + return dayjs().isBefore(dayjs(endTimestamp)); } - // Otherwise, check whether today's date is after the defined start date. - return dayjs().isAfter(dayjs(startTimestamp)); + // Given no start timestamp and no end timestamp, the system maintenance alert should be open, as + // it's enabled and has a message. + return true; } /** diff --git a/src/index.jsx b/src/index.jsx index fdc0968b6c..548abe2365 100644 --- a/src/index.jsx +++ b/src/index.jsx @@ -42,6 +42,7 @@ initialize({ IS_MAINTENANCE_ALERT_ENABLED: process.env.IS_MAINTENANCE_ALERT_ENABLED || null, MAINTENANCE_ALERT_MESSAGE: process.env.MAINTENANCE_ALERT_MESSAGE || null, MAINTENANCE_ALERT_START_TIMESTAMP: process.env.MAINTENANCE_ALERT_START_TIMESTAMP || null, + MAINTENANCE_ALERT_END_TIMESTAMP: process.env.MAINTENANCE_ALERT_END_TIMESTAMP || null, ENABLE_SKILLS_QUIZ: process.env.ENABLE_SKILLS_QUIZ || false, ENABLE_NOTICES: process.env.ENABLE_NOTICES || null, LEARNER_SUPPORT_URL: process.env.LEARNER_SUPPORT_URL || null, diff --git a/src/utils/common.js b/src/utils/common.js index b2f2781727..84774b0b28 100644 --- a/src/utils/common.js +++ b/src/utils/common.js @@ -145,6 +145,13 @@ export function isTodayWithinDateThreshold({ date, days }) { return today.isBetween(offsetDays, dateToCheck); } +export function isTodayBetweenDates({ startDate, endDate }) { + const today = dayjs(); + const formattedStartDate = dayjs(startDate); + const formattedEndDate = dayjs(endDate); + return today.isBetween(formattedStartDate, formattedEndDate); +} + /** * Returns a formatted date in the following format: *