-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#13 Create context provider for traveler data
- Loading branch information
1 parent
e4c1adb
commit 49c0e02
Showing
1 changed file
with
45 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,45 @@ | ||
import { createContext, useContext, useEffect, useState } from "react"; | ||
import { axiosReq } from "../api/axiosDefaults"; | ||
import { useCurrentUser } from "./CurrentUserContext"; | ||
|
||
export const TravelerDataContext = createContext(); | ||
export const SetTravelerDataContext = createContext(); | ||
|
||
export const useTravelerData = () => useContext(TravelerDataContext); | ||
export const useSetTravelerDataContext = () => | ||
useContext(SetTravelerDataContext); | ||
|
||
export const TravelerDataProvider = ({ children }) => { | ||
const [travelerData, setTravelerData] = useState({ | ||
pageTraveler: { results: [] }, | ||
popularTravelers: { results: [] }, | ||
}); | ||
const { popularTravelers } = travelerData; | ||
const currentUser = useCurrentUser(); | ||
|
||
useEffect(() => { | ||
const handleMount = async () => { | ||
try { | ||
const { data } = await axiosReq.get( | ||
"/travelers/?ordering=-followers_count" | ||
); | ||
setTravelerData((prevState) => ({ | ||
...prevState, | ||
popularTravelers: data, | ||
})); | ||
} catch (err) { | ||
console.log(err); | ||
} | ||
}; | ||
|
||
handleMount(); | ||
}, [currentUser]); | ||
}; | ||
|
||
return ( | ||
<TravelerDataContext.Provider value={travelerData}> | ||
<SetTravelerDataContext.Provider value={setTravelerData}> | ||
{children} | ||
</SetTravelerDataContext.Provider> | ||
</TravelerDataContext.Provider> | ||
) |