diff --git a/src/core/api.js b/src/core/api.js index a5731faf..d84dc2a6 100644 --- a/src/core/api.js +++ b/src/core/api.js @@ -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 */ @@ -289,6 +324,8 @@ export const hidePreferences = () => { state._preferencesModalVisible = false; + discardUnsavedPreferences(); + /** * Fix focus restoration */