feat: Add CMS context, hooks, and types for managing CMS data
- Implemented CmsContext and CmsProvider for global state management of CMS data. - Created a custom hook `useGetCms` to fetch CMS data using React Query. - Defined TypeScript interfaces for CMS response and its related data structures. - Added error handling for data fetching in both the service and context.
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
import api from "@/service/api";
|
||||
import { CmsResponse } from "@/types/TCms";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { useMemo } from "react";
|
||||
export const cmsService = {
|
||||
getCms: async (id: string): Promise<CmsResponse> => {
|
||||
try {
|
||||
return (await api.get(`cms/${id}`)).data;
|
||||
} catch (error) {
|
||||
console.log("Error fetching CMS data:", error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
};
|
||||
export const useGetCms = (pageId: string) => {
|
||||
const queryKey = useMemo(() => ["cms", pageId], [pageId]);
|
||||
return useQuery({
|
||||
queryKey,
|
||||
queryFn: () => cmsService.getCms(pageId),
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user