deskripsi:
- fix api job
This commit is contained in:
2025-02-20 10:20:54 +08:00
parent d474d7611b
commit 57d04450e1
15 changed files with 373 additions and 115 deletions

View File

@@ -1,4 +1,4 @@
export { apiGetJobByStatus };
export { apiGetJobByStatus, apiGetJob, apiGetJobArsip };
const apiGetJobByStatus = async ({
status,
@@ -15,10 +15,8 @@ const apiGetJobByStatus = async ({
return null;
}
console.log("status > ", status);
// Send PUT request to update portfolio logo
const isPage = `?page=${page}`
const isPage = `?page=${page}`;
const response = await fetch(`/api/job/status/${status}${isPage}`, {
method: "GET",
headers: {
@@ -45,3 +43,84 @@ const apiGetJobByStatus = async ({
throw error; // Re-throw the error to handle it in the calling function
}
};
const apiGetJob = async ({ page, search }: { page: string, search?: string }) => {
try {
// Fetch token from cookie
const { token } = await fetch("/api/get-cookie").then((res) => res.json());
if (!token) {
console.error("No token found");
return null;
}
// Send PUT request to update portfolio logo
const isPage = `?page=${page}`;
const isSearch = search ? `&search=${search}` : "";
const response = await fetch(`/api/job${isPage}${isSearch}`, {
method: "GET",
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(
"Error updating portfolio logo:",
errorData?.message || "Unknown error"
);
return null;
}
return await response.json();
} catch (error) {
console.error("Error updating portfolio logo:", error);
throw error; // Re-throw the error to handle it in the calling function
}
};
const apiGetJobArsip = async ({
page,
}: {
page: string;
}) => {
try {
// Fetch token from cookie
const { token } = await fetch("/api/get-cookie").then((res) => res.json());
if (!token) {
console.error("No token found");
return null;
}
// Send PUT request to update portfolio logo
const isPage = `?page=${page}`;
const response = await fetch(`/api/job/arsip${isPage}`, {
method: "GET",
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(
"Error updating portfolio logo:",
errorData?.message || "Unknown error"
);
return null;
}
return await response.json();
} catch (error) {
console.error("Error updating portfolio logo:", error);
throw error; // Re-throw the error to handle it in the calling function
}
};

View File

@@ -1,20 +1,21 @@
import { ComponentGlobal_CardStyles } from "@/app_modules/_global/component";
import CustomSkeleton from "@/app_modules/components/CustomSkeleton";
import { Box, Center, Group, Skeleton, Stack } from "@mantine/core";
export default function Job_ComponentSkeletonBeranda() {
return (
<>
<Box>
{Array.from(new Array(10)).map((e, i) => (
{Array.from(new Array(3)).map((e, i) => (
<ComponentGlobal_CardStyles key={i} marginBottom={"15px"}>
<Stack>
<Group position="left">
<Skeleton height={40} width={40} circle />
<Skeleton height={20} width={200} />
<CustomSkeleton height={40} width={40} circle />
<CustomSkeleton height={20} width={200} />
</Group>
<Center my={"md"}>
<Skeleton height={20} width={300} />
<CustomSkeleton height={20} width={300} />
</Center>
</Stack>
</ComponentGlobal_CardStyles>

View File

@@ -1,11 +1,109 @@
import { Box } from "@mantine/core";
"use client";
import { Box, Center, Stack } from "@mantine/core";
import { MODEL_JOB } from "../../model/interface";
import { Job_UI_Arsip } from "./ui_arsip";
import ComponentGlobal_IsEmptyData from "@/app_modules/_global/component/is_empty_data";
import { RouterJob } from "@/lib/router_hipmi/router_job";
import _ from "lodash";
import { ScrollOnly } from "next-scroll-loader";
import { useState } from "react";
import ComponentJob_CardStatus from "../../component/card/card_view";
import { job_getAllArsipById } from "../../fun/get/get_all_arsip";
import ComponentGlobal_Loader from "@/app_modules/_global/component/loader";
import { clientLogger } from "@/util/clientLogger";
import { useShallowEffect } from "@mantine/hooks";
import { apiGetJob, apiGetJobArsip } from "../../component/api_fetch_job";
import { Job_ComponentSkeletonBeranda } from "../../component";
import CustomSkeleton from "@/app_modules/components/CustomSkeleton";
export default function Job_ViewArsip() {
const [data, setData] = useState<MODEL_JOB[]>([]);
const [activePage, setActivePage] = useState(1);
const [hasMore, setHasMore] = useState(true);
const [isLoading, setIsLoading] = useState(false);
useShallowEffect(() => {
onLoadNewData();
}, []);
async function onLoadNewData() {
try {
setIsLoading(true);
const response = await apiGetJobArsip({
page: `${activePage}`,
});
if (response.success) {
setData(response.data);
setActivePage(1);
setHasMore(response.data.length > 0);
} else {
setData([]);
setHasMore(false);
}
} catch (error) {
clientLogger.error("Error get job", error);
setData([]);
setHasMore(false);
} finally {
setIsLoading(false);
}
}
const handleMoreData = async () => {
if (!hasMore || isLoading) return null;
try {
const nextPage = activePage + 1;
const response = await apiGetJob({
page: `${nextPage}`,
});
if (response?.data && response.data.length > 0) {
setActivePage(nextPage);
setHasMore(response.data.length > 0);
return response.data;
} else {
setHasMore(false);
return null;
}
} catch (error) {
clientLogger.error("Error get job", error);
setHasMore(false);
return null;
}
};
export default function Job_ViewArsip({ dataJob }: { dataJob: MODEL_JOB[] }) {
return (
<>
<Job_UI_Arsip listData={dataJob} />;
{!data?.length && isLoading ? (
<Stack>
<CustomSkeleton height={70} width={"100%"} />
<CustomSkeleton height={70} width={"100%"} />
</Stack>
) : _.isEmpty(data) ? (
<ComponentGlobal_IsEmptyData />
) : (
// --- Main component --- //
<Box>
<ScrollOnly
height="85vh"
renderLoading={() => <ComponentGlobal_Loader />}
data={data}
setData={setData}
moreData={handleMoreData}
>
{(item) => (
<ComponentJob_CardStatus
data={item}
path={RouterJob.detail_arsip}
/>
)}
</ScrollOnly>
</Box>
)}
</>
);
}

View File

@@ -1,6 +1,5 @@
"use client";
import { API_RouteJob } from "@/lib/api_user_router/route_api_job";
import { gs_jobTiggerBeranda } from "@/lib/global_state";
import { RouterJob } from "@/lib/router_hipmi/router_job";
import ComponentGlobal_CreateButton from "@/app_modules/_global/component/button_create";
@@ -18,11 +17,15 @@ import {
} from "../../component";
import ComponentJob_BerandaCardView from "../../component/beranda/card_view";
import { MODEL_JOB } from "../../model/interface";
import { apiGetJob } from "../../component/api_fetch_job";
import { clientLogger } from "@/util/clientLogger";
export default function Job_ViewBeranda() {
const [data, setData] = useState<MODEL_JOB[] | null>(null);
const [data, setData] = useState<MODEL_JOB[]>([]);
const [activePage, setActivePage] = useState(1);
const [isSearch, setIsSearch] = useState("");
const [hasMore, setHasMore] = useState(true);
const [isLoading, setIsLoading] = useState(false);
// Notifikasi
const [isShowUpdate, setIsShowUpdate] = useState(false);
@@ -38,26 +41,65 @@ export default function Job_ViewBeranda() {
setIsTriggerJob(false);
setIsShowUpdate(false);
onLoadNewData();
}, []);
}, [isSearch]);
async function onSearch(text: string) {
setIsSearch(text);
const loadData = await fetch(
API_RouteJob.get_all({ page: activePage, search: text })
);
const res = await loadData.json();
setData(res.data as any);
setActivePage(1);
setHasMore(true);
}
async function onLoadNewData() {
const loadData = await fetch(API_RouteJob.get_all({ page: activePage }));
const res = await loadData.json();
// console.log(res.data);
setData(res.data);
try {
setIsLoading(true);
const response = await apiGetJob({
page: `${activePage}`,
search: isSearch,
});
if (response.success) {
setData(response.data);
setActivePage(1);
setHasMore(response.data.length > 0);
} else {
setData([]);
setHasMore(false);
}
} catch (error) {
clientLogger.error("Error get job", error);
setData([]);
setHasMore(false);
} finally {
setIsLoading(false);
}
}
const handleMoreData = async () => {
if (!hasMore || isLoading) return null;
try {
const nextPage = activePage + 1;
const response = await apiGetJob({
page: `${nextPage}`,
search: isSearch,
});
if (response?.data && response.data.length > 0) {
setActivePage(nextPage);
setHasMore(response.data.length > 0);
return response.data;
} else {
setHasMore(false);
return null;
}
} catch (error) {
clientLogger.error("Error get job", error);
setHasMore(false);
return null;
}
};
return (
<>
<Stack my={1} spacing={30}>
@@ -89,7 +131,7 @@ export default function Job_ViewBeranda() {
}}
/>
{_.isNull(data) ? (
{!data?.length && isLoading ? (
<Job_ComponentSkeletonBeranda />
) : _.isEmpty(data) ? (
<ComponentGlobal_IsEmptyData />
@@ -104,16 +146,7 @@ export default function Job_ViewBeranda() {
)}
data={data}
setData={setData as any}
moreData={async () => {
const loadData = await fetch(
API_RouteJob.get_all({ page: activePage, search: isSearch })
);
const res = await loadData.json();
setActivePage((val) => val + 1);
return res.data;
}}
moreData={handleMoreData}
>
{(item) => <ComponentJob_BerandaCardView data={item} />}
</ScrollOnly>

View File

@@ -31,7 +31,6 @@ export default function Job_NewViewStatus() {
page: `${activePage}`,
});
if (response.success) {
console.log(response.data);
setData(response.data);
}
} catch (error) {