efbdb82535
- 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.
22 lines
608 B
TypeScript
22 lines
608 B
TypeScript
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),
|
|
});
|
|
};
|