-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
143 lines (118 loc) · 4.59 KB
/
script.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
"use strict";
// Import images
import starIcon from "url:./images/icon-star.svg";
import illustrationIcon from "url:./images/illustration-thank-you.svg";
// Select parent element
const parentElement = document.querySelector(".main-contents");
let ratingNums = document.querySelector(".rating-numbers");
let eachRating = document.querySelectorAll(".rating-number");
// Initialize selectedNum to null
let selectedNum = null;
// Add event listener to parent element
parentElement.addEventListener("click", function (e) {
// Get submit and back arrow buttons
const submitBtn = e.target.closest(".btn-submit");
const backArrow = e.target.closest(".back-arrow");
// If neither button is clicked, exit function
if (!submitBtn && !backArrow) return;
// If back arrow is clicked, return to rating state
if (backArrow) {
ratingState();
ratingSelection();
}
// If submit button is clicked and a rating is selected, enter thanking state
if (submitBtn) {
e.preventDefault();
if (selectedNum === null) return;
thankingState();
selectedNum = null;
}
});
// Define thanking state function
const thankingState = function () {
const markup = `
<div class="thanking-state">
<span class="back-arrow">◄</span>
<img
class="thanking-state__illustration"
src="${illustrationIcon}"
alt="Thank you illustration"
/>
<p class="thanking-state__rating-output">You selected ${selectedNum} out of 5</p>
<h1 class="thanking-state__header">Thank you!</h1>
<p class="thanking-state__description">
We appreciate you taking the time to give a rating. If you ever need
more support, don’t hesitate to get in touch!
</p>
</div>
`;
// Fade out parent element and replace contents with thanking state markup
parentElement.style.opacity = "0";
setTimeout(() => {
parentElement.innerHTML = markup;
parentElement.style.opacity = "1";
ratingSelection();
}, 200);
};
// Define rating state function
const ratingState = function () {
// Remove the existing event listener for 'click' on ratingNums
if (ratingNums) {
ratingNums.removeEventListener("click", ratingSelection);
}
// Create markup for rating state
const markup = `
<div class="rating-state">
<div class="rating-state__icon-star">
<img src="${starIcon}" alt="Star icon" />
</div>
<h1 class="rating-state__header">How did we do?</h1>
<p class="rating-state__description">
Please let us know how we did with your support request. All
feedback is appreciated to help us improve our offering!
</p>
<form action="">
<ul class="rating-numbers">
<li class="rating-number" data-ratingValue="1">1</li>
<li class="rating-number" data-ratingValue="2">2</li>
<li class="rating-number" data-ratingValue="3">3</li>
<li class="rating-number" data-ratingValue="4">4</li>
<li class="rating-number" data-ratingValue="5">5</li>
</ul>
<button class="btn-submit" type="submit">Submit</button>
</form>
</div>
`;
// Fade out parent element and update inner HTML
parentElement.style.opacity = "0";
setTimeout(() => {
parentElement.innerHTML = markup;
parentElement.style.opacity = "1";
ratingNums = document.querySelector(".rating-numbers");
ratingSelection();
eachRating = document.querySelectorAll(".rating-number");
}, 200);
};
/**
* Updates the rating selection when a user clicks on a rating number.
* Sets the selected number to the data-ratingvalue attribute of the clicked element
*/
const ratingSelection = function () {
// If ratingNums or eachRating is null, return early
if (!ratingNums || !eachRating) return;
// Add a 'click' event listener to ratingNums
ratingNums.addEventListener("click", function (e) {
// Find the closest '.rating-number' element to the clicked target
const el = e.target.closest(".rating-number");
// If no element was found, return early
if (!el) return;
// Remove the 'active' class from all rating numbers
eachRating.forEach((entry) => entry.classList.remove("active"));
// Add the 'active' class to the clicked rating number
el.classList.add("active");
// Set the selected number to the value of the data-ratingvalue attribute of the clicked rating number
selectedNum = Number(el.dataset.ratingvalue);
});
};
// Call the function to set up the initial rating selection
ratingSelection();