|
| 1 | +import React, { useState, useEffect } from 'react'; |
| 2 | +import logo from './logo.svg'; |
| 3 | +import Header from './components/Header'; |
| 4 | +import './App.css'; |
| 5 | +import Contacts from './components/Contacts'; |
| 6 | +import AddContact from './components/AddContact'; |
| 7 | + |
| 8 | +function App() { |
| 9 | + const url = 'http://localhost:5000/contacts'; |
| 10 | + const [showAddContact, setShowAddContact] = useState(false); |
| 11 | + const [contacts, setContacts] = useState([]); |
| 12 | + const fetchContacts = async () => { |
| 13 | + const res = await fetch(url); |
| 14 | + const data = await res.json(); |
| 15 | + return data; |
| 16 | + }; |
| 17 | + const fetchContact = async (id) => { |
| 18 | + const res = await fetch(`${url}/${id}`); |
| 19 | + const data = await res.json(); |
| 20 | + return data; |
| 21 | + }; |
| 22 | + useEffect(() => { |
| 23 | + const getGontacts = async () => { |
| 24 | + const contactsFromServer = await fetchContacts(); |
| 25 | + setContacts(contactsFromServer); |
| 26 | + }; |
| 27 | + getGontacts(); |
| 28 | + }, []); |
| 29 | + |
| 30 | + const deleteContact = async (id) => { |
| 31 | + await fetch(`${url}/${id}`, { |
| 32 | + method:'DELETE' |
| 33 | + }); |
| 34 | + setContacts(contacts.filter((a) => a.id !== id)); |
| 35 | + }; |
| 36 | + const toggleReminder = async (id) => { |
| 37 | + const reminderContact = await fetchContact(id); |
| 38 | + const updateContact = {...reminderContact, reminder:!reminderContact.reminder} |
| 39 | + const res = await fetch (`${url}/${id}`, { |
| 40 | + method:'PUT', |
| 41 | + headers:{'Content-type':'application/json'}, |
| 42 | + body:JSON.stringify(updateContact) |
| 43 | + }) |
| 44 | + const data = await res.json(); |
| 45 | + |
| 46 | + setContacts( |
| 47 | + contacts.map((contact) => |
| 48 | + contact.id === id |
| 49 | + ? { ...contact, reminder: data.reminder } |
| 50 | + : contact |
| 51 | + ) |
| 52 | + ); |
| 53 | + }; |
| 54 | + const addContact = async (contact) => { |
| 55 | + console.log(JSON.stringify(contact)); |
| 56 | + const res = await fetch (`${url}`, { |
| 57 | + method:'POST', |
| 58 | + headers:{'Content-type':'application/json'}, |
| 59 | + body:JSON.stringify(contact) |
| 60 | + }) |
| 61 | + const data = await res.json(); |
| 62 | + setContacts([...contacts, data]); |
| 63 | + setShowAddContact(false); |
| 64 | + // const id = Math.floor(Math.random() * 1000) + 1; |
| 65 | + // const newContact = { id, ...contact }; |
| 66 | + // setContacts([...contacts, newContact]); |
| 67 | + }; |
| 68 | + return ( |
| 69 | + <div className="App"> |
| 70 | + <header className="App-header"> |
| 71 | + <h2>Love Tracker</h2> |
| 72 | + <div className="container"> |
| 73 | + <Header |
| 74 | + title="Eros" |
| 75 | + onAdd={() => setShowAddContact(!showAddContact)} |
| 76 | + showAddContact={showAddContact} |
| 77 | + /> |
| 78 | + |
| 79 | + {contacts.length > 0 ? ( |
| 80 | + <Contacts |
| 81 | + contacts={contacts} |
| 82 | + onDelete={deleteContact} |
| 83 | + onToggleReminder={toggleReminder} |
| 84 | + /> |
| 85 | + ) : ( |
| 86 | + 'No contacts' |
| 87 | + )} |
| 88 | + {showAddContact && <AddContact onAdd={addContact} />} |
| 89 | + </div> |
| 90 | + </header> |
| 91 | + </div> |
| 92 | + ); |
| 93 | +} |
| 94 | + |
| 95 | +export default App; |
0 commit comments