Skip to content

Commit 388a3af

Browse files
committed
Save cookieConsent server-side, add animation
1 parent 0590562 commit 388a3af

File tree

6 files changed

+174
-130
lines changed

6 files changed

+174
-130
lines changed

client/modules/User/actions.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,19 @@ export function unlinkService(service) {
341341

342342
export function setUserCookieConsent(cookieConsent) {
343343
// maybe also send this to the server rn?
344-
return {
345-
type: ActionTypes.SET_COOKIE_CONSENT,
346-
cookieConsent
344+
return (dispatch) => {
345+
apiClient
346+
.put('/cookie-consent', { cookieConsent })
347+
.then(() => {
348+
dispatch({
349+
type: ActionTypes.SET_COOKIE_CONSENT,
350+
cookieConsent
351+
});
352+
})
353+
.catch((error) => {
354+
const { response } = error;
355+
const message = response.message || response.data.error;
356+
dispatch(authError(message));
357+
});
347358
};
348359
}

client/modules/User/components/CookieConsent.jsx

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@ import { useSelector, useDispatch } from 'react-redux';
33
import Cookies from 'js-cookie';
44
import styled from 'styled-components';
55
import ReactGA from 'react-ga';
6+
import { Transition } from 'react-transition-group';
67
import getConfig from '../../../utils/getConfig';
78
import { setUserCookieConsent } from '../actions';
89
import { remSize, prop } from '../../../theme';
910
import Button from '../../../common/Button';
1011

1112
const CookieConsentContainer = styled.div`
1213
position: fixed;
13-
bottom: 0;
14+
transition: 1.6s cubic-bezier(0.165, 0.84, 0.44, 1);
15+
bottom: ${({ state }) => {
16+
if (state === 'entered') {
17+
return '0';
18+
}
19+
return remSize(-200);
20+
}};
1421
left: 0;
1522
right: 0;
1623
z-index: 9999;
@@ -47,6 +54,7 @@ const CookieConsentButtons = styled.div`
4754
function CookieConsent() {
4855
const user = useSelector((state) => state.user);
4956
const [cookieConsent, setBrowserCookieConsent] = useState('none');
57+
const [inProp, setInProp] = useState(false);
5058
const dispatch = useDispatch();
5159

5260
function initializeCookieConsent() {
@@ -73,6 +81,7 @@ function CookieConsent() {
7381
}
7482
setBrowserCookieConsent('essential');
7583
Cookies.set('p5-cookie-consent', 'essential', { expires: 365 });
84+
// Remove Google Analytics Cookies
7685
Cookies.remove('_ga');
7786
Cookies.remove('_gat');
7887
Cookies.remove('_gid');
@@ -113,42 +122,45 @@ function CookieConsent() {
113122
mergeCookieConsent();
114123
}, [user.authenticated]);
115124

116-
// Turn off Google Analytics
117-
// useEffect(() => {
118-
// if (cookieConsent === 'all' || user.cookieConsent === 'all') {
119-
// ReactGA.initialize(getConfig('GA_MEASUREMENT_ID'));
120-
// ReactGA.pageview(window.location.pathname + window.location.search);
121-
// }
122-
// }, [cookieConsent, user.cookieConsent]);
123-
124-
const showCookieConsent =
125-
(user.authenticated && user.cookieConsent === 'none') ||
126-
(!user.authenticated && cookieConsent === 'none');
127-
128-
if (!showCookieConsent) return null;
125+
useEffect(() => {
126+
if (cookieConsent !== 'none') {
127+
setInProp(false);
128+
} else {
129+
setInProp(true);
130+
}
131+
}, [cookieConsent]);
129132

130133
return (
131-
<CookieConsentContainer>
132-
<CookieConsentDialog role="dialog" tabIndex="0">
133-
{/* <button aria-label="Close" tabIndex="0"></button> */}
134-
<CookieConsentHeader>Cookies</CookieConsentHeader>
135-
<CookieConsentContent>
136-
<CookieConsentCopy>
137-
The p5.js Editor uses cookies. Some are essential to the website
138-
functionality and allow you to manage an account and preferences.
139-
Others are used for analytics allow us to gather information and
140-
make improvements. You can decide which cookies you would like to
141-
allow.
142-
</CookieConsentCopy>
143-
<CookieConsentButtons>
144-
<Button kind={Button.kinds.secondary} onClick={acceptAllCookies}>
145-
Allow All
146-
</Button>
147-
<Button onClick={acceptEssentialCookies}>Allow Essential</Button>
148-
</CookieConsentButtons>
149-
</CookieConsentContent>
150-
</CookieConsentDialog>
151-
</CookieConsentContainer>
134+
<Transition in={inProp} timeout={500}>
135+
{(state) => (
136+
<CookieConsentContainer state={state}>
137+
<CookieConsentDialog role="dialog" tabIndex="0">
138+
{/* <button aria-label="Close" tabIndex="0"></button> */}
139+
<CookieConsentHeader>Cookies</CookieConsentHeader>
140+
<CookieConsentContent>
141+
<CookieConsentCopy>
142+
The p5.js Editor uses cookies. Some are essential to the website
143+
functionality and allow you to manage an account and
144+
preferences. Others are used for analytics allow us to gather
145+
information and make improvements. You can decide which cookies
146+
you would like to allow.
147+
</CookieConsentCopy>
148+
<CookieConsentButtons>
149+
<Button
150+
kind={Button.kinds.secondary}
151+
onClick={acceptAllCookies}
152+
>
153+
Allow All
154+
</Button>
155+
<Button onClick={acceptEssentialCookies}>
156+
Allow Essential
157+
</Button>
158+
</CookieConsentButtons>
159+
</CookieConsentContent>
160+
</CookieConsentDialog>
161+
</CookieConsentContainer>
162+
)}
163+
</Transition>
152164
);
153165
}
154166
// TODO need to merge browser cookie with user when u login

0 commit comments

Comments
 (0)