1- import { useEffect , useState } from "react" ;
21import { BrowserRouter , Navigate , Route , Routes } from "react-router-dom" ;
32
43import Homepage from "./pages/Homepage/Homepage" ;
@@ -11,53 +10,28 @@ import CityList from "./components/City/CityList";
1110import CountryList from "./components/Country/CountryList" ;
1211import City from "./components/City/City" ;
1312import Form from "./components/Form/Form" ;
14-
15- const BASE_URL = "http://localhost:9000" ;
13+ import { CitiesProvider } from "../contexts/CitiesContext" ;
1614
1715const App = ( ) => {
18- const [ cities , setCities ] = useState ( [ ] ) ;
19- const [ isLoading , setIsLoading ] = useState ( false ) ;
20-
21- useEffect ( ( ) => {
22- const fetchCities = async ( ) => {
23- try {
24- setIsLoading ( true ) ;
25- const res = await fetch ( `${ BASE_URL } /cities` ) ;
26- const data = await res . json ( ) ;
27- setCities ( data ) ;
28- } catch {
29- alert ( "There was an error loading data..." ) ;
30- } finally {
31- setIsLoading ( false ) ;
32- }
33- } ;
34-
35- fetchCities ( ) ;
36- } , [ ] ) ;
37-
3816 return (
39- < BrowserRouter >
40- < Routes >
41- < Route index element = { < Homepage /> } />
42- < Route path = "products" element = { < Products /> } />
43- < Route path = "pricing" element = { < Pricing /> } />
44- < Route path = "login" element = { < Login /> } />
45- < Route path = "app" element = { < AppLayout /> } >
46- < Route index element = { < Navigate replace to = "cities" /> } />
47- < Route
48- path = "cities"
49- element = { < CityList cities = { cities } isLoading = { isLoading } /> }
50- />
51- < Route path = "cities/:id" element = { < City /> } />
52- < Route
53- path = "countries"
54- element = { < CountryList cities = { cities } isLoading = { isLoading } /> }
55- />
56- < Route path = "form" element = { < Form /> } />
57- </ Route >
58- < Route path = "*" element = { < PageNotFound /> } />
59- </ Routes >
60- </ BrowserRouter >
17+ < CitiesProvider >
18+ < BrowserRouter >
19+ < Routes >
20+ < Route index element = { < Homepage /> } />
21+ < Route path = "products" element = { < Products /> } />
22+ < Route path = "pricing" element = { < Pricing /> } />
23+ < Route path = "login" element = { < Login /> } />
24+ < Route path = "app" element = { < AppLayout /> } >
25+ < Route index element = { < Navigate replace to = "cities" /> } />
26+ < Route path = "cities" element = { < CityList /> } />
27+ < Route path = "cities/:id" element = { < City /> } />
28+ < Route path = "countries" element = { < CountryList /> } />
29+ < Route path = "form" element = { < Form /> } />
30+ </ Route >
31+ < Route path = "*" element = { < PageNotFound /> } />
32+ </ Routes >
33+ </ BrowserRouter >
34+ </ CitiesProvider >
6135 ) ;
6236} ;
6337
0 commit comments