deskripsi:
- fix server action to API notification to admin
- fix api created job
This commit is contained in:
2025-03-27 15:37:44 +08:00
parent d4049a9fca
commit c94ecd8f9c
20 changed files with 482 additions and 58 deletions

View File

@@ -1,4 +1,4 @@
export { apiGetPdfToImage };
export { apiGetPdfToImage, apiCreatedNotificationToAdmin, apiGetSeasonUserId };
export interface PageData {
imageUrl: string;
@@ -35,10 +35,7 @@ const apiGetPdfToImage = async ({ id }: { id: string }) => {
// Check if the response is OK
if (!response.ok) {
const errorData = await response.json().catch(() => null);
console.error(
"Failed get file",
errorData?.message || "Unknown error"
);
console.error("Failed get file", errorData?.message || "Unknown error");
return null;
}
@@ -50,3 +47,68 @@ const apiGetPdfToImage = async ({ id }: { id: string }) => {
throw error; // Re-throw the error to handle it in the calling function
}
};
const apiCreatedNotificationToAdmin = async ({ data }: { data: any }) => {
try {
const { token } = await fetch("/api/get-cookie").then((res) => res.json());
if (!token) {
console.error("No token found");
return null;
}
const response = await fetch(`/api/notifications/to-admin`, {
method: "POST",
body: JSON.stringify({ data }),
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${token}`,
},
});
// Check if the response is OK
if (!response.ok) {
const errorData = await response.json().catch(() => null);
console.error(
"Failed to created notifications",
response.statusText,
errorData
);
throw new Error(errorData?.message || "Failed to created notifications");
}
// Return the JSON response
return await response.json();
} catch (error) {
console.error("Error to created notifications", error);
throw error; // Re-throw the error to handle it in the calling function
}
};
const apiGetSeasonUserId = async () => {
try {
const response = await fetch(`/api/season`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
});
// Check if the response is OK
if (!response.ok) {
const errorData = await response.json().catch(() => null);
console.error(
"Failed to created notifications",
response.statusText,
errorData
);
throw new Error(errorData?.message || "Failed to created notifications");
}
return await response.json();
} catch (error) {
console.error("Error get user id", error);
throw error; // Re-throw the error to handle it in the calling function
}
};

View File

@@ -20,41 +20,157 @@ import { WibuRealtime } from "wibu-pkg";
import { job_funCreateNoFile, job_funCreateWithFile } from "../../fun";
import { gs_job_hot_menu } from "../../global_state";
import { MODEL_JOB } from "../../model/interface";
import { apiCreatedJob } from "../../lib/api_fetch_job";
import {
apiCreatedNotificationToAdmin,
apiGetSeasonUserId,
} from "@/app_modules/_global/lib/api_fetch_global";
import { useShallowEffect } from "@mantine/hooks";
import { apiGetUserId } from "@/app_modules/_global/lib/api_user";
function Job_ComponentButtonSaveCreate({
value,
file,
userLoginId,
}: {
value: MODEL_JOB;
file: File;
userLoginId: string;
}) {
const router = useRouter();
const [isLoading, setIsLoading] = useState(false);
const [hotMenu, setHotMenu] = useAtom(gs_job_hot_menu);
async function onCreate() {
// async function onCreate() {
// try {
// setIsLoading(true);
// if (file === null) {
// const createNoFile = await job_funCreateNoFile({
// data: value,
// });
// if (createNoFile.status === 201) {
// const dataNotifikasi: IRealtimeData = {
// appId: createNoFile.data?.id as any,
// status: createNoFile.data?.MasterStatus?.name as any,
// userId: createNoFile.data?.authorId as any,
// pesan: createNoFile.data?.title as any,
// kategoriApp: "JOB",
// title: "Job baru",
// };
// const notif = await notifikasiToAdmin_funCreate({
// data: dataNotifikasi as any,
// });
// if (notif.status === 201) {
// WibuRealtime.setData({
// type: "notification",
// pushNotificationTo: "ADMIN",
// });
// WibuRealtime.setData({
// type: "trigger",
// pushNotificationTo: "ADMIN",
// dataMessage: dataNotifikasi,
// });
// setHotMenu(2);
// router.replace(RouterJob.status({ id: "2" }));
// ComponentGlobal_NotifikasiBerhasil(createNoFile.message);
// }
// } else {
// setIsLoading(false);
// ComponentGlobal_NotifikasiGagal(createNoFile.message);
// }
// } else {
// const uploadFile = await funGlobal_UploadToStorage({
// file: file,
// dirId: DIRECTORY_ID.job_image,
// });
// if (!uploadFile.success) {
// setIsLoading(false);
// ComponentGlobal_NotifikasiPeringatan("Gagal upload gambar");
// return;
// }
// const createWithFile = await job_funCreateWithFile({
// data: value,
// fileId: uploadFile.data.id,
// });
// if (createWithFile.status === 201) {
// const dataNotifikasi: IRealtimeData = {
// appId: createWithFile.data?.id as any,
// status: createWithFile.data?.MasterStatus?.name as any,
// userId: createWithFile.data?.authorId as any,
// pesan: createWithFile.data?.title as any,
// kategoriApp: "JOB",
// title: "Job baru",
// };
// const notif = await notifikasiToAdmin_funCreate({
// data: dataNotifikasi as any,
// });
// if (notif.status === 201) {
// WibuRealtime.setData({
// type: "notification",
// pushNotificationTo: "ADMIN",
// });
// WibuRealtime.setData({
// type: "trigger",
// pushNotificationTo: "ADMIN",
// dataMessage: dataNotifikasi,
// });
// setHotMenu(2);
// router.replace(RouterJob.status({ id: "2" }));
// ComponentGlobal_NotifikasiBerhasil(createWithFile.message);
// }
// } else {
// setIsLoading(false);
// ComponentGlobal_NotifikasiGagal(createWithFile.message);
// }
// }
// } catch (error) {
// setIsLoading(false);
// clientLogger.error("Error create job", error);
// }
// }
async function handleCreated() {
try {
let fixData;
setIsLoading(true);
if (file === null) {
const createNoFile = await job_funCreateNoFile({
data: value,
fixData = {
...value,
authorId: userLoginId,
};
const responseNoFile = await apiCreatedJob({
data: fixData,
});
if (createNoFile.status === 201) {
if (responseNoFile) {
const dataNotifikasi: IRealtimeData = {
appId: createNoFile.data?.id as any,
status: createNoFile.data?.MasterStatus?.name as any,
userId: createNoFile.data?.authorId as any,
pesan: createNoFile.data?.title as any,
appId: responseNoFile.data?.id as any,
status: responseNoFile.data?.MasterStatus?.name as any,
userId: responseNoFile.data?.authorId as any,
pesan: responseNoFile.data?.title as any,
kategoriApp: "JOB",
title: "Job baru",
};
const notif = await notifikasiToAdmin_funCreate({
data: dataNotifikasi as any,
const responseNotification = await apiCreatedNotificationToAdmin({
data: dataNotifikasi,
});
if (notif.status === 201) {
if (responseNotification) {
WibuRealtime.setData({
type: "notification",
pushNotificationTo: "ADMIN",
@@ -68,13 +184,13 @@ function Job_ComponentButtonSaveCreate({
setHotMenu(2);
router.replace(RouterJob.status({ id: "2" }));
ComponentGlobal_NotifikasiBerhasil(createNoFile.message);
ComponentGlobal_NotifikasiBerhasil(responseNoFile.message);
}
} else {
setIsLoading(false);
ComponentGlobal_NotifikasiGagal(createNoFile.message);
}
}
// else {
// setIsLoading(false);
// ComponentGlobal_NotifikasiGagal(responseNoFile.message);
// }
} else {
const uploadFile = await funGlobal_UploadToStorage({
file: file,
@@ -87,17 +203,22 @@ function Job_ComponentButtonSaveCreate({
return;
}
const createWithFile = await job_funCreateWithFile({
data: value,
fileId: uploadFile.data.id,
fixData = {
...value,
authorId: userLoginId,
imageId: uploadFile.data.id,
};
const responseWithFile = await apiCreatedJob({
data: fixData,
});
if (createWithFile.status === 201) {
if (responseWithFile.status === 201) {
const dataNotifikasi: IRealtimeData = {
appId: createWithFile.data?.id as any,
status: createWithFile.data?.MasterStatus?.name as any,
userId: createWithFile.data?.authorId as any,
pesan: createWithFile.data?.title as any,
appId: responseWithFile.data?.id as any,
status: responseWithFile.data?.MasterStatus?.name as any,
userId: responseWithFile.data?.authorId as any,
pesan: responseWithFile.data?.title as any,
kategoriApp: "JOB",
title: "Job baru",
};
@@ -120,12 +241,9 @@ function Job_ComponentButtonSaveCreate({
setHotMenu(2);
router.replace(RouterJob.status({ id: "2" }));
ComponentGlobal_NotifikasiBerhasil(createWithFile.message);
ComponentGlobal_NotifikasiBerhasil(responseWithFile.message);
}
} else {
setIsLoading(false);
ComponentGlobal_NotifikasiGagal(createWithFile.message);
}
}
}
} catch (error) {
setIsLoading(false);
@@ -169,9 +287,7 @@ function Job_ComponentButtonSaveCreate({
loading={isLoading ? true : false}
w={"100%"}
radius={"xl"}
onClick={() => {
onCreate();
}}
onClick={handleCreated}
>
Simpan
</Button>

View File

@@ -29,7 +29,7 @@ import {
import { Job_ComponentButtonSaveCreate } from "../component";
import { defaultDeskripsi, defaultSyarat } from "../component/default_value";
export default function Job_Create() {
export default function Job_Create({userLoginId}: {userLoginId: string}) {
const [value, setValue] = useState({
title: "",
content: "",
@@ -174,7 +174,11 @@ export default function Job_Create() {
</Stack>
</ComponentGlobal_CardStyles>
<Job_ComponentButtonSaveCreate value={value as any} file={file as any} />
<Job_ComponentButtonSaveCreate
userLoginId={userLoginId}
value={value as any}
file={file as any}
/>
</Stack>
);
}

View File

@@ -14,7 +14,7 @@ import { useState } from "react";
import { useDisclosure, useShallowEffect } from "@mantine/hooks";
import UIGlobal_Modal from "@/app_modules/_global/ui/ui_modal";
import { clientLogger } from "@/util/clientLogger";
import { apiGetJobById } from "../../component/api_fetch_job";
import { apiGetJobById } from "../../lib/api_fetch_job";
import CustomSkeleton from "@/app_modules/components/CustomSkeleton";
export default function Job_DetailArsip() {

View File

@@ -21,7 +21,7 @@ import { job_getOneById } from "../../fun/get/get_one_by_id";
import { IRealtimeData } from "@/lib/global_state";
import { WibuRealtime } from "wibu-pkg";
import { clientLogger } from "@/util/clientLogger";
import { apiGetJobById } from "../../component/api_fetch_job";
import { apiGetJobById } from "../../lib/api_fetch_job";
import CustomSkeleton from "@/app_modules/components/CustomSkeleton";
export default function Job_DetailDraft() {

View File

@@ -9,7 +9,7 @@ import { IconBrandWhatsapp } from "@tabler/icons-react";
import Link from "next/link";
import { useParams } from "next/navigation";
import { useState } from "react";
import { apiGetJobById } from "../../component/api_fetch_job";
import { apiGetJobById } from "../../lib/api_fetch_job";
import ComponentJob_DetailData from "../../component/detail/detail_data";
import { MODEL_JOB } from "../../model/interface";

View File

@@ -15,7 +15,7 @@ import { useDisclosure, useShallowEffect } from "@mantine/hooks";
import { useAtom } from "jotai";
import { useParams, useRouter } from "next/navigation";
import { useState } from "react";
import { apiGetJobById } from "../../component/api_fetch_job";
import { apiGetJobById } from "../../lib/api_fetch_job";
import ComponentJob_DetailData from "../../component/detail/detail_data";
import { Job_funEditArsipById } from "../../fun/edit/fun_edit_arsip_by_id";
import { gs_job_hot_menu } from "../../global_state";

View File

@@ -16,7 +16,7 @@ import { Job_funDeleteById } from "../../fun/delete/fun_delete_by_id";
import { Job_funEditStatusByStatusId } from "../../fun/edit/fun_edit_status_by_status_id";
import { MODEL_JOB } from "../../model/interface";
import { clientLogger } from "@/util/clientLogger";
import { apiGetJobById } from "../../component/api_fetch_job";
import { apiGetJobById } from "../../lib/api_fetch_job";
import CustomSkeleton from "@/app_modules/components/CustomSkeleton";
export default function Job_DetailReject() {

View File

@@ -14,7 +14,7 @@ import ComponentJob_DetailData from "../../component/detail/detail_data";
import { Job_funEditStatusByStatusId } from "../../fun/edit/fun_edit_status_by_status_id";
import { MODEL_JOB } from "../../model/interface";
import { clientLogger } from "@/util/clientLogger";
import { apiGetJobById } from "../../component/api_fetch_job";
import { apiGetJobById } from "../../lib/api_fetch_job";
import CustomSkeleton from "@/app_modules/components/CustomSkeleton";
export default function Job_DetailReview() {

View File

@@ -25,7 +25,7 @@ import { Job_ComponentButtonUpdate } from "../component";
import { clientLogger } from "@/util/clientLogger";
import { useShallowEffect } from "@mantine/hooks";
import { useParams } from "next/navigation";
import { apiGetJobById } from "../component/api_fetch_job";
import { apiGetJobById } from "../lib/api_fetch_job";
import { Job_SkeletonEdit } from "../component/skeleton/comp_skeleton_beranda";
const ReactQuill = dynamic(
() => {

View File

@@ -1,4 +1,10 @@
export { apiGetJob, apiGetJobArsip, apiGetJobById, apiGetJobByStatus };
export {
apiGetJob,
apiGetJobArsip,
apiGetJobById,
apiGetJobByStatus,
apiCreatedJob,
};
const apiGetJobByStatus = async ({
status,
@@ -166,3 +172,36 @@ const apiGetJobById = async ({ id }: { id: string }) => {
throw error; // Re-throw the error to handle it in the calling function
}
};
const apiCreatedJob = async ({ data }: { data: any }) => {
try {
const { token } = await fetch("/api/get-cookie").then((res) => res.json());
if (!token) {
console.error("No token found");
return null;
}
const response = await fetch(`/api/job`, {
method: "POST",
body: JSON.stringify({ data }),
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${token}`,
},
});
// Check if the response is OK
if (!response.ok) {
const errorData = await response.json().catch(() => null);
console.error("Failed created job", response.statusText, errorData);
throw new Error(errorData?.message || "Failed created job");
}
// Return the JSON response
return await response.json();
} catch (error) {
console.error("Error created job", error);
throw error; // Re-throw the error to handle it in the calling function
}
};

