-
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
Marina's Bigfoot JSON frontend #37
base: main
Are you sure you want to change the base?
Changes from all commits
46a5edf
9bec16e
7cbff5e
60653c3
d6507a1
8e904e3
0b69098
b9f6cc6
86b6140
91ff1d5
20bcb23
4210ea5
3fc1cbe
b18ca51
2a31420
21d9042
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 |
---|---|---|
@@ -1,20 +1,48 @@ | ||
import React from "react"; | ||
import logo from "./logo.png"; | ||
import bigfoot from "./bigfoot.png"; | ||
import "./App.css"; | ||
import { Button } from "@mui/material"; | ||
import { useNavigate, Link } from "react-router-dom"; | ||
|
||
function AppWrapper() { | ||
const navigate = useNavigate(); | ||
|
||
return <App navigate={navigate} />; | ||
} | ||
|
||
class App extends React.Component { | ||
handleClick = () => { | ||
const navigate = this.props.navigate; | ||
navigate("/sightings"); | ||
}; | ||
|
||
linkStyle = { | ||
textDecoration: "none", | ||
color: "white", | ||
}; | ||
|
||
render() { | ||
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> | ||
<h1>Bigfoot sightings</h1> | ||
<img src={bigfoot} className="bigfoot" alt="bigfoot" width={400} /> | ||
<Link to={"/sightings"} style={this.linkStyle}> | ||
<h4>Click here to view Bigfoot sightings</h4> | ||
</Link> | ||
<Button | ||
variant="standard" | ||
sx={{ backgroundColor: "orange" }} | ||
onClick={this.handleClick} | ||
> | ||
View sightings | ||
</Button> | ||
</header> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default App; | ||
// export default App; | ||
|
||
export default AppWrapper; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from "react"; | ||
import { useNavigate } from "react-router-dom"; | ||
import { Button } from "@mui/material"; | ||
|
||
export default function GoBackButton() { | ||
const navigate = useNavigate(); | ||
const handleGoBack = () => { | ||
navigate(-1); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<Button | ||
variant="standard" | ||
sx={{ | ||
backgroundColor: "orange", | ||
marginTop: 1, | ||
marginLeft: 1, | ||
}} | ||
onClick={handleGoBack} | ||
> | ||
Go back | ||
</Button> | ||
</div> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import React from "react"; | ||
import { Container } from "@mui/material"; | ||
|
||
export default function SearchPage(props) { | ||
const handleSubmit = (e) => { | ||
e.preventDefault(); | ||
const form = e.target; | ||
const query = form.search.value; | ||
Comment on lines
+7
to
+8
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. why not e.target.form.search.value? :D |
||
|
||
if (isNaN(query)) { | ||
props.setSearchParams({ state: query }); | ||
} else { | ||
props.setSearchParams({ year: query }); | ||
} | ||
Comment on lines
+10
to
+14
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. not bad, validating data type here. But why is NaN equal to state? |
||
}; | ||
|
||
return ( | ||
<div> | ||
<p className="query"> | ||
Make your query either by year or by the name of a US state.{" "} | ||
</p> | ||
<Container | ||
sx={{ | ||
display: "flex", | ||
justifyContent: "center", | ||
marginTop: 3, | ||
marginBottom: 3, | ||
Comment on lines
+24
to
+27
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. use your css file for this |
||
}} | ||
> | ||
<form onSubmit={handleSubmit}> | ||
<input | ||
type="search" | ||
name="search" | ||
size="35" | ||
className="input" | ||
placeholder="Enter a year/state name" | ||
></input> | ||
<input type="submit" value="Search" className="input"></input> | ||
</form> | ||
</Container> | ||
</div> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import React from "react"; | ||
import { useParams } from "react-router-dom"; | ||
import { useEffect, useState } from "react"; | ||
import axios from "axios"; | ||
import { Card, CardContent } from "@mui/material"; | ||
import GoBackButton from "./GoBackButton"; | ||
import { BACKEND_URL } from "../constants.js"; | ||
|
||
export default function SightingPage() { | ||
const { REPORT_NUMBER } = useParams(); | ||
const [sighting, setSighting] = useState(); | ||
useEffect(() => { | ||
const fetchSightingData = async () => { | ||
try { | ||
const data = await axios.get( | ||
`${BACKEND_URL}/sightings/${REPORT_NUMBER}` | ||
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. what if report number is undefined or a number? you will still make a request |
||
); | ||
setSighting(data.data); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
}; | ||
fetchSightingData(); | ||
}, [REPORT_NUMBER]); | ||
|
||
const newSighting = sighting ? ( | ||
<Card | ||
sx={{ | ||
width: 800, | ||
minHeight: 300, | ||
display: "flex", | ||
justifyContent: "center", | ||
marginTop: 20, | ||
marginLeft: "auto", | ||
marginRight: "auto", | ||
Comment on lines
+29
to
+35
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. cssssssss |
||
}} | ||
> | ||
<CardContent> | ||
<h4> | ||
{sighting.STATE} {sighting.YEAR} | ||
</h4> | ||
<p>Season: {sighting.SEASON}</p> | ||
<p> | ||
Date: {sighting.MONTH} {sighting.DATE}{" "} | ||
</p> | ||
<p>County: {sighting.COUNTY}</p> | ||
<p>Location: {sighting.LOCATION_DETAILS}</p> | ||
<p>{sighting.OBSERVED}</p> | ||
<p>Report number: {sighting.REPORT_NUMBER}</p> | ||
</CardContent> | ||
Comment on lines
+38
to
+50
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 could almost be a separate component imo |
||
</Card> | ||
) : null; | ||
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. no need for ternary operator. Just use |
||
|
||
return ( | ||
<div className="sighting-page"> | ||
<GoBackButton /> | ||
{newSighting} | ||
</div> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,78 @@ | ||||||||||||||||||||||||||||||||||
import React from "react"; | ||||||||||||||||||||||||||||||||||
import { useState, useEffect } from "react"; | ||||||||||||||||||||||||||||||||||
import axios from "axios"; | ||||||||||||||||||||||||||||||||||
import Card from "@mui/material/Card"; | ||||||||||||||||||||||||||||||||||
import { CardContent, Container } from "@mui/material"; | ||||||||||||||||||||||||||||||||||
import { Link, useSearchParams } from "react-router-dom"; | ||||||||||||||||||||||||||||||||||
import GoBackButton from "./GoBackButton"; | ||||||||||||||||||||||||||||||||||
import { BACKEND_URL } from "../constants.js"; | ||||||||||||||||||||||||||||||||||
import SearchPage from "./SearchPage.js"; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
export default function SightingsList() { | ||||||||||||||||||||||||||||||||||
const [sightings, setSightings] = useState([]); | ||||||||||||||||||||||||||||||||||
const [searchParams, setSearchParams] = useSearchParams(); | ||||||||||||||||||||||||||||||||||
const stateQuery = searchParams.get("state") || ""; | ||||||||||||||||||||||||||||||||||
const yearQuery = searchParams.get("year") || ""; | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
useEffect(() => { | ||||||||||||||||||||||||||||||||||
const fetchData = async () => { | ||||||||||||||||||||||||||||||||||
try { | ||||||||||||||||||||||||||||||||||
const query = {}; | ||||||||||||||||||||||||||||||||||
if (stateQuery) { | ||||||||||||||||||||||||||||||||||
query.state = stateQuery; | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
if (yearQuery) { | ||||||||||||||||||||||||||||||||||
query.year = yearQuery; | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
const { data } = await axios.get(`${BACKEND_URL}/sightings`, { | ||||||||||||||||||||||||||||||||||
params: query, | ||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||
Comment on lines
+20
to
+29
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.
Suggested change
|
||||||||||||||||||||||||||||||||||
setSightings(data); | ||||||||||||||||||||||||||||||||||
} catch (error) { | ||||||||||||||||||||||||||||||||||
console.log(error); | ||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||
}; | ||||||||||||||||||||||||||||||||||
fetchData(); | ||||||||||||||||||||||||||||||||||
}, [stateQuery, yearQuery]); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
console.log(sightings); | ||||||||||||||||||||||||||||||||||
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. remove logs |
||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
const newSightings = sightings.map((sighting, index) => | ||||||||||||||||||||||||||||||||||
sighting.YEAR && sighting.STATE ? ( | ||||||||||||||||||||||||||||||||||
<Link to={`./${sighting.REPORT_NUMBER}`} key={index}> | ||||||||||||||||||||||||||||||||||
<Container | ||||||||||||||||||||||||||||||||||
sx={{ | ||||||||||||||||||||||||||||||||||
display: "flex", | ||||||||||||||||||||||||||||||||||
justifyContent: "center", | ||||||||||||||||||||||||||||||||||
}} | ||||||||||||||||||||||||||||||||||
> | ||||||||||||||||||||||||||||||||||
<Card sx={{ marginBottom: 3, width: 300 }}> | ||||||||||||||||||||||||||||||||||
<CardContent sx={{ display: "flex", justifyContent: "flex-start" }}> | ||||||||||||||||||||||||||||||||||
<p> | ||||||||||||||||||||||||||||||||||
{index + 1}. {sighting.STATE} {sighting.YEAR} | ||||||||||||||||||||||||||||||||||
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 index + 1 doesnt make much sense here. Would put into variable |
||||||||||||||||||||||||||||||||||
</p> | ||||||||||||||||||||||||||||||||||
</CardContent> | ||||||||||||||||||||||||||||||||||
</Card> | ||||||||||||||||||||||||||||||||||
</Container> | ||||||||||||||||||||||||||||||||||
</Link> | ||||||||||||||||||||||||||||||||||
Comment on lines
+42
to
+57
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. Should make this whole thing a component |
||||||||||||||||||||||||||||||||||
) : null | ||||||||||||||||||||||||||||||||||
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. no need for ternary operator when using null for else. Just use && shorthand |
||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||
return ( | ||||||||||||||||||||||||||||||||||
<div> | ||||||||||||||||||||||||||||||||||
<GoBackButton /> | ||||||||||||||||||||||||||||||||||
<Container | ||||||||||||||||||||||||||||||||||
sx={{ | ||||||||||||||||||||||||||||||||||
display: "flex", | ||||||||||||||||||||||||||||||||||
justifyContent: "center", | ||||||||||||||||||||||||||||||||||
marginTop: 3, | ||||||||||||||||||||||||||||||||||
marginBottom: 3, | ||||||||||||||||||||||||||||||||||
}} | ||||||||||||||||||||||||||||||||||
> | ||||||||||||||||||||||||||||||||||
<h1 className="page-title">Bigfoot sightings</h1> | ||||||||||||||||||||||||||||||||||
</Container> | ||||||||||||||||||||||||||||||||||
<SearchPage setSearchParams={setSearchParams} /> | ||||||||||||||||||||||||||||||||||
{newSightings} | ||||||||||||||||||||||||||||||||||
</div> | ||||||||||||||||||||||||||||||||||
); | ||||||||||||||||||||||||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const BACKEND_URL = "http://localhost:3000"; |
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.
could use your css file for this