login , register api / category provider api
This commit is contained in:
34
components/context/categoryprovider.tsx
Normal file
34
components/context/categoryprovider.tsx
Normal file
@@ -0,0 +1,34 @@
|
||||
"use client";
|
||||
import { createContext, useContext, useEffect, useState } from "react";
|
||||
import { getAllCategories } from "@/public/src/services/categories/store";
|
||||
import { Category } from "@/public/src/types/categories";
|
||||
|
||||
interface CategoryContextType {
|
||||
categories: Category[];
|
||||
rootCategories: Category[];
|
||||
}
|
||||
|
||||
const CategoryContext = createContext<CategoryContextType>({
|
||||
categories: [],
|
||||
rootCategories: [],
|
||||
});
|
||||
|
||||
export function CategoryProvider({ children }: { children: React.ReactNode }) {
|
||||
const [categories, setCategories] = useState<Category[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
getAllCategories().then((data) => setCategories(data));
|
||||
}, []);
|
||||
|
||||
const rootCategories = categories.filter((c) => !c.parent);
|
||||
|
||||
return (
|
||||
<CategoryContext.Provider value={{ categories, rootCategories }}>
|
||||
{children}
|
||||
</CategoryContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useCategories() {
|
||||
return useContext(CategoryContext);
|
||||
}
|
||||
Reference in New Issue
Block a user