fix edit logo portofolio
This commit is contained in:
@@ -1 +1 @@
|
||||
nice -n 19 bun --env-file=.env.build run build
|
||||
nice -n 19 bun --env-file=.env.build run --bun build
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import prisma from "@/lib/prisma";
|
||||
import backendLogger from "@/util/backendLogger";
|
||||
import fs from "fs";
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
|
||||
export async function GET(
|
||||
req: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
export { GET, PUT };
|
||||
|
||||
async function GET(req: NextRequest, { params }: { params: { id: string } }) {
|
||||
const get = await prisma.images.findUnique({
|
||||
where: {
|
||||
id: params.id,
|
||||
@@ -30,3 +30,52 @@ export async function GET(
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async function PUT(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
if (request.method !== "PUT") {
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: "Method not allowed",
|
||||
},
|
||||
{ status: 405 }
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
const { id } = params;
|
||||
const portofolioId = id;
|
||||
|
||||
const { data } = await request.json();
|
||||
const logoId = data;
|
||||
|
||||
const updatePorto = await prisma.portofolio.update({
|
||||
where: {
|
||||
id: portofolioId,
|
||||
},
|
||||
data: {
|
||||
logoId: logoId,
|
||||
},
|
||||
});
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
message: "Berhasil mengubah Logo Bisnis!",
|
||||
data: updatePorto,
|
||||
},
|
||||
{ status: 200 } // default status: 200
|
||||
);
|
||||
} catch (error) {
|
||||
backendLogger.error("API Error Update Logo Portofolio", error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: false,
|
||||
message: "Internal Server Error",
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
import { Portofolio_EditLogoBisnis } from "@/app_modules/katalog/portofolio";
|
||||
import { portofolio_getOneById } from "@/app_modules/katalog/portofolio/fun/get/get_one_portofolio";
|
||||
import _ from "lodash";
|
||||
|
||||
export default async function Page() {
|
||||
|
||||
return (
|
||||
<>
|
||||
<Portofolio_EditLogoBisnis />
|
||||
<Portofolio_EditLogoBisnis />
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,9 @@
|
||||
export { apiCreatePortofolio, apiGetPortofolioById, apiUpdatePortofolioById };
|
||||
export {
|
||||
apiCreatePortofolio,
|
||||
apiGetPortofolioById,
|
||||
apiUpdatePortofolioById,
|
||||
apiUpdateLogoPortofolioById,
|
||||
};
|
||||
|
||||
const apiCreatePortofolio = async ({
|
||||
profileId,
|
||||
@@ -69,3 +74,48 @@ const apiUpdatePortofolioById = async ({
|
||||
|
||||
return await respone.json();
|
||||
};
|
||||
|
||||
const apiUpdateLogoPortofolioById = async ({
|
||||
id,
|
||||
data,
|
||||
}: {
|
||||
id: string;
|
||||
data: any;
|
||||
}) => {
|
||||
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 response = await fetch(`/api/portofolio/logo/${id}`, {
|
||||
method: "PUT",
|
||||
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 update portfolio logo:",
|
||||
response.statusText,
|
||||
errorData
|
||||
);
|
||||
throw new Error(errorData?.message || "Failed to update portfolio logo");
|
||||
}
|
||||
|
||||
// Return the JSON response
|
||||
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
|
||||
}
|
||||
};
|
||||
@@ -8,15 +8,15 @@ import {
|
||||
} from "@/app_modules/_global/notif_global";
|
||||
import { Box, Button } from "@mantine/core";
|
||||
|
||||
import { DIRECTORY_ID } from "@/lib";
|
||||
import {
|
||||
funGlobal_DeleteFileById,
|
||||
funGlobal_UploadToStorage,
|
||||
} from "@/app_modules/_global/fun";
|
||||
import { DIRECTORY_ID } from "@/lib";
|
||||
import { clientLogger } from "@/util/clientLogger";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useState } from "react";
|
||||
import { portofolio_funEditLogoBisnisById } from "../../fun";
|
||||
import { apiUpdateLogoPortofolioById } from "../api_fetch_portofolio";
|
||||
|
||||
export function ComponentPortofolio_ButtonEditLogoBisnis({
|
||||
file,
|
||||
@@ -29,6 +29,7 @@ export function ComponentPortofolio_ButtonEditLogoBisnis({
|
||||
}) {
|
||||
const router = useRouter();
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
async function onUpdate() {
|
||||
try {
|
||||
setLoading(true);
|
||||
@@ -55,18 +56,20 @@ export function ComponentPortofolio_ButtonEditLogoBisnis({
|
||||
}
|
||||
|
||||
const logoId = uploadFileToStorage.data.id;
|
||||
const res = await portofolio_funEditLogoBisnisById({
|
||||
portofolioId: portofolioId,
|
||||
logoId: logoId,
|
||||
|
||||
const response = await apiUpdateLogoPortofolioById({
|
||||
id: portofolioId,
|
||||
data: logoId,
|
||||
});
|
||||
|
||||
if (res.status === 200) {
|
||||
ComponentGlobal_NotifikasiBerhasil(res.message);
|
||||
router.back();
|
||||
} else {
|
||||
if (!response) {
|
||||
setLoading(false);
|
||||
ComponentGlobal_NotifikasiGagal(res.message);
|
||||
ComponentGlobal_NotifikasiGagal("Gagal update logo");
|
||||
return;
|
||||
}
|
||||
|
||||
ComponentGlobal_NotifikasiBerhasil("Berhasil mengubah Logo Bisnis!");
|
||||
router.back();
|
||||
} catch (error) {
|
||||
setLoading(false);
|
||||
clientLogger.error("Error update logo", error);
|
||||
|
||||
@@ -60,7 +60,9 @@ export default function Portofolio_EditLogoBisnis() {
|
||||
}}
|
||||
>
|
||||
{img ? (
|
||||
<Image maw={250} alt="Image" src={img} />
|
||||
<Center>
|
||||
<Image maw={250} alt="Image" src={img} />
|
||||
</Center>
|
||||
) : (
|
||||
<ComponentGlobal_LoadImage fileId={data.logoId} />
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user