fix edit logo portofolio

This commit is contained in:
2025-02-13 11:49:39 +08:00
parent b21c17027a
commit 61c43ef72a
6 changed files with 122 additions and 21 deletions

View File

@@ -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
}
};

View File

@@ -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);

View File

@@ -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} />
)}