forked from taranjeetsingh9/PetConnectBackend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpet-browser.html
More file actions
424 lines (366 loc) · 14.5 KB
/
pet-browser.html
File metadata and controls
424 lines (366 loc) · 14.5 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Browse Pets - Find Your Perfect Companion</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-50 min-h-screen">
<!-- Navigation -->
<nav class="bg-white shadow-sm border-b">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div class="flex justify-between h-16">
<div class="flex items-center">
<h1 class="text-xl font-bold text-indigo-700">PetConnect</h1>
</div>
<div class="flex items-center space-x-4">
<button onclick="window.location.href='user-profile.html'"
class="text-gray-700 hover:text-indigo-600">
My Profile
</button>
<button onclick="window.location.href='user-listings.html'"
class="text-gray-700 hover:text-indigo-600">
My Listings
</button>
<button id="logoutBtn"
class="bg-red-500 text-white px-4 py-2 rounded-lg hover:bg-red-600">
Logout
</button>
</div>
</div>
</div>
</nav>
<div class="max-w-7xl mx-auto py-6 px-4 sm:px-6 lg:px-8">
<!-- Header -->
<div class="text-center mb-8">
<h1 class="text-4xl font-bold text-gray-900 mb-4">Find Your Perfect Companion</h1>
<p class="text-lg text-gray-600">Browse pets available for adoption or fostering</p>
</div>
<!-- Filters -->
<div class="bg-white rounded-lg shadow-sm border p-6 mb-8">
<div class="flex flex-wrap gap-4 items-center">
<!-- Type Filter -->
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">I want to:</label>
<select id="typeFilter" class="border rounded-lg px-4 py-2 min-w-[200px]">
<option value="all">See All Pets</option>
<option value="adoption">Adopt a Pet</option>
<option value="fostering">Foster a Pet</option>
</select>
</div>
<!-- Search -->
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">Search:</label>
<input type="text" id="searchFilter" placeholder="Name, breed, description..."
class="border rounded-lg px-4 py-2 min-w-[300px]">
</div>
<!-- Location -->
<div>
<label class="block text-sm font-medium text-gray-700 mb-1">Location:</label>
<input type="text" id="locationFilter" placeholder="City, state..."
class="border rounded-lg px-4 py-2 min-w-[200px]">
</div>
<!-- Buttons -->
<div class="flex items-end gap-2">
<button id="applyFilters"
class="bg-indigo-600 text-white px-6 py-2 rounded-lg hover:bg-indigo-700">
Apply Filters
</button>
<button id="clearFilters"
class="bg-gray-500 text-white px-6 py-2 rounded-lg hover:bg-gray-600">
Clear
</button>
</div>
</div>
</div>
<!-- Results Count -->
<div id="resultsInfo" class="mb-4 text-gray-600">
Loading pets...
</div>
<!-- Pets Grid -->
<div id="petsContainer" class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-6 mb-8">
<!-- Pets will be loaded here -->
</div>
<!-- No Results -->
<div id="noResults" class="text-center py-12 hidden">
<p class="text-gray-500 text-lg">No pets found matching your criteria.</p>
<button id="resetSearch" class="mt-4 text-indigo-600 hover:text-indigo-800">
Reset filters and try again
</button>
</div>
<!-- Loading -->
<div id="loading" class="text-center py-8">
<p class="text-gray-500">Loading pets...</p>
</div>
</div>
<!-- Result Message -->
<div id="resultMessage" class="fixed top-4 right-4 p-4 rounded-lg shadow-lg hidden"></div>
<script>
const token = localStorage.getItem('token');
if (!token) {
window.location.href = 'index.html';
}
let currentFilters = {
type: 'all',
search: '',
location: ''
};
// DOM Elements
const petsContainer = document.getElementById('petsContainer');
const resultsInfo = document.getElementById('resultsInfo');
const noResults = document.getElementById('noResults');
const loading = document.getElementById('loading');
// Load pets on page load
document.addEventListener('DOMContentLoaded', () => {
loadPets();
setupEventListeners();
});
function setupEventListeners() {
// Filter changes
document.getElementById('applyFilters').addEventListener('click', applyFilters);
document.getElementById('clearFilters').addEventListener('click', clearFilters);
document.getElementById('resetSearch').addEventListener('click', clearFilters);
// Enter key in search
document.getElementById('searchFilter').addEventListener('keypress', (e) => {
if (e.key === 'Enter') applyFilters();
});
}
function applyFilters() {
currentFilters = {
type: document.getElementById('typeFilter').value,
search: document.getElementById('searchFilter').value,
location: document.getElementById('locationFilter').value
};
loadPets();
}
function clearFilters() {
document.getElementById('typeFilter').value = 'all';
document.getElementById('searchFilter').value = '';
document.getElementById('locationFilter').value = '';
currentFilters = { type: 'all', search: '', location: '' };
loadPets();
}
async function loadPets() {
showLoading();
try {
// Build query string
const params = new URLSearchParams();
if (currentFilters.type !== 'all') params.append('type', currentFilters.type);
if (currentFilters.search) params.append('search', currentFilters.search);
if (currentFilters.location) params.append('location', currentFilters.location);
const response = await fetch(`http://localhost:5001/api/pets/browse/all?${params}`, {
headers: {
'Content-Type': 'application/json',
'x-auth-token': token
}
});
const result = await response.json();
if (response.ok) {
displayPets(result.pets, result.total);
} else {
showError(result.msg || 'Failed to load pets');
}
} catch (error) {
console.error('Error loading pets:', error);
showError('Network error. Please try again.');
}
}
function displayPets(pets, total) {
hideLoading();
if (pets.length === 0) {
petsContainer.innerHTML = '';
noResults.classList.remove('hidden');
resultsInfo.textContent = 'No pets found';
return;
}
noResults.classList.add('hidden');
resultsInfo.textContent = `Found ${total} pet${total !== 1 ? 's' : ''}`;
petsContainer.innerHTML = pets.map(pet => `
<div class="bg-white rounded-xl shadow-md overflow-hidden hover:shadow-lg transition-shadow">
${pet.images && pet.images.length > 0 && pet.images[0].url ?
`<img src="${pet.images[0].url}"
class="w-full h-48 object-cover cursor-pointer"
onclick="viewPetDetail('${pet._id}')"
onerror="this.src='https://via.placeholder.com/300x200?text=No+Image'">` :
`<div class="w-full h-48 bg-gray-200 flex items-center justify-center text-gray-500 cursor-pointer"
onclick="viewPetDetail('${pet._id}')">
No Image
</div>`
}
<div class="p-4">
<!-- Badges -->
<div class="flex justify-between items-start mb-2">
<span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium
${pet.listingType === 'shelter' ? 'bg-blue-100 text-blue-800' : 'bg-green-100 text-green-800'}">
${pet.listingType === 'shelter' ? '🏠 Shelter Pet' : '👤 Personal Listing'}
</span>
<span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium bg-gray-100 text-gray-800">
${getStatusDisplay(pet.status)}
</span>
</div>
<!-- Pet Info -->
<h3 class="text-lg font-semibold text-gray-900 mb-1">${pet.name}</h3>
<p class="text-sm text-gray-600 mb-1">${pet.breed || 'Mixed Breed'} • ${pet.age || 'N/A'} years</p>
<p class="text-sm text-gray-600 mb-2">📍 ${pet.location || 'Location not specified'}</p>
<p class="text-sm text-gray-700 mb-4 line-clamp-2">${pet.description || 'No description available.'}</p>
<!-- Action Buttons -->
<div class="flex gap-2">
${pet.listingType === 'shelter' ? `
<button onclick="adoptPet('${pet._id}')"
class="flex-1 bg-indigo-600 text-white py-2 px-3 rounded-lg text-sm hover:bg-indigo-700">
Adopt
</button>
<button onclick="fosterPet('${pet._id}')"
class="flex-1 bg-green-600 text-white py-2 px-3 rounded-lg text-sm hover:bg-green-700">
Foster
</button>
` : `
<button onclick="fosterPet('${pet._id}')"
class="flex-1 bg-green-600 text-white py-2 px-3 rounded-lg text-sm hover:bg-green-700">
Offer to Foster
</button>
`}
<button onclick="addToFavorites('${pet._id}')"
class="bg-gray-200 text-gray-700 p-2 rounded-lg hover:bg-gray-300">
♡
</button>
</div>
</div>
</div>
`).join('');
}
function getStatusDisplay(status) {
const statusMap = {
'Available': 'Available',
'available_fostering': 'Available for Foster',
'Pending Adoption': 'Pending Adoption',
'Pending Foster': 'Pending Foster',
'pending_foster': 'Pending Foster',
'Adopted': 'Adopted',
'Fostered': 'Fostered'
};
return statusMap[status] || status;
}
// Action Functions
async function adoptPet(petId) {
if (!confirm('Are you sure you want to adopt this pet?')) return;
try {
const response = await fetch(`http://localhost:5001/api/pets/${petId}/adopt`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-auth-token': token
}
});
const result = await response.json();
if (response.ok) {
showMessage('Adoption request submitted successfully!', 'success');
loadPets(); // Refresh the list
} else {
showMessage(result.msg || 'Failed to submit adoption request', 'error');
}
} catch (error) {
console.error('Adoption error:', error);
showMessage('Network error. Please try again.', 'error');
}
}
// async function fosterPet(petId) {
// if (!confirm('Are you sure you want to foster this pet?')) return;
// try {
// const response = await fetch(`http://localhost:5001/api/pets/${petId}/foster`, {
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json',
// 'x-auth-token': token
// }
// });
// const result = await response.json();
// if (response.ok) {
// showMessage('Foster request submitted successfully!', 'success');
// loadPets(); // Refresh the list
// } else {
// showMessage(result.msg || 'Failed to submit foster request', 'error');
// }
// } catch (error) {
// console.error('Foster error:', error);
// showMessage('Network error. Please try again.', 'error');
// }
// }
// Replace your current fosterPet function with this:
async function fosterPet(petId) {
const message = prompt("Why would you like to foster this pet? (Optional)");
try {
const response = await fetch(`http://localhost:5001/api/pets/${petId}/foster`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-auth-token': token
},
body: JSON.stringify({ message })
});
const result = await response.json();
if (response.ok) {
showMessage(result.msg || 'Foster request submitted successfully!', 'success');
} else {
showMessage(result.msg || 'Failed to submit foster request', 'error');
}
} catch (error) {
console.error('Foster error:', error);
showMessage('Network error. Please try again.', 'error');
}
}
async function addToFavorites(petId) {
try {
const response = await fetch(`http://localhost:5001/api/users/favorites/${petId}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-auth-token': token
}
});
const result = await response.json();
if (response.ok) {
showMessage('Added to favorites!', 'success');
} else {
showMessage(result.msg || 'Failed to add to favorites', 'error');
}
} catch (error) {
console.error('Favorite error:', error);
showMessage('Network error. Please try again.', 'error');
}
}
function viewPetDetail(petId) {
// You can implement a detailed pet view modal or page
alert(`Viewing details for pet ${petId}. Implement detailed view here.`);
}
// UI Helper Functions
function showLoading() {
loading.classList.remove('hidden');
petsContainer.innerHTML = '';
}
function hideLoading() {
loading.classList.add('hidden');
}
function showMessage(message, type) {
const messageDiv = document.getElementById('resultMessage');
messageDiv.textContent = message;
messageDiv.className = `fixed top-4 right-4 p-4 rounded-lg shadow-lg ${
type === 'success' ? 'bg-green-500 text-white' : 'bg-red-500 text-white'
}`;
messageDiv.classList.remove('hidden');
setTimeout(() => {
messageDiv.classList.add('hidden');
}, 4000);
}
function showError(message) {
showMessage(message, 'error');
}
// Logout
document.getElementById('logoutBtn').addEventListener('click', () => {
localStorage.removeItem('token');
window.location.href = 'index.html';
});
</script>
</body>
</html>