2025-01-17 05:23:28 +00:00
|
|
|
"use client";
|
|
|
|
|
import { FormEvent, Fragment, useEffect, useRef, useState } from "react";
|
|
|
|
|
import { Controller, useForm } from "react-hook-form";
|
|
|
|
|
import * as z from "zod";
|
|
|
|
|
import { zodResolver } from "@hookform/resolvers/zod";
|
|
|
|
|
import Swal from "sweetalert2";
|
|
|
|
|
import withReactContent from "sweetalert2-react-content";
|
|
|
|
|
import { Input, Textarea } from "@nextui-org/input";
|
|
|
|
|
import dynamic from "next/dynamic";
|
|
|
|
|
import JoditEditor from "jodit-react";
|
|
|
|
|
import { useDropzone } from "react-dropzone";
|
|
|
|
|
import { Button } from "@nextui-org/button";
|
|
|
|
|
import { CloudUploadIcon, TimesIcon } from "@/components/icons";
|
|
|
|
|
import Image from "next/image";
|
|
|
|
|
import { Switch } from "@nextui-org/switch";
|
2025-01-19 16:13:06 +00:00
|
|
|
import {
|
|
|
|
|
createArticle,
|
|
|
|
|
getArticleByCategory,
|
|
|
|
|
uploadArticleFile,
|
|
|
|
|
uploadArticleThumbnail,
|
|
|
|
|
} from "@/service/article";
|
2025-01-17 05:23:28 +00:00
|
|
|
import ReactSelect from "react-select";
|
|
|
|
|
import makeAnimated from "react-select/animated";
|
2025-01-19 16:13:06 +00:00
|
|
|
import { Checkbox, Chip } from "@nextui-org/react";
|
2025-01-17 10:23:54 +00:00
|
|
|
import GenerateSingleArticleForm from "./generate-ai-single-form";
|
|
|
|
|
import { htmlToString } from "@/utils/global";
|
|
|
|
|
import { close, error, loading } from "@/config/swal";
|
|
|
|
|
import { useRouter } from "next/navigation";
|
|
|
|
|
import Link from "next/link";
|
2025-01-20 12:10:04 +00:00
|
|
|
import { getCategoryById } from "@/service/master-categories";
|
2025-01-17 05:23:28 +00:00
|
|
|
|
2025-01-20 17:04:10 +00:00
|
|
|
const CustomEditor = dynamic(
|
|
|
|
|
() => {
|
|
|
|
|
return import("@/components/editor/custom-editor");
|
|
|
|
|
},
|
|
|
|
|
{ ssr: false }
|
|
|
|
|
);
|
2025-01-17 05:23:28 +00:00
|
|
|
|
|
|
|
|
interface FileWithPreview extends File {
|
|
|
|
|
preview: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface CategoryType {
|
|
|
|
|
id: number;
|
|
|
|
|
label: string;
|
|
|
|
|
value: number;
|
|
|
|
|
}
|
|
|
|
|
const categorySchema = z.object({
|
|
|
|
|
id: z.number(),
|
|
|
|
|
label: z.string(),
|
|
|
|
|
value: z.number(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const createArticleSchema = z.object({
|
|
|
|
|
title: z.string().min(2, {
|
|
|
|
|
message: "Judul harus diisi",
|
|
|
|
|
}),
|
|
|
|
|
slug: z.string().min(2, {
|
|
|
|
|
message: "Slug harus diisi",
|
|
|
|
|
}),
|
|
|
|
|
description: z.string().min(2, {
|
|
|
|
|
message: "Deskripsi harus diisi",
|
|
|
|
|
}),
|
|
|
|
|
category: z.array(categorySchema).nonempty({
|
|
|
|
|
message: "Kategori harus memiliki setidaknya satu item",
|
|
|
|
|
}),
|
|
|
|
|
tags: z.array(z.string()).nonempty({
|
|
|
|
|
message: "Minimal 1 tag",
|
2025-01-19 16:13:06 +00:00
|
|
|
}),
|
2025-01-17 05:23:28 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
export default function CreateArticleForm() {
|
|
|
|
|
const animatedComponents = makeAnimated();
|
|
|
|
|
const MySwal = withReactContent(Swal);
|
2025-01-17 10:23:54 +00:00
|
|
|
const router = useRouter();
|
2025-01-17 05:23:28 +00:00
|
|
|
const editor = useRef(null);
|
|
|
|
|
const [files, setFiles] = useState<FileWithPreview[]>([]);
|
|
|
|
|
const [useAi, setUseAI] = useState(false);
|
|
|
|
|
const [listCategory, setListCategory] = useState<CategoryType[]>([]);
|
|
|
|
|
const [tag, setTag] = useState("");
|
2025-01-19 16:13:06 +00:00
|
|
|
const [thumbnailImg, setThumbnailImg] = useState<File[]>([]);
|
|
|
|
|
const [selectedMainImage, setSelectedMainImage] = useState<number>();
|
2025-01-17 05:23:28 +00:00
|
|
|
|
|
|
|
|
const { getRootProps, getInputProps } = useDropzone({
|
|
|
|
|
onDrop: (acceptedFiles) => {
|
2025-01-19 16:13:06 +00:00
|
|
|
setFiles((prevFiles) => [
|
|
|
|
|
...prevFiles,
|
|
|
|
|
...acceptedFiles.map((file) => Object.assign(file)),
|
|
|
|
|
]);
|
2025-01-17 05:23:28 +00:00
|
|
|
},
|
2025-01-19 16:13:06 +00:00
|
|
|
multiple: true,
|
2025-01-17 05:23:28 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const formOptions = {
|
|
|
|
|
resolver: zodResolver(createArticleSchema),
|
|
|
|
|
defaultValues: { title: "", description: "", category: [], tags: [] },
|
|
|
|
|
};
|
|
|
|
|
type UserSettingSchema = z.infer<typeof createArticleSchema>;
|
|
|
|
|
const {
|
|
|
|
|
register,
|
|
|
|
|
control,
|
|
|
|
|
handleSubmit,
|
|
|
|
|
formState: { errors },
|
|
|
|
|
setValue,
|
|
|
|
|
getValues,
|
|
|
|
|
watch,
|
|
|
|
|
setError,
|
|
|
|
|
clearErrors,
|
|
|
|
|
} = useForm<UserSettingSchema>(formOptions);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
fetchCategory();
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
const fetchCategory = async () => {
|
|
|
|
|
const res = await getArticleByCategory();
|
|
|
|
|
if (res?.data?.data) {
|
|
|
|
|
setupCategory(res?.data?.data);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const setupCategory = (data: any) => {
|
|
|
|
|
const temp = [];
|
|
|
|
|
for (const element of data) {
|
|
|
|
|
temp.push({
|
|
|
|
|
id: element.id,
|
|
|
|
|
label: element.title,
|
|
|
|
|
value: element.id,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
setListCategory(temp);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onSubmit = async (values: z.infer<typeof createArticleSchema>) => {
|
|
|
|
|
MySwal.fire({
|
|
|
|
|
title: "Simpan Data",
|
|
|
|
|
text: "",
|
|
|
|
|
icon: "warning",
|
|
|
|
|
showCancelButton: true,
|
|
|
|
|
cancelButtonColor: "#d33",
|
|
|
|
|
confirmButtonColor: "#3085d6",
|
|
|
|
|
confirmButtonText: "Simpan",
|
|
|
|
|
}).then((result) => {
|
|
|
|
|
if (result.isConfirmed) {
|
|
|
|
|
save(values);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2025-01-17 10:23:54 +00:00
|
|
|
function removeImgTags(htmlString: string) {
|
|
|
|
|
const parser = new DOMParser();
|
|
|
|
|
const doc = parser.parseFromString(String(htmlString), "text/html");
|
|
|
|
|
|
|
|
|
|
const images = doc.querySelectorAll("img");
|
|
|
|
|
images.forEach((img) => img.remove());
|
|
|
|
|
return doc.body.innerHTML;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-17 05:23:28 +00:00
|
|
|
const save = async (values: z.infer<typeof createArticleSchema>) => {
|
2025-01-17 10:23:54 +00:00
|
|
|
loading();
|
|
|
|
|
const formData = {
|
|
|
|
|
title: values.title,
|
|
|
|
|
typeId: 1,
|
|
|
|
|
slug: values.slug,
|
2025-01-20 12:10:04 +00:00
|
|
|
categoryIds: values.category.map((a) => a.id).join(","),
|
|
|
|
|
// categoryId: values.category[0].id,
|
2025-01-17 10:23:54 +00:00
|
|
|
tags: values.tags.join(","),
|
|
|
|
|
description: htmlToString(removeImgTags(values.description)),
|
|
|
|
|
htmlDescription: removeImgTags(values.description),
|
2025-01-20 12:10:04 +00:00
|
|
|
aiArticleId: "",
|
2025-01-17 10:23:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const response = await createArticle(formData);
|
|
|
|
|
|
|
|
|
|
if (response?.error) {
|
|
|
|
|
error(response.message);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2025-01-19 16:13:06 +00:00
|
|
|
const articleId = response?.data?.data?.id;
|
|
|
|
|
if (files?.length > 0) {
|
|
|
|
|
const formFiles = new FormData();
|
|
|
|
|
|
|
|
|
|
for (const element of files) {
|
|
|
|
|
formFiles.append("file", element);
|
|
|
|
|
const resFile = await uploadArticleFile(articleId, formFiles);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-01-20 12:10:04 +00:00
|
|
|
console.log("thyu,", thumbnailImg[0]);
|
2025-01-19 16:13:06 +00:00
|
|
|
if (thumbnailImg?.length > 0 || files?.length > 0) {
|
2025-01-20 12:10:04 +00:00
|
|
|
if (thumbnailImg?.length > 0) {
|
|
|
|
|
const formFiles = new FormData();
|
2025-01-19 16:13:06 +00:00
|
|
|
|
2025-01-20 12:10:04 +00:00
|
|
|
formFiles.append("files", thumbnailImg[0]);
|
2025-01-19 16:13:06 +00:00
|
|
|
const resFile = await uploadArticleThumbnail(articleId, formFiles);
|
|
|
|
|
} else {
|
2025-01-20 12:10:04 +00:00
|
|
|
const formFiles = new FormData();
|
|
|
|
|
|
|
|
|
|
if (selectedMainImage) {
|
|
|
|
|
formFiles.append("files", files[selectedMainImage]);
|
|
|
|
|
|
|
|
|
|
const resFile = await uploadArticleThumbnail(articleId, formFiles);
|
|
|
|
|
} else {
|
|
|
|
|
formFiles.append("files", files[0]);
|
|
|
|
|
const resFile = await uploadArticleThumbnail(articleId, formFiles);
|
|
|
|
|
}
|
2025-01-19 16:13:06 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-17 10:23:54 +00:00
|
|
|
close();
|
|
|
|
|
successSubmit("/admin/article");
|
2025-01-17 05:23:28 +00:00
|
|
|
};
|
|
|
|
|
|
2025-01-17 10:23:54 +00:00
|
|
|
function successSubmit(redirect: string) {
|
|
|
|
|
MySwal.fire({
|
|
|
|
|
title: "Sukses",
|
|
|
|
|
icon: "success",
|
|
|
|
|
confirmButtonColor: "#3085d6",
|
|
|
|
|
confirmButtonText: "OK",
|
|
|
|
|
}).then((result) => {
|
|
|
|
|
if (result.isConfirmed) {
|
|
|
|
|
router.push(redirect);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-17 05:23:28 +00:00
|
|
|
const watchTitle = watch("title");
|
|
|
|
|
const generateSlug = (title: string) => {
|
|
|
|
|
return title
|
|
|
|
|
.toLowerCase()
|
|
|
|
|
.trim()
|
|
|
|
|
.replace(/[^\w\s-]/g, "")
|
|
|
|
|
.replace(/\s+/g, "-");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setValue("slug", generateSlug(watchTitle));
|
|
|
|
|
}, [watchTitle]);
|
|
|
|
|
|
|
|
|
|
const renderFilePreview = (file: FileWithPreview) => {
|
|
|
|
|
if (file.type.startsWith("image")) {
|
|
|
|
|
return (
|
|
|
|
|
<Image
|
|
|
|
|
width={48}
|
|
|
|
|
height={48}
|
|
|
|
|
alt={file.name}
|
|
|
|
|
src={URL.createObjectURL(file)}
|
|
|
|
|
className=" rounded border p-0.5"
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
return "Not Found";
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleRemoveFile = (file: FileWithPreview) => {
|
|
|
|
|
const uploadedFiles = files;
|
|
|
|
|
const filtered = uploadedFiles.filter((i) => i.name !== file.name);
|
|
|
|
|
setFiles([...filtered]);
|
|
|
|
|
};
|
|
|
|
|
|
2025-01-19 16:13:06 +00:00
|
|
|
const fileList = files.map((file, index) => (
|
2025-01-17 05:23:28 +00:00
|
|
|
<div
|
|
|
|
|
key={file.name}
|
|
|
|
|
className=" flex justify-between border px-3.5 py-3 my-6 rounded-md"
|
|
|
|
|
>
|
|
|
|
|
<div className="flex gap-3 items-center">
|
|
|
|
|
<div className="file-preview">{renderFilePreview(file)}</div>
|
|
|
|
|
<div>
|
|
|
|
|
<div className=" text-sm text-card-foreground">{file.name}</div>
|
|
|
|
|
<div className=" text-xs font-light text-muted-foreground">
|
|
|
|
|
{Math.round(file.size / 100) / 10 > 1000 ? (
|
|
|
|
|
<>{(Math.round(file.size / 100) / 10000).toFixed(1)}</>
|
|
|
|
|
) : (
|
|
|
|
|
<>{(Math.round(file.size / 100) / 10).toFixed(1)}</>
|
|
|
|
|
)}
|
|
|
|
|
{" kb"}
|
|
|
|
|
</div>
|
2025-01-19 16:13:06 +00:00
|
|
|
<Checkbox
|
|
|
|
|
id={String(index)}
|
|
|
|
|
value={String(index)}
|
|
|
|
|
size="sm"
|
|
|
|
|
isSelected={selectedMainImage === index}
|
|
|
|
|
onValueChange={() => setSelectedMainImage(index)}
|
|
|
|
|
>
|
|
|
|
|
<p className="text-black text-xs"> Jadikan Thumbnail</p>
|
|
|
|
|
</Checkbox>
|
2025-01-17 05:23:28 +00:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<Button
|
|
|
|
|
className=" border-none rounded-full"
|
2025-01-19 16:13:06 +00:00
|
|
|
variant="bordered"
|
2025-01-17 05:23:28 +00:00
|
|
|
onClick={() => handleRemoveFile(file)}
|
|
|
|
|
>
|
|
|
|
|
<TimesIcon />
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
));
|
|
|
|
|
|
2025-01-19 16:13:06 +00:00
|
|
|
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
const selectedFiles = event.target.files;
|
|
|
|
|
if (selectedFiles) {
|
|
|
|
|
setThumbnailImg(Array.from(selectedFiles));
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-01-20 12:10:04 +00:00
|
|
|
const selectedCategory = watch("category");
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
getDetailCategory();
|
|
|
|
|
}, [selectedCategory]);
|
|
|
|
|
|
|
|
|
|
const getDetailCategory = async () => {
|
|
|
|
|
let temp = getValues("tags");
|
|
|
|
|
for (const element of selectedCategory) {
|
|
|
|
|
const res = await getCategoryById(element?.id);
|
|
|
|
|
const tagList = res?.data?.data?.tags;
|
|
|
|
|
if (tagList) {
|
|
|
|
|
temp = [...temp, ...res?.data?.data?.tags];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const uniqueArray = temp.filter(
|
|
|
|
|
(item, index) => temp.indexOf(item) === index
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
setValue("tags", uniqueArray as [string, ...string[]]);
|
|
|
|
|
};
|
|
|
|
|
|
2025-01-17 05:23:28 +00:00
|
|
|
return (
|
|
|
|
|
<form
|
|
|
|
|
className="flex flex-row gap-8 text-black"
|
|
|
|
|
onSubmit={handleSubmit(onSubmit)}
|
|
|
|
|
>
|
|
|
|
|
<div className="w-[65%] bg-white rounded-lg p-8 flex flex-col gap-1">
|
|
|
|
|
<p className="text-sm">Judul</p>
|
|
|
|
|
<Controller
|
|
|
|
|
control={control}
|
|
|
|
|
name="title"
|
|
|
|
|
render={({ field: { onChange, value } }) => (
|
|
|
|
|
<Input
|
|
|
|
|
type="text"
|
|
|
|
|
id="title"
|
|
|
|
|
placeholder=""
|
|
|
|
|
label=""
|
|
|
|
|
value={value}
|
|
|
|
|
onChange={onChange}
|
|
|
|
|
labelPlacement="outside"
|
2025-01-17 10:23:54 +00:00
|
|
|
className="w-full "
|
2025-01-17 05:23:28 +00:00
|
|
|
classNames={{
|
|
|
|
|
inputWrapper: [
|
|
|
|
|
"border-1 rounded-lg",
|
|
|
|
|
"dark:group-data-[focused=false]:bg-transparent !border-1 dark:!border-gray-400",
|
|
|
|
|
],
|
|
|
|
|
}}
|
|
|
|
|
variant="bordered"
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
{errors?.title && (
|
2025-01-17 10:23:54 +00:00
|
|
|
<p className="text-red-400 text-sm mb-3">{errors.title?.message}</p>
|
2025-01-17 05:23:28 +00:00
|
|
|
)}
|
2025-01-17 10:23:54 +00:00
|
|
|
<p className="text-sm mt-3">Slug</p>
|
2025-01-17 05:23:28 +00:00
|
|
|
<Controller
|
|
|
|
|
control={control}
|
|
|
|
|
name="slug"
|
|
|
|
|
render={({ field: { onChange, value } }) => (
|
|
|
|
|
<Input
|
|
|
|
|
type="text"
|
|
|
|
|
id="title"
|
|
|
|
|
placeholder=""
|
|
|
|
|
label=""
|
|
|
|
|
value={value}
|
|
|
|
|
isReadOnly
|
|
|
|
|
onChange={onChange}
|
|
|
|
|
labelPlacement="outside"
|
2025-01-17 10:23:54 +00:00
|
|
|
className="w-full "
|
2025-01-17 05:23:28 +00:00
|
|
|
classNames={{
|
|
|
|
|
inputWrapper: [
|
|
|
|
|
"border-1 rounded-lg",
|
|
|
|
|
"dark:group-data-[focused=false]:bg-transparent !border-1 dark:!border-gray-400",
|
|
|
|
|
],
|
|
|
|
|
}}
|
|
|
|
|
variant="bordered"
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
{errors?.slug && (
|
2025-01-17 10:23:54 +00:00
|
|
|
<p className="text-red-400 text-sm mb-3">{errors.slug?.message}</p>
|
2025-01-17 05:23:28 +00:00
|
|
|
)}
|
|
|
|
|
|
2025-01-17 10:23:54 +00:00
|
|
|
<Switch isSelected={useAi} onValueChange={setUseAI} className="mt-3">
|
2025-01-17 05:23:28 +00:00
|
|
|
<p className="text-sm text-black">Bantuan AI</p>
|
|
|
|
|
</Switch>
|
|
|
|
|
|
2025-01-17 10:23:54 +00:00
|
|
|
{useAi && (
|
|
|
|
|
<GenerateSingleArticleForm
|
|
|
|
|
content={(data) => setValue("description", data)}
|
2025-01-20 12:10:04 +00:00
|
|
|
generatedId={(data) => {}}
|
2025-01-17 10:23:54 +00:00
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
<p className="text-sm mt-3">Deskripsi</p>
|
2025-01-17 05:23:28 +00:00
|
|
|
<Controller
|
|
|
|
|
control={control}
|
|
|
|
|
name="description"
|
|
|
|
|
render={({ field: { onChange, value } }) => (
|
|
|
|
|
// <CustomEditor onChange={onChange} initialData={value} />
|
2025-01-20 17:04:10 +00:00
|
|
|
// <JoditEditor
|
|
|
|
|
// ref={editor}
|
|
|
|
|
// value={value}
|
|
|
|
|
// onChange={onChange}
|
|
|
|
|
// className="dark:text-black"
|
|
|
|
|
// />
|
|
|
|
|
<CustomEditor
|
2025-01-17 05:23:28 +00:00
|
|
|
onChange={onChange}
|
2025-01-20 17:04:10 +00:00
|
|
|
initialData={value}
|
2025-01-17 05:23:28 +00:00
|
|
|
/>
|
2025-01-20 17:04:10 +00:00
|
|
|
|
2025-01-17 05:23:28 +00:00
|
|
|
)}
|
|
|
|
|
/>
|
2025-01-17 10:23:54 +00:00
|
|
|
{errors?.description && (
|
|
|
|
|
<p className="text-red-400 text-sm mb-3">
|
|
|
|
|
{errors.description?.message}
|
|
|
|
|
</p>
|
|
|
|
|
)}
|
2025-01-17 05:23:28 +00:00
|
|
|
|
2025-01-17 10:23:54 +00:00
|
|
|
<p className="text-sm mt-3">File Media</p>
|
2025-01-17 05:23:28 +00:00
|
|
|
<Fragment>
|
|
|
|
|
<div {...getRootProps({ className: "dropzone" })}>
|
|
|
|
|
<input {...getInputProps()} />
|
|
|
|
|
<div className=" w-full text-center border-dashed border border-default-200 dark:border-default-300 rounded-md py-[52px] flex items-center flex-col">
|
|
|
|
|
<CloudUploadIcon size={50} className="text-gray-300" />
|
|
|
|
|
<h4 className=" text-2xl font-medium mb-1 mt-3 text-card-foreground/80">
|
|
|
|
|
Tarik file disini atau klik untuk upload.
|
|
|
|
|
</h4>
|
|
|
|
|
<div className=" text-xs text-muted-foreground">
|
|
|
|
|
( Upload file dengan format .jpg, .jpeg, atau .png. Ukuran
|
|
|
|
|
maksimal 100mb.)
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
{files.length ? (
|
|
|
|
|
<Fragment>
|
|
|
|
|
<div>{fileList}</div>
|
|
|
|
|
<div className=" flex justify-between gap-2">
|
2025-01-19 16:13:06 +00:00
|
|
|
<Button onPress={() => setFiles([])} size="sm">
|
|
|
|
|
Hapus Semua
|
|
|
|
|
</Button>
|
2025-01-17 05:23:28 +00:00
|
|
|
</div>
|
|
|
|
|
</Fragment>
|
|
|
|
|
) : null}
|
|
|
|
|
</Fragment>
|
|
|
|
|
</div>
|
2025-01-17 10:23:54 +00:00
|
|
|
<div className="w-[35%] flex flex-col gap-8">
|
|
|
|
|
<div className="h-fit bg-white rounded-lg p-8 flex flex-col gap-1">
|
|
|
|
|
<p className="text-sm">Thubmnail</p>
|
2025-01-19 16:13:06 +00:00
|
|
|
|
|
|
|
|
{thumbnailImg.length > 0 ? (
|
|
|
|
|
<div className="flex flex-row">
|
|
|
|
|
<img
|
|
|
|
|
src={URL.createObjectURL(thumbnailImg[0])}
|
|
|
|
|
className="w-[30%]"
|
|
|
|
|
alt="thumbnail"
|
|
|
|
|
/>
|
|
|
|
|
<Button
|
|
|
|
|
className=" border-none rounded-full"
|
|
|
|
|
variant="bordered"
|
|
|
|
|
size="sm"
|
|
|
|
|
color="danger"
|
|
|
|
|
onClick={() => setThumbnailImg([])}
|
|
|
|
|
>
|
|
|
|
|
<TimesIcon />
|
|
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
) : (
|
|
|
|
|
<>
|
|
|
|
|
<label htmlFor="file-upload">
|
|
|
|
|
<Button className="w-fit" color="primary" size="sm" as="span">
|
|
|
|
|
Upload Thumbnail
|
|
|
|
|
</Button>
|
|
|
|
|
</label>{" "}
|
|
|
|
|
<input
|
|
|
|
|
id="file-upload"
|
|
|
|
|
type="file"
|
|
|
|
|
multiple
|
|
|
|
|
style={{ display: "none" }}
|
2025-01-20 12:10:04 +00:00
|
|
|
className="w-fit h-fit"
|
2025-01-19 16:13:06 +00:00
|
|
|
onChange={handleFileChange}
|
|
|
|
|
/>
|
|
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
|
2025-01-17 10:23:54 +00:00
|
|
|
<p className="text-sm mt-3">Kategori</p>
|
|
|
|
|
<Controller
|
|
|
|
|
control={control}
|
|
|
|
|
name="category"
|
|
|
|
|
render={({ field: { onChange, value } }) => (
|
|
|
|
|
<ReactSelect
|
|
|
|
|
className="basic-single text-black z-50"
|
|
|
|
|
classNames={{
|
|
|
|
|
control: (state: any) =>
|
|
|
|
|
"!rounded-lg bg-white !border-1 !border-gray-200 dark:!border-stone-500",
|
|
|
|
|
}}
|
|
|
|
|
classNamePrefix="select"
|
|
|
|
|
onChange={onChange}
|
|
|
|
|
closeMenuOnSelect={false}
|
|
|
|
|
components={animatedComponents}
|
|
|
|
|
isClearable={true}
|
|
|
|
|
isSearchable={true}
|
|
|
|
|
isMulti={true}
|
|
|
|
|
placeholder="Kategori..."
|
|
|
|
|
name="sub-module"
|
|
|
|
|
options={listCategory}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
{errors?.category && (
|
|
|
|
|
<p className="text-red-400 text-sm mb-3">
|
|
|
|
|
{errors.category?.message}
|
|
|
|
|
</p>
|
2025-01-17 05:23:28 +00:00
|
|
|
)}
|
|
|
|
|
|
2025-01-17 10:23:54 +00:00
|
|
|
<p className="text-sm">Tags</p>
|
|
|
|
|
<Controller
|
|
|
|
|
control={control}
|
|
|
|
|
name="tags"
|
|
|
|
|
render={({ field: { onChange, value } }) => (
|
|
|
|
|
<Textarea
|
|
|
|
|
type="text"
|
|
|
|
|
id="tags"
|
|
|
|
|
placeholder=""
|
|
|
|
|
label=""
|
|
|
|
|
value={tag}
|
|
|
|
|
onValueChange={setTag}
|
|
|
|
|
startContent={
|
|
|
|
|
<div className="flex flex-wrap gap-1">
|
|
|
|
|
{value.map((item, index) => (
|
|
|
|
|
<Chip
|
|
|
|
|
color="primary"
|
|
|
|
|
key={index}
|
|
|
|
|
className=""
|
|
|
|
|
onClose={() => {
|
|
|
|
|
const filteredTags = value.filter(
|
|
|
|
|
(tag) => tag !== item
|
2025-01-17 05:23:28 +00:00
|
|
|
);
|
2025-01-17 10:23:54 +00:00
|
|
|
if (filteredTags.length === 0) {
|
|
|
|
|
setError("tags", {
|
|
|
|
|
type: "manual",
|
|
|
|
|
message: "Tags tidak boleh kosong",
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
clearErrors("tags");
|
|
|
|
|
setValue(
|
|
|
|
|
"tags",
|
|
|
|
|
filteredTags as [string, ...string[]]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{item}
|
|
|
|
|
</Chip>
|
|
|
|
|
))}
|
|
|
|
|
</div>
|
2025-01-17 05:23:28 +00:00
|
|
|
}
|
2025-01-17 10:23:54 +00:00
|
|
|
onKeyDown={(e) => {
|
|
|
|
|
if (e.key === "Enter") {
|
|
|
|
|
if (tag.trim() !== "") {
|
|
|
|
|
setValue("tags", [...value, tag.trim()]);
|
|
|
|
|
setTag("");
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
labelPlacement="outside"
|
|
|
|
|
className="w-full h-fit"
|
|
|
|
|
classNames={{
|
|
|
|
|
inputWrapper: [
|
|
|
|
|
"border-1 rounded-lg",
|
|
|
|
|
"dark:group-data-[focused=false]:bg-transparent !border-1 dark:!border-gray-400",
|
|
|
|
|
],
|
|
|
|
|
}}
|
|
|
|
|
variant="bordered"
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
|
|
|
|
{errors?.tags && (
|
|
|
|
|
<p className="text-red-400 text-sm mb-3">{errors.tags?.message}</p>
|
2025-01-17 05:23:28 +00:00
|
|
|
)}
|
2025-01-17 10:23:54 +00:00
|
|
|
</div>
|
|
|
|
|
<div className="flex flex-row justify-end gap-3">
|
|
|
|
|
<Button color="primary" type="submit">
|
|
|
|
|
Publish
|
|
|
|
|
</Button>
|
|
|
|
|
<Button color="success" type="button">
|
|
|
|
|
<p className="text-white">Draft</p>
|
|
|
|
|
</Button>
|
|
|
|
|
<Link href="/admin/article">
|
|
|
|
|
<Button color="danger" variant="bordered" type="button">
|
|
|
|
|
Kembali
|
|
|
|
|
</Button>
|
|
|
|
|
</Link>
|
|
|
|
|
</div>
|
2025-01-17 05:23:28 +00:00
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
);
|
|
|
|
|
}
|