generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MWPW-153363] Countdown Timer implementation based on page metadata (#…
…2928) * CDT Approach based on metadata * Add unit tests for CDT * self code review * removed comments * Add support for CDT in media block * Code Coverage fix * code coverage * code coverage * Handling Review Comments * Review comments * add unit test for code coverage * added cdt feature tests * increase code coverage * NIT for one line coverage * review comments for parallel load * code coverage * Fix UTC * Support instant query param * Fix code coverage
- Loading branch information
1 parent
7d7eb63
commit c5e352f
Showing
13 changed files
with
351 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
.horizontal, | ||
.vertical { | ||
display: flex; | ||
padding: 20px 0; | ||
} | ||
|
||
.vertical { | ||
flex-direction: column; | ||
} | ||
|
||
.center { | ||
align-items: center; | ||
justify-content: center; | ||
} | ||
|
||
.timer-label { | ||
font-size: var(--type-body-s-size); | ||
font-weight: 700; | ||
height: 27px; | ||
} | ||
|
||
.light .timer-label { | ||
color: #000; | ||
} | ||
|
||
.dark .timer-label { | ||
color: #FFF; | ||
} | ||
|
||
.horizontal .timer-label { | ||
margin: 0 2px 27px; | ||
} | ||
|
||
.timer-block { | ||
display: flex; | ||
} | ||
|
||
.horizontal .timer-block { | ||
margin-left: 10px; | ||
} | ||
|
||
.timer-fragment { | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
} | ||
|
||
.timer-box { | ||
padding: 0 9px; | ||
width: 10px; | ||
border-radius: 5px; | ||
font-size: var(--type-body-m-size); | ||
font-weight: 700; | ||
text-align: center; | ||
} | ||
|
||
.light .timer-box { | ||
background-color: #222; | ||
color: #FFF; | ||
} | ||
|
||
.dark .timer-box { | ||
background-color: #EBEBEB; | ||
color: #1D1D1D; | ||
} | ||
|
||
.timer-unit-container { | ||
display: flex; | ||
column-gap: 2px; | ||
align-items: center; | ||
} | ||
|
||
.timer-unit-label { | ||
width: 100%; | ||
font-size: var(--type-body-xs-size); | ||
font-weight: 400; | ||
text-align: start; | ||
} | ||
|
||
.light .timer-unit-label { | ||
color: #464646; | ||
} | ||
|
||
.dark .timer-unit-label { | ||
color: #D1D1D1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
import { getMetadata, getConfig, createTag } from '../../utils/utils.js'; | ||
import { replaceKey } from '../placeholders.js'; | ||
|
||
function loadCountdownTimer( | ||
container, | ||
cdtLabel, | ||
cdtDays, | ||
cdtHours, | ||
cdtMins, | ||
timeRangesEpoch, | ||
) { | ||
let isVisible = false; | ||
let interval; | ||
|
||
function appendTimerBox(parent, value, label) { | ||
const fragment = createTag('div', { class: 'timer-fragment' }, null, { parent }); | ||
const unitContainer = createTag('div', { class: 'timer-unit-container' }, null, { parent: fragment }); | ||
createTag('div', { class: 'timer-unit-label' }, label, { parent: fragment }); | ||
|
||
createTag('div', { class: 'timer-box' }, Math.floor(value / 10).toString(), { parent: unitContainer }); | ||
createTag('div', { class: 'timer-box' }, (value % 10).toString(), { parent: unitContainer }); | ||
} | ||
|
||
function appendSeparator(parent) { | ||
createTag('div', { class: 'timer-separator' }, ':', { parent }); | ||
} | ||
|
||
function appendTimerBlock(parent, daysLeft, hoursLeft, minutesLeft) { | ||
const timerBlock = createTag('div', { class: 'timer-block' }, null, { parent }); | ||
appendTimerBox(timerBlock, daysLeft, cdtDays); | ||
appendSeparator(timerBlock); | ||
appendTimerBox(timerBlock, hoursLeft, cdtHours); | ||
appendSeparator(timerBlock); | ||
appendTimerBox(timerBlock, minutesLeft, cdtMins); | ||
} | ||
|
||
function appendTimerLabel(parent, label) { | ||
createTag('div', { class: 'timer-label' }, label, { parent }); | ||
} | ||
|
||
function removeCountdown() { | ||
container.replaceChildren(); | ||
} | ||
|
||
function render(daysLeft, hoursLeft, minutesLeft) { | ||
if (!isVisible) return; | ||
|
||
removeCountdown(); | ||
|
||
appendTimerLabel(container, cdtLabel); | ||
appendTimerBlock(container, daysLeft, hoursLeft, minutesLeft); | ||
} | ||
|
||
function updateCountdown() { | ||
const instant = new URL(window.location.href)?.searchParams?.get('instant'); | ||
const currentTime = instant ? new Date(instant) : Date.now(); | ||
|
||
for (let i = 0; i < timeRangesEpoch.length; i += 2) { | ||
const startTime = timeRangesEpoch[i]; | ||
const endTime = timeRangesEpoch[i + 1]; | ||
|
||
if (currentTime >= startTime && currentTime <= endTime) { | ||
isVisible = true; | ||
const diffTime = endTime - currentTime; | ||
const daysLeft = Math.floor(diffTime / (1000 * 60 * 60 * 24)); | ||
const hoursLeft = Math.floor((diffTime % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); | ||
const minutesLeft = Math.floor((diffTime % (1000 * 60 * 60)) / (1000 * 60)); | ||
render(daysLeft, hoursLeft, minutesLeft); | ||
return; | ||
} | ||
} | ||
|
||
isVisible = false; | ||
clearInterval(interval); | ||
removeCountdown(); | ||
} | ||
|
||
function startCountdown() { | ||
const oneMinuteinMs = 60000; | ||
updateCountdown(); | ||
interval = setInterval(updateCountdown, oneMinuteinMs); | ||
} | ||
|
||
startCountdown(); | ||
} | ||
|
||
function isMobile() { | ||
return window.matchMedia('(max-width: 767px)').matches; | ||
} | ||
|
||
export default async function initCDT(el, classList) { | ||
const placeholders = ['cdt-ends-in', 'cdt-days', 'cdt-hours', 'cdt-mins']; | ||
const [cdtLabel, cdtDays, cdtHours, cdtMins] = await Promise.all( | ||
placeholders.map((placeholder) => replaceKey(placeholder, getConfig())), | ||
); | ||
|
||
const cdtMetadata = getMetadata('countdown-timer'); | ||
if (cdtMetadata === null) { | ||
throw new Error('Metadata for countdown-timer is not available'); | ||
} | ||
|
||
const cdtRange = cdtMetadata.split(','); | ||
if (cdtRange.length % 2 !== 0) { | ||
throw new Error('Invalid countdown timer range'); | ||
} | ||
|
||
const timeRangesEpoch = cdtRange.map((time) => { | ||
const parsedTime = Date.parse(time?.trim()); | ||
return Number.isNaN(parsedTime) ? null : parsedTime; | ||
}); | ||
if (timeRangesEpoch.includes(null)) { | ||
throw new Error('Invalid format for countdown timer range'); | ||
} | ||
|
||
const cdtDiv = createTag('div', { class: 'countdown-timer' }, null, { parent: el }); | ||
cdtDiv.classList.add(isMobile() ? 'vertical' : 'horizontal'); | ||
cdtDiv.classList.add(classList.contains('dark') ? 'dark' : 'light'); | ||
if (classList.contains('center')) cdtDiv.classList.add('center'); | ||
|
||
loadCountdownTimer(cdtDiv, cdtLabel, cdtDays, cdtHours, cdtMins, timeRangesEpoch); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"total": 21, | ||
"offset": 0, | ||
"limit": 21, | ||
"data": [ | ||
{ | ||
"key": "cdt-ends-in", | ||
"value": "ENDS IN" | ||
}, | ||
{ | ||
"key": "cdt-days", | ||
"value": "days" | ||
}, | ||
{ | ||
"key": "cdt-hours", | ||
"value": "hours" | ||
}, | ||
{ | ||
"key": "cdt-mins", | ||
"value": "mins" | ||
} | ||
], | ||
":type": "sheet" | ||
} |
Oops, something went wrong.