Skip to content

Commit

Permalink
Update App.jsx
Browse files Browse the repository at this point in the history
  • Loading branch information
Miro-wq committed Sep 3, 2024
1 parent f0777f1 commit 339847f
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/components/App/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,29 @@ import './App.module.css';

class App extends Component {
state = {
contacts: [
{id: 'id-1', name: 'Rosie Simpson', number: '459-12-56'},
{id: 'id-2', name: 'Hermione Kline', number: '443-89-12'},
],
contacts: [],
filter: '',
name: '',
number: ''
}

componentDidMount() {
const contacts = localStorage.getItem('contacts');
if (contacts) {
this.setState({ contacts: JSON.parse(contacts) });
}
}
componentDidUpdate(prevState) {
if (this.state.contacts !== prevState.contacts) {
localStorage.setItem('contacts', JSON.stringify(this.state.contacts));
}
}

addContact = (name, number) => {
const { contacts } = this.state;

const nameExists = contacts.some(contact => contact.name.toLowerCase() === name.toLowerCase());

if (nameExists) {
alert(`Contact with the name "${name}" already exists.`);
const isDuplicate = contacts.some(
contact => contact.name.toLowerCase() === name.toLowerCase()
);
if (isDuplicate) {
alert(`${name} is already in contacts.`);
return;
}

Expand Down

0 comments on commit 339847f

Please sign in to comment.