-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContactForm.js
More file actions
113 lines (104 loc) · 3.92 KB
/
ContactForm.js
File metadata and controls
113 lines (104 loc) · 3.92 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
import React, { useState, useEffect } from 'react';
import axios from 'axios';
import './Contact.css';
const ContactForm = ({ currentContact, onContactAddedOrUpdated }) => {
const [formData, setFormData] = useState({
name: '',
email: '',
phone: ''
});
const [loading, setLoading] = useState(false);
const [error, setError] = useState(null);
const [success, setSuccess] = useState(null);
// This useEffect hook populates the form when a contact is selected for editing
useEffect(() => {
if (currentContact) {
setFormData({
name: currentContact.name,
email: currentContact.email,
phone: currentContact.phone,
});
} else {
// Reset form if no contact is selected (e.g., after an update)
setFormData({ name: '', email: '', phone: '' });
}
}, [currentContact]);
const handleChange = (e) => {
const { name, value } = e.target;
setFormData(prevState => ({
...prevState,
[name]: value
}));
};
const handleSubmit = async (e) => {
e.preventDefault();
setLoading(true);
setError(null);
setSuccess(null);
try {
if (currentContact) {
// Update existing contact
await axios.put(`/api/contacts/${currentContact.contactId}`, formData);
setSuccess('Contact updated successfully!');
} else {
// Create a new contact
await axios.post('/api/contacts', formData);
setSuccess('Contact added successfully!');
setFormData({ name: '', email: '', phone: '' });
}
onContactAddedOrUpdated();
} catch (err) {
console.error('API Error:', err.response || err);
const backendErrorMessage = err.response?.data?.errors?.phone || err.response?.data?.message || 'An unknown error occurred.';
setError(backendErrorMessage);
} finally {
setLoading(false);
}
};
return (
<div className="contact-form-container">
<h2>{currentContact ? 'Edit Contact' : 'Add New Contact'}</h2>
<form onSubmit={handleSubmit}>
{success && <div className="alert-success">{success}</div>}
{error && <div className="alert-error">{error}</div>}
<div className="form-group">
<label htmlFor="name">Name:</label>
<input
type="text"
id="name"
name="name"
value={formData.name}
onChange={handleChange}
required
/>
</div>
<div className="form-group">
<label htmlFor="email">Email:</label>
<input
type="email"
id="email"
name="email"
value={formData.email}
onChange={handleChange}
required
/>
</div>
<div className="form-group">
<label htmlFor="phone">Phone:</label>
<input
type="tel"
id="phone"
name="phone"
value={formData.phone}
onChange={handleChange}
required
/>
</div>
<button type="submit" disabled={loading}>
{loading ? (currentContact ? 'Updating...' : 'Adding...') : (currentContact ? 'Update Contact' : 'Add Contact')}
</button>
</form>
</div>
);
};
export default ContactForm;