This commit is contained in:
faisolavolut 2025-01-21 15:22:55 +07:00
parent c3dbeb8c33
commit bec966b3fa
3 changed files with 29 additions and 10 deletions

View File

@ -117,8 +117,12 @@ export const ThumbPreview = ({
); );
}; };
export const FilePreview = ({ url }: { url: string }) => { export const FilePreview = ({ url }: { url: any }) => {
const file = getFileName(url); let ural = url;
if (url instanceof File) {
ural = `${URL.createObjectURL(url)}.${url.name.split(".").pop()}`;
}
const file = getFileName(ural);
if (typeof file === "string") if (typeof file === "string")
return ( return (
<div <div
@ -164,13 +168,14 @@ export const FilePreview = ({ url }: { url: string }) => {
parts.pop(); // Hapus bagian terakhir (ekstensi) parts.pop(); // Hapus bagian terakhir (ekstensi)
return parts.join("."); // Gabungkan kembali return parts.join("."); // Gabungkan kembali
}; };
const ura = url.startsWith("blob:") ? getFileNameWithoutExtension(url) : url; const ura =
if ([".png", ".jpeg", ".jpg", ".webp"].find((e) => url.endsWith(e))) { ural && ural.startsWith("blob:") ? getFileNameWithoutExtension(ural) : ural;
if ([".png", ".jpeg", ".jpg", ".webp"].find((e) => ural.endsWith(e))) {
content = ( content = (
<div className="rounded-lg flex-grow overflow-hidden"> <div className="rounded-lg flex-grow overflow-hidden">
<img <img
onClick={() => { onClick={() => {
let _url = siteurl(url || ""); let _url = siteurl(ural || "");
window.open(_url, "_blank"); window.open(_url, "_blank");
}} }}
className={cx( className={cx(
@ -195,7 +200,7 @@ export const FilePreview = ({ url }: { url: string }) => {
background-position: 0 0, 12.5px 0, 12.5px -12.5px, 0px 12.5px; /* Must be half of one side of the square */ background-position: 0 0, 12.5px 0, 12.5px -12.5px, 0px 12.5px; /* Must be half of one side of the square */
` `
)} )}
src={url.startsWith("blob:") ? ura : url} src={ural && ural.startsWith("blob:") ? ura : ural}
/> />
</div> </div>
); );
@ -216,7 +221,8 @@ export const FilePreview = ({ url }: { url: string }) => {
` `
)} )}
onClick={() => { onClick={() => {
let _url: any = url.startsWith("blob:") ? ura : siteurl(ura || ""); let _url: any =
url && url.startsWith("blob:") ? ura : siteurl(ura || "");
console.log(_url); console.log(_url);
window.open(_url, "_blank"); window.open(_url, "_blank");
}} }}
@ -269,7 +275,7 @@ function generateRandomColor(str: string): string {
return color; return color;
} }
const getFileName = (url: string) => { const getFileName = (url: string) => {
if (url && url.startsWith("[")) { if (url && typeof url === "string" && url.startsWith("[")) {
try { try {
const list = JSON.parse(url); const list = JSON.parse(url);
if (list.length === 0) return "Empty"; if (list.length === 0) return "Empty";

View File

@ -1,6 +1,6 @@
import get from "lodash.get"; import get from "lodash.get";
import { Loader2, Paperclip, Trash2, Upload } from "lucide-react"; import { Loader2, Paperclip, Trash2, Upload } from "lucide-react";
import { ChangeEvent, FC } from "react"; import { ChangeEvent, FC, useEffect } from "react";
import * as XLSX from "xlsx"; import * as XLSX from "xlsx";
import { useLocal } from "@/lib/utils/use-local"; import { useLocal } from "@/lib/utils/use-local";
import { siteurl } from "@/lib/utils/siteurl"; import { siteurl } from "@/lib/utils/siteurl";
@ -26,6 +26,15 @@ export const FieldUploadSingle: FC<{
preview: null as any, preview: null as any,
isLocal: false, isLocal: false,
}); });
useEffect(() => {
if (value instanceof File) {
input.preview = `${URL.createObjectURL(value)}.${value.name
.split(".")
.pop()}`;
input.isLocal = true;
input.render();
}
}, []);
const on_upload = async (event: ChangeEvent<HTMLInputElement>) => { const on_upload = async (event: ChangeEvent<HTMLInputElement>) => {
let file = null; let file = null;

View File

@ -39,10 +39,14 @@ export const apix = async ({
try { try {
// Convert data to FormData if type is "form" // Convert data to FormData if type is "form"
const execption = ["certificate"];
const requestData = const requestData =
type === "form" && data type === "form" && data
? Object.entries(data as any).reduce((formData, [key, value]) => { ? Object.entries(data as any).reduce((formData, [key, value]) => {
formData.append(key.replace(/\[\d+\]/, ""), value as any); formData.append(
key.includes("certificate") ? key : key.replace(/\[\d+\]/, ""),
value as any
);
return formData; return formData;
}, new FormData()) }, new FormData())
: data; : data;