-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Frost
authored and
Frost
committed
Sep 7, 2024
1 parent
d57830a
commit ae55e6a
Showing
3 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
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,19 @@ | ||
import { useEffect } from "react"; | ||
import { useState } from "react"; | ||
|
||
function useCountries() { | ||
const [countries, setCountries] = useState([]); | ||
useEffect(() => { | ||
const requestCountries = async () => { | ||
const countriesRequest = await fetch( | ||
"https://restcountries.com/v3.1/all" | ||
); | ||
const countriesResponse = await countriesRequest.json(); | ||
setCountries(countriesResponse); | ||
}; | ||
requestCountries(); | ||
}, []); | ||
return [countries]; | ||
} | ||
|
||
export { useCountries }; |
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,21 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
function useCountryInfo(countryName) { | ||
const [countryInfo,setCountryInfo] = useState(null); | ||
useEffect(() => { | ||
let isRequestCanceled = false; | ||
const getCountryDetails = async () => { | ||
if(!isRequestCanceled){ | ||
const COUNTRY_API = `https://restcountries.com/v3.1/name/${countryName}`; | ||
const countryRequest = await fetch(COUNTRY_API); | ||
const countryResponse = await countryRequest.json(); | ||
setCountryInfo(countryResponse[0]); | ||
} | ||
} | ||
getCountryDetails(); | ||
return () => isRequestCanceled = true; | ||
},[countryName]); | ||
return [countryInfo,setCountryInfo]; | ||
} | ||
|
||
export default useCountryInfo; |
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,17 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
function UseDarkMode() { | ||
const userDefaultTheme = (window.matchMedia("(prefers-color-scheme:dark)").matches) ? "dark" : "light"; | ||
const userSetTheme = localStorage.getItem("rest-country-theme"); | ||
const currentTheme = userSetTheme ?? userDefaultTheme; | ||
const [theme,setTheme] = useState(currentTheme); | ||
|
||
useEffect(() => { | ||
document.documentElement.setAttribute("class",theme); | ||
localStorage.setItem("rest-country-theme",theme); | ||
},[theme]); | ||
|
||
return [theme,setTheme]; | ||
} | ||
|
||
export default UseDarkMode; |