card requisite done

This commit is contained in:
2025-07-09 10:14:40 +05:00
parent 7ce0b92f92
commit 90d9a7b309
12 changed files with 614 additions and 29 deletions

View File

@@ -6,6 +6,8 @@ const BaseEnumsContext = createContext({ enums: null, refresh: () => {}, getEnum
export const BaseEnumsProvider = ({ children }) => {
const [enums, setEnums] = useState(null);
const [lastFetched, setLastFetched] = useState(0);
const [branchesByRegion, setBranchesByRegion] = useState({});
const [branchesFetchedAt, setBranchesFetchedAt] = useState(0);
const fetchEnums = async () => {
try {
@@ -18,11 +20,24 @@ export const BaseEnumsProvider = ({ children }) => {
}
};
const fetchBranches = async () => {
try {
const res = await fetch(`${API_CONFIG.BASE_URL}/branches?groupBy=region`);
const json = await res.json();
setBranchesByRegion(json || {});
setBranchesFetchedAt(Date.now());
} catch (e) {
console.warn('Failed to fetch branches', e.message);
}
};
// initial fetch and 60s refresh
useEffect(() => {
fetchEnums();
fetchBranches();
const id = setInterval(fetchEnums, 60000);
return () => clearInterval(id);
const idB = setInterval(fetchBranches, 60000);
return () => { clearInterval(id); clearInterval(idB);} ;
}, []);
const getEnums = async () => {
@@ -42,8 +57,15 @@ export const BaseEnumsProvider = ({ children }) => {
return Object.entries(enums[category]).map(([value, label]) => ({ value, label }));
};
const getBranches = async (regionKey) => {
if (Date.now() - branchesFetchedAt > 60000) {
await fetchBranches();
}
return regionKey ? branchesByRegion[regionKey] || [] : [];
};
return (
<BaseEnumsContext.Provider value={{ enums, refresh: fetchEnums, getEnums, getLabel, getOptions }}>
<BaseEnumsContext.Provider value={{ enums, refresh: fetchEnums, getEnums, getLabel, getOptions, getBranches }}>
{children}
</BaseEnumsContext.Provider>
);