Skip to content

Commit

Permalink
feat: add contacts to local storage
Browse files Browse the repository at this point in the history
  • Loading branch information
LesiaUKR committed Feb 22, 2024
1 parent d227115 commit 84c7a83
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@

### Фінальний результат

![Screencapture](./assets/screencapture.png)
![Screencapture](./assets/final-result.jpg)
Binary file modified assets/final-result.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 23 additions & 6 deletions src/components/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,28 @@ import { Layout, Header, MainHeader } from './Layout.js';
import { ContactsForm } from 'components/ContactsForm/ContactsForm.jsx';
import { ContactList } from 'components/ContactList/ContactList.jsx';
import { Filter } from 'components/Filter/Filter.jsx';
import initialContacts from '../contacts.json';
// import initialContacts from '../contacts.json';

export class App extends Component {
state = {
contacts: [...initialContacts],
contacts: [],
filter: '',
};

componentDidMount() {
const localContact = localStorage.getItem('contact');
if (localContact !== null) {
this.setState({ contacts: JSON.parse(localContact) });
return;
}
}

componentDidUpdate(_, prevState) {
if (prevState.contacts !== this.state.contacts) {
localStorage.setItem('contact', JSON.stringify(this.state.contacts));
}
}

addContact = newContact => {
this.state.contacts.filter(contact => contact.name === newContact.name)
.length
Expand Down Expand Up @@ -39,17 +53,20 @@ export class App extends Component {
};

render() {
const contacts = this.getFilterContacts();
return (
<Layout>
<GlobalStyle />
<MainHeader>Phonebook</MainHeader>
<ContactsForm onSubmit={this.addContact} />
<Header>Contacts</Header>
<Filter onChange={this.changeFilter} value={this.state.filter}></Filter>
<ContactList
contacts={this.getFilterContacts()}
onDeleteContact={this.deleteContact}
></ContactList>
{contacts.length > 0 && (
<ContactList
contacts={contacts}
onDeleteContact={this.deleteContact}
></ContactList>
)}
</Layout>
);
}
Expand Down

0 comments on commit 84c7a83

Please sign in to comment.