-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Exercise 3: Reuse the add and edit form as much as possible
- Loading branch information
1 parent
9ad8d2f
commit 1e00145
Showing
3 changed files
with
104 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import React, { useState } from "react"; | ||
import FormInput from "./form-input"; | ||
|
||
const ContactForm = ({ | ||
name = "", | ||
phone = "", | ||
email = "", | ||
title, | ||
setFormActive, | ||
...props | ||
}) => { | ||
const [formValues, setFormValues] = useState({ name, phone, email }); | ||
|
||
const resetForm = () => { | ||
setFormValues({ name: "", phone: "", email: "" }); | ||
setFormActive(false); | ||
}; | ||
|
||
const onSubmit = (e) => { | ||
e.preventDefault(); | ||
|
||
const { name, email, phone } = formValues; | ||
if (name && email && phone) { | ||
props.onSubmit(formValues); | ||
} | ||
|
||
resetForm(); | ||
}; | ||
|
||
const onDiscard = () => { | ||
resetForm(); | ||
}; | ||
|
||
const onChange = (e) => { | ||
const key = e.target.name; | ||
const value = e.target.value; | ||
setFormValues((previousFormValues) => { | ||
return { | ||
...previousFormValues, | ||
[key]: value, | ||
}; | ||
}); | ||
}; | ||
|
||
return ( | ||
<React.Fragment> | ||
<form onSubmit={onSubmit}> | ||
<h3>{title}</h3> | ||
<FormInput | ||
type="text" | ||
label="Name" | ||
value={formValues.name} | ||
onChange={onChange} | ||
placeholder="Contact name" | ||
required | ||
/> | ||
<FormInput | ||
type="text" | ||
label="Phone" | ||
value={formValues.phone} | ||
onChange={onChange} | ||
placeholder="Contact phone" | ||
required | ||
/> | ||
<FormInput | ||
type="email" | ||
label="Email" | ||
value={formValues.email} | ||
onChange={onChange} | ||
placeholder="Contact email" | ||
required | ||
/> | ||
<button type="submit">Save contact</button> | ||
<button type="button" onClick={onDiscard}> | ||
Discard | ||
</button> | ||
</form> | ||
</React.Fragment> | ||
); | ||
}; | ||
|
||
export default ContactForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,17 @@ | ||
import React, { useState } from "react"; | ||
import React from "react"; | ||
import cloneDeep from "lodash/cloneDeep"; | ||
import FormInput from "./form-input"; | ||
|
||
const EditContact = ({ | ||
name = "", | ||
phone = "", | ||
email = "", | ||
id = 0, | ||
setContacts, | ||
setFormActive, | ||
}) => { | ||
const [formValues, setFormValues] = useState({ name, phone, email }); | ||
|
||
const resetForm = () => { | ||
setFormValues({ name: "", phone: "", email: "" }); | ||
setFormActive(false); | ||
}; | ||
|
||
const onSubmit = (e) => { | ||
e.preventDefault(); | ||
|
||
const { name, email, phone } = formValues; | ||
if (name && email && phone) { | ||
// Save the edited content | ||
setContacts((contacts) => { | ||
const clonedContacts = cloneDeep(contacts); | ||
clonedContacts[id] = { | ||
name, | ||
email, | ||
phone, | ||
}; | ||
return clonedContacts; | ||
}); | ||
} | ||
|
||
resetForm(); | ||
}; | ||
|
||
const onDiscard = () => { | ||
resetForm(); | ||
}; | ||
|
||
const onChange = (e) => { | ||
const key = e.target.name; | ||
const value = e.target.value; | ||
setFormValues((previousFormValues) => { | ||
return { | ||
...previousFormValues, | ||
[key]: value, | ||
}; | ||
import ContactForm from "./contact-form"; | ||
|
||
const EditContact = ({ id = 0, setContacts, ...props }) => { | ||
const onSubmit = (submitData) => { | ||
setContacts((contacts) => { | ||
const clonedContacts = cloneDeep(contacts); | ||
clonedContacts[id] = { ...submitData }; | ||
return clonedContacts; | ||
}); | ||
}; | ||
|
||
return ( | ||
<React.Fragment> | ||
<form onSubmit={onSubmit}> | ||
<h3>Edit contact</h3> | ||
<FormInput | ||
type="text" | ||
label="Name" | ||
value={formValues.name} | ||
onChange={onChange} | ||
placeholder="Contact name" | ||
required | ||
/> | ||
<FormInput | ||
type="text" | ||
label="Phone" | ||
value={formValues.phone} | ||
onChange={onChange} | ||
placeholder="Contact phone" | ||
required | ||
/> | ||
<FormInput | ||
type="email" | ||
label="Email" | ||
value={formValues.email} | ||
onChange={onChange} | ||
placeholder="Contact email" | ||
required | ||
/> | ||
<button type="submit">Save contact</button> | ||
<button type="button" onClick={onDiscard}> | ||
Discard | ||
</button> | ||
</form> | ||
</React.Fragment> | ||
); | ||
return <ContactForm title={"Edit contact"} onSubmit={onSubmit} {...props} />; | ||
}; | ||
|
||
export default EditContact; |