Fix upload image profile and portofolio

This commit is contained in:
2024-12-13 07:37:31 +08:00
parent 03c11c617c
commit 7cbbcd0370
16 changed files with 824 additions and 608 deletions

View File

@@ -12,25 +12,22 @@ import _ from "lodash";
import { useRouter } from "next/navigation";
import { useState } from "react";
import funCreatePortofolio from "../../fun/fun_create_portofolio";
import { funGlobal_UploadToStorage } from "@/app_modules/_global/fun";
import { DIRECTORY_ID } from "@/app/lib";
export function Portofolio_ComponentButtonSelanjutnya({
profileId,
dataPortofolio,
file,
dataMedsos,
imageId,
}: {
profileId: string;
dataPortofolio: MODEL_PORTOFOLIO_OLD;
file: File;
dataMedsos: any;
imageId: string
}) {
const router = useRouter();
const [loading, setLoading] = useState(false);
async function onSubmit() {
setLoading(true);
const porto = {
namaBisnis: dataPortofolio.namaBisnis,
masterBidangBisnisId: dataPortofolio.masterBidangBisnisId,
@@ -39,34 +36,33 @@ export function Portofolio_ComponentButtonSelanjutnya({
deskripsi: dataPortofolio.deskripsi,
};
if (_.values(porto).includes(""))
return ComponentGlobal_NotifikasiPeringatan("Lengkapi Data");
const uploadFileToStorage = await funGlobal_UploadToStorage({
file: file,
dirId: DIRECTORY_ID.portofolio_logo,
});
if (!uploadFileToStorage.success)
return ComponentGlobal_NotifikasiPeringatan("Gagal upload gambar");
const res = await funCreatePortofolio({
profileId: profileId,
data: dataPortofolio as any,
medsos: dataMedsos,
fileId: uploadFileToStorage.data.id,
});
if (res.status === 201) {
ComponentGlobal_NotifikasiBerhasil("Berhasil disimpan");
router.replace(RouterMap.create + res.id, { scroll: false });
} else {
ComponentGlobal_NotifikasiGagal("Gagal disimpan");
try {
setLoading(true);
if (_.values(porto).includes("")) {
return ComponentGlobal_NotifikasiPeringatan("Lengkapi Data");
}
const res = await funCreatePortofolio({
profileId: profileId,
data: dataPortofolio as any,
medsos: dataMedsos,
fileId: imageId,
});
if (res.status === 201) {
ComponentGlobal_NotifikasiBerhasil("Berhasil disimpan");
router.replace(RouterMap.create + res.id, { scroll: false });
} else {
ComponentGlobal_NotifikasiGagal("Gagal disimpan");
}
} catch (error) {
console.error(error);
} finally {
setLoading(false);
}
}
return (
<>
<Button
disabled={_.values(dataPortofolio).includes("") || file === null}
disabled={_.values(dataPortofolio).includes("") || imageId == ""}
mt={"md"}
radius={50}
loading={loading ? true : false}

View File

@@ -25,6 +25,11 @@ import { Portofolio_ComponentButtonSelanjutnya } from "../component";
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";
import {
funGlobal_DeleteFileById,
funGlobal_UploadToStorage,
} from "@/app_modules/_global/fun";
import { DIRECTORY_ID } from "@/app/lib";
export default function CreatePortofolio({
bidangBisnis,
@@ -49,8 +54,8 @@ export default function CreatePortofolio({
tiktok: "",
});
const [file, setFile] = useState<File | any>(null);
const [img, setImg] = useState<any | null>(null);
const [imageId, setImageId] = useState("");
return (
<>
@@ -187,15 +192,61 @@ export default function CreatePortofolio({
const buffer = URL.createObjectURL(
new Blob([new Uint8Array(await files.arrayBuffer())])
);
if (files.size > MAX_SIZE) {
setImg(null);
setFile(null);
ComponentGlobal_NotifikasiPeringatan(
PemberitahuanMaksimalFile
);
return;
}
// if (files.size > MAX_SIZE) {
// setImg(null);
// setFile(null);
// ComponentGlobal_NotifikasiPeringatan(
// PemberitahuanMaksimalFile
// );
// } else {
// setImg(buffer);
// setFile(files);
// }
if (imageId != "") {
const deletePhoto = await funGlobal_DeleteFileById({
fileId: imageId,
});
if (deletePhoto.success) {
setImageId("");
const uploadPhoto = await funGlobal_UploadToStorage({
file: files,
dirId: DIRECTORY_ID.portofolio_logo,
});
if (uploadPhoto.success) {
setImageId(uploadPhoto.data.id);
setImg(buffer);
} else {
ComponentGlobal_NotifikasiPeringatan(
"Gagal upload foto"
);
}
}
} else {
setImg(buffer);
setFile(files);
const uploadPhoto = await funGlobal_UploadToStorage({
file: files,
dirId: DIRECTORY_ID.portofolio_logo,
});
if (uploadPhoto.success) {
setImageId(uploadPhoto.data.id);
setImg(buffer);
} else {
ComponentGlobal_NotifikasiPeringatan("Gagal upload foto");
}
}
} catch (error) {
console.log(error);
@@ -306,12 +357,10 @@ export default function CreatePortofolio({
<Portofolio_ComponentButtonSelanjutnya
dataPortofolio={dataPortofolio as any}
dataMedsos={dataMedsos}
file={file}
profileId={profileId}
imageId={imageId}
/>
</Stack>
{/* <pre> {JSON.stringify(bidangBisnis, null, 2)}</pre> */}
</>
);
}

View File

@@ -1,9 +1,7 @@
"use client";
import { DIRECTORY_ID } from "@/app/lib";
import { RouterHome } from "@/app/lib/router_hipmi/router_home";
import { AccentColor, MainColor } from "@/app_modules/_global/color";
import { funGlobal_UploadToStorage } from "@/app_modules/_global/fun";
import {
ComponentGlobal_NotifikasiBerhasil,
ComponentGlobal_NotifikasiGagal,
@@ -19,14 +17,10 @@ import { MODEL_PROFILE } from "../../model/interface";
export function Profile_ComponentCreateNewProfile({
value,
// filePP,
// fileBG,
fotoProfileId,
backgroundProfileId,
}: {
value: MODEL_PROFILE;
// filePP: File;
// fileBG: File;
fotoProfileId: string;
backgroundProfileId: string;
}) {
@@ -55,24 +49,6 @@ export function Profile_ComponentCreateNewProfile({
try {
setLoading(true);
// const uploadPhoto = await funGlobal_UploadToStorage({
// file: filePP,
// dirId: DIRECTORY_ID.profile_foto,
// });
// if (!uploadPhoto.success) {
// ComponentGlobal_NotifikasiPeringatan("Gagal upload foto profile");
// return;
// }
// const uploadBackground = await funGlobal_UploadToStorage({
// file: fileBG,
// dirId: DIRECTORY_ID.profile_background,
// });
// if (!uploadBackground.success) {
// ComponentGlobal_NotifikasiPeringatan("Gagal upload background profile");
// return;
// }
const create = await funCreateNewProfile({
data: newData as any,
imageId: fotoProfileId,
@@ -91,7 +67,6 @@ export function Profile_ComponentCreateNewProfile({
if (create.status === 500) {
ComponentGlobal_NotifikasiGagal(create.message);
}
} catch (error) {
console.log("Terjadi kesalahan", error);
} finally {

View File

@@ -7,7 +7,10 @@ import {
ComponentGlobal_BoxUploadImage,
ComponentGlobal_ErrorInput,
} from "@/app_modules/_global/component";
import { funGlobal_UploadToStorage } from "@/app_modules/_global/fun";
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";
@@ -95,6 +98,35 @@ export default function CreateProfile() {
ComponentGlobal_NotifikasiPeringatan(
PemberitahuanMaksimalFile
);
setImgPP(null);
setFilePP(null);
return;
}
if (fotoProfileId != "") {
const deleteFotoProfile = await funGlobal_DeleteFileById({
fileId: fotoProfileId,
});
if (deleteFotoProfile.success) {
setFotoProfileId("");
const uploadPhoto = await funGlobal_UploadToStorage({
file: files,
dirId: DIRECTORY_ID.profile_foto,
});
if (uploadPhoto.success) {
setFotoProfileId(uploadPhoto.data.id);
setImgPP(buffer);
setFilePP(files);
} else {
ComponentGlobal_NotifikasiPeringatan(
"Gagal upload foto profile"
);
}
}
} else {
const uploadPhoto = await funGlobal_UploadToStorage({
file: files,
@@ -169,6 +201,35 @@ export default function CreateProfile() {
ComponentGlobal_NotifikasiPeringatan(
PemberitahuanMaksimalFile
);
setImgBG(null);
setFileBG(null);
return;
}
if (backgroundProfileId != "") {
const deleteFotoBg = await funGlobal_DeleteFileById({
fileId: backgroundProfileId,
});
if (deleteFotoBg.success) {
setBackgroundProfileId("");
const uploadBackground =
await funGlobal_UploadToStorage({
file: files,
dirId: DIRECTORY_ID.profile_background,
});
if (uploadBackground.success) {
setBackgroundProfileId(uploadBackground.data.id);
setImgBG(buffer);
setFileBG(files);
} else {
ComponentGlobal_NotifikasiPeringatan(
"Gagal upload background profile"
);
}
}
} else {
const uploadBackground = await funGlobal_UploadToStorage({
file: files,
@@ -284,8 +345,6 @@ export default function CreateProfile() {
<Profile_ComponentCreateNewProfile
value={value as any}
// filePP={filePP as any}
// fileBG={fileBG as any}
fotoProfileId={fotoProfileId}
backgroundProfileId={backgroundProfileId}
/>