1
1
document . addEventListener ( "htmx:afterRequest" , function ( ) {
2
2
const reportDataEl = document . getElementById ( "reportData" ) ;
3
- if ( ! reportDataEl ) {
3
+ if ( ! reportDataEl || ! reportDataEl . textContent ) {
4
4
return ;
5
5
}
6
6
const reportData = JSON . parse ( reportDataEl . textContent ) ;
@@ -9,6 +9,7 @@ document.addEventListener("htmx:afterRequest", function () {
9
9
const frequencyNumberInput = document . getElementById ( "id_occurrence" ) ;
10
10
const frequencyPeriodSelect = document . getElementById ( "id_frequency" ) ;
11
11
const startDateInput = document . getElementById ( "id_start" ) ;
12
+ const doesNotRepeatInput = document . getElementById ( "id_does_not_repeat" ) ;
12
13
13
14
// Form slots
14
15
const projectEndSlot = document . querySelector ( ".js-project-end-slot" ) ;
@@ -23,105 +24,147 @@ document.addEventListener("htmx:afterRequest", function () {
23
24
const nextReportDueSlot = document . querySelector ( ".js-next-report-due-slot" ) ;
24
25
25
26
/**
26
- * Set initial values & setup event listeners
27
+ * Initialize the report frequency configuration form
28
+ * Sets up initial values and event listeners
27
29
*/
28
30
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
+ }
31
38
setFrequency ( ) ;
32
39
setReportPeriodStart ( ) ;
33
40
setReportPeriod ( ) ;
34
41
35
- // Add event listeners
36
42
addFrequencyEvents ( ) ;
37
43
addReportPeriodEvents ( ) ;
44
+ setDoesNotRepeat ( ) ;
38
45
}
39
46
40
47
/**
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
42
50
*/
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 ;
45
72
}
46
73
47
74
/**
48
- * Set the reporting frequency
75
+ * Updates frequency display in the UI
76
+ * Sets the frequency number and period text
49
77
*/
50
78
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 || "" ;
53
89
pluraliseTimePeriod ( frequencyNumberInput . value ) ;
54
90
}
55
91
56
92
/**
57
- * Set the reporting period start date
93
+ * Sets the initial report period start date in the UI
58
94
*/
59
95
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 ) ;
62
102
}
63
103
64
104
/**
65
- * Set the reporting period
105
+ * Updates the report period end date and next report due date in the UI
66
106
*/
67
107
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
+
69
117
periodEndSlot . innerHTML = startDateInput . value ;
70
118
71
- // Update the reporting period range (next report date - today)
72
119
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 ) ;
75
121
const pluraliseWeeks = weeks === 1 ? "" : "s" ;
76
122
const pluraliseDays = days === 1 ? "" : "s" ;
77
123
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 } `;
83
127
}
84
128
85
129
/**
86
- * Set report period once start date receives input
130
+ * Attaches event listeners for report period inputs
87
131
*/
88
132
function addReportPeriodEvents ( ) {
89
- startDateInput . addEventListener ( "input" , ( ) => {
90
- setReportPeriod ( ) ;
91
- } ) ;
133
+ if ( startDateInput ) {
134
+ startDateInput . oninput = setReportPeriod ;
135
+ }
92
136
}
93
137
94
138
/**
95
- * Update reporting frequency as the options are changed
139
+ * Attaches event listeners for frequency inputs
96
140
*/
97
141
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
+ }
105
148
}
106
149
107
150
/**
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
111
153
*/
112
154
function pluraliseTimePeriod ( number ) {
113
- frequencyPeriodSlot . innerHTML = `${ frequencyPeriodSelect . value } ${
155
+ if ( ! frequencyPeriodSlot || ! frequencyPeriodSelect ) {
156
+ return ;
157
+ }
158
+ frequencyPeriodSlot . innerHTML = `${ frequencyPeriodSelect . value || "" } ${
114
159
Number ( number ) === 1 ? "" : "s"
115
160
} `;
116
161
}
117
162
118
163
/**
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
125
168
*/
126
169
function dateDiffInDays ( startDate , endDate ) {
127
170
const msPerDay = 1000 * 60 * 60 * 24 ;
@@ -140,11 +183,9 @@ document.addEventListener("htmx:afterRequest", function () {
140
183
}
141
184
142
185
/**
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
148
189
*/
149
190
function getWeeks ( days ) {
150
191
return {
0 commit comments