diff --git a/components/form/article/create-article-form.tsx b/components/form/article/create-article-form.tsx index cfe4e28..1f6b448 100644 --- a/components/form/article/create-article-form.tsx +++ b/components/form/article/create-article-form.tsx @@ -27,7 +27,7 @@ import { saveManualContext, updateManualArticle, } from "@/service/generate-article"; -import { getUserLevels } from "@/service/user-levels/user-levels-service"; +import { getUserLevels } from "@/service/user-levels-service"; import { Checkbox } from "@/components/ui/checkbox"; import { Button } from "@/components/ui/button"; import { getCategoryById } from "@/service/master-categories"; diff --git a/components/form/form-master-user-edit.tsx b/components/form/form-master-user-edit.tsx index 75c3b5f..75b2654 100644 --- a/components/form/form-master-user-edit.tsx +++ b/components/form/form-master-user-edit.tsx @@ -12,7 +12,7 @@ import { z } from "zod"; import ReactSelect from "react-select"; import makeAnimated from "react-select/animated"; import { editMasterUsers, getDetailMasterUsers } from "@/service/master-user"; -import { getAllUserLevels } from "@/service/user-levels/user-levels-service"; +import { getAllUserLevels } from "@/service/user-levels-service"; import { listUserRole } from "@/service/master-user-role"; import { Card } from "../ui/card"; import { Label } from "../ui/label"; diff --git a/components/form/form-master-user.tsx b/components/form/form-master-user.tsx index de4a3d9..3484c09 100644 --- a/components/form/form-master-user.tsx +++ b/components/form/form-master-user.tsx @@ -13,7 +13,7 @@ import ReactSelect from "react-select"; import makeAnimated from "react-select/animated"; import { zodResolver } from "@hookform/resolvers/zod"; import { createMasterUser } from "@/service/master-user"; -import { getAllUserLevels } from "@/service/user-levels/user-levels-service"; +import { getAllUserLevels } from "@/service/user-levels-service"; import { listUserRole } from "@/service/master-user-role"; import { Card } from "../ui/card"; import { Input } from "../ui/input"; diff --git a/components/form/login.tsx b/components/form/login.tsx index 7eeee15..42e3829 100644 --- a/components/form/login.tsx +++ b/components/form/login.tsx @@ -78,13 +78,13 @@ export default function Login() { secure: true, sameSite: "strict", }); - const resActivity = await saveActivity( + await saveActivity( { activityTypeId: 1, url: "https://dev.mikulnews.com/auth", userId: profile?.data?.data?.id, }, - accessData?.id_token + response?.data?.data?.access_token ); Cookies.set("profile_picture", profile?.data?.data?.profilePictureUrl, { expires: 1, diff --git a/components/table/advertise/advertise-table.tsx b/components/table/advertise/advertise-table.tsx index 7213f83..ee07aa4 100644 --- a/components/table/advertise/advertise-table.tsx +++ b/components/table/advertise/advertise-table.tsx @@ -27,12 +27,12 @@ import Image from "next/image"; import { Switch } from "@/components/ui/switch"; import useDisclosure from "@/components/useDisclosure"; import { - createAdvertiseById, createMediaFileAdvertise, deleteAdvertise, editAdvertise, editAdvertiseIsActive, getAdvertise, + getAdvertiseById, } from "@/service/advertisement"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; @@ -226,7 +226,7 @@ export default function AdvertiseTable(props: { triggerRefresh: boolean }) { }; const openModal = async (id: number) => { - const res = await createAdvertiseById(Number(id)); + const res = await getAdvertiseById(Number(id)); const data = res?.data?.data; setValue("id", String(data?.id)); setValue("title", data?.title); diff --git a/service/activity-log.ts b/service/activity-log.ts index 2056d6f..98634ee 100644 --- a/service/activity-log.ts +++ b/service/activity-log.ts @@ -1,5 +1,4 @@ -import { PaginationRequest } from "@/types/globals"; -import { httpGet, httpPost, httpPut } from "./http-config/axios-base-service"; +import { httpGet, httpPost } from "./http-config/http-base-services"; export async function saveActivity(data: any, token?: string) { const headers = token @@ -11,11 +10,10 @@ export async function saveActivity(data: any, token?: string) { "content-type": "application/json", }; const pathUrl = `/activity-logs`; - return await httpPost(pathUrl, headers, data); + return await httpPost(pathUrl, data, headers); } export async function getActivity() { - const headers = { "content-type": "application/json" }; const pathUrl = `/activity-logs/statistics`; - return await httpGet(pathUrl, headers); + return await httpGet(pathUrl); } diff --git a/service/advertisement.ts b/service/advertisement.ts index fe5fa69..90b5123 100644 --- a/service/advertisement.ts +++ b/service/advertisement.ts @@ -1,68 +1,42 @@ -import { - httpDeleteInterceptor, - httpGet, - httpPost, - httpPut, -} from "./http-config/axios-base-service"; import Cookies from "js-cookie"; +import { httpDeleteInterceptor, httpPostInterceptor, httpPutInterceptor } from "./http-config/http-interceptor-services"; +import { httpGet } from "./http-config/http-base-services"; const token = Cookies.get("access_token"); export async function createAdvertise(data: any) { - const headers = { - "content-type": "application/json", - Authorization: `Bearer ${token}`, - }; const pathUrl = `/advertisement`; - return await httpPost(pathUrl, headers, data); + return await httpPostInterceptor(pathUrl, data); } export async function createMediaFileAdvertise(id: string | number, data: any) { const headers = { "content-type": "multipart/form-data", }; const pathUrl = `/advertisement/upload/${id}`; - return await httpPost(pathUrl, headers, data); + return await httpPostInterceptor(pathUrl, data, headers); } export async function getAdvertise(data: any) { - const headers = { - "content-type": "application/json", - }; - const pathUrl = `/advertisement?page=${data?.page || 1}&limit=${ - data?.limit || "" - }&placement=${data?.placement || ""}&isPublish=${data.isPublish || ""}`; - return await httpGet(pathUrl, headers); + const pathUrl = `/advertisement?page=${data?.page || 1}&limit=${data?.limit || ""}&placement=${data?.placement || ""}&isPublish=${data.isPublish || ""}`; + return await httpGet(pathUrl); } -export async function createAdvertiseById(id: number) { - const headers = { - "content-type": "application/json", - }; +export async function getAdvertiseById(id: number) { const pathUrl = `/advertisement/${id}`; - return await httpGet(pathUrl, headers); + return await httpGet(pathUrl); } export async function editAdvertise(data: any) { - const headers = { - "content-type": "application/json", - }; const pathUrl = `/advertisement/${data?.id}`; - return await httpPut(pathUrl, headers, data); + return await httpPutInterceptor(pathUrl, data); } export async function editAdvertiseIsActive(data: any) { - const headers = { - "content-type": "application/json", - Authorization: `Bearer ${token}`, - }; const pathUrl = `/advertisement/publish/${data?.id}?isPublish=${data?.isActive}`; - return await httpPut(pathUrl, headers); + return await httpPutInterceptor(pathUrl, data); } export async function deleteAdvertise(id: number) { - const headers = { - "content-type": "application/json", - }; const pathUrl = `/advertisement/${id}`; - return await httpDeleteInterceptor(pathUrl, headers); + return await httpDeleteInterceptor(pathUrl); } diff --git a/service/article.ts b/service/article.ts index 5d570aa..8faddf4 100644 --- a/service/article.ts +++ b/service/article.ts @@ -1,5 +1,4 @@ import { PaginationRequest } from "@/types/globals"; -import Cookies from "js-cookie"; import { httpGet } from "./http-config/http-base-services"; import { httpDeleteInterceptor, httpGetInterceptor, httpPostInterceptor, httpPutInterceptor } from "./http-config/http-interceptor-services"; diff --git a/service/http-config/axios-base-instance.ts b/service/http-config/axios-base-instance.ts index 03f30f6..de3efbe 100644 --- a/service/http-config/axios-base-instance.ts +++ b/service/http-config/axios-base-instance.ts @@ -5,7 +5,8 @@ const baseURL = "https://dev.mikulnews.com/api"; const axiosBaseInstance = axios.create({ baseURL, headers: { - "content-type": "application/json", + "Content-Type": "application/json", + "X-Client-Key": "bb65b1ad-e954-4a1a-b4d0-74df5bb0b640" }, }); diff --git a/service/http-config/axios-base-service.ts b/service/http-config/axios-base-service.ts deleted file mode 100644 index c33fd16..0000000 --- a/service/http-config/axios-base-service.ts +++ /dev/null @@ -1,154 +0,0 @@ -import { getCsrfToken } from "../master-user"; -import axiosInterceptorInstance from "./axios-interceptor-instance"; -import axiosBaseInstance from "./http-base-instance"; -import mediahubBaseInstance from "./mediahub-base-service"; -import Cookies from "js-cookie"; - -const defaultHeaders = { - "Content-Type": "application/json", -}; - -export async function httpPost(pathUrl: any, headers: any, data?: any) { - const resCsrf = await getCsrfToken(); - const csrfToken = resCsrf?.data?.csrf_token; - - const mergedHeaders = { - ...defaultHeaders, - ...headers, - ...(csrfToken ? { "X-CSRF-TOKEN": csrfToken } : {}), - }; - - const response = await axiosBaseInstance - .post(pathUrl, data, { headers: mergedHeaders }) - .catch(function (error: any) { - console.log(error); - return error.response; - }); - console.log("Response base svc : ", response); - if (response?.status == 200 || response?.status == 201) { - return { - error: false, - message: "success", - data: response?.data, - }; - } else { - return { - error: true, - message: response?.data?.message || response?.data || null, - data: null, - }; - } -} - -export async function httpGet(pathUrl: any, headers: any) { - const response = await axiosInterceptorInstance - .get(pathUrl, { headers }) - .catch(function (error: any) { - console.log(error); - return error.response; - }); - console.log("Response base svc : ", response); - if (response?.status == 200 || response?.status == 201) { - return { - error: false, - message: "success", - data: response?.data, - }; - } else { - return { - error: true, - message: response?.data?.message || response?.data || null, - data: null, - }; - } -} - -export async function httpPut(pathUrl: any, headers: any, data?: any) { - const resCsrf = await getCsrfToken(); - const csrfToken = resCsrf?.data?.csrf_token; - - const defaultHeaders = { - "Content-Type": "application/json", - }; - const mergedHeaders = { - ...defaultHeaders, - ...headers, - ...(csrfToken ? { "X-CSRF-TOKEN": csrfToken } : {}), - }; - - const response = await axiosBaseInstance - .put(pathUrl, data, { headers: mergedHeaders }) - .catch(function (error: any) { - console.log(error); - return error.response; - }); - console.log("Response base svc : ", response); - if (response?.status == 200 || response?.status == 201) { - return { - error: false, - message: "success", - data: response?.data, - }; - } else { - return { - error: true, - message: response?.data?.message || response?.data || null, - data: null, - }; - } -} - -export async function httpDeleteInterceptor(pathUrl: any, headers: any) { - const resCsrf = await getCsrfToken(); - const csrfToken = resCsrf?.data?.csrf_token; - - const defaultHeaders = { - "Content-Type": "application/json", - }; - const mergedHeaders = { - ...defaultHeaders, - ...headers, - ...(csrfToken ? { "X-CSRF-TOKEN": csrfToken } : {}), - }; - - const response = await axiosBaseInstance - .delete(pathUrl, { headers: mergedHeaders }) - .catch((error) => error.response); - console.log("Response interceptor : ", response); - if (response?.status == 200 || response?.status == 201) { - return { - error: false, - message: "success", - data: response?.data, - }; - } else { - return { - error: true, - message: response?.data?.message || response?.data || null, - data: null, - }; - } -} - -export async function mediahubGet(pathUrl: any, headers: any) { - const response = await mediahubBaseInstance - .get(pathUrl, { headers }) - .catch(function (error: any) { - console.log(error); - return error.response; - }); - console.log("Response base svc : ", response); - if (response?.status == 200 || response?.status == 201) { - return { - error: false, - message: "success", - data: response?.data, - }; - } else { - return { - error: true, - message: response?.data?.message || response?.data || null, - data: null, - }; - } -} diff --git a/service/http-config/http-base-instance.ts b/service/http-config/http-base-instance.ts deleted file mode 100644 index 19fff91..0000000 --- a/service/http-config/http-base-instance.ts +++ /dev/null @@ -1,14 +0,0 @@ -import axios from "axios"; - -const baseURL = "https://dev.mikulnews.com/api"; - -const axiosBaseInstance = axios.create({ - baseURL, - headers: { - "content-type": "application/json", - "X-Client-Key": "bb65b1ad-e954-4a1a-b4d0-74df5bb0b640" - }, - withCredentials: true, -}); - -export default axiosBaseInstance; diff --git a/service/http-config/http-base-services.ts b/service/http-config/http-base-services.ts index 7df973c..755d158 100644 --- a/service/http-config/http-base-services.ts +++ b/service/http-config/http-base-services.ts @@ -1,4 +1,4 @@ -import axiosBaseInstance from "./http-base-instance"; +import axiosBaseInstance from "./axios-base-instance"; const defaultHeaders = { "Content-Type": "application/json", diff --git a/service/http-config/mediahub-base-service.ts b/service/http-config/mediahub-base-service.ts deleted file mode 100644 index 499c570..0000000 --- a/service/http-config/mediahub-base-service.ts +++ /dev/null @@ -1,12 +0,0 @@ -import axios from "axios"; - -const baseURL = "https://mediahub.polri.go.id/api"; - -const mediahubBaseInstance = axios.create({ - baseURL, - headers: { - "content-type": "application/json", - }, -}); - -export default mediahubBaseInstance; diff --git a/service/magazine.tsx b/service/magazine.tsx index 9c76d36..d711c8d 100644 --- a/service/magazine.tsx +++ b/service/magazine.tsx @@ -1,75 +1,50 @@ import { PaginationRequest } from "@/types/globals"; -import { - httpDeleteInterceptor, - httpGet, - httpPost, - httpPut, -} from "./http-config/axios-base-service"; import Cookies from "js-cookie"; +import { httpDeleteInterceptor, httpGetInterceptor, httpPostInterceptor, httpPutInterceptor } from "./http-config/http-interceptor-services"; const token = Cookies.get("access_token"); export async function createMagazine(data: any) { - const headers = { - "content-type": "application/json", - Authorization: `Bearer ${token}`, - }; const pathUrl = `/magazines`; - return await httpPost(pathUrl, headers, data); + return await httpPostInterceptor(pathUrl, data); } export async function getListMagazine(props: PaginationRequest) { const { page, limit, search, startDate, endDate } = props; - const headers = { - "content-type": "application/json", - }; - return await httpGet( + return await httpGetInterceptor( `/magazines?limit=${limit}&page=${page}&title=${search}&startDate=${ startDate || "" - }&endDate=${endDate || ""}`, - headers + }&endDate=${endDate || ""}` ); } export async function updateMagazine(id: string, data: any) { - const headers = { - "content-type": "application/json", - }; const pathUrl = `/magazines/${id}`; - return await httpPut(pathUrl, headers, data); + return await httpPutInterceptor(pathUrl, data); } export async function getMagazineById(id: string) { - const headers = { - "content-type": "application/json", - }; - return await httpGet(`/magazines/${id}`, headers); + return await httpGetInterceptor(`/magazines/${id}`); } export async function deleteMagazine(id: string) { - const headers = { - "content-type": "application/json", - }; - return await httpDeleteInterceptor(`magazines/${id}`, headers); + return await httpDeleteInterceptor(`magazines/${id}`); } export async function uploadMagazineFile(id: string, data: any) { const headers = { "content-type": "multipart/form-data", }; - return await httpPost(`/magazine-files/${id}`, headers, data); + return await httpPostInterceptor(`/magazine-files/${id}`, data, headers); } export async function uploadMagazineThumbnail(id: string, data: any) { const headers = { "content-type": "multipart/form-data", }; - return await httpPost(`/magazines/thumbnail/${id}`, headers, data); + return await httpPostInterceptor(`/magazines/thumbnail/${id}`, data, headers); } export async function deleteMagazineFiles(id: number) { - const headers = { - "content-type": "multipart/form-data", - }; - return await httpDeleteInterceptor(`magazine-files/${id}`, headers); + return await httpDeleteInterceptor(`magazine-files/${id}`); } diff --git a/service/master-categories.tsx b/service/master-categories.tsx index 9efd3ec..44276b5 100644 --- a/service/master-categories.tsx +++ b/service/master-categories.tsx @@ -1,47 +1,29 @@ -import { - httpDeleteInterceptor, - httpGet, - httpPost, - httpPut, -} from "./http-config/axios-base-service"; import Cookies from "js-cookie"; +import { httpDeleteInterceptor, httpGetInterceptor, httpPostInterceptor, httpPutInterceptor } from "./http-config/http-interceptor-services"; const token = Cookies.get("access_token"); export async function createCategory(data: any) { - const headers = { - "content-type": "application/json", - Authorization: `Bearer ${token}`, - }; const pathUrl = `/article-categories`; - return await httpPost(pathUrl, headers, data); + return await httpPostInterceptor(pathUrl, data); } export async function updateCategory(id: string, data: any) { - const headers = { - "content-type": "application/json", - }; const pathUrl = `/article-categories/${id}`; - return await httpPut(pathUrl, headers, data); + return await httpPutInterceptor(pathUrl, data); } export async function getCategoryById(id: number) { - const headers = { - "content-type": "application/json", - }; - return await httpGet(`/article-categories/${id}`, headers); + return await httpGetInterceptor(`/article-categories/${id}`); } export async function deleteCategory(id: number) { - const headers = { - "content-type": "application/json", - }; - return await httpDeleteInterceptor(`article-categories/${id}`, headers); + return await httpDeleteInterceptor(`article-categories/${id}`); } export async function uploadCategoryThumbnail(id: string, data: any) { const headers = { "content-type": "multipart/form-data", }; - return await httpPost(`/article-categories/thumbnail/${id}`, headers, data); + return await httpPostInterceptor(`/article-categories/thumbnail/${id}`, data, headers); } diff --git a/service/master-user-role.ts b/service/master-user-role.ts index eda2837..567045b 100644 --- a/service/master-user-role.ts +++ b/service/master-user-role.ts @@ -1,42 +1,29 @@ -import { - httpDeleteInterceptor, - httpGet, - httpPost, -} from "./http-config/axios-base-service"; - import Cookies from "js-cookie"; +import { httpDeleteInterceptor, httpGetInterceptor, httpPostInterceptor } from "./http-config/http-interceptor-services"; const token = Cookies.get("access_token"); export async function listUserRole(data: any) { - const headers = { - "content-type": "application/json", - }; - return await httpGet( - `/user-roles?limit=${data.limit}&page=${data.page}`, - headers + return await httpGetInterceptor( + `/user-roles?limit=${data.limit}&page=${data.page}` ); } export async function createMasterUserRole(data: any) { - const headers = { - "content-type": "application/json", - Authorization: `Bearer ${token}`, - }; const pathUrl = `/user-roles`; - return await httpPost(pathUrl, headers, data); + return await httpPostInterceptor(pathUrl, data); } export async function getMasterUserRoleById(id: any) { const headers = { "content-type": "application/json", }; - return await httpGet(`/user-roles/${id}`, headers); + return await httpGetInterceptor(`/user-roles/${id}`); } export async function deleteMasterUserRole(id: string) { const headers = { "content-type": "application/json", }; - return await httpDeleteInterceptor(`/user-roles/${id}`, headers); + return await httpDeleteInterceptor(`/user-roles/${id}`); } diff --git a/service/master-user.ts b/service/master-user.ts index 931d468..ede054b 100644 --- a/service/master-user.ts +++ b/service/master-user.ts @@ -46,9 +46,13 @@ export async function postSignIn(data: any) { return await httpPost(pathUrl, data); } -export async function getProfile(code?: string) { +export async function getProfile(token?: string) { + const headers = { + "content-type": "application/json", + "Authorization": `Bearer ${token}`, + }; const pathUrl = `/users/info`; - return await httpGet(pathUrl); + return await httpGet(pathUrl, headers); } export async function updateProfile(data: any) { diff --git a/service/static-page-service.ts b/service/static-page-service.ts index ad2a87d..41631ec 100644 --- a/service/static-page-service.ts +++ b/service/static-page-service.ts @@ -1,47 +1,28 @@ import { PaginationRequest } from "@/types/globals"; -import { - httpDeleteInterceptor, - httpGet, - httpPost, - httpPut, -} from "./http-config/axios-base-service"; +import { httpGetInterceptor, httpPostInterceptor, httpPutInterceptor } from "./http-config/http-interceptor-services"; +import { httpGet } from "./http-config/http-base-services"; export async function createCustomStaticPage(data: any) { - const headers = { - "content-type": "application/json", - }; const pathUrl = `/custom-static-pages`; - return await httpPost(pathUrl, headers, data); + return await httpPostInterceptor(pathUrl, data); } export async function editCustomStaticPage(data: any) { - const headers = { - "content-type": "application/json", - }; const pathUrl = `/custom-static-pages/${data.id}`; - return await httpPut(pathUrl, headers, data); + return await httpPutInterceptor(pathUrl, data); } export async function getCustomStaticPage(props: PaginationRequest) { const { page, limit, search } = props; - const headers = { - "content-type": "application/json", - }; - return await httpGet( - `/custom-static-pages?limit=${limit}&page=${page}&title=${search}`, - headers - ); + const pathUrl = `/custom-static-pages?limit=${limit}&page=${page}&title=${search}`; + return await httpGetInterceptor(pathUrl); } + export async function getCustomStaticDetail(id: string) { - const headers = { - "content-type": "application/json", - }; - return await httpGet(`/custom-static-pages/${id}`, headers); + return await httpGetInterceptor(`/custom-static-pages/${id}`); } export async function getCustomStaticDetailBySlug(slug: string) { - const headers = { - "content-type": "application/json", - }; - return await httpGet(`/custom-static-pages/slug/${slug}`, headers); + const pathUrl = `/custom-static-pages/slug/${slug}`; + return await httpGet(pathUrl); } diff --git a/service/user-levels-service.ts b/service/user-levels-service.ts new file mode 100644 index 0000000..3a2ca3e --- /dev/null +++ b/service/user-levels-service.ts @@ -0,0 +1,30 @@ + +import Cookies from "js-cookie"; +import { httpGetInterceptor, httpPostInterceptor, httpPutInterceptor } from "./http-config/http-interceptor-services"; +const token = Cookies.get("access_token"); + +export async function getAllUserLevels(data?: any) { + const pathUrl = `user-levels?limit=${data?.limit || ""}&levelNumber=${ + data?.levelNumber || "" + }&name=${data?.search || ""}&page=${data?.page || "1"}` + return await httpGetInterceptor(pathUrl); +} +export async function getUserLevels(id: string) { + return await httpGetInterceptor(`user-levels/${id}`); +} + +export async function getAccountById(id: string) { + return await httpGetInterceptor(`user-account/findById/${id}`); +} + +export async function createUserLevels(data: any) { + return await httpPostInterceptor(`user-levels`, data); +} + +export async function editUserLevels(id: string, data: any) { + return await httpPutInterceptor(`user-levels/${id}`, data); +} + +export async function changeIsApproval(data: any) { + return await httpPutInterceptor(`user-levels/enable-approval`, data); +} diff --git a/service/user-levels/user-levels-service.ts b/service/user-levels/user-levels-service.ts deleted file mode 100644 index 5f1f457..0000000 --- a/service/user-levels/user-levels-service.ts +++ /dev/null @@ -1,52 +0,0 @@ - -import Cookies from "js-cookie"; -import { httpGet, httpPost, httpPut } from "../http-config/axios-base-service"; - -const token = Cookies.get("access_token"); - -export async function getAllUserLevels(data?: any) { - const headers = { - "content-type": "application/json", - }; - return await httpGet( - `user-levels?limit=${data?.limit || ""}&levelNumber=${ - data?.levelNumber || "" - }&name=${data?.search || ""}&page=${data?.page || "1"}`, - headers - ); -} -export async function getUserLevels(id: string) { - const headers = { - "content-type": "application/json", - }; - return await httpGet(`user-levels/${id}`, headers); -} - -export async function getAccountById(id: string) { - const headers = { - "content-type": "application/json", - }; - return await httpGet(`user-account/findById/${id}`, headers); -} - -export async function createUserLevels(request: any) { - const headers = { - "content-type": "application/json", - }; - return await httpPost(`user-levels`, headers, request); -} - -export async function editUserLevels(id: string, request: any) { - const headers = { - "content-type": "application/json", - }; - return await httpPut(`user-levels/${id}`, headers, request); -} - -export async function changeIsApproval(request: any) { - const headers = { - "content-type": "application/json", - Authorization: `Bearer ${token}`, - }; - return await httpPut(`user-levels/enable-approval`, headers, request); -}