-
Notifications
You must be signed in to change notification settings - Fork 9
/
Layout.js
205 lines (171 loc) · 6 KB
/
Layout.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import { useState, useEffect, useContext } from 'react'
import styled, { createGlobalStyle, keyframes } from 'styled-components'
import dynamic from 'next/dynamic'
import { useRouter } from 'next/router'
import * as gtag from '../../lib/gtag'
import CookiesContext from '../context/cookiesContext'
import Header from './layout/Header'
const AuthForm = dynamic(() => import('./layout/AuthForm'))
import CookieBanner from './layout/CookieBanner'
import Footer from './layout/Footer'
export default function Layout(props) {
// auth modal ↓
const [ isAuthModalVisible, setIsAuthModalVisible ] = useState(false)
const [ isLogInTabVisible, setIsLogInTabVisible ] = useState(null)
const handleAuthModalVisibility = e => {
if (e.currentTarget.name === 'logIn') {
setIsAuthModalVisible(true)
setIsLogInTabVisible(true)
document.body.style.overflow = 'hidden'
} else if (e.currentTarget.name === 'singUp') {
setIsAuthModalVisible(true)
setIsLogInTabVisible(false)
document.body.style.overflow = 'hidden'
}
}
const closeAuthModal = e => {
if (e.target.id === 'outsideAuthModal') {
setIsAuthModalVisible(false)
document.body.style.overflow = 'visible'
}
}
// cookies ↓
const { areCookiesAccepted, setAreCookiesAccepted } = useContext(CookiesContext)
const [ isCookieBannerVisible, setIsCookieBannerVisible ] = useState(false)
const router = useRouter()
const setCookies = () => {
if (localStorage.getItem('areCookiesAccepted') !== null) {
if (localStorage.getItem('areCookiesAccepted') === 'false') {
setAreCookiesAccepted(false)
} else {
setAreCookiesAccepted(true)
}
}
}
const setCookiesBanner = () => {
if (
localStorage.getItem('isCookieBannerVisible') !== null ||
localStorage.getItem('isCookieBannerVisible') === 'false'
) {
setIsCookieBannerVisible(false)
} else {
setIsCookieBannerVisible(true)
}
}
const manageGAScript = () => {
if (localStorage.getItem('areCookiesAccepted') === 'true') {
// initialize script on route change
const handleRouteChange = url => {
gtag.pageview(url)
}
router.events.on('routeChangeComplete', handleRouteChange)
return () => {
router.events.off('routeChangeComplete', handleRouteChange)
}
} else if (localStorage.getItem('areCookiesAccepted') === 'false') {
// delete all GA cookies by expiring them
document.cookie = '_ga=; Max-Age=0;'
const cookiePair = document.cookie.split('; ').find(row => row.startsWith('_ga_'))
if (cookiePair !== undefined) {
const cookieName = cookiePair.substring(0, cookiePair.indexOf('='))
document.cookie = `${cookieName}=; Max-Age=0;`
}
document.cookie = '_gid=; Max-Age=0;'
}
}
const showCookieBanner = e => {
e.preventDefault()
setIsCookieBannerVisible(true)
}
useEffect(() => {
setCookies()
}, [isCookieBannerVisible]) // triggers here in setCookiesBanner() and showCookieBanner(), and in components/layout/CookieBanner.js in rejectCookies() and acceptCookies()
useEffect(() => {
setCookiesBanner()
}, [areCookiesAccepted]) // triggers here in setCookies(), and in components/layout/CookieBanner.js in rejectCookies() and acceptCookies()
useEffect(() => {
manageGAScript()
}, [router.events, areCookiesAccepted]) // router.events triggers on route change and set here in manageGAScript(); areCookiesAccepted - see comment above
return (
<>
<GlobalStyle />
<DivGrid>
<Header handleAuthModalVisibility={handleAuthModalVisibility} />
{
isAuthModalVisible
? <AuthForm
isLogInTabVisible={isLogInTabVisible}
closeAuthModal={closeAuthModal}
handleAuthModalVisibility={handleAuthModalVisibility}
/>
: null
}
{props.children}
<CookieBanner
isCookieBannerVisible={isCookieBannerVisible}
setAreCookiesAccepted={setAreCookiesAccepted}
setIsCookieBannerVisible={setIsCookieBannerVisible}
/>
<Footer showCookieBanner={showCookieBanner} />
</DivGrid>
</>
)
}
const loading = keyframes`
12.5% {border-radius: 37% 63% 70% 30% / 30% 62% 38% 70%}
25% {border-radius: 84% 16% 15% 85% / 55% 79% 21% 45%}
37.5% {border-radius: 73% 27% 74% 26% / 64% 32% 68% 36%}
50% {border-radius: 73% 27% 18% 82% / 52% 32% 68% 48%}
62.5% {border-radius: 33% 67% 18% 82% / 52% 75% 25% 48%}
75% {border-radius: 12% 88% 69% 31% / 10% 66% 34% 90%}
87.5% {border-radius: 50% 50% 70% 30% / 52% 62% 38% 48%}
`
const GlobalStyle = createGlobalStyle`
* {
box-sizing: border-box;
&:not([data-offset-key]):not([data-text]) {
font-family: 'Roboto Condensed', sans-serif;
}
}
html, body {
padding: 0;
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen,
Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, Georgia, sans-serif;
height: 100%;
scroll-behavior: smooth;
background-color: #e9e9e9;
}
#__next {
height: 100%;
}
.loader {
width: 70px;
height: 70px;
padding: 10px;
background: #fff;
mix-blend-mode: darken;
display: grid;
filter: blur(4px) contrast(10) hue-rotate(270deg);
&:before, &:after {
content:"";
grid-area: 1/1;
animation: ${loading} 3s infinite linear;
background:#ff00ff;
border-radius: 50%;
}
&:after {
animation-delay: -.8s;
}
}
`
const DivGrid = styled.div`
display: grid;
grid-template-rows: auto 1fr auto;
grid-template-columns: 1fr 1200px 1fr;
height: 100%;
position: relative;
@media only screen and (max-width: 1220px) {
grid-template-columns: auto 100% auto;
}
`