-
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
React Contacts #71
Open
johnnywrightiv
wants to merge
9
commits into
projectshft:main
Choose a base branch
from
johnnywrightiv:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
React Contacts #71
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6100bcc
Setup app components and dependencies
johnnywrightiv 93203c4
Fetch data from JSON file, add React Router dependency
johnnywrightiv 484a425
Got stuck on rendering contacts to list component with map
johnnywrightiv e63b922
Struggling to get table rendered
johnnywrightiv fb368f8
Implement router and nav links. Add UI elements (navbar, contacts lis…
johnnywrightiv 2f23086
Render list and contact views, add 'go back' routes. Stuck on updatin…
johnnywrightiv 75bf90c
Update state from form input and ensure correct rendering
johnnywrightiv 15c3506
Fix form validation
johnnywrightiv 86b3444
Typecheck with proptypes
johnnywrightiv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,60 @@ | ||
import logo from './logo.svg'; | ||
import React, { useState, useEffect} from "react"; | ||
import './App.css'; | ||
import Home from "./componenets/home"; | ||
import ContactItem from "./componenets/ContactItem"; | ||
import ContactsList from "./componenets/ContactList"; | ||
import AddContact from "./componenets/AddContact"; | ||
import NotFound from "./componenets/NotFound"; | ||
import { Routes, Route, Link } from 'react-router-dom'; | ||
import { Navbar, Nav } from "react-bootstrap"; | ||
import data from "./data.json"; | ||
|
||
|
||
function App() { | ||
// Rename Document Title (using as componentDidMount) | ||
useEffect(() => { | ||
document.title = "Contacts App"; | ||
}, []); | ||
|
||
// Define initial state of contacts and define setContacts method(?) -- setup useState hook | ||
const [contacts, setContacts] = useState(data.contacts) | ||
|
||
// Func to add new contact | ||
const addNewContact = (newContact) => { | ||
console.log(newContact); | ||
setContacts(() => [...contacts, 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> | ||
); | ||
<> | ||
<Navbar bg="purple" className="gradient-navbar" variant="dark" expand="lg"> | ||
<Navbar.Brand as={Link} to="/">Contacts App</Navbar.Brand> | ||
<Navbar.Toggle aria-controls="basic-navbar-nav" /> | ||
<Navbar.Collapse id="basic-navbar-nav"> | ||
<Nav className="mr-auto"> | ||
<Nav.Link as={Link} to="/">Home</Nav.Link> | ||
<Nav.Link as={Link} to="/contacts">Contacts</Nav.Link> | ||
<Nav.Link as={Link} to="/contacts/new">New Contact</Nav.Link> | ||
</Nav> | ||
</Navbar.Collapse> | ||
</Navbar> | ||
|
||
<div className="container"> | ||
<div className="row"> | ||
<h1 className="text-center gradient-color">Contacts App</h1> | ||
<Routes> | ||
<Route path="/" element={<Home />} /> | ||
<Route path="/contacts" > | ||
<Route index element={<ContactsList contacts={contacts} />} /> | ||
<Route path=":id" element={<ContactItem />} /> | ||
<Route path="new" element={<AddContact onSubmit={addNewContact} />} /> | ||
</Route> | ||
<Route path="/*" element={<NotFound />} /> | ||
</Routes> | ||
</div> | ||
</div> | ||
</> | ||
) | ||
} | ||
|
||
export default App; | ||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
import { useState } from 'react'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import PropTypes from 'prop-types'; | ||
import FloatingLabel from 'react-bootstrap/FloatingLabel'; | ||
import Form from 'react-bootstrap/Form'; | ||
|
||
const AddContact = ({onSubmit}) => { | ||
|
||
const navigate = useNavigate(); | ||
|
||
// Define local state array, object keys, and set methods | ||
const [contactData, setContactData] = useState([]); | ||
const [name, setName] = useState("") | ||
const [email, setEmail] = useState("") | ||
const [phone, setPhone] = useState("") | ||
const [photo, setPhoto] = useState("") | ||
|
||
const addContact = () => { | ||
const generateID = () => { | ||
return (Math.round(Math.random() * 100000000)) | ||
}; | ||
const id = generateID(); | ||
const newContact = {id:id, name: name, email: email, phone_number: phone, image_url: photo}; | ||
setContactData([...contactData, newContact]) | ||
onSubmit(newContact); | ||
navigate("/contacts"); | ||
} | ||
|
||
const goBack = () => { | ||
navigate("/contacts"); | ||
} | ||
|
||
return ( | ||
<div> | ||
<button className='btn btn-dark d-flex float-end' onClick={goBack} type="button" value="Submit">Go Back</button> | ||
<br /> | ||
<h2>Add a New Contact</h2> | ||
<hr /> | ||
<Form id="add-contact-form" onSubmit={addContact}> | ||
<FloatingLabel controlId="floatingInput" label="Name"> | ||
<Form.Control | ||
type="text" | ||
name="name" | ||
placeholder='name' | ||
value={name} | ||
onChange={(e) => setName(e.target.value)} | ||
required> | ||
</Form.Control> | ||
</FloatingLabel> | ||
<FloatingLabel controlId="floatingInput" label="Email Address"> | ||
<Form.Control | ||
// className='was-validated' | ||
type="email" | ||
name="email" | ||
placeholder='email' | ||
value={email} | ||
onChange={(e) => setEmail(e.target.value)} | ||
required> | ||
</Form.Control> | ||
{/* <Form.Control.Feedback type="invalid"> | ||
Please provide a valid email address. | ||
</Form.Control.Feedback> */} | ||
</FloatingLabel> | ||
<FloatingLabel controlId="floatingInput" label="Phone Number"> | ||
<Form.Control | ||
type="tel" | ||
name="phone" | ||
placeholder='xxx-xxx-xxxx' | ||
pattern='[0-9]{3}-[0-9]{3}-[0-9]{4}' | ||
value={phone} | ||
onChange={(e) => setPhone(e.target.value)} | ||
required> | ||
</Form.Control> | ||
<small className='form-text'>format: xxx-xxx-xxxx</small> | ||
</FloatingLabel> | ||
<FloatingLabel controlId="floatingInput" label="Photo URL"> | ||
<Form.Control | ||
type="url" | ||
name="photo" | ||
placeholder='photo' | ||
value={photo} | ||
onChange={(e) => setPhoto(e.target.value)} | ||
required> | ||
</Form.Control> | ||
</FloatingLabel> | ||
<button className='btn btn-dark' type="submit" value="Submit">Submit</button> | ||
</Form> | ||
</div> | ||
); | ||
}; | ||
|
||
// typecheck with proptypes | ||
AddContact.propTypes = { | ||
onSubmit: PropTypes.func.isRequired | ||
} | ||
|
||
export default AddContact; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { useLocation, useNavigate } from "react-router-dom"; | ||
import NotFound from "./NotFound"; | ||
|
||
const ContactItem = () => { | ||
const { state } = useLocation() | ||
const navigate = useNavigate(); | ||
|
||
const goBack = () => { | ||
navigate("/contacts"); | ||
} | ||
|
||
if (state) { | ||
return ( | ||
<div className="text-center mt-5"> | ||
<img className="contactPhoto" src={state.image_url} alt="A photo of the contact" /> | ||
<div>{state.name}</div> | ||
<div>{state.phone_number}</div> | ||
<div>{state.email}</div> | ||
<hr /> | ||
<button className='btn btn-dark' onClick={goBack} type="button" value="Submit">Go Back</button> | ||
</div> | ||
); | ||
} else { | ||
return ( | ||
<NotFound /> | ||
) | ||
} | ||
}; | ||
|
||
export default ContactItem; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import Table from 'react-bootstrap/Table' | ||
import PropTypes from 'prop-types'; | ||
import { Link } from "react-router-dom"; | ||
import { useNavigate } from 'react-router-dom'; | ||
|
||
|
||
const ContactsList = ({contacts}) => { | ||
const navigate = useNavigate(); | ||
|
||
const contactClick = (data) => { | ||
navigate(`/contacts/${data.id}`, {state: data} ) | ||
} | ||
|
||
return ( | ||
<div> | ||
<h2>Contacts List</h2> | ||
|
||
<Link to="new"> | ||
<button className="btn btn-dark">Create New Contact</button> | ||
</Link> | ||
|
||
<hr /> | ||
|
||
<Table striped bordered hover className="table"> | ||
<thead> | ||
<tr> | ||
<th scope="col">Photo</th> | ||
<th scope="col">Name</th> | ||
<th scope="col">Phone</th> | ||
<th scope="col">Email</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
{contacts.map((contact, index) => { | ||
return ( | ||
<tr key={index} onClick={() => contactClick(contact)}> | ||
<td width='250px'> | ||
<img className='contactPhoto' src={contact.image_url} alt="A photo of the contact" /> | ||
</td> | ||
<td>{contact.name}</td> | ||
<td>{contact.phone_number}</td> | ||
<td>{contact.email}</td> | ||
</tr> | ||
); | ||
})} | ||
</tbody> | ||
</Table> | ||
</div> | ||
) | ||
}; | ||
|
||
// typecheck props with propTypes | ||
ContactsList.propTypes = { | ||
contacts: PropTypes.array.isRequired, | ||
} | ||
|
||
export default ContactsList; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
const NotFound = (props) => { | ||
return ( | ||
<div> | ||
<h2>404 Error: <br/> Page Not Found <br/> ¯\_(ツ)_/¯</h2> | ||
</div> | ||
); | ||
}; | ||
|
||
export default NotFound; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Link } from "react-router-dom"; | ||
import { Row, Col } from 'react-bootstrap'; | ||
import gif from './welcome.gif' | ||
|
||
const Home = (props) => { | ||
return ( | ||
<> | ||
<h2>Welcome!</h2> | ||
<img src={gif} className="welcome_gif" alt="A waiving hand and smile" /> | ||
<h5>Choose an Option Below:</h5> | ||
<div style={{display: 'flex', justifyContent: 'start'}}> | ||
<Link to="contacts"> | ||
<button className="btn btn-dark">View Contacts</button> | ||
</Link> | ||
<Link to="contacts/new"> | ||
<button className="btn btn-dark mx-1">Create New Contact</button> | ||
</Link> | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
export default Home; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"contacts": [ | ||
{ | ||
"id": 70219577, | ||
"name": "Albert Einstein", | ||
"image_url": "https://upload.wikimedia.org/wikipedia/commons/thumb/3/3e/Einstein_1921_by_F_Schmutzer_-_restoration.jpg/1280px-Einstein_1921_by_F_Schmutzer_-_restoration.jpg", | ||
"email": "[email protected]", | ||
"phone_number": "949-468-2750" | ||
}, | ||
{ | ||
"id": 70219578, | ||
"name": "Rick Rubin", | ||
"image_url": "https://cdn.britannica.com/98/108998-050-1531FDB8/American-record-producer-Rick-Rubin-2014.jpg", | ||
"email": "[email protected]", | ||
"phone_number": "949-468-2750" | ||
}, | ||
{ | ||
"id": 70219578, | ||
"name": "Giannis Antetokounmpo", | ||
"image_url": "https://imageio.forbes.com/specials-images/imageserve/627bd323672c41ea74c88a13/0x0.jpg?format=jpg&crop=1834,1833,x583,y167,safe&height=416&width=416&fit=bounds", | ||
"email": "[email protected]", | ||
"phone_number": "949-468-2750" | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,35 @@ | ||
body { | ||
margin: 0; | ||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', | ||
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', | ||
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen", | ||
"Ubuntu", "Cantarell", "Fira Sans", "Droid Sans", "Helvetica Neue", | ||
sans-serif; | ||
-webkit-font-smoothing: antialiased; | ||
-moz-osx-font-smoothing: grayscale; | ||
} | ||
|
||
code { | ||
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', | ||
font-family: source-code-pro, Menlo, Monaco, Consolas, "Courier New", | ||
monospace; | ||
} | ||
|
||
:root { | ||
--gradient-color: linear-gradient(to right, #7c4dff, #18a9e6); | ||
} | ||
|
||
.gradient-navbar { | ||
background: var(--gradient-color); | ||
text-indent: 25px; | ||
} | ||
|
||
h1, | ||
button { | ||
font-size: 50px; | ||
background: var(--gradient-color); | ||
-webkit-background-clip: text; | ||
-webkit-text-fill-color: transparent; | ||
} | ||
|
||
button:hover { | ||
background: white; | ||
-webkit-text-fill-color: rgba(255, 255, 255, 0.967); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
You don't need to name props here