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

33 lines
797 B
JavaScript

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);
}