Update cookie value (#125)

* Chore: resetCookieConsentValue function

* Chore: VISIBLE_OPTIONS enum & render update in order to use it

* Readme review
This commit is contained in:
Situ
2021-09-17 18:22:12 +02:00
committed by GitHub
parent 239001cb19
commit c3ba87e62f
2 changed files with 79 additions and 41 deletions

View File

@@ -14,6 +14,12 @@ export const SAME_SITE_OPTIONS = {
NONE: "none",
};
export const VISIBLE_OPTIONS = {
HIDDEN: 'hidden',
SHOW: 'show',
BY_COOKIE_VALUE: 'byCookieValue'
}
/**
* Returns the value of the consent cookie
* Retrieves the regular value first and if not found the legacy one according
@@ -30,6 +36,15 @@ export const getCookieConsentValue = (name = defaultCookieConsentName) => {
return cookieValue;
};
/**
* Reset the consent cookie
* Remove the cookie on browser in order to allow user to change their consent
* @param {*} name optional name of the cookie
*/
export const resetCookieConsentValue = (name = defaultCookieConsentName) => {
Cookies.remove(name);
};
/**
* Get the legacy cookie name by the regular cookie name
* @param {string} name of cookie to get
@@ -227,8 +242,16 @@ class CookieConsent extends Component {
render() {
// If the bar shouldn't be visible don't render anything.
if (!this.state.visible) {
return null;
switch (this.props.visible) {
case VISIBLE_OPTIONS.HIDDEN:
return null;
case VISIBLE_OPTIONS.BY_COOKIE_VALUE:
if (!this.state.visible) {
return null;
}
break;
default:
break;
}
const {
@@ -366,6 +389,7 @@ class CookieConsent extends Component {
CookieConsent.propTypes = {
location: PropTypes.oneOf(Object.keys(OPTIONS).map((key) => OPTIONS[key])),
visible: PropTypes.oneOf(Object.keys(VISIBLE_OPTIONS).map((key) => VISIBLE_OPTIONS[key])),
sameSite: PropTypes.oneOf(Object.keys(SAME_SITE_OPTIONS).map((key) => SAME_SITE_OPTIONS[key])),
style: PropTypes.object,
buttonStyle: PropTypes.object,
@@ -412,6 +436,7 @@ CookieConsent.defaultProps = {
hideOnAccept: true,
hideOnDecline: true,
location: OPTIONS.BOTTOM,
visible: VISIBLE_OPTIONS.BY_COOKIE_VALUE,
onAccept: () => {},
onDecline: () => {},
cookieName: defaultCookieConsentName,