-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
100 lines (83 loc) · 2.88 KB
/
Copy pathscript.js
File metadata and controls
100 lines (83 loc) · 2.88 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
$(document).ready(function () {
// Object to store assigned images for each email
const assignedImages = {};
// Function to fetch a random image
function fetchRandomImage() {
return new Promise((resolve) => {
const newSrc = `https://picsum.photos/800/600?random=${Date.now()}`;
const img = new Image();
img.onload = function () {
$("#currentImage").attr("src", newSrc);
resolve(newSrc);
};
img.src = newSrc;
});
}
// Function to display all assigned images
function displayAssignedImages() {
const $assignedImages = $("#assignedImages").empty();
Object.entries(assignedImages).forEach(([email, images]) => {
const $emailGroup = $("<div>").addClass("email-group mb-3");
const $emailHeader = $("<div>").addClass("d-flex justify-content-between align-items-center");
$emailHeader.append($("<h3>").text(email));
$emailHeader.append(
$("<button>")
.addClass("btn btn-danger btn-sm")
.text("Reset")
.on("click", function () {
delete assignedImages[email];
displayAssignedImages();
})
);
$emailGroup.append($emailHeader);
const $imageGrid = $("<div>").addClass("image-grid");
images.forEach((imageUrl) => {
$imageGrid.append($("<img>").attr("src", imageUrl).addClass("img-thumbnail"));
});
$emailGroup.append($imageGrid);
$assignedImages.append($emailGroup);
});
}
// Function to show error message with animation
function showError(message) {
const $errorMessage = $("#errorMessage");
$errorMessage.text(message).addClass("show");
$errorMessage[0].offsetHeight;
setTimeout(() => {
$errorMessage.removeClass("show");
}, 3000);
}
// Function to validate email address
function validateEmail(email) {
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
return emailRegex.test(String(email).toLowerCase());
}
// Event handler for form submission
$("#emailForm").on("submit", function (formEvent) {
formEvent.preventDefault();
const email = $("#emailInput").val().toLowerCase();
const currentImageUrl = $("#currentImage").attr("src");
if (!validateEmail(email)) {
showError("Please enter a valid email address.");
return;
}
if (currentImageUrl) {
if (!assignedImages[email]) {
assignedImages[email] = [];
}
if (assignedImages[email].includes(currentImageUrl)) {
showError("This image has already been assigned to this email address.");
} else {
assignedImages[email].push(currentImageUrl);
displayAssignedImages();
$("#emailInput").val("");
}
}
});
// Event handler for next button click
$("#nextButton").on("click", function () {
fetchRandomImage();
});
// Initial image fetch on page load
fetchRandomImage();
});