Skip to content

Commit b7ce1fe

Browse files
elhussienalmasritomkins
authored andcommitted
move-checkboxes-state-to-localStorage
1 parent cc9403a commit b7ce1fe

1 file changed

Lines changed: 28 additions & 28 deletions

File tree

client/src/includes/chooserModal.js

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ class SearchController {
131131
this.searchFromForm();
132132
return false;
133133
});
134+
135+
this.multipleChoice = new Set();
134136
}
135137

136138
attachSearchInput(selector) {
@@ -165,9 +167,7 @@ class SearchController {
165167
'form[data-multiple-choice-form] input[type="checkbox"]',
166168
);
167169
checkboxes.forEach((checkbox) => {
168-
const savedState = sessionStorage.getItem(checkbox.id);
169-
170-
if (savedState === 'true') {
170+
if (this.multipleChoice.has(checkbox.value)) {
171171
checkbox.setAttribute('checked', true);
172172
}
173173
});
@@ -191,6 +191,10 @@ class SearchController {
191191
searchFromForm() {
192192
this.search(this.form.serialize());
193193
}
194+
195+
updateMultipleChoice(choices) {
196+
this.multipleChoice = choices;
197+
}
194198
}
195199

196200
class ChooserModalOnloadHandlerFactory {
@@ -222,6 +226,7 @@ class ChooserModalOnloadHandlerFactory {
222226
this.creationFormEventName = opts?.creationFormEventName;
223227

224228
this.searchController = null;
229+
this.multipleChoice = new Set();
225230
}
226231

227232
ajaxifyLinks(modal, containerElement) {
@@ -249,7 +254,7 @@ class ChooserModalOnloadHandlerFactory {
249254
this.updateMultipleChoiceSubmitEnabledState(modal);
250255
});
251256

252-
this.updateMultipleChoiceSubmitLocalStorage(modal);
257+
this.updateMultipleChoiceSubmitLocalStorage();
253258

254259
const form = modal.container[0].querySelector(
255260
'[data-multiple-choice-form]',
@@ -269,40 +274,35 @@ class ChooserModalOnloadHandlerFactory {
269274
}
270275
}
271276

272-
updateMultipleChoiceSubmitLocalStorage(modal) {
273-
// eslint-disable-next-line func-names
274-
$(modal.body).on('change', '[data-multiple-choice-select]', function () {
275-
$(this).each(() => {
276-
sessionStorage.setItem($(this).prop('id'), $(this).prop('checked'));
277-
});
277+
updateMultipleChoiceSubmitLocalStorage() {
278+
$(document).on('change', '[data-multiple-choice-select]', (e) => {
279+
const el = e.currentTarget;
280+
281+
if (el.checked) {
282+
this.multipleChoice.add(el.value);
283+
} else {
284+
this.multipleChoice.delete(el.value);
285+
}
286+
this.searchController.updateMultipleChoice(this.multipleChoice);
278287
});
279288
}
280289

281290
// get Checkbox States and create hidden inputs on submit to update the form with the missing checkboxes.
282291
getMissingCheckboxes(form) {
283-
for (let i = 0; i < sessionStorage.length; i += 1) {
284-
const key = sessionStorage.key(i);
285-
const value = sessionStorage.getItem(key);
286-
287-
if (
288-
key.startsWith('chooser-modal-select') &&
289-
value === 'true' &&
290-
(document.getElementById(key) == null ||
291-
(document.getElementById(key) &&
292-
document.getElementById(key).checked !== true))
293-
) {
294-
const id = key.substring('chooser-modal-select-'.length);
292+
this.multipleChoice.forEach((choice) => {
293+
const checkbox = form.querySelector(
294+
`input[type="checkbox"][value="${choice}"]`,
295+
);
296+
297+
if (checkbox?.checked === false || !checkbox) {
295298
const input = document.createElement('input');
296299
input.type = 'hidden';
297300
input.name = 'id';
298-
input.value = id;
301+
input.value = choice;
299302
form.appendChild(input);
300303
}
301-
302-
if (key.startsWith('chooser-modal-select') && value === 'true') {
303-
sessionStorage.setItem(key, false);
304-
}
305-
}
304+
});
305+
this.multipleChoice.clear();
306306
}
307307

308308
modalHasTabs(modal) {

0 commit comments

Comments
 (0)