Skip to content

Commit df4e5a9

Browse files
authored
Fix reporting modal - does not repeat checkbox (#4490)
When the does not repeat is checked, we should not show the frequecy selector, currently it doesn't hide it
1 parent 0136874 commit df4e5a9

File tree

1 file changed

+92
-51
lines changed

1 file changed

+92
-51
lines changed

hypha/static_src/javascript/report-calculator.js

+92-51
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
document.addEventListener("htmx:afterRequest", function () {
22
const reportDataEl = document.getElementById("reportData");
3-
if (!reportDataEl) {
3+
if (!reportDataEl || !reportDataEl.textContent) {
44
return;
55
}
66
const reportData = JSON.parse(reportDataEl.textContent);
@@ -9,6 +9,7 @@ document.addEventListener("htmx:afterRequest", function () {
99
const frequencyNumberInput = document.getElementById("id_occurrence");
1010
const frequencyPeriodSelect = document.getElementById("id_frequency");
1111
const startDateInput = document.getElementById("id_start");
12+
const doesNotRepeatInput = document.getElementById("id_does_not_repeat");
1213

1314
// Form slots
1415
const projectEndSlot = document.querySelector(".js-project-end-slot");
@@ -23,105 +24,147 @@ document.addEventListener("htmx:afterRequest", function () {
2324
const nextReportDueSlot = document.querySelector(".js-next-report-due-slot");
2425

2526
/**
26-
* Set initial values & setup event listeners
27+
* Initialize the report frequency configuration form
28+
* Sets up initial values and event listeners
2729
*/
2830
function init() {
29-
// Set on page load
30-
setProjectEnd();
31+
if (!frequencyNumberInput || !frequencyPeriodSelect || !startDateInput) {
32+
return;
33+
}
34+
// set project end date
35+
if (projectEndSlot && reportData.projectEndDate) {
36+
projectEndSlot.innerHTML = reportData.projectEndDate;
37+
}
3138
setFrequency();
3239
setReportPeriodStart();
3340
setReportPeriod();
3441

35-
// Add event listeners
3642
addFrequencyEvents();
3743
addReportPeriodEvents();
44+
setDoesNotRepeat();
3845
}
3946

4047
/**
41-
* Sets the project end date in the form info box
48+
* Handles the "Does not repeat" checkbox behavior
49+
* Shows/hides frequency inputs based on checkbox state
4250
*/
43-
function setProjectEnd() {
44-
projectEndSlot.innerHTML = reportData.projectEndDate;
51+
function setDoesNotRepeat() {
52+
if (!doesNotRepeatInput) {
53+
return;
54+
}
55+
function showHideFrequencyInputs() {
56+
const elements = document.querySelectorAll(
57+
".form__group--report-every, .form__group--schedule"
58+
);
59+
if (doesNotRepeatInput.checked) {
60+
elements.forEach((element) => {
61+
element.classList.add("!hidden");
62+
});
63+
} else {
64+
elements.forEach((element) => {
65+
element.classList.remove("!hidden");
66+
});
67+
}
68+
}
69+
70+
showHideFrequencyInputs();
71+
doesNotRepeatInput.onchange = showHideFrequencyInputs;
4572
}
4673

4774
/**
48-
* Set the reporting frequency
75+
* Updates frequency display in the UI
76+
* Sets the frequency number and period text
4977
*/
5078
function setFrequency() {
51-
frequencyPeriodSlot.innerHTML = `${frequencyPeriodSlot.value}`;
52-
frequencyNumberSlot.innerHTML = frequencyNumberInput.value;
79+
if (
80+
!frequencyNumberSlot ||
81+
!frequencyPeriodSlot ||
82+
!frequencyPeriodSelect ||
83+
!frequencyNumberInput
84+
) {
85+
return;
86+
}
87+
frequencyPeriodSlot.innerHTML = `${frequencyPeriodSelect.value || ""}`;
88+
frequencyNumberSlot.innerHTML = frequencyNumberInput.value || "";
5389
pluraliseTimePeriod(frequencyNumberInput.value);
5490
}
5591

5692
/**
57-
* Set the reporting period start date
93+
* Sets the initial report period start date in the UI
5894
*/
5995
function setReportPeriodStart() {
60-
const startDate = new Date(reportData.startDate);
61-
periodStartSlot.innerHTML = startDate.toISOString().slice(0, 10);
96+
if (!periodStartSlot || !reportData.startDate) {
97+
return;
98+
}
99+
periodStartSlot.innerHTML = new Date(reportData.startDate)
100+
.toISOString()
101+
.slice(0, 10);
62102
}
63103

64104
/**
65-
* Set the reporting period
105+
* Updates the report period end date and next report due date in the UI
66106
*/
67107
function setReportPeriod() {
68-
// Update the reporting period end date (next report date)
108+
if (
109+
!periodEndSlot ||
110+
!nextReportDueSlot ||
111+
!startDateInput ||
112+
!startDateInput.value
113+
) {
114+
return;
115+
}
116+
69117
periodEndSlot.innerHTML = startDateInput.value;
70118

71-
// Update the reporting period range (next report date - today)
72119
const daysDiff = dateDiffInDays(new Date(), new Date(startDateInput.value));
73-
const weeksAndDays = getWeeks(daysDiff);
74-
const { weeks, days } = weeksAndDays;
120+
const { weeks, days } = getWeeks(daysDiff);
75121
const pluraliseWeeks = weeks === 1 ? "" : "s";
76122
const pluraliseDays = days === 1 ? "" : "s";
77123

78-
nextReportDueSlot.innerHTML = `
79-
${
80-
weeks > 0 ? `${weeks} week${pluraliseWeeks}` : ""
81-
} ${days} day${pluraliseDays}
82-
`;
124+
nextReportDueSlot.innerHTML = `${
125+
weeks > 0 ? `${weeks} week${pluraliseWeeks}` : ""
126+
} ${days} day${pluraliseDays}`;
83127
}
84128

85129
/**
86-
* Set report period once start date receives input
130+
* Attaches event listeners for report period inputs
87131
*/
88132
function addReportPeriodEvents() {
89-
startDateInput.addEventListener("input", () => {
90-
setReportPeriod();
91-
});
133+
if (startDateInput) {
134+
startDateInput.oninput = setReportPeriod;
135+
}
92136
}
93137

94138
/**
95-
* Update reporting frequency as the options are changed
139+
* Attaches event listeners for frequency inputs
96140
*/
97141
function addFrequencyEvents() {
98-
frequencyNumberInput.addEventListener("input", () => {
99-
setFrequency();
100-
});
101-
102-
frequencyPeriodSelect.addEventListener("change", () => {
103-
setFrequency();
104-
});
142+
if (frequencyNumberInput) {
143+
frequencyNumberInput.oninput = setFrequency;
144+
}
145+
if (frequencyPeriodSelect) {
146+
frequencyPeriodSelect.onchange = setFrequency;
147+
}
105148
}
106149

107150
/**
108-
* Adds an "s" to the frequencyPeriodSelect text if plural, removes "s" otherwise.
109-
*
110-
* @param {number} number - number to determine plural for
151+
* Adds proper pluralization to time period display
152+
* @param {string|number} number - The frequency number
111153
*/
112154
function pluraliseTimePeriod(number) {
113-
frequencyPeriodSlot.innerHTML = `${frequencyPeriodSelect.value}${
155+
if (!frequencyPeriodSlot || !frequencyPeriodSelect) {
156+
return;
157+
}
158+
frequencyPeriodSlot.innerHTML = `${frequencyPeriodSelect.value || ""}${
114159
Number(number) === 1 ? "" : "s"
115160
}`;
116161
}
117162

118163
/**
119-
* Get the difference of days between two dates
120-
*
121-
* @param {Date} startDate - the subtrahend date
122-
* @param {Date} endDate - the minuend date
123-
*
124-
* @returns {number} Difference of days betwen the given dates
164+
* Calculates the difference in days between two dates
165+
* @param {Date} startDate - The start date
166+
* @param {Date} endDate - The end date
167+
* @returns {number} Number of days between dates
125168
*/
126169
function dateDiffInDays(startDate, endDate) {
127170
const msPerDay = 1000 * 60 * 60 * 24;
@@ -140,11 +183,9 @@ document.addEventListener("htmx:afterRequest", function () {
140183
}
141184

142185
/**
143-
* Convert days into weeks and days
144-
*
145-
* @param {number} days - number of days
146-
*
147-
* @returns {{weeks: number, days: number}} number of weeks & days
186+
* Converts days to weeks and remaining days
187+
* @param {number} days - Total days
188+
* @returns {Object} Object containing weeks and remaining days
148189
*/
149190
function getWeeks(days) {
150191
return {

0 commit comments

Comments
 (0)