-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.html
More file actions
123 lines (121 loc) · 3.65 KB
/
index.html
File metadata and controls
123 lines (121 loc) · 3.65 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Unsubscribe</title>
<style>
body {
font-family: "SF Pro Text", sans-serif;
background: #f6f7f8;
display: flex;
justify-content: center;
flex-direction: column;
align-items: center;
padding-top: 10vh;
color: #051b2c;
}
.container {
background-color: #ffffff;
padding: 24px 24px 26px 24px;
border-radius: 8px;
max-width: 438px;
flex-shrink: 0;
box-shadow: 0px 3px 4px -1px rgba(5, 27, 44, 0.08),
0px 1px 8px -1px rgba(5, 27, 44, 0.12);
}
h1 {
font-size: 24px;
font-weight: 400;
line-height: normal;
margin: 0;
}
p {
margin: 24px 0;
font-size: 16px;
font-style: normal;
font-weight: 400;
line-height: 24px;
}
button {
display: flex;
padding: 8px 12px;
justify-content: center;
align-items: center;
gap: 10px;
background: #4346ce;
color: #ffffff;
border-radius: 4px;
border: 1px solid #3a3db3;
font-size: 16px;
box-shadow: 0px 1px 1px 0px rgba(5, 27, 44, 0.16),
0px 2px 0px 0px rgba(255, 255, 255, 0.05) inset;
}
button:hover,
button:focus {
background: #3a3db3;
}
button:focus {
box-shadow: 0px 1px 1px 0px rgba(5, 27, 44, 0.16),
0px 2px 0px 0px rgba(255, 255, 255, 0.05) inset;
}
</style>
</head>
<body>
<div class="container">
<h1>Unsubscribe</h1>
<p>
Unsubscribe <span id="email"></span> from all marketing email messages?
</p>
<button id="unsubscribe_button" type="button" class="unsubscribe-button">
Unsubscribe
</button>
</div>
</body>
<script>
function createUnsubscribeURL(href) {
try {
const url = new URL(href)
const appID = url.searchParams.get("app_id")
const notificationID = url.searchParams.get("notification_id")
const unsubscribeToken = url.searchParams.get("token")
const language = url.searchParams.get("language")
const email = url.searchParams.get("email")
return {
unsubscribeURL: `https://api.onesignal.com/apps/${appID}/notifications/${notificationID}/unsubscribe?token=${unsubscribeToken}`,
meta: { language, email },
}
} catch (error) {
console.error(`Failed to parse '${href}'`, error.message)
}
}
const createClickHandler = (unsubscribeURL) => (event) => {
fetch(unsubscribeURL, {
method: "POST",
})
.then((res) => {
switch (res.status) {
case 202:
alert("Perfect! You've been unsubscribed.")
break
case 400:
alert("Something went wrong, get in touch to unsubscribe.")
break
case 500:
alert("Something went wrong, get in touch to unsubscribe.")
break
}
})
.catch((error) => console.error("Fatal error occurred", error.message))
}
document.addEventListener("DOMContentLoaded", () => {
const { unsubscribeURL, meta } = createUnsubscribeURL(
window.location.href
)
const buttonEl = document.getElementById("unsubscribe_button")
buttonEl.addEventListener("click", createClickHandler(unsubscribeURL))
const emailEl = document.getElementById("email")
emailEl.innerText = `${meta.email || ""}`
})
</script>
</html>