fix portofolio
deskripsiL: - fix server acton create to API
This commit is contained in:
@@ -1,3 +0,0 @@
|
||||
git add -A
|
||||
git commit -m "data auto"
|
||||
git push origin bagas/10-feb-25
|
||||
74
src/app/api/map/[id]/route.ts
Normal file
74
src/app/api/map/[id]/route.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { prisma } from "@/app/lib";
|
||||
import { funGetUserIdByToken } from "@/app_modules/_global/fun/get";
|
||||
import backendLogger from "@/util/backendLogger";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
export { POST };
|
||||
|
||||
async function POST(request: Request, { params }: { params: { id: string } }) {
|
||||
if (request.method !== "POST") {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: "Method not allowed",
|
||||
},
|
||||
{ status: 405 }
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
const userLoginId = await funGetUserIdByToken();
|
||||
if (!userLoginId) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: "User tidak ditemukan",
|
||||
},
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
const { id } = params;
|
||||
const { data } = await request.json();
|
||||
|
||||
const created = await prisma.businessMaps.create({
|
||||
data: {
|
||||
latitude: data.latitude,
|
||||
longitude: data.longitude,
|
||||
namePin: data.namePin,
|
||||
imageId: data.imageId,
|
||||
portofolioId: id,
|
||||
authorId: userLoginId,
|
||||
},
|
||||
});
|
||||
|
||||
if (!created) {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: "Gagal membuat pin map",
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
message: "Berhasil membuat portofolio",
|
||||
data: created,
|
||||
},
|
||||
{ status: 201 }
|
||||
);
|
||||
} catch (error) {
|
||||
backendLogger.error("Error create pin map", error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: "Gagal membuat pin map",
|
||||
reason: (error as Error).message,
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,15 +1,8 @@
|
||||
import { prisma } from "@/app/lib";
|
||||
import backendLogger from "@/util/backendLogger";
|
||||
import { clientLogger } from "@/util/clientLogger";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
export async function GET(request: Request) {
|
||||
if (request.method !== "GET") {
|
||||
return NextResponse.json(
|
||||
{ success: false, message: "Method not allowed" },
|
||||
{ status: 405 }
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
let fixData;
|
||||
|
||||
@@ -18,25 +18,53 @@ async function POST(request: Request, { params }: { params: { id: string } }) {
|
||||
const { id } = params;
|
||||
const { data } = await request.json();
|
||||
|
||||
// const createPortofolio = await prisma.portofolio.create({
|
||||
// data: {
|
||||
// profileId: id,
|
||||
// id_Portofolio: "Porto" + Date.now().toString(),
|
||||
// namaBisnis: data.namaBisnis,
|
||||
// deskripsi: data.deskripsi,
|
||||
// tlpn: data.tlpn,
|
||||
// alamatKantor: data.alamatKantor,
|
||||
// masterBidangBisnisId: data.masterBidangBisnisId,
|
||||
// logoId: data.fileId,
|
||||
// },
|
||||
// });
|
||||
const createPortofolio = await prisma.portofolio.create({
|
||||
data: {
|
||||
profileId: id,
|
||||
id_Portofolio: "Porto" + Date.now().toString(),
|
||||
namaBisnis: data.namaBisnis,
|
||||
deskripsi: data.deskripsi,
|
||||
tlpn: data.tlpn,
|
||||
alamatKantor: data.alamatKantor,
|
||||
masterBidangBisnisId: data.masterBidangBisnisId,
|
||||
logoId: data.fileId,
|
||||
},
|
||||
});
|
||||
|
||||
if (!createPortofolio)
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: "Gagal membuat portofolio",
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
|
||||
const createMedsos = await prisma.portofolio_MediaSosial.create({
|
||||
data: {
|
||||
portofolioId: createPortofolio.id,
|
||||
facebook: data?.facebook,
|
||||
instagram: data?.instagram,
|
||||
tiktok: data?.tiktok,
|
||||
twitter: data?.twitter,
|
||||
youtube: data?.youtube,
|
||||
},
|
||||
});
|
||||
|
||||
if (!createMedsos)
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: "Gagal menambahkan medsos",
|
||||
},
|
||||
{ status: 400 }
|
||||
);
|
||||
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
message: "Berhasil mendapatkan data",
|
||||
id,
|
||||
data,
|
||||
data: createPortofolio,
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import { Map_CreateNewPin } from "@/app_modules/map/view";
|
||||
|
||||
export default async function Page({ params }: { params: { id: string } }) {
|
||||
let portofolioId = params.id;
|
||||
export default async function Page() {
|
||||
return (
|
||||
<>
|
||||
<Map_CreateNewPin portofolioId={portofolioId} />
|
||||
<Map_CreateNewPin />
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,14 +1,9 @@
|
||||
import { CreatePortofolio } from "@/app_modules/katalog/portofolio";
|
||||
import { Portofolio_getMasterBidangBisnis } from "@/app_modules/katalog/portofolio/fun/master/get_bidang_bisnis";
|
||||
|
||||
export default async function Page() {
|
||||
// const profileId = params.id;
|
||||
// const bidangBisnis = await Portofolio_getMasterBidangBisnis();
|
||||
|
||||
return (
|
||||
<>
|
||||
<CreatePortofolio
|
||||
/>
|
||||
<CreatePortofolio />
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -2,11 +2,11 @@ export {
|
||||
apiCreatePortofolio,
|
||||
};
|
||||
|
||||
const apiCreatePortofolio = async ({ data }: { data: any }) => {
|
||||
const apiCreatePortofolio = async ({ profileId, data }: { profileId: string, data: any }) => {
|
||||
const { token } = await fetch("/api/get-cookie").then((res) => res.json());
|
||||
if (!token) return await token.json().catch(() => null);
|
||||
|
||||
const res = await fetch(`/api/portofolio`, {
|
||||
const res = await fetch(`/api/portofolio/${profileId}`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ data }),
|
||||
headers: {
|
||||
|
||||
@@ -14,7 +14,6 @@ import { funGlobal_UploadToStorage } from "@/app_modules/_global/fun";
|
||||
import { clientLogger } from "@/util/clientLogger";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useState } from "react";
|
||||
import funCreatePortofolio from "../../fun/fun_create_portofolio";
|
||||
import { apiCreatePortofolio } from "../api_fetch_portofolio";
|
||||
|
||||
interface ICreatePortofolio {
|
||||
@@ -45,17 +44,52 @@ export function Portofolio_ComponentButtonSelanjutnya({
|
||||
const router = useRouter();
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
async function onSubmit() {
|
||||
if (_.values(dataPortofolio).includes("")) {
|
||||
const validateData = () => {
|
||||
if (_.includes(_.values(dataPortofolio), "")) {
|
||||
ComponentGlobal_NotifikasiPeringatan("Lengkapi Data");
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (dataPortofolio.tlpn.length < 10) {
|
||||
ComponentGlobal_NotifikasiPeringatan("Nomor telepon minimal 10 angka");
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
const handleCreatePortofolio = async (fileId: string) => {
|
||||
const newData: ICreatePortofolio = {
|
||||
namaBisnis: dataPortofolio.namaBisnis,
|
||||
masterBidangBisnisId: dataPortofolio.masterBidangBisnisId,
|
||||
alamatKantor: dataPortofolio.alamatKantor,
|
||||
tlpn: dataPortofolio.tlpn,
|
||||
deskripsi: dataPortofolio.deskripsi,
|
||||
facebook: dataMedsos.facebook,
|
||||
twitter: dataMedsos.twitter,
|
||||
instagram: dataMedsos.instagram,
|
||||
tiktok: dataMedsos.tiktok,
|
||||
youtube: dataMedsos.youtube,
|
||||
fileId: fileId,
|
||||
};
|
||||
|
||||
const response = await apiCreatePortofolio({
|
||||
profileId: profileId,
|
||||
data: newData,
|
||||
});
|
||||
|
||||
if (response.success) {
|
||||
ComponentGlobal_NotifikasiBerhasil("Berhasil disimpan");
|
||||
router.replace(RouterMap.create + response.data.id, { scroll: false });
|
||||
} else {
|
||||
setLoading(false);
|
||||
throw new Error("Failed to create portfolio");
|
||||
}
|
||||
};
|
||||
|
||||
const onSubmit = async () => {
|
||||
if (!validateData()) return;
|
||||
|
||||
try {
|
||||
setLoading(true);
|
||||
|
||||
@@ -65,59 +99,22 @@ export function Portofolio_ComponentButtonSelanjutnya({
|
||||
});
|
||||
|
||||
if (!uploadFile.success) {
|
||||
setLoading(false);
|
||||
ComponentGlobal_NotifikasiPeringatan("Gagal upload gambar");
|
||||
return;
|
||||
}
|
||||
|
||||
const fileId = uploadFile.data.id;
|
||||
|
||||
const newData: ICreatePortofolio = {
|
||||
namaBisnis: dataPortofolio.namaBisnis,
|
||||
masterBidangBisnisId: dataPortofolio.masterBidangBisnisId,
|
||||
alamatKantor: dataPortofolio.alamatKantor,
|
||||
tlpn: dataPortofolio.tlpn,
|
||||
deskripsi: dataPortofolio.deskripsi,
|
||||
facebook: dataMedsos.facebook,
|
||||
twitter: dataMedsos.twitter,
|
||||
instagram: dataMedsos.instagram,
|
||||
tiktok: dataMedsos.tiktok,
|
||||
youtube: dataMedsos.youtube,
|
||||
fileId: fileId,
|
||||
};
|
||||
|
||||
// const responeCreated = await apiCreatePortofolio({
|
||||
// data: newData,
|
||||
// });
|
||||
|
||||
// if (responeCreated.success) {
|
||||
// ComponentGlobal_NotifikasiBerhasil("Berhasil disimpan");
|
||||
// router.replace(RouterMap.create + responeCreated.id, { scroll: false });
|
||||
// }
|
||||
|
||||
|
||||
const res = await funCreatePortofolio({
|
||||
profileId: profileId,
|
||||
data: dataPortofolio as any,
|
||||
medsos: dataMedsos,
|
||||
fileId: fileId,
|
||||
});
|
||||
if (res.status === 201) {
|
||||
ComponentGlobal_NotifikasiBerhasil("Berhasil disimpan");
|
||||
router.replace(RouterMap.create + res.id, { scroll: false });
|
||||
} else {
|
||||
setLoading(false);
|
||||
ComponentGlobal_NotifikasiGagal("Gagal disimpan");
|
||||
}
|
||||
await handleCreatePortofolio(uploadFile.data.id);
|
||||
} catch (error) {
|
||||
setLoading(false);
|
||||
ComponentGlobal_NotifikasiGagal("Gagal disimpan");
|
||||
clientLogger.error("Error create portofolio", error);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<>
|
||||
<Button
|
||||
disabled={_.values(dataPortofolio).includes("") || file === null}
|
||||
disabled={_.values(dataPortofolio).includes("") || !file}
|
||||
mt={"md"}
|
||||
radius={50}
|
||||
loading={loading}
|
||||
|
||||
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}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
"use client";
|
||||
|
||||
import { DIRECTORY_ID } from "@/app/lib";
|
||||
import {
|
||||
AccentColor,
|
||||
MainColor,
|
||||
@@ -10,25 +9,17 @@ import {
|
||||
ComponentGlobal_BoxUploadImage,
|
||||
ComponentGlobal_ButtonUploadFileImage,
|
||||
} from "@/app_modules/_global/component";
|
||||
import {
|
||||
funGlobal_DeleteFileById,
|
||||
funGlobal_UploadToStorage,
|
||||
} from "@/app_modules/_global/fun";
|
||||
import { MAX_SIZE } from "@/app_modules/_global/lib";
|
||||
import { PemberitahuanMaksimalFile } from "@/app_modules/_global/lib/max_size";
|
||||
import { ComponentGlobal_NotifikasiPeringatan } from "@/app_modules/_global/notif_global/notifikasi_peringatan";
|
||||
import {
|
||||
AspectRatio,
|
||||
Button,
|
||||
Center,
|
||||
FileButton,
|
||||
Image,
|
||||
Paper,
|
||||
Stack,
|
||||
TextInput,
|
||||
TextInput
|
||||
} from "@mantine/core";
|
||||
import { IconCamera, IconPhoto } from "@tabler/icons-react";
|
||||
import { IconPhoto } from "@tabler/icons-react";
|
||||
import _ from "lodash";
|
||||
import { useParams } from "next/navigation";
|
||||
import { useState } from "react";
|
||||
import Map, {
|
||||
AttributionControl,
|
||||
@@ -39,13 +30,9 @@ import Map, {
|
||||
import { ComponentMap_ButtonSavePin } from "../_component";
|
||||
import { defaultLatLong, defaultMapZoom } from "../lib/default_lat_long";
|
||||
|
||||
export function UiMap_CreatePin({
|
||||
mapboxToken,
|
||||
portofolioId,
|
||||
}: {
|
||||
mapboxToken: string;
|
||||
portofolioId: string;
|
||||
}) {
|
||||
export function UiMap_CreatePin({ mapboxToken }: { mapboxToken: string }) {
|
||||
const params = useParams<{ id: string }>();
|
||||
const portofolioId = params.id;
|
||||
const [[lat, long], setLatLong] = useState([0, 0]);
|
||||
const [isPin, setIsPin] = useState(false);
|
||||
const [namePin, setNamePin] = useState("");
|
||||
|
||||
@@ -1,17 +1,10 @@
|
||||
import ComponentGlobal_IsEmptyData from "@/app_modules/_global/component/is_empty_data";
|
||||
import UIGlobal_LayoutHeaderTamplate from "@/app_modules/_global/ui/ui_header_tamplate";
|
||||
import UIGlobal_LayoutTamplate from "@/app_modules/_global/ui/ui_layout_tamplate";
|
||||
import { UiMap_CreatePin } from "../ui/ui_create_pin";
|
||||
import ComponentGlobal_IsEmptyData from "@/app_modules/_global/component/is_empty_data";
|
||||
|
||||
const mapboxToken = process.env.MAPBOX_TOKEN!;
|
||||
export async function Map_CreateNewPin({
|
||||
portofolioId,
|
||||
}: {
|
||||
portofolioId: string;
|
||||
}) {
|
||||
if (!mapboxToken)
|
||||
return <ComponentGlobal_IsEmptyData text="Mapbox token not found" />;
|
||||
|
||||
export async function Map_CreateNewPin() {
|
||||
return (
|
||||
<>
|
||||
<UIGlobal_LayoutTamplate
|
||||
@@ -19,10 +12,11 @@ export async function Map_CreateNewPin({
|
||||
<UIGlobal_LayoutHeaderTamplate title="Tambah Pin" hideButtonLeft />
|
||||
}
|
||||
>
|
||||
<UiMap_CreatePin
|
||||
mapboxToken={mapboxToken}
|
||||
portofolioId={portofolioId}
|
||||
/>
|
||||
{!mapboxToken ? (
|
||||
<ComponentGlobal_IsEmptyData text="Mapbox token not found" />
|
||||
) : (
|
||||
<UiMap_CreatePin mapboxToken={mapboxToken} />
|
||||
)}
|
||||
</UIGlobal_LayoutTamplate>
|
||||
</>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user