fix: collaboration

deskripsi:
- fix: use server pada create dan edit menjadi API
This commit is contained in:
2025-05-26 17:15:46 +08:00
parent b9db3f6e02
commit 5007827d51
10 changed files with 231 additions and 141 deletions

View File

@@ -1,21 +1,9 @@
import { Colab_Edit } from "@/app_modules/colab";
import colab_getOneCollaborationById from "@/app_modules/colab/fun/get/get_one_by_id";
import colab_funGetMasterIndustri from "@/app_modules/colab/fun/master/fun_get_master_industri";
import _ from "lodash";
export default async function Page({ params }: { params: { id: string } }) {
const colabId = params.id;
const dataColab = await colab_getOneCollaborationById(colabId);
const selectedData = _.omit(dataColab, [
"ProjectCollaboration_Partisipasi",
"Author",
]);
// console.log(selectedData);
const listIndustri = await colab_funGetMasterIndustri()
export default async function Page() {
return (
<>
<Colab_Edit selectedData={selectedData as any} listIndustri={listIndustri as any} />
<Colab_Edit />
</>
);
}

View File

@@ -1,12 +1,9 @@
import { funGetUserIdByToken } from "@/app_modules/_global/fun/get";
import { Colab_Beranda } from "@/app_modules/colab";
export default async function Page() {
const userLoginId = await funGetUserIdByToken();
export default function Page() {
return (
<>
<Colab_Beranda userLoginId={userLoginId as string} />
<Colab_Beranda />
</>
);
}

View File

@@ -1,7 +1,6 @@
import Colab_GrupDiskus from "@/app_modules/colab/main/grup";
export default async function Page() {
// const listRoom = await colab_getListRoomChatByAuthorId({page: 1});
export default function Page() {
return (
<>
<Colab_GrupDiskus />

View File

@@ -1,9 +1,6 @@
import { Colab_Proyek } from "@/app_modules/colab";
export default async function Page() {
// const listPartisipasiProyek = await colab_getListPartisipasiProyekByAuthorId({page: 1})
// const listProyekSaya = await colab_getListAllProyekSayaByAuthorId({page: 1})
export default function Page() {
return (
<>
<Colab_Proyek />

View File

@@ -4,6 +4,7 @@ export {
apiGetMasterStatusTransaksi,
apiGetAdminContact,
apiGetMasterEmotions,
apiGetMasterIndustri,
};
const apiGetMasterBank = async () => {
@@ -108,3 +109,37 @@ const apiGetMasterEmotions = async () => {
return await response.json().catch(() => null);
};
const apiGetMasterIndustri = async () => {
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;
}
const response = await fetch(`/api/master/industri`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${token}`,
},
});
if (!response.ok) {
const errorData = await response.json().catch(() => null);
console.error(
"Error get master industri:",
errorData?.message || "Unknown error"
);
return null;
}
return await response.json();
} catch (error) {
console.error("Error get master industri:", error);
throw error;
}
};

View File

@@ -20,10 +20,44 @@ export const apiGetOneCollaborationById = async ({
kategori: "detail" | "list_partisipan" | "cek_partisipasi";
page?: string;
}) => {
const respone = await fetch(
`/api/collaboration/${id}?kategori=${kategori}&page=${page}`
);
return await respone.json().catch(() => null);
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;
}
const respone = await fetch(
`/api/collaboration/${id}?kategori=${kategori}&page=${page}`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${token}`,
},
}
);
if (!respone.ok) {
const errorData = await respone.json().catch(() => null);
console.error(
"Failed to get one collaboration by id",
respone.statusText,
errorData
);
throw new Error(
errorData?.message || "Failed to get one collaboration by id"
);
}
const data = await respone.json();
return data;
} catch (error) {
console.error("Error get one collaboration by id", error);
return null;
}
};
export const apiGetDataGroupById = async ({
@@ -32,12 +66,47 @@ export const apiGetDataGroupById = async ({
page,
}: {
id: string;
kategori: "detail" | "info_group"
kategori: "detail" | "info_group";
page?: string;
}) => {
const respone = await fetch(
`/api/collaboration/group/${id}?kategori=${kategori}&page=${page}`
);
return await respone.json().catch(() => null)
}
return await respone.json().catch(() => null);
};
export const apiGetMasterIndustri = async () => {
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;
}
const response = await fetch(`/api/collaboration/master`, {
method: "GET",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `Bearer ${token}`,
},
});
if (!response.ok) {
const errorData = await response.json().catch(() => null);
console.error(
"Error get master industri:",
errorData?.message || "Unknown error"
);
return null;
}
return await response.json();
} catch (error) {
console.error("Error get master industri:", error);
throw error;
}
};

View File

@@ -1,4 +0,0 @@
export async function apiGetMasterCollaboration() {
const data = await fetch(`/api/collaboration/master`);
return await data.json().catch(() => null);
}

View File

@@ -16,9 +16,9 @@ import { useShallowEffect } from "@mantine/hooks";
import { useRouter } from "next/navigation";
import { useState } from "react";
import { Collaboration_SkeletonCreate } from "../component";
import { apiGetMasterCollaboration } from "../component/lib/api_collaboration";
import colab_funCreateProyek from "../fun/create/fun_create_proyek";
import { MODEL_COLLABORATION_MASTER } from "../model/interface";
import { apiGetMasterIndustri } from "../_lib/api_collaboration";
interface IValue {
title: string;
@@ -42,17 +42,18 @@ export default function Colab_Create() {
>(null);
useShallowEffect(() => {
onLoadMaster();
onLoadIndustri();
}, []);
async function onLoadMaster() {
async function onLoadIndustri() {
try {
const respone = await apiGetMasterCollaboration();
if (respone.success) {
setListIndustri(respone.data);
const response = await apiGetMasterIndustri();
if (response.success) {
setListIndustri(response.data);
}
} catch (error) {
clientLogger.error("Error get master collaboration", error);
clientLogger.error("Error get master industri", error);
}
}

View File

@@ -6,7 +6,7 @@ import { ComponentGlobal_NotifikasiBerhasil } from "@/app_modules/_global/notif_
import { ComponentGlobal_NotifikasiGagal } from "@/app_modules/_global/notif_global/notifikasi_gagal";
import { ComponentGlobal_NotifikasiPeringatan } from "@/app_modules/_global/notif_global/notifikasi_peringatan";
import { Button, Select, Stack, Textarea, TextInput } from "@mantine/core";
import { useRouter } from "next/navigation";
import { useParams, useRouter } from "next/navigation";
import { useState } from "react";
import colab_funEditById from "../fun/edit/fun_edit_by_id";
import {
@@ -18,15 +18,62 @@ import Component_V3_Label_TextInput from "@/app_modules/_global/component/new/co
import { Component_V3_TextEditor } from "@/app_modules/_global/component/new/comp_V3_text_editor";
import { funReplaceHtml } from "@/app_modules/_global/fun/fun_replace_html";
import { maxInputLength } from "@/app_modules/_global/lib/maximal_setting";
import { useShallowEffect } from "@mantine/hooks";
import {
apiGetMasterIndustri,
apiGetOneCollaborationById,
} from "../_lib/api_collaboration";
import _ from "lodash";
import CustomSkeleton from "@/app_modules/components/CustomSkeleton";
export default function Colab_Edit() {
const { id } = useParams();
const [data, setData] = useState<MODEL_COLLABORATION | any | null>();
const [listIndustri, setListIndustri] = useState<
MODEL_COLLABORATION_MASTER[] | null
>(null);
useShallowEffect(() => {
onLoadIndustri();
}, []);
useShallowEffect(() => {
onLoadData();
}, []);
async function onLoadData() {
try {
const response = await apiGetOneCollaborationById({
id: id as string,
kategori: "detail",
});
if (response.success) {
const fixData = _.omit(response.data, [
"ProjectCollaboration_Partisipasi",
"Author",
]);
setData(fixData as MODEL_COLLABORATION);
}
} catch (error) {
clientLogger.error("Error get one collaboration by id", error);
}
}
async function onLoadIndustri() {
try {
const response = await apiGetMasterIndustri();
if (response.success) {
setListIndustri(response.data);
}
} catch (error) {
clientLogger.error("Error get master industri", error);
}
}
if (data === undefined || listIndustri === null)
return <CustomSkeleton height={400} />;
export default function Colab_Edit({
selectedData,
listIndustri,
}: {
selectedData: MODEL_COLLABORATION;
listIndustri: MODEL_COLLABORATION_MASTER[];
}) {
const [value, setValue] = useState(selectedData);
return (
<>
<Stack px={"xs"} py={"md"}>
@@ -46,10 +93,10 @@ export default function Colab_Edit({
label="Judul"
withAsterisk
placeholder="Masukan judul proyek"
value={value.title}
value={data?.title}
onChange={(val) =>
setValue({
...value,
setData({
...data,
title: val.currentTarget.value,
})
}
@@ -71,10 +118,10 @@ export default function Colab_Edit({
label="Lokasi"
withAsterisk
placeholder="Masukan lokasi proyek"
value={value.lokasi}
value={data?.lokasi}
onChange={(val) =>
setValue({
...value,
setData({
...data,
lokasi: val.currentTarget.value,
})
}
@@ -98,15 +145,19 @@ export default function Colab_Edit({
placeholder="Pilih kategori industri"
label="Pilih Industri"
withAsterisk
value={value?.ProjectCollaborationMaster_Industri.id}
data={listIndustri.map((e) => ({
value: e.id,
label: e.name,
}))}
value={data?.ProjectCollaborationMaster_Industri.id}
data={
_.isEmpty(listIndustri)
? []
: listIndustri.map((e) => ({
value: e.id,
label: e.name,
}))
}
onChange={
(val) =>
setValue({
...(value as any),
setData({
...(data as any),
ProjectCollaborationMaster_Industri: {
id: val as any,
},
@@ -119,17 +170,17 @@ export default function Colab_Edit({
<Component_V3_Label_TextInput text="Tujuan Proyek" />
<Component_V3_TextEditor
data={value.purpose}
data={data?.purpose}
onSetData={(val) => {
setValue({
...value,
setData({
...data,
purpose: val,
});
}}
/>
<ComponentGlobal_InputCountDown
lengthInput={funReplaceHtml({ html: value.purpose }).length}
lengthInput={funReplaceHtml({ html: data?.purpose }).length}
maxInput={maxInputLength}
/>
</Stack>
@@ -138,80 +189,22 @@ export default function Colab_Edit({
<Component_V3_Label_TextInput text="Keuntungan" />
<Component_V3_TextEditor
data={value.benefit}
data={data?.benefit}
onSetData={(val) => {
setValue({
...value,
setData({
...data,
benefit: val,
});
}}
/>
<ComponentGlobal_InputCountDown
lengthInput={funReplaceHtml({ html: value.benefit }).length}
lengthInput={funReplaceHtml({ html: data?.benefit }).length}
maxInput={maxInputLength}
/>
</Stack>
{/* <Stack spacing={5}>
<Textarea
styles={{
label: {
color: MainColor.white,
},
input: {
backgroundColor: MainColor.white,
},
required: {
color: MainColor.red,
}
}}
label="Tujuan Proyek"
placeholder="Masukan tujuan proyek"
withAsterisk
minRows={5}
value={value.purpose}
onChange={(val) =>
setValue({
...value,
purpose: val.currentTarget.value,
})
}
/>
<ComponentGlobal_InputCountDown
lengthInput={value.purpose.length}
maxInput={500}
/>
</Stack> */}
{/* <Stack spacing={5}>
<Textarea
styles={{
label: {
color: MainColor.white,
},
input: {
backgroundColor: MainColor.white,
},
}}
label="Keuntungan "
placeholder="Masukan keuntungan dalam proyek"
minRows={5}
value={value.benefit}
onChange={(val) =>
setValue({
...value,
benefit: val.currentTarget.value,
})
}
/>
<ComponentGlobal_InputCountDown
lengthInput={value.benefit.length}
maxInput={500}
/>
</Stack> */}
<ButtonAction value={value as any} />
<ButtonAction value={data as any} />
</Stack>
</>
);

View File

@@ -1,8 +1,10 @@
"use client";
import { RouterColab } from "@/lib/router_hipmi/router_colab";
import ComponentGlobal_CreateButton from "@/app_modules/_global/component/button_create";
import ComponentGlobal_IsEmptyData from "@/app_modules/_global/component/is_empty_data";
import { apiNewGetUserIdByToken } from "@/app_modules/_global/lib/api_fetch_global";
import { RouterColab } from "@/lib/router_hipmi/router_colab";
import { clientLogger } from "@/util/clientLogger";
import mqtt_client from "@/util/mqtt_client";
import { Box, Center, Loader } from "@mantine/core";
import { useShallowEffect } from "@mantine/hooks";
@@ -12,18 +14,14 @@ import { useState } from "react";
import { apiGetAllCollaboration } from "../_lib/api_collaboration";
import { ComponentColab_ButtonUpdateBeranda } from "../component/button/button_update_beranda";
import { ComponentColab_CardBeranda } from "../component/card_view/card_beranda";
import { MODEL_COLLABORATION } from "../model/interface";
import { Collaboration_SkeletonBeranda } from "../component/skeleton_view";
import { clientLogger } from "@/util/clientLogger";
import { MODEL_COLLABORATION } from "../model/interface";
export default function Colab_Beranda({
userLoginId,
}: {
userLoginId: string;
}) {
export default function Colab_Beranda() {
const [data, setData] = useState<MODEL_COLLABORATION[] | null>(null);
const [activePage, setActivePage] = useState(1);
const [isNewPost, setIsNewPost] = useState(false);
const [userLoginId, setUserLoginId] = useState<string | null>(null);
useShallowEffect(() => {
onLoadData();
@@ -44,6 +42,23 @@ export default function Colab_Beranda({
}
}
useShallowEffect(() => {
handleGetUserLoginId();
}, []);
async function handleGetUserLoginId() {
try {
const response = await apiNewGetUserIdByToken();
if (response.success) {
setUserLoginId(response.userId);
} else {
setUserLoginId(null);
}
} catch (error) {
setUserLoginId(null);
}
}
useShallowEffect(() => {
mqtt_client.subscribe("Colab_create");
@@ -99,7 +114,7 @@ export default function Colab_Beranda({
{(item) => (
<ComponentColab_CardBeranda
data={item}
userLoginId={userLoginId}
userLoginId={userLoginId as string}
/>
)}
</ScrollOnly>