-
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.
Merge pull request #378 from bounswe/3design_mobile
Merge Finalized Version Of Mobile Into main Branch
- Loading branch information
Showing
15 changed files
with
1,424 additions
and
331 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
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
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
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 |
---|---|---|
@@ -1,13 +1,54 @@ | ||
import React, { createContext, useState } from 'react'; | ||
import React, { createContext, useState, useEffect, useContext } from 'react'; | ||
import axios from 'axios'; | ||
import { AuthContext } from './AuthContext'; | ||
|
||
export const CategoryContext = createContext(); | ||
export const CategoryContext = createContext({ | ||
categories: [], | ||
loading: true, | ||
error: null, | ||
}); | ||
|
||
export function CategoryProvider({ children }) { | ||
const [category, setCategory] = useState(null); | ||
export const CategoryProvider = ({ children }) => { | ||
const [categories, setCategories] = useState([]); | ||
const [loading, setLoading] = useState(true); | ||
const [error, setError] = useState(null); | ||
const { user } = useContext(AuthContext); | ||
|
||
useEffect(() => { | ||
if (user?.accessToken) { | ||
const fetchCategories = async () => { | ||
try { | ||
const response = await axios.get( | ||
`${process.env.EXPO_PUBLIC_VITE_API_URL}/api/v1/categories`, | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${user.accessToken}`, | ||
'Content-Type': 'multipart/form-data', | ||
}, | ||
} | ||
); | ||
|
||
const transformedCategories = response.data.map((category) => ({ | ||
label: category.name, | ||
value: category.id, | ||
})); | ||
setCategories(transformedCategories); | ||
setLoading(false); | ||
} catch (err) { | ||
setError('Failed to load categories'); | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
fetchCategories(); | ||
} | ||
}, [user?.accessToken]); | ||
|
||
return ( | ||
<CategoryContext.Provider value={{ category, setCategory }}> | ||
<CategoryContext.Provider value={{ categories, loading, error }}> | ||
{children} | ||
</CategoryContext.Provider> | ||
); | ||
} | ||
}; | ||
|
||
export const useCategories = () => useContext(CategoryContext); |
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
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
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
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
Oops, something went wrong.