From c763a8147e1744de66a1b454b0e92650e47ee39a Mon Sep 17 00:00:00 2001 From: Mayur Borse Date: Fri, 14 May 2021 07:00:18 +0530 Subject: [PATCH] Add WeatherCardDetails component --- src/weather/components/TempBarChart.jsx | 22 +- src/weather/components/WeatherCardDetails.jsx | 201 ++++++++++++++++++ src/weather/components/WeatherCardList.jsx | 83 ++------ src/weather/components/WeatherForecast.jsx | 2 +- 4 files changed, 230 insertions(+), 78 deletions(-) create mode 100644 src/weather/components/WeatherCardDetails.jsx diff --git a/src/weather/components/TempBarChart.jsx b/src/weather/components/TempBarChart.jsx index 35fa2f6..0a9349f 100644 --- a/src/weather/components/TempBarChart.jsx +++ b/src/weather/components/TempBarChart.jsx @@ -9,6 +9,7 @@ import { ResponsiveContainer, } from "recharts"; import { useSelector } from "react-redux"; +import { Typography } from "@material-ui/core"; export default function TempBarChart(props) { const selectedWeatherItem = useSelector( @@ -18,15 +19,18 @@ export default function TempBarChart(props) { return ( <> {selectedWeatherItem && ( - - - - - - - - - + <> + {selectedWeatherItem.date} + + + + + + + + + + )} ); diff --git a/src/weather/components/WeatherCardDetails.jsx b/src/weather/components/WeatherCardDetails.jsx new file mode 100644 index 0000000..4d4af76 --- /dev/null +++ b/src/weather/components/WeatherCardDetails.jsx @@ -0,0 +1,201 @@ +import React from "react"; +import { makeStyles } from "@material-ui/core/styles"; +import { + Divider, + Grid, + Paper, + List, + ListItem, + ListItemText, + ListItemIcon, + Collapse, + Avatar, +} from "@material-ui/core"; +import ExpandLess from "@material-ui/icons/ExpandLess"; +import ExpandMore from "@material-ui/icons/ExpandMore"; +import { useSelector, useDispatch } from "react-redux"; +import { setSelectedWeatherItem } from "../weatherSlice"; + +const useStyles = makeStyles((theme) => ({ + paper: { + width: 280, + }, + listRoot: { + width: "100%", + maxWidth: 280, + }, + nested: { + paddingLeft: theme.spacing(4), + }, +})); + +const WeatherCardDetails = ({ weatherItem, selectedWeatherItem }) => { + const classes = useStyles(); + const dispatch = useDispatch(); + + const tempUnit = useSelector((state) => state.weather.tempUnit); + + const unit = tempUnit === "celsius" ? "°C" : "°F"; + + const [openTemperature, setOpenTemperature] = React.useState(false); + const [openPressure, setOpenPressure] = React.useState(false); + const [openHumidity, setOpenHumidity] = React.useState(false); + + const handleClickTemperature = () => { + setOpenTemperature(!openTemperature); + }; + const handleClickPressure = () => { + setOpenPressure(!openPressure); + }; + const handleClickHumidity = () => { + setOpenHumidity(!openHumidity); + }; + + function getWindDirection(degree) { + const directions = ["N", "NW", "W", "SW", "S", "SE", "E", "NE"]; + const index = + Math.round(((degree %= 360) < 0 ? degree + 360 : degree) / 45) % 8; + return directions[index]; + } + + return ( + + + + { + dispatch( + setSelectedWeatherItem({ + selectedWeatherItem: weatherItem, + }) + ); + }} + > + + + + + + + + + {openTemperature ? : } + + + + + + + + + + + + + + + {openPressure ? : } + + + + + + + + + + + + + + {openHumidity ? : } + + + + + + + + + + + { + dispatch( + setSelectedWeatherItem({ + selectedWeatherItem: weatherItem, + }) + ); + }} + > + + + + + { + dispatch( + setSelectedWeatherItem({ + selectedWeatherItem: weatherItem, + }) + ); + }} + > + + + + + + + ); +}; + +export default WeatherCardDetails; diff --git a/src/weather/components/WeatherCardList.jsx b/src/weather/components/WeatherCardList.jsx index 7d3fb28..b72ae29 100644 --- a/src/weather/components/WeatherCardList.jsx +++ b/src/weather/components/WeatherCardList.jsx @@ -1,19 +1,14 @@ import React, { useEffect } from "react"; import { makeStyles } from "@material-ui/core/styles"; -import { Grid, Paper } from "@material-ui/core"; -import { useSelector, useDispatch } from "react-redux"; +import { Grid } from "@material-ui/core"; +import WeatherCardDetails from "./WeatherCardDetails"; import { setSelectedWeatherItem } from "../weatherSlice"; +import { useSelector, useDispatch } from "react-redux"; const useStyles = makeStyles((theme) => ({ root: { flexGrow: 1, - }, - paper: { - height: 260, - width: 160, - }, - control: { - padding: theme.spacing(2), + marginBottom: theme.spacing(2), }, })); @@ -21,7 +16,6 @@ const WeatherCardList = ({ weatherItems }) => { const classes = useStyles(); const dispatch = useDispatch(); - const tempUnit = useSelector((state) => state.weather.tempUnit); const selectedWeatherItem = useSelector( (state) => state.weather.selectedWeatherItem ); @@ -39,67 +33,20 @@ const WeatherCardList = ({ weatherItems }) => { } }, [weatherItems]); - function getWindDirection(degree) { - const directions = ["N", "NW", "W", "SW", "S", "SE", "E", "NE"]; - const index = - Math.round(((degree %= 360) < 0 ? degree + 360 : degree) / 45) % 8; - return directions[index]; - } - return ( - <> - - - - {weatherItems.map((weatherItem) => ( - - { - dispatch( - setSelectedWeatherItem({ - selectedWeatherItem: weatherItem, - }) - ); - }} - > - - {selectedWeatherItem && - weatherItem.date === selectedWeatherItem.date && - "Selected"} -

- Temp:{" "} - {tempUnit === "celsius" - ? `${weatherItem.average.main.temp}°C` - : `${weatherItem.average.main.temp}°F`} -

-

TrueFeel: {weatherItem.average.main.feels_like}

-

Min: {weatherItem.average.main.temp_min}

-

Max: {weatherItem.average.main.temp_max}

- -

Pressure: {weatherItem.average.main.pressure}hPa

-

Sea Level: {weatherItem.average.main.sea_level}hPa

-

Ground Level: {weatherItem.average.main.grnd_level}hPa

-

Humidity: {weatherItem.average.main.humidity}%

-

- Wind: - {weatherItem.average.wind.speed}mph{" "} - {getWindDirection(weatherItem.average.wind.deg)} -

-

Cloudiness: {weatherItem.average.clouds.all}%

-

Visibility: {weatherItem.average.visibility / 1000}KM

-

Precipitation: {weatherItem.average.pop}

-
-
- ))} -
+ + + + {weatherItems.map((weatherItem) => ( + + ))} - + ); }; diff --git a/src/weather/components/WeatherForecast.jsx b/src/weather/components/WeatherForecast.jsx index 5a29cd4..9e03cbd 100644 --- a/src/weather/components/WeatherForecast.jsx +++ b/src/weather/components/WeatherForecast.jsx @@ -21,7 +21,7 @@ export default function WeatherForecast({ city }) { if (isFetching) return "fetching..."; if (!isSuccess) return "Error while fetching weather"; - const cardsPerPage = 3; + const cardsPerPage = 2; const pageStart = pageIndex * cardsPerPage; const pageEnd = pageStart + cardsPerPage;