View File

@@ -10,7 +10,7 @@ import { useShallowEffect } from "@mantine/hooks";
import _ from "lodash";
import { ScrollOnly } from "next-scroll-loader";
import { useState } from "react";
import { apiGetJob, apiGetJobArsip } from "../../component/api_fetch_job";
import { apiGetJob, apiGetJobArsip } from "../../lib/api_fetch_job";
import ComponentJob_CardStatus from "../../component/card/card_view";
import { MODEL_JOB } from "../../model/interface";

View File

@@ -16,7 +16,7 @@ import {
Job_ComponentButtonUpdateBeranda,
Job_ComponentSkeletonBeranda,
} from "../../component";
import { apiGetJob } from "../../component/api_fetch_job";
import { apiGetJob } from "../../lib/api_fetch_job";
import ComponentJob_BerandaCardView from "../../component/beranda/card_view";
import { MODEL_JOB } from "../../model/interface";

View File

@@ -9,7 +9,7 @@ import { useState } from "react";
import ComponentJob_CardStatus from "../../component/card/card_view";
import job_getAllStatusPublish from "../../fun/get/status/get_list_publish";
import { useShallowEffect } from "@mantine/hooks";
import { apiGetJobByStatus } from "../../component/api_fetch_job";
import { apiGetJobByStatus } from "../../lib/api_fetch_job";
export default function Job_Publish({ nameStatus }: { nameStatus: string }) {
const [data, setData] = useState<any[] | null>(null);

View File

@@ -9,7 +9,7 @@ import { useState } from "react";
import ComponentJob_CardStatus from "../../component/card/card_view";
import job_getAllStatusReview from "../../fun/get/status/get_list_review";
import { useShallowEffect } from "@mantine/hooks";
import { apiGetJobByStatus } from "../../component/api_fetch_job";
import { apiGetJobByStatus } from "../../lib/api_fetch_job";
export default function Job_Review({ nameStatus }: { nameStatus: string }) {
const [data, setData] = useState<any[] | null>(null);

View File

@@ -11,7 +11,7 @@ import _ from "lodash";
import { ScrollOnly } from "next-scroll-loader";
import { useParams } from "next/navigation";
import { useState } from "react";
import { apiGetJobByStatus } from "../../component/api_fetch_job";
import { apiGetJobByStatus } from "../../lib/api_fetch_job";
import ComponentJob_CardStatus from "../../component/card/card_view";
export default function Job_NewViewStatus() {