Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MWPW-159556] Enhance time picker input tracking logics (#275) Dev -> Stage #276

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 82 additions & 61 deletions ecc/blocks/form-handler/controllers/event-info-component-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,13 @@ export default async function init(component, props) {

const eventTitleInput = component.querySelector('#info-field-event-title');
const startTimeInput = component.querySelector('#time-picker-start-time');
const allStartTimeOptions = startTimeInput.querySelectorAll('sp-menu-item');
const startAmpmInput = component.querySelector('#ampm-picker-start-time');
const startAmpmOptions = startAmpmInput.querySelectorAll('sp-menu-item');
const endTimeInput = component.querySelector('#time-picker-end-time');
const allEndTimeOptions = endTimeInput.querySelectorAll('sp-menu-item');
const endAmpmInput = component.querySelector('#ampm-picker-end-time');
const endAmpmOptions = endAmpmInput.querySelectorAll('sp-menu-item');
const startTime = component.querySelector('#time-picker-start-time-value');
const endTime = component.querySelector('#time-picker-end-time-value');
const datePicker = component.querySelector('#event-info-date-picker');
Expand All @@ -385,97 +389,114 @@ export default async function init(component, props) {
BlockMediator.set('eventDupMetrics', { ...BlockMediator.get('eventDupMetrics'), title: eventTitleInput.value });
});

const resetAllOptions = () => {
[allEndTimeOptions, allStartTimeOptions, endAmpmOptions, startAmpmOptions]
.forEach((options) => {
options.forEach((option) => {
option.disabled = false;
});
});
};

const sameDayEvent = () => datePicker.dataset.startDate
&& datePicker.dataset.endDate
&& datePicker.dataset.startDate === datePicker.dataset.endDate;

const onEndTimeUpdate = () => {
if (endAmpmInput.value && endTimeInput.value) {
endTime.value = convertTo24HourFormat(`${endTimeInput.value} ${endAmpmInput.value}`);
} else {
return;
endTime.value = null;
}

if (datePicker.dataset.startDate !== datePicker.dataset.endDate) return;

if (startAmpmInput.value) {
let toReset = false;
const allOptions = startTimeInput.querySelectorAll('sp-menu-item');
allOptions.forEach((option) => {
const optionTime = convertTo24HourFormat(`${option.value} ${startAmpmInput.value}`);
if (optionTime >= endTime.value) {
option.disabled = true;
if (option.selected) {
toReset = true;
}
} else {
option.disabled = false;
if (!sameDayEvent()) return;

startAmpmOptions[1].disabled = endAmpmInput.value === 'AM';
let onlyPossibleStartAmpm = startAmpmInput.value;
if (!onlyPossibleStartAmpm && startAmpmOptions[1].disabled) onlyPossibleStartAmpm = 'AM';

if (startTimeInput.value) {
if (onlyPossibleStartAmpm) {
const onlyPossibleStartTime = convertTo24HourFormat(`${startTimeInput.value} ${onlyPossibleStartAmpm}`);
if (endAmpmInput.value) {
allEndTimeOptions.forEach((option) => {
const optionTime = convertTo24HourFormat(`${option.value} ${endAmpmInput.value}`);
option.disabled = optionTime <= onlyPossibleStartTime;
});
}
});

if (toReset) {
startTimeInput.value = '';
startAmpmInput.value = '';
startTime.value = null;
allOptions.forEach((option) => {
option.disabled = false;
});
if (endTimeInput.value) {
endAmpmOptions.forEach((option) => {
const optionTime = convertTo24HourFormat(`${endTimeInput.value} ${option.value}`);
option.disabled = optionTime <= onlyPossibleStartTime;
});
}
}
} else if (endTime.value) {
allStartTimeOptions.forEach((option) => {
const optionTime = convertTo24HourFormat(`${option.value} ${onlyPossibleStartAmpm}`);
option.disabled = optionTime >= endTime.value;
});
}
};

const onStartTimeUpdate = () => {
if (startAmpmInput.value && startTimeInput.value) {
startTime.value = convertTo24HourFormat(`${startTimeInput.value} ${startAmpmInput.value}`);
} else {
return;
startTime.value = null;
}

if (datePicker.dataset.startDate !== datePicker.dataset.endDate) return;

if (endAmpmInput.value) {
let toReset = false;
const allOptions = endTimeInput.querySelectorAll('sp-menu-item');
allOptions.forEach((option) => {
const optionTime = convertTo24HourFormat(`${option.value} ${endAmpmInput.value}`);
if (optionTime <= startTime.value) {
option.disabled = true;
if (option.selected) {
toReset = true;
}
} else {
option.disabled = false;
if (!sameDayEvent()) return;

endAmpmOptions[0].disabled = startAmpmInput.value === 'PM';
let onlyPossibleEndAmpm = endAmpmInput.value;
if (!onlyPossibleEndAmpm && endAmpmOptions[0].disabled) onlyPossibleEndAmpm = 'PM';

if (endTimeInput.value) {
if (onlyPossibleEndAmpm) {
const onlyPossibleEndTime = convertTo24HourFormat(`${endTimeInput.value} ${onlyPossibleEndAmpm}`);
if (startAmpmInput.value) {
allStartTimeOptions.forEach((option) => {
const optionTime = convertTo24HourFormat(`${option.value} ${startAmpmInput.value}`);
option.disabled = optionTime >= onlyPossibleEndTime;
});
}
});

if (toReset) {
endTimeInput.value = '';
endAmpmInput.value = '';
endTime.value = null;
allOptions.forEach((option) => {
option.disabled = false;
});
if (startTimeInput.value) {
startAmpmOptions.forEach((option) => {
const optionTime = convertTo24HourFormat(`${startTimeInput.value} ${option.value}`);
option.disabled = optionTime >= onlyPossibleEndTime;
});
}
}
} else if (startTime.value) {
allEndTimeOptions.forEach((option) => {
const optionTime = convertTo24HourFormat(`${option.value} ${onlyPossibleEndAmpm}`);
option.disabled = optionTime <= startTime.value;
});
}
};

const updateTimeOptionsBasedOnDate = (e) => {
if (datePicker.dataset.startDate !== datePicker.dataset.endDate) {
startTimeInput?.querySelectorAll('sp-menu-item')?.forEach((option) => {
option.disabled = false;
});
endTimeInput?.querySelectorAll('sp-menu-item')?.forEach((option) => {
option.disabled = false;
});
} else if (e?.target === endAmpmInput) {
onEndTimeUpdate();
onStartTimeUpdate();
const updateTimeOptionsBasedOnDate = () => {
if (!sameDayEvent()) {
resetAllOptions();
} else {
onStartTimeUpdate();
onEndTimeUpdate();
startTimeInput.value = '';
startAmpmInput.value = '';
endTimeInput.value = '';
endAmpmInput.value = '';
startTime.value = null;
endTime.value = null;

resetAllOptions();
}
};

startTimeInput.addEventListener('change', onStartTimeUpdate);
endTimeInput.addEventListener('change', onEndTimeUpdate);
startAmpmInput.addEventListener('change', updateTimeOptionsBasedOnDate);
endAmpmInput.addEventListener('change', updateTimeOptionsBasedOnDate);
startAmpmInput.addEventListener('change', onStartTimeUpdate);
endAmpmInput.addEventListener('change', onEndTimeUpdate);

datePicker.addEventListener('change', (e) => {
updateTimeOptionsBasedOnDate(e);
Expand Down
Loading