diff --git a/components/table/article-table.tsx b/components/table/article-table.tsx index ff15804..319f42a 100644 --- a/components/table/article-table.tsx +++ b/components/table/article-table.tsx @@ -19,7 +19,7 @@ import Cookies from "js-cookie"; import { deleteArticle, getArticleByCategory, - getListArticle, + getArticlePagination, updateIsBannerArticle, } from "@/service/article"; import { @@ -91,9 +91,6 @@ export default function ArticleTable() { useEffect(() => { initState(); - }, [page, showData, startDateValue, selectedCategories]); - - useEffect(() => { getCategories(); }, []); @@ -103,7 +100,7 @@ export default function ArticleTable() { setCategories(data); } - async function initState() { + const initState = useCallback(async () => { loading(); const req = { limit: showData, @@ -116,11 +113,11 @@ export default function ArticleTable() { sort: "desc", sortBy: "created_at", }; - const res = await getListArticle(req); + const res = await getArticlePagination(req); await getTableNumber(parseInt(showData), res.data?.data); setTotalPage(res?.data?.meta?.totalPage); close(); - } + }, [page]); const getTableNumber = async (limit: number, data: Article[]) => { if (data) { diff --git a/service/article.ts b/service/article.ts index 98d1d5b..5d570aa 100644 --- a/service/article.ts +++ b/service/article.ts @@ -1,7 +1,7 @@ import { PaginationRequest } from "@/types/globals"; import Cookies from "js-cookie"; import { httpGet } from "./http-config/http-base-services"; -import { httpDeleteInterceptor, httpPostInterceptor, httpPutInterceptor } from "./http-config/http-interceptor-services"; +import { httpDeleteInterceptor, httpGetInterceptor, httpPostInterceptor, httpPutInterceptor } from "./http-config/http-interceptor-services"; export async function getListArticle(props: PaginationRequest) { const { @@ -29,6 +29,28 @@ export async function getListArticle(props: PaginationRequest) { ); } +export async function getArticlePagination(props: PaginationRequest) { + const { + page, + limit, + search, + startDate, + endDate, + category, + sortBy, + sort, + categorySlug, + isBanner, + } = props; + return await httpGetInterceptor( + `/articles?limit=${limit}&page=${page}&title=${search}&startDate=${startDate || ""}&endDate=${ + endDate || "" + }&categoryId=${category || ""}&sortBy=${sortBy || "created_at"}&sort=${ + sort || "asc" + }&category=${categorySlug || ""}&isBanner=${isBanner || ""}` + ); +} + export async function getTopArticles(props: PaginationRequest) { const { page, limit, search, startDate, endDate, isPublish, category } = props; @@ -75,7 +97,7 @@ export async function deleteArticle(id: string) { } export async function getArticleByCategory() { - return await httpGet(`/article-categories?limit=1000`); + return await httpGetInterceptor(`/article-categories?limit=1000`); } export async function getCategoryPagination(data: any) { return await httpGet( diff --git a/service/http-config/http-base-services.ts b/service/http-config/http-base-services.ts index 92e46f9..f958b02 100644 --- a/service/http-config/http-base-services.ts +++ b/service/http-config/http-base-services.ts @@ -5,8 +5,8 @@ const defaultHeaders = { "X-Client-Key": process.env.MEDOLS_CLIENT_KEY }; -export async function httpGet(pathUrl: any, headers?: any) { - +export async function httpGet(pathUrl: any, headers?: any) { + console.log("X-HEADERS : ", defaultHeaders) const mergedHeaders = { ...defaultHeaders, ...headers, diff --git a/service/http-config/http-interceptor-services.ts b/service/http-config/http-interceptor-services.ts index a9238b6..4dbe657 100644 --- a/service/http-config/http-interceptor-services.ts +++ b/service/http-config/http-interceptor-services.ts @@ -9,8 +9,9 @@ const defaultHeaders = { }; export async function httpGetInterceptor(pathUrl: any) { + console.log("X-HEADERS : ", defaultHeaders) const response = await axiosInterceptorInstance - .get(pathUrl) + .get(pathUrl, { headers: defaultHeaders }) .catch((error) => error.response); console.log("Response interceptor : ", response); if (response?.status == 200 || response?.status == 201) {