-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
192 lines (165 loc) · 5.48 KB
/
script.js
File metadata and controls
192 lines (165 loc) · 5.48 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
const socials = [
{
icon: 'fa-brands fa-facebook',
link: 'https://www.facebook.com/profile.php?id=61557401282119'
},
{
icon: 'fa-brands fa-instagram',
link: 'https://www.instagram.com/meadowcollection_/'
},
{
icon: 'fa-brands fa-tiktok',
link: 'https://www.tiktok.com/@thelonesomelilyx'
},
{
icon: 'fa-brands fa-pinterest',
link: 'https://ph.pinterest.com/meadowcollection_/'
},
{
icon: 'fa-solid fa-store',
link: 'https://tr.ee/nUch66zER4'
}
]
const navLinks = [
{
name: 'About',
link: 'about.html'
},
{
name: 'Services',
link: 'services.html'
},
{
name: 'Inquire',
link: 'inquire.html'
}
]
const products = [
{
name: "Crochet Card Holder",
price: 180.46,
image: "assets/buttons/22.png",
description: "Handcrafted crochet card holder made with care and detail."
},
{
name: "Strawberry Keychain",
price: 80.30,
image: "assets/buttons/23.png",
description: "Cute crochet strawberry keychain to brighten up your keys or bag."
},
{
name: "Purple Themed Wedding Invitation",
price: 45.00,
image: "assets/buttons/24.png",
description: "Elegant purple-themed wedding invitation for your special day."
},
{
name: "Cozy Crochet Blanket",
price: 45.00,
image: "assets/buttons/25.png",
description: "Soft and cozy handmade crochet blanket perfect for any season."
}
];
/**
* Renders social media icons based on the socials array.
* It creates a new anchor element for each social media object and appends it to the social-icons-container element.
*/
const renderSocialIcons = () => {
const socialIcons = document.getElementById('social-icons-container');
socials.forEach(social => {
const a = document.createElement('a');
a.href = social.link;
a.target = '_blank';
a.rel = 'noopener noreferrer';
a.innerHTML = `<i class="${social.icon}"></i>`;
socialIcons.appendChild(a);
})
}
/**
* Renders navigation links based on the navLinks array.
* It selects the links-container element from the nav element and appends an anchor element for each navigation link.
* If the link matches the current page, it adds the text-active class to the anchor element.
*/
const renderNavigations = () => {
const nav_container = document.getElementsByTagName('nav')[0].getElementsByClassName('links-container')[0];
navLinks.forEach( link => {
const a = document.createElement('a');
a.href = link.link;
a.innerHTML = link.name;
if(link.link === window.location.pathname.split('/').pop()) {
a.classList.add('text-active');
}
nav_container.appendChild(a);
})
}
/**
* Generates an array of random digits.
* @param {number} size - The size of the array to be generated.
* @param {number} min - The minimum value for the random digits.
* @param {number} max - The maximum value for the random digits.
* @return {number[]} An array of random digits.
*/
const randomDigits = (size, min, max) => {
const digits = [];
for (let i = 0; i < size; i++) {
digits.push(Math.floor(Math.random() * (max - min + 1)) + min);
}
return digits;
}
/**
* Renders 4 random featured products on the products section of the page.
* It takes the products array and selects 4 random indices from it.
* Then it creates an HTML string for each product and appends it to the products container element.
*/
const renderFeaturedProducts = () => {
const container = document.getElementById('products-container');
const rndmDigits = [...new Set(randomDigits(4, 0, products.length - 1))];
rndmDigits.forEach(index => {
const innerHTML = `
<div class="product-card">
<img src="${products[index].image}" alt="${products[index].name}" class="product-image">
<div class="product-description">
<h3 class="product-title">${products[index].name}</h3>
<p class="product-price">₱${products[index].price.toFixed(2)}</p>
</div>
</div>
`
container.innerHTML += innerHTML;
})
}
/**
* Validates a form by checking if each input field has a value.
* If a field does not have a value, it displays an error message and adds an input-error class to the field.
* If all fields have a value, it returns true.
* @returns {boolean} True if all fields have a value, false otherwise.
*/
const validateForm = () => {
let isValid = true;
const formGroups = document.querySelectorAll('.form-group');
formGroups.forEach(group => {
const input = group.querySelector('input, textarea, select');
const errorMessage = group.querySelector('.error-message');
if (input) {
const value = input.value.trim();
if (!value) {
errorMessage.style.display = 'block';
input.classList.add('input-error');
isValid = false;
} else {
errorMessage.style.display = 'none';
input.classList.remove('input-error');
}
}
});
return isValid;
}
/**
* Renders navigation links and social media icons on the page.
*/
const render = () => {
renderNavigations();
renderSocialIcons();
renderFeaturedProducts();
validateForm();
}
render();