-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
735 lines (652 loc) · 31.2 KB
/
Copy pathindex.html
File metadata and controls
735 lines (652 loc) · 31.2 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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Add/Edit Friend - FriendForge</title>
<link rel="icon" href="icon.png" type="image/png">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
<style>
/* Universal Box Sizing */
*, *::before, *::after {
box-sizing: border-box;
}
/* General Body Styling - Modern Dark Mode */
body {
font-family: 'Segoe UI', 'Roboto', 'Helvetica Neue', Arial, sans-serif; /* Modern, clean font stack */
background-color: #121212; /* Deeper, more sophisticated dark background */
color: #E0E0E0; /* Soft white for primary text */
margin: 0;
padding: 20px 15px; /* More responsive padding for smaller screens */
display: flex;
flex-direction: column;
align-items: center;
min-height: 100vh;
box-sizing: border-box;
line-height: 1.6;
text-rendering: optimizeLegibility; /* Improve text rendering */
}
/* Container for the entire app - Modern Dark Mode */
.container {
width: 100%; /* Default to full width minus padding */
max-width: 800px; /* Limit max width for large screens */
background-color: #1F1F1F; /* Darker charcoal for container */
border-radius: 15px; /* More rounded corners */
box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5); /* More pronounced, softer shadow */
padding: 25px 20px; /* Adjusted padding for responsiveness */
box-sizing: border-box;
margin-bottom: 30px;
border: 1px solid rgba(60, 60, 60, 0.8); /* Subtle border for definition */
transition: all 0.3s ease-in-out; /* Smooth transition for hover effects */
}
.container:hover {
box-shadow: 0 15px 40px rgba(0, 0, 0, 0.7); /* Deeper shadow on hover */
transform: translateY(-3px); /* Slight lift on hover */
}
/* Navigation Styling - Modern Dark Mode */
nav {
display: flex;
flex-direction: column; /* Stack navigation links in portrait by default */
justify-content: center;
gap: 15px; /* Adjust gap for stacking */
margin-bottom: 30px; /* More separation from content */
padding-bottom: 15px;
border-bottom: 1px solid rgba(70, 70, 70, 0.6);
}
nav a {
text-decoration: none;
color: #81D4FA;
padding: 10px 20px; /* Smaller padding for stacked links */
border-radius: 8px;
transition: background-color 0.3s ease, color 0.3s ease, transform 0.2s ease;
font-weight: 600;
font-size: 1em;
display: flex;
align-items: center;
justify-content: center; /* Center text and icon in stacked mode */
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
white-space: nowrap; /* Prevent text wrapping */
}
nav a i {
margin-right: 8px; /* Slightly less space for icons */
font-size: 1.1em;
}
nav a:hover {
background-color: #00838F;
color: #FFFFFF;
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.4);
}
nav a.active {
background-color: #00ACC1;
color: white;
box-shadow: 0 3px 10px rgba(0, 0, 0, 0.3);
}
/* Main Heading - Modern Dark Mode */
.main-title-link {
text-decoration: none;
color: inherit;
display: block;
text-align: center;
}
h1 {
text-align: center;
color: #B3E5FC;
margin-bottom: 25px; /* Reduced margin */
font-size: 2.5em; /* Slightly smaller for mobile first */
font-weight: 700;
letter-spacing: 0.05em;
text-shadow: 0 2px 8px rgba(0, 172, 193, 0.4);
border-bottom: 2px solid #00ACC1;
padding-bottom: 15px; /* Reduced padding */
transition: all 0.3s ease-in-out;
}
h1:hover {
color: #E0F7FA;
text-shadow: 0 3px 12px rgba(0, 172, 193, 0.7);
transform: translateY(-2px);
}
/* Section Headings - Modern Dark Mode */
.input-section h2, .display-section h2 {
color: #81D4FA;
margin-top: 0;
margin-bottom: 20px; /* Reduced margin */
border-bottom: 1px solid #00838F;
padding-bottom: 10px; /* Reduced padding */
font-size: 1.8em; /* Adjusted for smaller screens */
font-weight: 600;
text-align: center;
}
/* Form Styling */
form {
display: flex;
flex-direction: column;
gap: 15px; /* Reduced gap */
padding: 20px; /* Reduced padding */
border: 1px solid rgba(0, 172, 193, 0.4);
border-radius: 12px;
background-color: #282828;
box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.4); /* Reduced shadow */
}
.form-group {
display: flex;
flex-direction: column;
}
.form-group label {
margin-bottom: 5px; /* Reduced margin */
font-weight: bold;
color: #B3E5FC;
font-size: 1em; /* Adjusted font size */
}
.form-group input[type="text"],
.form-group input[type="date"],
.form-group textarea,
.form-group select { /* Added select input type */
padding: 10px 12px; /* Reduced padding */
border: 1px solid #444;
border-radius: 8px;
font-size: 1em; /* Adjusted font size */
background-color: #333;
color: #E0E0E0;
resize: vertical;
min-height: 45px; /* Adjusted min height */
box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.4); /* Reduced shadow */
transition: border-color 0.3s ease, box-shadow 0.3s ease;
}
.form-group select {
/* Custom arrow for select dropdowns */
appearance: none;
-webkit-appearance: none;
-moz-appearance: none;
background-image: url('data:image/svg+xml;utf8,<svg fill="%23E0E0E0" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><path d="M7 10l5 5 5-5z"/><path d="M0 0h24v24H0z" fill="none"/></svg>');
background-repeat: no-repeat;
background-position: right 10px center;
background-size: 24px;
cursor: pointer;
}
.form-group input[type="text"]:focus,
.form-group input[type="date"]:focus,
.form-group textarea:focus,
.form-group select:focus { /* Added select input type */
outline: none;
border-color: #00ACC1;
box-shadow: 0 0 0 3px rgba(0, 172, 193, 0.3), inset 0 1px 2px rgba(0, 0, 0, 0.6);
}
/* Styling for range inputs (sliders) */
.form-group input[type="range"] {
-webkit-appearance: none; /* Remove default styling */
width: 100%;
height: 10px; /* Adjust height of the track */
background: #444; /* Default track color */
border-radius: 5px;
outline: none;
margin: 10px 0; /* Space above/below */
box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.4);
transition: background 0.3s ease;
}
/* Thumb (handle) styling for Webkit browsers (Chrome, Safari) */
.form-group input[type="range"]::-webkit-slider-thumb {
-webkit-appearance: none;
appearance: none;
width: 25px; /* Size of the thumb */
height: 25px; /* Size of the thumb */
border-radius: 50%; /* Make it circular */
background: #00ACC1; /* Thumb color */
cursor: pointer;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.4);
margin-top: -8px; /* Center thumb vertically on track */
transition: background 0.3s ease, transform 0.2s ease, box-shadow 0.3s ease;
}
/* Thumb (handle) styling for Firefox */
.form-group input[type="range"]::-moz-range-thumb {
width: 25px;
height: 25px;
border-radius: 50%;
background: #00ACC1;
cursor: pointer;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.4);
border: none; /* Remove default border */
transition: background 0.3s ease, transform 0.2s ease, box-shadow 0.3s ease;
}
/* Focus styles for the slider thumb */
.form-group input[type="range"]:focus::-webkit-slider-thumb {
box-shadow: 0 0 0 4px rgba(0, 172, 193, 0.4), 0 2px 8px rgba(0, 0, 0, 0.6);
transform: scale(1.1);
}
.form-group input[type="range"]:focus::-moz-range-thumb {
box-shadow: 0 0 0 4px rgba(0, 172, 193, 0.4), 0 2px 8px rgba(0, 0, 0, 0.6);
transform: scale(1.1);
}
/* Container for slider and its value */
.slider-group {
display: flex;
align-items: center;
gap: 10px; /* Space between slider and value display */
}
.slider-group input[type="range"] {
flex-grow: 1; /* Make slider take up available space */
margin: 0; /* Reset margin from general range styling */
}
.slider-value {
font-size: 1.1em;
font-weight: bold;
color: #B3E5FC;
min-width: 30px; /* Ensure space for two digits + /10 */
text-align: right;
}
/* Button Styling - Modern Dynamic */
button {
padding: 10px 20px; /* Reduced padding */
background-color: #00ACC1;
color: white;
border: none;
border-radius: 8px;
cursor: pointer;
font-size: 1.05em; /* Adjusted font size */
font-weight: bold;
transition: background-color 0.3s ease, transform 0.2s ease, box-shadow 0.3s ease;
box-shadow: 0 3px 10px rgba(0, 172, 193, 0.4); /* Reduced shadow */
}
button:hover {
background-color: #00838F;
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.6);
}
button:active {
transform: translateY(0);
box-shadow: 0 2px 8px rgba(0, 172, 193, 0.3);
}
#clearForm {
background-color: #FFB300;
box-shadow: 0 3px 10px rgba(255, 179, 0, 0.4);
}
#clearForm:hover {
background-color: #FB8C00;
box-shadow: 0 5px 15px rgba(255, 179, 0, 0.6);
}
#deleteFriendBtn {
background-color: #E53935;
margin-top: 20px; /* Reduced margin */
box-shadow: 0 3px 10px rgba(229, 57, 53, 0.4);
display: none; /* Hidden by default, shown when editing */
}
#deleteFriendBtn:hover {
background-color: #C62828;
box-shadow: 0 5px 15px rgba(229, 57, 53, 0.6);
}
/* Form Actions Group */
.form-actions {
display: flex;
flex-direction: column; /* Stack buttons on small screens */
gap: 15px; /* Space between stacked buttons */
margin-top: 20px;
}
/* --- Media Queries for Landscape / Larger Screens --- */
/* For screens wider than 576px (e.g., larger phones in landscape, small tablets) */
@media (min-width: 576px) {
.container {
padding: 30px 30px; /* More generous padding */
}
nav {
flex-direction: row; /* Navigation links appear side-by-side */
gap: 20px; /* More space between links */
}
nav a {
padding: 10px 22px; /* Slightly larger clickable area */
font-size: 1.05em;
}
h1 {
font-size: 2.8em; /* Slightly larger heading */
margin-bottom: 30px;
padding-bottom: 18px;
}
.input-section h2 {
font-size: 2em; /* Larger section headings */
margin-bottom: 22px;
padding-bottom: 12px;
}
.form-actions {
flex-direction: row; /* Buttons side-by-side */
justify-content: flex-end; /* Align to the right */
gap: 20px; /* Larger space between buttons */
}
button {
flex-grow: 0; /* Don't grow to fill space */
width: auto; /* Auto width based on content */
min-width: 150px; /* Ensure a minimum width */
}
#saveFriendBtn, #clearForm {
order: 2; /* Save and Clear on the right */
}
#deleteFriendBtn {
order: 1; /* Delete on the left */
}
}
/* For screens wider than 768px (e.g., tablets in landscape, desktops) */
@media (min-width: 768px) {
body {
padding: 40px 20px; /* Restore original body padding */
}
.container {
padding: 35px 45px; /* Restore original container padding */
}
nav {
gap: 25px; /* Restore original nav gap */
margin-bottom: 40px;
padding-bottom: 20px;
}
nav a {
padding: 12px 25px; /* Restore original nav link padding */
font-size: 1.1em;
justify-content: center; /* Ensure centered still for better alignment */
}
h1 {
font-size: 3em; /* Restore original heading size */
margin-bottom: 35px;
padding-bottom: 18px;
}
.input-section h2 {
font-size: 2.2em; /* Restore original section heading size */
margin-bottom: 25px;
padding-bottom: 12px;
}
form {
gap: 20px; /* Restore original form gap */
padding: 25px; /* Restore original form padding */
}
.form-group label {
margin-bottom: 8px; /* Restore original label margin */
font-size: 1.05em;
}
.form-group input[type="text"],
.form-group input[type="date"],
.form-group textarea,
.form-group select { /* Added select input type */
padding: 12px 15px; /* Restore original input padding */
font-size: 1.1em;
min-height: 50px; /* Restore original min height */
}
button {
padding: 12px 25px; /* Restore original button padding */
font-size: 1.15em;
}
#deleteFriendBtn {
margin-top: 0; /* Align with save button group */
}
.form-actions {
margin-top: 30px;
}
}
/* Specific adjustment for navigation in very wide screens to prevent stretching */
@media (min-width: 992px) {
nav {
max-width: 800px; /* Limit nav width for aesthetic */
width: 100%;
}
}
</style>
</head>
<body>
<div class="container">
<a href="about.html" class="main-title-link">
<h1>FriendForge</h1>
</a>
<nav>
<a href="index.html" class="active"><i class="fas fa-user-plus"></i> Add Friend</a>
<a href="list.html"><i class="fas fa-users"></i> View Friends List</a>
</nav>
<div class="input-section">
<h2 id="formTitle">Add New Friend</h2>
<form id="friendForm">
<div class="form-group">
<label for="name">Name:</label>
<input type="text" id="name" required placeholder="e.g., Alice Wonderland">
</div>
<div class="form-group">
<label for="birthday">Birthday:</label>
<input type="date" id="birthday">
</div>
<div class="form-group">
<label for="relationshipType">Relationship Type:</label>
<select id="relationshipType">
<option value="">Select Relationship Type</option>
<option value="Close Friend">Close Friend</option>
<option value="Friend">Friend</option>
<option value="Acquaintance">Acquaintance</option>
<option value="Family">Family</option>
<option value="Work Colleague">Work Colleague</option>
<option value="Neighbor">Neighbor</option>
<option value="Mentor">Mentor</option>
<option value="Online Friend">Online Friend</option>
<option value="Other">Other</option>
</select>
</div>
<div class="form-group slider-group">
<label for="truthness">Truthfulness:</label>
<input type="range" id="truthness" min="0" max="10" value="5">
<span class="slider-value" id="truthnessValue">5/10</span>
</div>
<div class="form-group slider-group">
<label for="angriness">Angriness:</label>
<input type="range" id="angriness" min="0" max="10" value="5">
<span class="slider-value" id="angrinessValue">5/10</span>
</div>
<div class="form-group slider-group">
<label for="humor">Humor:</label>
<input type="range" id="humor" min="0" max="10" value="5">
<span class="slider-value" id="humorValue">5/10</span>
</div>
<div class="form-group slider-group">
<label for="empathy">Empathy:</label>
<input type="range" id="empathy" min="0" max="10" value="5">
<span class="slider-value" id="empathyValue">5/10</span>
</div>
<div class="form-group slider-group">
<label for="optimism">Optimism:</label>
<input type="range" id="optimism" min="0" max="10" value="5">
<span class="slider-value" id="optimismValue">5/10</span>
</div>
<div class="form-group slider-group">
<label for="creativity">Creativity:</label>
<input type="range" id="creativity" min="0" max="10" value="5">
<span class="slider-value" id="creativityValue">5/10</span>
</div>
<div class="form-group slider-group">
<label for="socialScore">Social Score:</label>
<input type="range" id="socialScore" min="0" max="10" value="5">
<span class="slider-value" id="socialScoreValue">5/10</span>
</div>
<div class="form-group slider-group">
<label for="calmness">Calmness:</label>
<input type="range" id="calmness" min="0" max="10" value="5">
<span class="slider-value" id="calmnessValue">5/10</span>
</div>
<div class="form-group slider-group">
<label for="punctuality">Punctuality:</label>
<input type="range" id="punctuality" min="0" max="10" value="5">
<span class="slider-value" id="punctualityValue">5/10</span>
</div>
<div class="form-group">
<label for="foodPreference">Food Preferences:</label>
<textarea id="foodPreference" rows="3" placeholder="e.g., Vegetarian, loves Italian, dislikes spicy food"></textarea>
</div>
<div class="form-group">
<label for="hobbies">Hobbies/Interests:</label>
<textarea id="hobbies" rows="3" placeholder="e.g., Reading, hiking, gaming, painting"></textarea>
</div>
<div class="form-group">
<label for="notes">Notes:</label>
<textarea id="notes" rows="3" placeholder="Any additional notes or important details"></textarea>
</div>
<div class="form-actions">
<button type="submit" id="saveFriendBtn"><i class="fas fa-save"></i> Save Friend</button>
<button type="button" id="clearForm"><i class="fas fa-sync-alt"></i> Reset Form</button>
<button type="button" id="deleteFriendBtn"><i class="fas fa-trash-alt"></i> Delete Friend</button>
</div>
</form>
</div>
</div>
<script>
// Helper function to get friends from localStorage
function getFriends() {
const friendsJSON = localStorage.getItem('friends');
return friendsJSON ? JSON.parse(friendsJSON) : [];
}
// Helper function to save friends to localStorage
function saveFriends(friends) {
localStorage.setItem('friends', JSON.stringify(friends));
}
document.addEventListener('DOMContentLoaded', function() {
const friendForm = document.getElementById('friendForm');
const formTitle = document.getElementById('formTitle');
const nameInput = document.getElementById('name');
const birthdayInput = document.getElementById('birthday');
const relationshipTypeSelect = document.getElementById('relationshipType'); // NEW
const foodPreferenceInput = document.getElementById('foodPreference');
const hobbiesInput = document.getElementById('hobbies');
const notesInput = document.getElementById('notes');
const clearFormBtn = document.getElementById('clearForm');
const deleteFriendBtn = document.getElementById('deleteFriendBtn');
const saveFriendBtn = document.getElementById('saveFriendBtn'); // Get reference to the save button
// Numerical inputs and their value displays
const numericalFields = {
truthness: document.getElementById('truthness'),
angriness: document.getElementById('angriness'),
humor: document.getElementById('humor'),
empathy: document.getElementById('empathy'),
optimism: document.getElementById('optimism'),
creativity: document.getElementById('creativity'),
socialScore: document.getElementById('socialScore'),
calmness: document.getElementById('calmness'),
punctuality: document.getElementById('punctuality')
};
const numericalValueDisplays = {
truthness: document.getElementById('truthnessValue'),
angriness: document.getElementById('angrinessValue'),
humor: document.getElementById('humorValue'),
empathy: document.getElementById('empathyValue'),
optimism: document.getElementById('optimismValue'),
creativity: document.getElementById('creativityValue'),
socialScore: document.getElementById('socialScoreValue'),
calmness: document.getElementById('calmnessValue'),
punctuality: document.getElementById('punctualityValue')
};
let editingFriendName = null; // To store the name of the friend being edited
// Add event listeners for range inputs to update their value display
for (const key in numericalFields) {
numericalFields[key].addEventListener('input', function() {
numericalValueDisplays[key].textContent = `${this.value}/10`;
});
}
// Function to clear the form
function clearForm() {
friendForm.reset(); // Resets all form fields to their initial values
for (const key in numericalFields) {
numericalFields[key].value = 5; // Reset sliders to 5
numericalValueDisplays[key].textContent = '5/10'; // Update display
}
formTitle.textContent = 'Add New Friend';
editingFriendName = null;
nameInput.readOnly = false; // Make name editable for new friend
deleteFriendBtn.style.display = 'none'; // Hide delete button
saveFriendBtn.textContent = 'Save Friend'; // Change button text back
saveFriendBtn.prepend(document.createElement('i')).className = 'fas fa-save'; // Add icon back
}
// Handle form submission (add/edit friend)
friendForm.addEventListener('submit', function(event) {
event.preventDefault(); // Prevent default form submission
let friends = getFriends();
const newFriend = {
name: nameInput.value.trim(),
birthday: birthdayInput.value, // YYYY-MM-DD format
relationshipType: relationshipTypeSelect.value, // NEW
foodPreference: foodPreferenceInput.value.trim(),
hobbies: hobbiesInput.value.trim(),
notes: notesInput.value.trim()
};
// Add numerical values
for (const key in numericalFields) {
newFriend[key] = parseInt(numericalFields[key].value);
}
if (newFriend.name === '') {
alert('Friend name cannot be empty!');
return;
}
if (editingFriendName) {
// Editing existing friend
const index = friends.findIndex(f => f.name === editingFriendName);
if (index !== -1) {
friends[index] = newFriend; // Update the friend object
saveFriends(friends);
alert(`${newFriend.name} updated successfully!`);
window.location.href = 'list.html'; // Redirect to list after update
}
} else {
// Adding new friend
// Check if friend name already exists
if (friends.some(f => f.name.toLowerCase() === newFriend.name.toLowerCase())) {
alert('A friend with this name already exists. Please use a different name or edit the existing friend.');
return;
}
friends.push(newFriend); // Add new friend to the array
saveFriends(friends); // Save updated array to localStorage
alert(`${newFriend.name} added successfully!`);
clearForm(); // Clear the form after successful addition
}
});
// Handle clear form button
clearFormBtn.addEventListener('click', clearForm);
// Handle delete friend button (only visible in edit mode)
deleteFriendBtn.addEventListener('click', function() {
if (editingFriendName && confirm(`Are you sure you want to delete ${editingFriendName}? This action cannot be undone.`)) {
let friends = getFriends();
friends = friends.filter(friend => friend.name !== editingFriendName);
saveFriends(friends);
alert(`${editingFriendName} has been deleted.`);
window.location.href = 'list.html'; // Redirect to list after deletion
}
});
// Function to load friend data into the form for editing
function loadFriendForEdit() {
const urlParams = new URLSearchParams(window.location.search);
const nameToEdit = urlParams.get('edit');
if (nameToEdit) {
let friends = getFriends();
const friendToEdit = friends.find(friend => friend.name === nameToEdit);
if (friendToEdit) {
formTitle.textContent = `Edit Friend: ${friendToEdit.name}`;
editingFriendName = friendToEdit.name;
nameInput.value = friendToEdit.name;
nameInput.readOnly = true; // Prevent changing name during edit to avoid issues
birthdayInput.value = friendToEdit.birthday || '';
relationshipTypeSelect.value = friendToEdit.relationshipType || ''; // NEW: Set relationship type
foodPreferenceInput.value = friendToEdit.foodPreference || '';
hobbiesInput.value = friendToEdit.hobbies || '';
notesInput.value = friendToEdit.notes || '';
for (const key in numericalFields) {
// Use 0 as default if value is missing (for older entries)
const value = friendToEdit[key] !== undefined ? friendToEdit[key] : 5;
numericalFields[key].value = value;
numericalValueDisplays[key].textContent = `${value}/10`;
}
deleteFriendBtn.style.display = 'block'; // Show delete button
saveFriendBtn.textContent = 'Update Friend'; // Change button text
saveFriendBtn.prepend(document.createElement('i')).className = 'fas fa-save'; // Add icon back
} else {
alert('Friend not found!');
window.location.href = 'index.html'; // Redirect to add new friend form
}
} else {
// If not in edit mode, ensure form is clear and ready for new entry
clearForm();
}
}
// Load friend data if in edit mode, otherwise clear the form
loadFriendForEdit();
});
</script>
<footer>
<p>© 2025 Reman Dey. All rights reserved.</p>
</footer>
</body>
</html>