Skip to content

Commit

Permalink
feat: Bug fix: 1: Try to add same name and resolve the error in the c…
Browse files Browse the repository at this point in the history
…onsole
  • Loading branch information
Param-Harrison committed Apr 17, 2020
1 parent 1e00145 commit f38810b
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import React, { useState } from "react";
import uniqueId from "lodash/uniqueId";
import ContactList from "./components/contact-list";
import AddContact from "./components/add-contact";

const INITIAL_CONTACTS = [
{
id: uniqueId(),
name: "Vennila",
phone: "+372 5993789",
email: "[email protected]",
},
{
id: uniqueId(),
name: "Afrin",
phone: "+372 5663421",
email: "[email protected]",
Expand Down
2 changes: 2 additions & 0 deletions src/components/add-contact.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import React, { useState } from "react";
import uniqueId from "lodash/uniqueId";
import ContactForm from "./contact-form";

const AddContactForm = ({ setContacts, ...props }) => {
const onSubmit = (submitData) => {
setContacts((contacts) => {
return [
{
id: uniqueId(),
...submitData,
},
...contacts,
Expand Down
7 changes: 1 addition & 6 deletions src/components/contact-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,7 @@ const ContactList = ({ contacts, setContacts }) => {
<ol>
{contacts.map((contact, index) => {
return (
<Contact
key={contact.name}
{...contact}
id={index}
setContacts={setContacts}
/>
<Contact key={contact.id} {...contact} setContacts={setContacts} />
);
})}
</ol>
Expand Down
4 changes: 3 additions & 1 deletion src/components/contact.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, { useState } from "react";
import cloneDeep from "lodash/cloneDeep";
import findIndex from "lodash/findIndex";
import EditContact from "./edit-contact";

const Contact = ({ setContacts, ...props }) => {
Expand All @@ -13,7 +14,8 @@ const Contact = ({ setContacts, ...props }) => {
if (confirm) {
setContacts((contacts) => {
const clonedContacts = cloneDeep(contacts);
clonedContacts.splice(id, 1);
const index = findIndex(clonedContacts, { id });
clonedContacts.splice(index, 1);
return clonedContacts;
});
}
Expand Down
6 changes: 4 additions & 2 deletions src/components/edit-contact.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import React from "react";
import cloneDeep from "lodash/cloneDeep";
import findIndex from "lodash/findIndex";
import ContactForm from "./contact-form";

const EditContact = ({ id = 0, setContacts, ...props }) => {
const EditContact = ({ id, setContacts, ...props }) => {
const onSubmit = (submitData) => {
setContacts((contacts) => {
const clonedContacts = cloneDeep(contacts);
clonedContacts[id] = { ...submitData };
const index = findIndex(clonedContacts, { id });
clonedContacts[index] = { ...clonedContacts[index], ...submitData };
return clonedContacts;
});
};
Expand Down

0 comments on commit f38810b

Please sign in to comment.