-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
130 lines (110 loc) · 4.1 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
const googleScriptURL = 'https://script.google.com/macros/s/AKfycbwvzMHDUFibfbISzNRWRNQRkqmhC2NVNOPua_idgNvzY6_cHdfGq0jOMGtLmj5cWmHgGg/exec';
let selectedRating = 0;
// Load movies when page loads
async function loadMovies() {
try {
const response = await fetch(googleScriptURL + '?action=getMovies');
if (!response.ok) throw new Error('Network response was not ok');
const movies = await response.json();
const movieSelect = document.getElementById("movieSelect");
movieSelect.innerHTML = '<option value="">-- Select a movie --</option>' +
movies.map(movie => `<option value="${movie.id}">${movie.title} (${movie.year})</option>`).join('');
} catch (error) {
console.error("Error loading movies:", error);
alert("Failed to load movies. Please refresh the page.");
}
}
// Initialize the page
document.addEventListener('DOMContentLoaded', () => {
loadMovies();
initializeSnowflakeRating();
});
// Snowflake rating system
function initializeSnowflakeRating() {
const snowflakes = document.querySelectorAll("#snowflakeRating span");
snowflakes.forEach(snowflake => {
snowflake.addEventListener('mouseover', () => {
const rating = parseInt(snowflake.getAttribute('data-value'));
updateSnowflakeDisplay(rating, true);
});
snowflake.addEventListener('mouseout', () => {
updateSnowflakeDisplay(selectedRating, false);
});
snowflake.addEventListener('click', () => {
selectedRating = parseInt(snowflake.getAttribute('data-value'));
updateSnowflakeDisplay(selectedRating, false);
});
});
}
function updateSnowflakeDisplay(rating, isHover) {
const snowflakes = document.querySelectorAll("#snowflakeRating span");
snowflakes.forEach((snowflake, index) => {
const value = index + 1;
if (value <= rating) {
snowflake.style.color = isHover ? '#ffa500' : '#ffd700'; // Orange for hover, Gold for selected
snowflake.style.transform = 'scale(1.2)';
snowflake.classList.add('selected');
} else {
snowflake.style.color = '#ddd';
snowflake.style.transform = 'scale(1)';
snowflake.classList.remove('selected');
}
});
}
async function submitRating() {
const movieSelect = document.getElementById("movieSelect");
const review = document.getElementById("review");
// Validation
if (!movieSelect.value) {
alert('Please select a movie');
return;
}
if (selectedRating === 0) {
alert('Please select a rating');
return;
}
if (!review.value.trim()) {
alert('Please write a review');
return;
}
const formData = {
MovieID: movieSelect.value,
Title: movieSelect.selectedOptions[0].text.split(' (')[0],
Rating: selectedRating,
Comment: review.value.trim()
};
try {
const response = await fetch(googleScriptURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(formData)
});
if (!response.ok) throw new Error('Network response was not ok');
const result = await response.json();
if (result.result === 'success') {
showThankYouBox();
clearForm();
} else {
throw new Error(result.error || 'Submission failed');
}
} catch (error) {
console.error('Error:', error);
alert('Failed to submit rating. Please try again.');
}
}
function showThankYouBox() {
document.getElementById("thankYouBox").style.display = "block";
}
function closeThankYouBox() {
document.getElementById("thankYouBox").style.display = "none";
}
function clearForm() {
document.getElementById("movieSelect").value = "";
document.getElementById("review").value = "";
selectedRating = 0;
updateSnowflakeDisplay(0, false);
}
// Initialize loading of movies when page is ready
document.addEventListener('DOMContentLoaded', loadMovies);