-
Notifications
You must be signed in to change notification settings - Fork 22
Countries-React-Ofek #7
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
base: main
Are you sure you want to change the base?
Changes from all commits
d92b7ea
ecc3528
a2ebb62
9e8eb0c
3fa582e
e69f1e7
0b0a5bf
92585b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,13 +2,44 @@ import "../src/assets/css/common.css"; | |
| import "../src/assets/css/details.css"; | ||
| import "../src/assets/css/main.css"; | ||
| import "../src/assets/scss/common.scss"; | ||
| import Home from "./pages/Home"; | ||
|
|
||
| import Home from "./Home.jsx"; | ||
| import {useState} from "react"; | ||
|
|
||
| /** | ||
| * App Component | ||
| * Manages the application, including theme state and persistence. | ||
| * | ||
| * State: | ||
| * - theme: Tracks the current theme ("light" or "dark"), initialized from localStorage. | ||
| * | ||
| */ | ||
| function App() { | ||
| const [theme, setTheme] = useState(() => { | ||
| const savedTheme = localStorage.getItem("theme"); | ||
|
|
||
|
Member
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. You can extract all logic related to the localStorage into storageSession.getIemFromLocalStora(id)etc... |
||
| if (savedTheme) { | ||
| document.body.classList.toggle("dark-theme", savedTheme === "dark"); | ||
| return savedTheme; | ||
|
Member
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. Good usage of the second argument of toggle function |
||
| } | ||
|
|
||
| return "light"; | ||
| }); | ||
|
|
||
|
|
||
| const toggleTheme = () => { | ||
| const isDark = theme === "dark"; | ||
| const newTheme = isDark ? "light" : "dark"; | ||
| setTheme(newTheme); | ||
|
|
||
| document.body.classList.toggle("dark-theme", newTheme === "dark"); | ||
| localStorage.setItem("theme", newTheme); | ||
| }; | ||
|
|
||
| return ( | ||
| <> | ||
| <Home /> | ||
| </> | ||
| <div> | ||
| <Home theme={theme} toggleTheme={toggleTheme}/> | ||
| </div> | ||
| ); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import {useState} from "react"; | ||
| import Country from "./components/Country.jsx"; | ||
| import Header from "./components/Header.jsx"; | ||
| import RegionFilter from "./components/RegionFilter.jsx"; | ||
| import SearchBox from "./components/SearchBox.jsx"; | ||
| import countriesData from './assets/json/CountriesData.json'; | ||
|
|
||
| /** | ||
| * Home Component | ||
| * Manages the main layout, filtering, and display of countries. | ||
| * | ||
| * Props: | ||
| * - theme: Current theme ("light" or "dark"). | ||
| * - toggleTheme: Function to toggle the theme. | ||
| * | ||
| * Functionality: | ||
| * - Displays a list of countries with search and region filtering. | ||
| * - Allows toggling between light and dark themes. | ||
| */ | ||
| const Home = ({theme, toggleTheme}) => { | ||
| const [countries, setCountries] = useState(countriesData); | ||
|
|
||
|
|
||
| const showOneCountryDetails = (countryName) => { | ||
| setCountries(countriesData.filter((country) => country.name === countryName)); | ||
| }; | ||
|
|
||
|
|
||
| const showAllCountries = () => { | ||
| setCountries(countriesData); | ||
|
Member
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. It is not a good practice too add comment that explain what a function of a variable is doing, the code should explain itself |
||
| }; | ||
|
|
||
|
|
||
| const searchByInput = (e) => { | ||
| const query = e.target.value.toLowerCase(); | ||
| if (query === "") { | ||
| setCountries(countriesData); | ||
| } else { | ||
| setCountries(countriesData.filter((country) => | ||
| country.name.toLowerCase().startsWith(query) | ||
| )); | ||
| } | ||
| }; | ||
|
|
||
| const searchByRegion = (e) => { | ||
| const region = e.target.getAttribute("data-region"); | ||
| if (region === 'All') { | ||
| showAllCountries(); | ||
| } else { | ||
| setCountries(countriesData.filter((country) => country.region === region)); | ||
| } | ||
| } | ||
| const oneCountry = 1; | ||
| return ( | ||
| <> | ||
| <Header theme={theme} toggleTheme={toggleTheme}/> | ||
| <section className="filters"> | ||
| <div className="container"> | ||
|
|
||
| {countries.length === oneCountry && ( | ||
| <button className="button-all-countries" onClick={showAllCountries}> | ||
|
Member
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. Do not use magic numbers, extract them into variable with meaningful name. For some one else that does not know your context of what you are working on, they don't know what 1 represents |
||
| Show All Countries | ||
| </button> | ||
| )} | ||
| <SearchBox action={searchByInput}/> | ||
| <RegionFilter action={searchByRegion}/> | ||
| </div> | ||
| </section> | ||
| <div className="countries-grid"> | ||
| {countries.map((country) => ( | ||
| <a | ||
| key={country.name} | ||
| className="country" | ||
| onClick={() => { | ||
| showOneCountryDetails(country.name); | ||
| }}> | ||
| <Country country={country}/> | ||
| </a> | ||
| ))} | ||
| </div> | ||
| </> | ||
| ); | ||
| }; | ||
|
|
||
| export default Home; | ||
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.
Please don`t use comments in hebrew