Skip to content
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

Display continent details and list of countries #3

Merged
16 changes: 15 additions & 1 deletion src/App.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { useEffect } from 'react';
import { useDispatch } from 'react-redux';
import { Route, Routes } from 'react-router-dom';
import { Route, Routes, useLocation } from 'react-router-dom';

import { fetchCountries, fetchTotalData } from './redux/covidSlice';

import Nav from './components/Nav/Nav';
import Home from './components/Home/Home';
import ContinentDetails from './components/ContinentDetails/ContinentDetails';

const App = () => {
const dispatch = useDispatch();
Expand All @@ -14,12 +15,25 @@ const App = () => {
dispatch(fetchTotalData());
}, [dispatch]);

const location = useLocation();
useEffect(() => {
window.scrollTo({
top: 0,
left: 0,
behavior: 'smooth',
});
}, [location]);

return (
<>
<Nav />
<div className="mt-16 px-12 pt-6">
<Routes>
<Route path="/" element={<Home />} />
<Route
path="/continent/:continentName"
element={<ContinentDetails />}
/>
</Routes>
</div>
</>
Expand Down
4 changes: 3 additions & 1 deletion src/components/Continent/Continent.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { Link } from 'react-router-dom';
import PropTypes from 'prop-types';
import { BsFillArrowUpRightCircleFill } from 'react-icons/bs';
import images from '../../images';

const Continent = ({ continent: { continent, cases } }) => (
<Link
to={`/continent/${continent}`}
className="md:flex-row flex flex-col px-3 py-10 hover:bg-gray-600 justify-between group h-full"
className="md:flex-row flex flex-col px-3 py-10 hover:bg-gray-600 justify-between group h-full relative"
>
<BsFillArrowUpRightCircleFill className="text-blue-800 right-2 top-2 absolute text-lg group-hover:text-white" />
<img
src={images[continent]}
alt="continent map"
Expand Down
59 changes: 59 additions & 0 deletions src/components/ContinentDetails/ContinentDetails.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* eslint-disable implicit-arrow-linebreak, function-paren-newline */
import { useSelector } from 'react-redux';
import { useParams } from 'react-router-dom';
import CountUp from 'react-countup';
import images from '../../images';

import Spinner from '../Spinner/Spinner';
import CountryList from '../CountryList/CountryList';

const ContinentDetails = () => {
const { continentName } = useParams();
const continentDetails = useSelector((state) =>
state.covid.continents.find((cont) => cont.continent === continentName),
);
const countries = useSelector((state) =>
state.covid.countries.filter(
(country) => country.continent === continentName,
),
);

return (
<>
{!continentDetails && <Spinner />}
{continentDetails && (
<>
<div className="flex md:flex-row flex-col gap-6 md:gap-3 items-center w-full justify-center pb-16 pt-6 px-3 bg-blue-dark rounded-t-lg">
<img
src={images[continentName]}
alt="continent map"
className="w-40 invert"
/>
<div className="flex flex-col font-bold text-2xl text-white">
<span className="text-center md:text-left mb-4 text-4xl">
{continentDetails.continent}
</span>
<CountUp
delay={1}
end={continentDetails.cases}
separator=","
prefix="Total Cases: "
duration={3}
/>
<CountUp
delay={1}
prefix="Total Deaths: "
end={continentDetails.deaths}
separator=","
duration={3}
/>
</div>
</div>
<CountryList countries={countries} />
</>
)}
</>
);
};

export default ContinentDetails;
18 changes: 8 additions & 10 deletions src/components/Country/Country.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
/* eslint-disable object-curly-newline */
/* eslint-disable object-curly-newline, implicit-arrow-linebreak */
import PropTypes from 'prop-types';

const Country = ({
country: {
country,
continent,
cases,
countryInfo: { flag },
},
index,
}) => {
const bgColor = (fade = 'ff') => `#${
(index + 1) % 4 === 2 || (index + 1) % 4 === 3 ? '7c9edf' : '708dc2'
}${fade}`;
const bgColor = (fade = 'ff') =>
`#${
(index + 1) % 4 === 2 || (index + 1) % 4 === 3 ? '7c9edf' : '708dc2'
}${fade}`;
return (
<div
className="flex flex-col px-3 py-10 bg-no-repeat"
className="flex flex-col px-3 py-10 bg-no-repeat relative"
style={{
backgroundImage: `linear-gradient(to right, #ffffff00, ${bgColor(
'aa',
Expand All @@ -24,9 +24,8 @@ const Country = ({
}}
>
<div className="flex flex-col gap-2 self-end font-bold text-white items-end">
<h4 className="flex flex-col items-end">
<span className="text-2xl">{`${country}, \n`}</span>
<span className="text-sm">{continent}</span>
<h4 className="flex flex-col items-end text-right">
<span className="text-2xl">{country}</span>
</h4>
<span className="text-xl">{cases.toLocaleString()}</span>
</div>
Expand All @@ -37,7 +36,6 @@ const Country = ({
Country.propTypes = {
country: PropTypes.shape({
country: PropTypes.string.isRequired,
continent: PropTypes.string.isRequired,
cases: PropTypes.number.isRequired,
countryInfo: PropTypes.shape({
flag: PropTypes.string.isRequired,
Expand Down
2 changes: 1 addition & 1 deletion src/components/CountryList/CountryList.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import styles from './CountryList.module.css';
import Country from '../Country/Country';

const CountryList = ({ countries }) => (
<ul className="grid grid-cols-2 rounded-lg overflow-hidden gap-0.5">
<ul className="grid md:grid-cols-2 rounded-lg overflow-hidden gap-0.5">
{countries.map((country, index) => (
<li key={country.country} className={styles['country-card']}>
<Country country={country} index={index} />
Expand Down
33 changes: 0 additions & 33 deletions src/components/Home/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,14 @@ import { FcGlobe } from 'react-icons/fc';

import Spinner from '../Spinner/Spinner';
import ContinentList from '../ContinentList/ContinentList';
// import CountryList from '../CountryList/CountryList';

const Home = () => {
// const {
// countries,
// totalData: { totalCases, totalDeaths },
// } = useSelector((state) => state.covid);
const {
continents,
totalData: { totalCases, totalDeaths },
} = useSelector((state) => state.covid);

return (
// <>
// {!countries.length && <Spinner />}
// {countries.length > 0 && (
// <>
// <div className="flex gap-3 items-center w-full justify-center
// pb-16 pt-6 bg-blue-dark rounded-t-lg">
// <FcGlobe className="text-8xl bg-white rounded-full" />
// <div className="flex flex-col font-bold text-2xl text-white">
// <CountUp
// delay={1}
// end={totalCases}
// separator=","
// prefix="Total Cases: "
// duration={3}
// />
// <CountUp
// delay={1}
// prefix="Total Deaths: "
// end={totalDeaths}
// separator=","
// duration={3}
// />
// </div>
// </div>
// <CountryList countries={countries} />
// </>
// )}
// </>
<>
{!continents.length && <Spinner />}
{continents.length > 0 && (
Expand Down
28 changes: 23 additions & 5 deletions src/components/Nav/Nav.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
const Nav = () => (
<nav className="z-10 fixed top-0 w-screen py-3 px-6 text-center bg-blue-dark text-white tracking-widest shadow-xl">
<h1 className="text-2xl font-extrabold">Covid Tracker</h1>
</nav>
);
import { IoIosArrowBack } from 'react-icons/io';
import { useNavigate, useLocation } from 'react-router-dom';

const Nav = () => {
const navigate = useNavigate();
const location = useLocation();

return (
<nav className="z-10 fixed top-0 w-screen py-3 px-6 text-center bg-blue-dark text-white tracking-widest shadow-xl">
{location.pathname !== '/' && (
<div className="absolute text-3xl hover:bg-blue-900 cursor-pointer left-0 top-0 h-full flex items-center pl-6 pr-2">
<IoIosArrowBack
onClick={() => {
navigate(-1);
}}
/>
</div>
)}

<h1 className="text-2xl font-extrabold">Covid Tracker</h1>
</nav>
);
};

export default Nav;
2 changes: 2 additions & 0 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ body {
line-height: 1.5;
background-color: #5485e2;
font-family: GillSans, sans-serif;
scroll-behavior: smooth;
overflow-x: hidden;
}

/* A elements that don't have a class get default styles */
Expand Down