forked from openedx/frontend-component-cookie-policy-banner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.js
124 lines (102 loc) · 3.18 KB
/
utilities.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
import Cookie from 'universal-cookie';
import {
DEFAULT_IETF_TAG,
IETF_TAGS,
STAGE_ENVIRONMENTS,
LANGUAGE_CODE_TO_IETF_TAGS,
LOCALHOST,
COOKIE_POLICY_VIEWED_NAME,
} from './constants';
const firstMatchingStageEnvironment = () => {
const matches = Object.keys(STAGE_ENVIRONMENTS)
.filter(key => window.location.hostname.indexOf(STAGE_ENVIRONMENTS[key].baseURL) >= 0);
if (matches.length > 0) {
return STAGE_ENVIRONMENTS[matches[0]];
}
return null;
};
// Setting path to '/' to be apply to all subdomains
// Setting maxAge to 2^31 -1
// because Number.SAFE_MAX_INTEGER does not get processed properly by the browser
// nor does the max Date defined in http://www.ecma-international.org/ecma-262/5.1/#sec-15.9.1.1
const buildCookieCreationData = ({ prefix, domain, cookieName = null }) => ({
cookieName: `${prefix}-${cookieName || COOKIE_POLICY_VIEWED_NAME}`,
domain,
path: '/',
maxAge: 2147483647,
});
const getCookieCreationData = (cookieName = null) => {
if (window.location.hostname.indexOf(LOCALHOST) >= 0) {
return buildCookieCreationData({
prefix: LOCALHOST,
domain: LOCALHOST,
cookieName,
});
}
const stageEnvironment = firstMatchingStageEnvironment();
if (stageEnvironment) {
return buildCookieCreationData({
prefix: stageEnvironment.prefix,
domain: `.${stageEnvironment.baseURL}`,
cookieName,
});
}
if (window.location.hostname.indexOf('.edx.org') >= 0) {
return buildCookieCreationData({
prefix: 'prod',
domain: '.edx.org',
cookieName,
});
}
return null;
};
const isProduction = () => !firstMatchingStageEnvironment()
&& window.location.hostname.indexOf(LOCALHOST) < 0
&& window.location.hostname.indexOf('.edx.org') >= 0;
const getIETFTag = () => {
const cookie = new Cookie('edx.org');
const ietfTag = isProduction() ? cookie.get('prod-edx-language-preference') : cookie.get('stage-edx-language-preference');
if (!ietfTag || IETF_TAGS.indexOf(ietfTag) <= -1) {
return DEFAULT_IETF_TAG;
}
return ietfTag;
};
const getIETFTagFromLanguageCode = (languageCode) => {
const ietfTag = LANGUAGE_CODE_TO_IETF_TAGS[languageCode];
if (!ietfTag || IETF_TAGS.indexOf(ietfTag) <= -1) {
return DEFAULT_IETF_TAG;
}
return ietfTag;
};
const createHasViewedCookieBanner = (cookieName = null) => {
const cookieCreationData = getCookieCreationData(cookieName);
if (!!cookieCreationData
&& !!cookieCreationData.cookieName
&& !!cookieCreationData.domain
&& !!cookieCreationData.path
&& !!cookieCreationData.maxAge) {
return new Cookie().set(
cookieCreationData.cookieName,
true,
{
domain: cookieCreationData.domain,
path: cookieCreationData.path,
maxAge: cookieCreationData.maxAge,
},
);
}
return false;
};
const hasViewedCookieBanner = (cookieName = null) => {
const cookieCreationData = getCookieCreationData(cookieName);
return !!cookieCreationData && !!new Cookie().get(cookieCreationData.cookieName);
};
export {
getIETFTag,
createHasViewedCookieBanner,
hasViewedCookieBanner,
firstMatchingStageEnvironment,
getCookieCreationData,
getIETFTagFromLanguageCode,
isProduction,
};