Skip to content

Commit 9ceb853

Browse files
committed
Change from code computer
1 parent 041e2a1 commit 9ceb853

File tree

4 files changed

+66
-47
lines changed

4 files changed

+66
-47
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { createContext, useContext, useEffect, useState } from "react";
2+
3+
const CitiesContext = createContext();
4+
const BASE_URL = "http://localhost:9000";
5+
6+
const CitiesProvider = ({ children }) => {
7+
const [cities, setCities] = useState([]);
8+
const [isLoading, setIsLoading] = useState(false);
9+
10+
useEffect(() => {
11+
const fetchCities = async () => {
12+
try {
13+
setIsLoading(true);
14+
const res = await fetch(`${BASE_URL}/cities`);
15+
const data = await res.json();
16+
setCities(data);
17+
} catch {
18+
alert("There was an error loading data...");
19+
} finally {
20+
setIsLoading(false);
21+
}
22+
};
23+
24+
fetchCities();
25+
}, []);
26+
27+
return (
28+
<CitiesContext.Provider value={{ cities, isLoading }}>
29+
{children}
30+
</CitiesContext.Provider>
31+
);
32+
};
33+
34+
function useCities() {
35+
const context = useContext(CitiesContext);
36+
return context;
37+
}
38+
39+
export { CitiesProvider, useCities };

11-worldwise/starter/src/App.jsx

Lines changed: 19 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { useEffect, useState } from "react";
21
import { BrowserRouter, Navigate, Route, Routes } from "react-router-dom";
32

43
import Homepage from "./pages/Homepage/Homepage";
@@ -11,53 +10,28 @@ import CityList from "./components/City/CityList";
1110
import CountryList from "./components/Country/CountryList";
1211
import City from "./components/City/City";
1312
import Form from "./components/Form/Form";
14-
15-
const BASE_URL = "http://localhost:9000";
13+
import { CitiesProvider } from "../contexts/CitiesContext";
1614

1715
const 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

11-worldwise/starter/src/components/City/CityList.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import { useCities } from "../../../contexts/CitiesContext";
12
import Message from "../Message/Message";
23
import Spinner from "../Spinner/Spinner";
34
import CityItem from "./CityItem";
45
import styles from "./CityList.module.css";
56

6-
const CityList = ({ cities, isLoading }) => {
7+
const CityList = () => {
8+
const { cities, isLoading } = useCities();
9+
710
if (isLoading) return <Spinner />;
811

912
if (!cities.length)

11-worldwise/starter/src/components/Country/CountryList.jsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
import { useCities } from "../../../contexts/CitiesContext";
12
import Message from "../Message/Message";
23
import Spinner from "../Spinner/Spinner";
34
import CountryItem from "./CountryItem";
45
import styles from "./CountryList.module.css";
56

6-
const CountryList = ({ cities, isLoading }) => {
7+
const CountryList = () => {
8+
const { cities, isLoading } = useCities();
9+
710
if (isLoading) return <Spinner />;
811

912
if (!cities.length)

0 commit comments

Comments
 (0)