Skip to content

Commit

Permalink
Feat: discard unsaved preferences on modal close (ref. #481)
Browse files Browse the repository at this point in the history
  • Loading branch information
orestbida committed Apr 23, 2023
1 parent 3d54c53 commit a51dd88
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/core/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,41 @@ export const showPreferences = () => {
fireEvent(globalObj._customEvents._onModalShow, PREFERENCES_MODAL_NAME);
};

/**
* https://github.com/orestbida/cookieconsent/issues/481
*/
const discardUnsavedPreferences = () => {

const consentIsValid = validConsent();
const allDefinedCategories = globalObj._state._allDefinedCategories;
const categoryInputs = globalObj._dom._categoryCheckboxInputs;
const serviceInputs = globalObj._dom._serviceCheckboxInputs;

/**
* @param {string} category
*/
const categoryEnabledByDefault = (category) => globalObj._state._userConfig.mode === OPT_OUT_MODE && !!allDefinedCategories[category].enabled;

for(const category in categoryInputs) {

const isReadOnly = !!allDefinedCategories[category].readOnly;

categoryInputs[category].checked = isReadOnly || (consentIsValid
? acceptedCategory(category)
: categoryEnabledByDefault(category)
);

for(const service in serviceInputs[category]) {
serviceInputs[category][service].checked = isReadOnly || (consentIsValid
? acceptedService(service, category)
: categoryEnabledByDefault(category)
);
}
}


};

/**
* Hide preferences modal
*/
Expand All @@ -289,6 +324,8 @@ export const hidePreferences = () => {

state._preferencesModalVisible = false;

discardUnsavedPreferences();

/**
* Fix focus restoration
*/
Expand Down

0 comments on commit a51dd88

Please sign in to comment.