From 342dde068a53c531520c417452aceaf0c0b6df32 Mon Sep 17 00:00:00 2001 From: Marcos Date: Fri, 13 Dec 2024 15:34:36 -0300 Subject: [PATCH] feat: Updated some logic for the useCourseUpgrade() hook --- src/hooks/use-course-upgrade.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/hooks/use-course-upgrade.js b/src/hooks/use-course-upgrade.js index a97f32ce..f5544890 100644 --- a/src/hooks/use-course-upgrade.js +++ b/src/hooks/use-course-upgrade.js @@ -3,21 +3,37 @@ import { useModel } from '@src/generic/model-store'; // eslint-disable-line impo import { useSelector } from 'react-redux'; import { CourseInfoContext } from '../context'; +const millisecondsInOneDay = 24 * 60 * 60 * 1000; // hours*minutes*seconds*milliseconds + export default function useCourseUpgrade() { const { courseId, isUpgradeEligible } = useContext(CourseInfoContext); const { offer } = useModel('coursewareMeta', courseId); const { verifiedMode } = useModel('courseHomeMeta', courseId); const { auditTrialLengthDays, + auditTrial, } = useSelector(state => state.learningAssistant); const upgradeUrl = offer?.upgradeUrl || verifiedMode?.upgradeUrl; if (!isUpgradeEligible || !upgradeUrl) { return { upgradeable: false }; } + let auditTrialExpired = false; + let auditTrialDaysRemaining; + + if (auditTrial?.expirationDate) { + const auditTrialExpirationDate = new Date(auditTrial.expirationDate); + auditTrialDaysRemaining = Math.ceil((auditTrialExpirationDate - Date.now()) / millisecondsInOneDay); + + auditTrialExpired = auditTrialDaysRemaining < 0; + } + return { upgradeable: true, auditTrialLengthDays, + auditTrialDaysRemaining, + auditTrialExpired, + auditTrial, upgradeUrl, }; }