-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadopt.js
More file actions
274 lines (239 loc) · 8.53 KB
/
adopt.js
File metadata and controls
274 lines (239 loc) · 8.53 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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
document.addEventListener('DOMContentLoaded', () => {
Init();
AddListenerstoButtons();
UpdateCartButtons();
Highlight(); // Call highlight after DOM is fully loaded
const petId = localStorage.getItem('highlightPetId');
if (petId) {
console.log('Found pet ID to highlight:', petId);
const card = document.getElementById(petId);
if (card) {
console.log('Found card with ID:', petId);
card.classList.add('highlighted');
card.scrollIntoView({ behavior: 'smooth', block: 'center' });
setTimeout(() => {
card.classList.add('fade-out');
}, 3000);
setTimeout(() => {
card.classList.remove('highlighted', 'fade-out');
}, 4000);
} else {
console.log('Could not find card with ID:', petId);
}
localStorage.removeItem('highlightPetId');
}
});
const pets = [
{
name: 'Karman',
age: 7,
gender: 'Male',
type: 'German Shepherd',
location: 'Pune',
owner: 'Krishanth',
image: 'images/karman.jpg',
cart: false,
},
{
name: 'Busky',
age: 3,
gender: 'Female',
type: 'Pug',
location: 'Delhi',
owner: 'Mithuraa',
image: 'images/busky.jpg',
cart: false,
},
{
name: 'Max',
age: 3,
gender: 'Male',
type: 'Maine Coon Cat',
location: 'Andheri West, Mumbai',
owner: 'Karthik R.',
image: 'images/cat.jpg',
cart: false,
},
{
name: 'Rico',
age: 4,
gender: 'Male',
type: 'Labrador Retriever',
location: 'India Gate, New Delhi',
owner: 'Revathi N.',
image: 'https://cdn.pixabay.com/photo/2023/08/18/15/02/dog-8198719_1280.jpg',
cart: false,
},
{
name: 'Jesse',
age: 3,
gender: 'Male',
type: 'Golden Retriever',
location: 'Campal, Panjim, Goa',
owner: 'Anirudh Babu',
image: 'https://cdn.pixabay.com/photo/2016/12/13/05/15/puppy-1903313_1280.jpg',
cart: false,
},
{
name: 'McCabe',
age: 5,
gender: 'Male',
type: 'English Spaniel',
location: 'Lalbagh Garden, Bangalore',
owner: 'Sowmya Rajan',
image: 'https://cdn.pixabay.com/photo/2019/08/07/14/11/dog-4390885_1280.jpg',
cart: false,
},
{
name: 'Luna',
age: 1,
gender: 'Female',
type: 'Beagle Mix',
location: 'HiTech City, Hyderabad',
owner: 'Harsha V.',
image: 'https://upload.wikimedia.org/wikipedia/commons/thumb/4/43/Cute_dog.jpg/2560px-Cute_dog.jpg',
cart: false,
},
{
name: 'Min',
age: 2,
gender: 'Male',
type: 'Golden Retriever',
location: 'Charminar Area, Hyderabad',
owner: 'Meghana S.',
image: 'https://cdn.pixabay.com/photo/2017/09/25/13/12/puppy-2785074_1280.jpg',
cart: false,
},
{
name: 'Bella',
age: 8,
gender: 'Female',
type: 'Pembroke Welsh Corgi',
location: 'Egmore, Chennai',
owner: 'Arvind M.',
image: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTROLLARPFP-cJesIVqS44x8QONuh8rlMQjjQ&s',
cart: false,
},
];
// Init();
// AddListenerstoButtons();
// UpdateCartButtons();
// Highlight();
function Init() {
const container = document.getElementById('container');
pets.forEach(pet => {
const card = document.createElement('div');
card.className = 'card';
card.id = pet.name.toLowerCase();
card.innerHTML = `
<img src="${pet.image}" alt="${pet.name}" />
<div class="card-content">
<h2>${pet.name} – ${pet.age} yrs</h2>
<div class="pet-meta">
<p>${pet.type} <span style="font-weight:bold;">•</span> ${pet.gender}</p>
<p>${pet.location}</p>
<p class="owner"><span style="font-weight:bold;">Owner</span>: ${pet.owner}</p>
</div>
<a href="larger-card.html?${new URLSearchParams(pet)}" target="_blank" class="details-link">See more details ↗</a>
<button class="cart-button" data-name=${pet.name}>Add To Cart</button>
<button class="buy-button" data-name=${pet.name}>Buy Now</button>
</div>
`;
container.appendChild(card);
});
}
function AddListenerstoButtons() {
// buy-button
let buyButtons = document.getElementsByClassName('buy-button');
[...buyButtons].forEach(element => {
element.addEventListener('click', function () {
BuyItem(element.dataset.name);
});
});
// add to cart button
let cartButtons = document.getElementsByClassName('cart-button');
[...cartButtons].forEach(element => {
element.addEventListener('click', function () {
AddItemToCart(element.dataset.name, this);
});
});
}
// function to happen when buy-now is clicked
function BuyItem(name) {
let pet = pets.find(pet => pet.name === name);
let BuyNowpets = [];
BuyNowpets.push(pet);
// store the pet in storage
localStorage.setItem('buyNow', JSON.stringify(BuyNowpets));
localStorage.setItem('buyNowType', 'pet');
// redirect to checkout page
window.location.href = 'cart.html';
}
// function to add a pet to cart
function AddItemToCart(name, btn) {
let pet = pets.find(pet => pet.name === name);
pet.cart = !pet.cart;
if (pet.cart) {
btn.innerText = 'Remove from Cart';
} else {
btn.innerText = 'Add To Cart';
}
console.log(pet.cart);
// find all pets in cart
let petsInCart = pets.filter(pet => pet.cart);
localStorage.setItem('petsInCart', JSON.stringify(petsInCart));
}
function UpdateCartButtons() {
const storedCart = JSON.parse(localStorage.getItem('petsInCart')) || [];
pets.forEach(pet => {
pet.cart = storedCart.some(item => item.name === pet.name);
});
let cartButtons = document.getElementsByClassName('cart-button');
[...cartButtons].forEach(element => {
let pet = pets.find(p => element.dataset.name === p.name);
if (pet.cart) {
element.innerHTML = 'Remove from Cart';
}
});
}
function Highlight() {
// ONLY check localStorage, not sessionStorage
const nameToHighlight = localStorage.getItem('highlightPet');
const idToHighlight = localStorage.getItem('highlightPetId');
console.log('Highlighting pet:', nameToHighlight, 'or ID:', idToHighlight);
document.querySelectorAll('.card').forEach(card => {
// Get the pet name from the h2 tag
const petNameTag = card.querySelector('h2');
if (petNameTag) {
const petName = petNameTag.textContent.split(' - ')[0].trim();
// Set ID attribute on card based on pet name if not already set
if (!card.id && petName) {
card.id = petName.toLowerCase();
}
console.log('Checking card:', petName, 'with ID:', card.id);
// Check if this card matches either the name or id criteria (case insensitive)
if (
(nameToHighlight && petName.toLowerCase() === nameToHighlight.toLowerCase()) ||
(idToHighlight && card.id.toLowerCase() === idToHighlight.toLowerCase())
) {
console.log('Found match! Highlighting:', petName);
// Scroll to the card
card.scrollIntoView({ behavior: 'smooth', block: 'center' });
// Add highlight class
card.classList.add('highlighted');
// Set a timeout to fade out highlight
setTimeout(() => {
card.classList.add('fade-out'); // Use fade-out here since it's defined in your CSS
card.classList.remove('highlighted');
// Clean up after animation
setTimeout(() => {
card.classList.remove('fade-out');
}, 1000);
}, 3000);
// Clear both storage items
localStorage.removeItem('highlightPet');
localStorage.removeItem('highlightPetId');
}
}
});
}