-
Notifications
You must be signed in to change notification settings - Fork 93
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Contact-List #84
base: main
Are you sure you want to change the base?
Contact-List #84
Changes from 5 commits
ad79a4f
146eac8
16c5672
bf1f1c5
dcb8220
a2b9c00
2606074
b03a973
d2827a8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
<head> | ||
<meta charset="utf-8" /> | ||
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" /> | ||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<meta name="theme-color" content="#000000" /> | ||
<meta | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,49 @@ | ||
import logo from './logo.svg'; | ||
|
||
import './App.css'; | ||
import React, {useState, useEffect} from 'react' | ||
import {Routes, Route} from 'react-router-dom' | ||
import AddNewContact from './components/add_new_contact'; | ||
import Contact from './components/contact_id'; | ||
import ContactList from './components/contact_list'; | ||
|
||
|
||
function App() { | ||
const [contacts, setContacts] = useState( | ||
JSON.parse(localStorage.getItem('contacts')) || [ | ||
{ | ||
id: 1, | ||
name: 'Maximus Mighty-Dog Mueller II', | ||
email: '[email protected]', | ||
phoneNumber: '5558881234', | ||
ImageUrl: 'https://en.wikipedia.org/wiki/Mayor_Max_II#/media/File:Max_II_with_medal,_11.24.13.jpg' | ||
} | ||
] | ||
); | ||
|
||
useEffect(() => { | ||
storeContact(contacts); | ||
}, [contacts]); | ||
|
||
const addContact = (newContact) => { | ||
setContacts((previousContact) => [...previousContact, newContact]); | ||
}; | ||
return ( | ||
<div className="App"> | ||
<header className="App-header"> | ||
<img src={logo} className="App-logo" alt="logo" /> | ||
<p> | ||
Edit <code>src/App.js</code> and save to reload. | ||
</p> | ||
<a | ||
className="App-link" | ||
href="https://reactjs.org" | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
> | ||
Learn React | ||
</a> | ||
</header> | ||
<div> | ||
<h1>Contact List</h1> | ||
<Routes> | ||
<Route exact path="/" element={<ContactList contacts={contacts} />} /> | ||
<Route path="/contacts" element={<ContactList contacts={contacts} />}/> | ||
<Route path="/new" element={<AddNewContact addContact={addContact} setContacts={setContacts}/>}/> | ||
<Route path="contacts/id" element={<Contact contacts={contacts}/>} /> | ||
</Routes> | ||
</div> | ||
); | ||
} | ||
|
||
const storeContact = (contacts) => { | ||
localStorage.setItem('contacts',JSON.stringify(contacts)); | ||
}; | ||
|
||
|
||
|
||
export default App; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import React, { useState } from 'react'; | ||
import PropTypes from 'prop-types' | ||
import { useNavigate, Link } from 'react-router-dom'; | ||
|
||
const AddNewContact = ({ addContact }) => { | ||
const [name, setName] = useState(''); | ||
const [email, setEmail] = useState(''); | ||
const [number, setNumber] = useState(''); | ||
const [ImageUrl, setImageUrl] = useState(''); | ||
|
||
const nav = useNavigate(); | ||
|
||
const handleNameChange = (e) => { | ||
setName(e.target.value); | ||
}; | ||
|
||
const handleEmailChange = (e) => { | ||
setEmail(e.target.value); | ||
}; | ||
|
||
const handleNumberChange = (e) => { | ||
setNumber(e.target.value); | ||
}; | ||
|
||
const handleImageUrlChange = (e) => { | ||
setImageUrl(e.target.value); | ||
}; | ||
|
||
const giveId = () => Math.round(Math.random() * 100000000); | ||
|
||
const handleSubmit = (e) => { | ||
e.preventDefault(); | ||
const newContact = { | ||
id: giveId(), | ||
name, | ||
email, | ||
number, | ||
ImageUrl, | ||
}; | ||
|
||
addContact(newContact); | ||
nav('/contactList'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this route doesn't exist |
||
}; | ||
|
||
|
||
|
||
return ( | ||
<div className="container"> | ||
<h2>Add Contact</h2> | ||
<form onSubmit={handleSubmit}> | ||
<div className="form-group"> | ||
<label htmlFor="name">Full Name</label> | ||
<input | ||
type="text" | ||
className="form-control" | ||
id="name" | ||
value={name} | ||
onChange={handleNameChange} | ||
placeholder="Enter Full Name" | ||
required | ||
/> | ||
</div> | ||
<div className="form-group"> | ||
<label htmlFor="phone">Phone Number</label> | ||
<input | ||
type="text" | ||
className="form-control" | ||
id="phone" | ||
onChange={handleNumberChange} | ||
value={number} | ||
pattern="[0-9]{10}" | ||
placeholder="5552223333" | ||
required | ||
/> | ||
</div> | ||
<div className="form-group"> | ||
<label htmlFor="email">Email</label> | ||
<input | ||
type="text" | ||
className="form-control" | ||
id="email" | ||
onChange={handleEmailChange} | ||
value={email} | ||
placeholder="[email protected]" | ||
required | ||
/> | ||
</div> | ||
<div className="form-group"> | ||
<label htmlFor="image">Image URL</label> | ||
<input | ||
type="text" | ||
className="form-control" | ||
id="image" | ||
onChange={handleImageUrlChange} | ||
value={ImageUrl} | ||
placeholder="https://URL.jpg" | ||
required | ||
/> | ||
</div> | ||
<button type="submit" className="btn btn-primary"> | ||
Add Contact | ||
</button> | ||
<Link to="/contactList" className="btn btn-primary" > Back to Contact List | ||
</Link> | ||
</form> | ||
</div> | ||
); | ||
}; | ||
|
||
|
||
AddNewContact.propTypes = { | ||
addContact :PropTypes.func.isRequired, | ||
}; | ||
|
||
export default AddNewContact; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { useParams, Link } from 'react-router-dom'; | ||
|
||
|
||
const Contact = ({ contacts }) => { | ||
const { id } = useParams(); | ||
const contact = contacts.find((c) => c.id === parseInt(id, 10)); | ||
return ( | ||
|
||
<div className='container'> | ||
<div className="card"> | ||
<img | ||
src={contact.imageUrl} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The I in imageUrl is uppercase in other places |
||
alt="contact info" | ||
/> | ||
<div className="card-body"> | ||
<h5 className="card-title">{contact.name}</h5> | ||
<h6 className="card-subtitle mb-2 text-muted">Email: {contact.email}</h6> | ||
<p className="card-text">Phone Number: {contact.phoneNumber}</p> | ||
</div> | ||
<div className="card-footer"> | ||
<Link to="contacts" className="btn btn-primary"> Back to Contact List | ||
</Link> | ||
</div> | ||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
|
||
Contact.propTypes = { | ||
contacts: PropTypes.array.isRequired, | ||
}; | ||
|
||
export default Contact; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Link } from 'react-router-dom' | ||
|
||
|
||
const ContactList = ({ contacts }) => ( | ||
<div className="container"> | ||
<Link to="/new" className="btn btn-primary"> | ||
Add Contact | ||
</Link> | ||
<table className="table table-bordered"> | ||
<thead> | ||
<tr> | ||
<th scope="col">Profile Picture</th> | ||
<th scope="col">Name</th> | ||
<th scope="col">Email</th> | ||
<th scope="col">PhoneNumber</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{contacts.map((contact) => ( | ||
<tr | ||
key={contact.id} className="contact-row" onClick={() => window.location.href = `/contacts/${contact.id}`}> | ||
|
||
<td> | ||
<img | ||
src={contact.imageUrl} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. another lower case i |
||
className="rounded mx-auto d-block" | ||
width="25%" | ||
height="auto" | ||
alt="new" | ||
/> | ||
</td> | ||
<td>{contact.name}</td> | ||
<td>{contact.email}</td> | ||
<td>{contact.phoneNumber}</td> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sometimes you use phoneNumber and other times its number |
||
</tr> | ||
))} | ||
</tbody> | ||
</table> | ||
</div> | ||
); | ||
|
||
ContactList.propType = { | ||
contacts: PropTypes.array.isRequired | ||
} | ||
|
||
export default ContactList; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The syntax for including the id as part of the path is incorrect