fix portofolio
deskripsiL: - fix server acton create to API
This commit is contained in:
25
src/app_modules/map/_component/api_fetch_map.ts
Normal file
25
src/app_modules/map/_component/api_fetch_map.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
export { apiCreatePinMap };
|
||||
|
||||
const apiCreatePinMap = async ({
|
||||
portofolioId,
|
||||
data,
|
||||
}: {
|
||||
portofolioId: string;
|
||||
data: any;
|
||||
}) => {
|
||||
const { token } = await fetch("/api/get-cookie").then((res) => res.json());
|
||||
if (!token) return await token.json().catch(() => null);
|
||||
|
||||
const respone = await fetch(`/api/map/${portofolioId}`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ data }),
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Accept: "application/json",
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
return await respone.json().catch(() => null);
|
||||
};
|
||||
@@ -12,7 +12,16 @@ import { useState } from "react";
|
||||
import { map_funCreatePin } from "../../fun/create/fun_create_pin";
|
||||
import { funGlobal_UploadToStorage } from "@/app_modules/_global/fun";
|
||||
import { DIRECTORY_ID } from "@/app/lib";
|
||||
import { clientLogger } from "@/util/clientLogger";
|
||||
import { data } from "autoprefixer";
|
||||
import { apiCreatePinMap } from "../api_fetch_map";
|
||||
|
||||
interface ICreatePinMAp {
|
||||
latitude: string;
|
||||
longitude: string;
|
||||
namePin: string;
|
||||
imageId: string;
|
||||
}
|
||||
export function ComponentMap_ButtonSavePin({
|
||||
namePin,
|
||||
lat,
|
||||
@@ -29,43 +38,72 @@ export function ComponentMap_ButtonSavePin({
|
||||
const router = useRouter();
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
async function onSavePin() {
|
||||
const handleUploadImage = async () => {
|
||||
const uploadResult = await funGlobal_UploadToStorage({
|
||||
file: file,
|
||||
dirId: DIRECTORY_ID.map_image,
|
||||
});
|
||||
|
||||
if (!uploadResult.success) {
|
||||
setLoading(false);
|
||||
ComponentGlobal_NotifikasiPeringatan("Gagal upload gambar");
|
||||
}
|
||||
|
||||
return uploadResult.data.id;
|
||||
};
|
||||
|
||||
const handleCreatePin = async (imageId: string) => {
|
||||
const newData: ICreatePinMAp = {
|
||||
latitude: lat,
|
||||
longitude: long,
|
||||
namePin: namePin,
|
||||
imageId: imageId,
|
||||
};
|
||||
|
||||
const respone = await apiCreatePinMap({
|
||||
portofolioId: portofolioId,
|
||||
data: newData,
|
||||
});
|
||||
|
||||
console.log("respone >", respone);
|
||||
|
||||
if (respone && respone.success) {
|
||||
ComponentGlobal_NotifikasiBerhasil(respone.message);
|
||||
router.back();
|
||||
}
|
||||
|
||||
return respone;
|
||||
};
|
||||
|
||||
const validateInput = () => {
|
||||
if (!namePin || !file) {
|
||||
ComponentGlobal_NotifikasiPeringatan(
|
||||
"Nama pin dan file gambar harus diisi"
|
||||
);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
const onSavePin = async () => {
|
||||
if (!validateInput()) return;
|
||||
|
||||
try {
|
||||
setLoading(true);
|
||||
|
||||
const uploadFile = await funGlobal_UploadToStorage({
|
||||
file: file,
|
||||
dirId: DIRECTORY_ID.map_image,
|
||||
});
|
||||
const imageId = await handleUploadImage();
|
||||
const createPinResult = await handleCreatePin(imageId);
|
||||
|
||||
if (!uploadFile.success) {
|
||||
if (!createPinResult.success) {
|
||||
setLoading(false);
|
||||
ComponentGlobal_NotifikasiPeringatan("Gagal upload gambar");
|
||||
return;
|
||||
ComponentGlobal_NotifikasiPeringatan("Gagal membuat pin");
|
||||
}
|
||||
|
||||
const imageId = uploadFile.data.id;
|
||||
|
||||
const res = await map_funCreatePin({
|
||||
data: {
|
||||
latitude: lat as any,
|
||||
longitude: long as any,
|
||||
namePin: namePin as any,
|
||||
imageId: imageId,
|
||||
Portofolio: {
|
||||
create: { id: portofolioId } as any,
|
||||
},
|
||||
},
|
||||
});
|
||||
res.status === 200
|
||||
? (ComponentGlobal_NotifikasiBerhasil(res.message), router.back())
|
||||
: ComponentGlobal_NotifikasiGagal(res.message);
|
||||
} catch (error) {
|
||||
setLoading(false);
|
||||
|
||||
console.error(error);
|
||||
clientLogger.error("Error create pin", (error as Error).message);
|
||||
ComponentGlobal_NotifikasiGagal("Terjadi kesalahan saat menyimpan pin");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
@@ -73,7 +111,7 @@ export function ComponentMap_ButtonSavePin({
|
||||
loading={loading}
|
||||
my={"xl"}
|
||||
style={{ transition: "0.5s" }}
|
||||
disabled={namePin === "" || file === null}
|
||||
disabled={!namePin || !file}
|
||||
radius={"xl"}
|
||||
loaderPosition="center"
|
||||
bg={MainColor.yellow}
|
||||
|
||||
Reference in New Issue
Block a user