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 prisma from "@/lib/prisma";
|
||||||
|
import backendLogger from "@/util/backendLogger";
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import { NextRequest, NextResponse } from "next/server";
|
import { NextRequest, NextResponse } from "next/server";
|
||||||
|
|
||||||
export async function GET(
|
export { GET, PUT };
|
||||||
req: NextRequest,
|
|
||||||
{ params }: { params: { id: string } }
|
async function GET(req: NextRequest, { params }: { params: { id: string } }) {
|
||||||
) {
|
|
||||||
const get = await prisma.images.findUnique({
|
const get = await prisma.images.findUnique({
|
||||||
where: {
|
where: {
|
||||||
id: params.id,
|
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_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() {
|
export default async function Page() {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Portofolio_EditLogoBisnis />
|
<Portofolio_EditLogoBisnis />
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,9 @@
|
|||||||
export { apiCreatePortofolio, apiGetPortofolioById, apiUpdatePortofolioById };
|
export {
|
||||||
|
apiCreatePortofolio,
|
||||||
|
apiGetPortofolioById,
|
||||||
|
apiUpdatePortofolioById,
|
||||||
|
apiUpdateLogoPortofolioById,
|
||||||
|
};
|
||||||
|
|
||||||
const apiCreatePortofolio = async ({
|
const apiCreatePortofolio = async ({
|
||||||
profileId,
|
profileId,
|
||||||
@@ -69,3 +74,48 @@ const apiUpdatePortofolioById = async ({
|
|||||||
|
|
||||||
return await respone.json();
|
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";
|
} from "@/app_modules/_global/notif_global";
|
||||||
import { Box, Button } from "@mantine/core";
|
import { Box, Button } from "@mantine/core";
|
||||||
|
|
||||||
import { DIRECTORY_ID } from "@/lib";
|
|
||||||
import {
|
import {
|
||||||
funGlobal_DeleteFileById,
|
funGlobal_DeleteFileById,
|
||||||
funGlobal_UploadToStorage,
|
funGlobal_UploadToStorage,
|
||||||
} from "@/app_modules/_global/fun";
|
} from "@/app_modules/_global/fun";
|
||||||
|
import { DIRECTORY_ID } from "@/lib";
|
||||||
import { clientLogger } from "@/util/clientLogger";
|
import { clientLogger } from "@/util/clientLogger";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import { useState } from "react";
|
import { useState } from "react";
|
||||||
import { portofolio_funEditLogoBisnisById } from "../../fun";
|
import { apiUpdateLogoPortofolioById } from "../api_fetch_portofolio";
|
||||||
|
|
||||||
export function ComponentPortofolio_ButtonEditLogoBisnis({
|
export function ComponentPortofolio_ButtonEditLogoBisnis({
|
||||||
file,
|
file,
|
||||||
@@ -29,6 +29,7 @@ export function ComponentPortofolio_ButtonEditLogoBisnis({
|
|||||||
}) {
|
}) {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
|
|
||||||
async function onUpdate() {
|
async function onUpdate() {
|
||||||
try {
|
try {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
@@ -55,18 +56,20 @@ export function ComponentPortofolio_ButtonEditLogoBisnis({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const logoId = uploadFileToStorage.data.id;
|
const logoId = uploadFileToStorage.data.id;
|
||||||
const res = await portofolio_funEditLogoBisnisById({
|
|
||||||
portofolioId: portofolioId,
|
const response = await apiUpdateLogoPortofolioById({
|
||||||
logoId: logoId,
|
id: portofolioId,
|
||||||
|
data: logoId,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (res.status === 200) {
|
if (!response) {
|
||||||
ComponentGlobal_NotifikasiBerhasil(res.message);
|
|
||||||
router.back();
|
|
||||||
} else {
|
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
ComponentGlobal_NotifikasiGagal(res.message);
|
ComponentGlobal_NotifikasiGagal("Gagal update logo");
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ComponentGlobal_NotifikasiBerhasil("Berhasil mengubah Logo Bisnis!");
|
||||||
|
router.back();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
clientLogger.error("Error update logo", error);
|
clientLogger.error("Error update logo", error);
|
||||||
|
|||||||
@@ -60,7 +60,9 @@ export default function Portofolio_EditLogoBisnis() {
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{img ? (
|
{img ? (
|
||||||
<Image maw={250} alt="Image" src={img} />
|
<Center>
|
||||||
|
<Image maw={250} alt="Image" src={img} />
|
||||||
|
</Center>
|
||||||
) : (
|
) : (
|
||||||
<ComponentGlobal_LoadImage fileId={data.logoId} />
|
<ComponentGlobal_LoadImage fileId={data.logoId} />
|
||||||
)}
|
)}
|
||||||
|
|||||||
Reference in New Issue
Block a user