sms-extension-1777783872/client/src/context/BrandContext.jsx
2026-03-26 19:15:08 +05:30

34 lines
855 B
JavaScript

/* eslint-disable react-refresh/only-export-components */
import { createContext, useContext, useState, useEffect, useCallback } from 'react';
import apiClient from '../api/client';
const BrandContext = createContext(null);
export function BrandProvider({ children }) {
const [brand, setBrand] = useState(null);
const [loading, setLoading] = useState(true);
const fetchBrand = useCallback(async () => {
try {
const res = await apiClient.get('/api/brand');
setBrand(res.data);
} catch {
setBrand(null);
} finally {
setLoading(false);
}
}, []);
useEffect(() => { fetchBrand(); }, [fetchBrand]);
return (
<BrandContext.Provider value={{ brand, loading, refetch: fetchBrand }}>
{children}
</BrandContext.Provider>
);
}
export function useBrand() {
return useContext(BrandContext);
}