Skip to content

Commit

Permalink
Merge pull request #240 from ahsanzizan/main
Browse files Browse the repository at this point in the history
Suddent fixes and shits
  • Loading branch information
ahsanzizan authored Jul 10, 2024
2 parents 0db513e + c5e02e9 commit 0b7c50e
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 147 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* eslint-disable */
"use client";

import { fileSizeToMb } from "@/utils/atomics";
import MDEditor, {
commands,
bold,
Expand Down Expand Up @@ -43,25 +44,32 @@ export default function Editor({
icon: <FaFileImage />,
execute: async (_, api: TextAreaTextApi) => {
if (insertImageRef.current) {
const toastId = toast.loading("Uploading image...");
const result = await getImage();
if (!result) return toast.error("Failed to load image");

const imageSizeInMb = fileSizeToMb(result.size);
if (imageSizeInMb >= 4.3)
return toast.error(
"Ukuran file terlalu besar! Ukuran maximum 4,3 MB",
);

const data = new FormData();

data.append("file", result!);

const toastId = toast.loading("Uploading image...");
const upload = await fetch("/api/upload/image", {
method: "POST",
body: data,
}).then((res) => res.json());

if (upload.status != 201) {
toast.error("Failed upload image", { id: toastId });
toast.error("Ukuran file terlalu besar! Ukuran maximum 4,3 MB", {
id: toastId,
});
} else {
const modifyText = `![user image](${upload.data?.url})\n`;
api.replaceSelection(modifyText);
toast.success("Success upload image", { id: toastId });
toast.success("Sukses upload gambar", { id: toastId });
}
insertImageRef!.current!.value = "";
}
Expand Down
13 changes: 12 additions & 1 deletion src/app/(admin)/admin/organisasi/_components/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import {
} from "@/app/_components/global/Input";
import { Organisasi, Organisasi_Type } from "@prisma/client";
import { useState } from "react";
import Editor from "./MdEditor";
import Editor from "@/app/(admin)/admin/components/MdEditor";
import Image from "@/app/_components/global/Image";
import { organisasiUpsert } from "@/actions/organisasi";
import { toast } from "sonner";
import SubmitButton from "@/app/_components/global/SubmitButton";
import { useRouter } from "next-nprogress-bar";
import { fileSizeToMb } from "@/utils/atomics";

export default function Form({
organisasi,
Expand Down Expand Up @@ -45,6 +46,16 @@ export default function Form({
if (logo?.name === "") data.delete("logo");
if (image?.name === "") data.delete("image");

const logoSizeInMb = logo ? fileSizeToMb(logo.size) : 0;
const imageSizeInMb = image ? fileSizeToMb(image.size) : 0;

if (logoSizeInMb + imageSizeInMb > 4.3) {
return toast.error(
"Ukuran file terlalu besar! Ukuran maximum 4,3 MB",
{ id: toastId },
);
}

const result = await organisasiUpsert({
data,
id: organisasi.id || null,
Expand Down
136 changes: 0 additions & 136 deletions src/app/(admin)/admin/organisasi/_components/MdEditor.tsx

This file was deleted.

14 changes: 11 additions & 3 deletions src/app/(admin)/admin/posts/[id]/_components/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import Image from "@/app/_components/global/Image";
import { TextArea, TextField } from "@/app/_components/global/Input";
import { PostWithTagsAndUser, TagWithPostCount } from "@/types/entityRelations";

import Editor from "../../_components/MdEditor";
import Editor from "@/app/(admin)/admin/components/MdEditor";
import FormButton from "../../_components/parts/SubmitButton";

import { fileSizeToMb } from "@/utils/atomics";
import Tags from "./Tags";

export default function EditForm({
Expand Down Expand Up @@ -54,8 +55,15 @@ export default function EditForm({
action={async (data) => {
const toastId = toast.loading("Loading...");

const thumbnailUrl = data.get("thumbnail") as File;
if (thumbnailUrl.name === "") data.delete("thumbnail");
const thumbnail = data.get("thumbnail") as File | undefined;
if (thumbnail?.name === "") data.delete("thumbnail");

const thumbnailSizeInMb = thumbnail ? fileSizeToMb(thumbnail.size) : 0;
if (thumbnailSizeInMb >= 4.3)
return toast.error(
"Ukuran file terlalu besar! Ukuran maximum 4,3 MB",
{ id: toastId },
);

const result = await postUpdate(data, value, tag, post.id);
if (result.error) {
Expand Down
20 changes: 18 additions & 2 deletions src/app/(admin)/admin/posts/create/_components/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import { TextArea, TextField } from "@/app/_components/global/Input";
import { TagWithPostCount } from "@/types/entityRelations";

import Modal from "../../_components/ImageModal";
import Editor from "../../_components/MdEditor";
import Editor from "@/app/(admin)/admin/components/MdEditor";
import FormButton from "../../_components/parts/SubmitButton";

import Tags from "./Tags";
import { fileSizeToMb } from "@/utils/atomics";

export default function PostForm({ tags }: { tags: TagWithPostCount[] }) {
const [tag, setTag] =
Expand Down Expand Up @@ -50,9 +51,24 @@ export default function PostForm({ tags }: { tags: TagWithPostCount[] }) {
{isOpen && <Modal setIsOpenModal={setIsOpen} />}
<form
action={async (data) => {
const toastId = toast.loading("Loading...");
const result = await postCreate(data, value, tag!);

const thumbnail = data.get("thumbnail") as File;
const thumbnailSizeInMb = thumbnail
? fileSizeToMb(thumbnail.size)
: 0;

if (thumbnailSizeInMb >= 4.3)
return toast.error(
"Ukuran file terlalu besar! Ukuran maximum 4,3 MB",
{ id: toastId },
);

if (result.error || !result.result?.id) {
return toast.error(result.message || "Failed to create post");
return toast.error(result.message || "Failed to create post", {
id: toastId,
});
}
toast.success(result.message);
redirect(`/admin/posts/${result.result?.id}`);
Expand Down
10 changes: 9 additions & 1 deletion src/app/(admin)/admin/twibbon/_components/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import FormButton from "./part/SubmitButton";
import { TwibbonWithUser } from "@/types/entityRelations";
import Image from "@/app/_components/global/Image";
import { upsertTwibbon } from "@/actions/twibbon";
import { fileSizeToMb } from "@/utils/atomics";

export default function Modal({
setIsOpenModal,
Expand All @@ -27,6 +28,13 @@ export default function Modal({
const frameUrl = formdata.get("frame_url") as File;

if (frameUrl.name === "") formdata.delete("frame_url");
else {
const fileSize = fileSizeToMb(frameUrl.size);
if (fileSize > 4.3)
toast.error("File terlalu besar! Ukuran maximum 4,3 MB", {
id: toastId,
});
}

const result = await upsertTwibbon(data?.id as string, formdata);
if (result?.error) {
Expand All @@ -35,7 +43,7 @@ export default function Modal({
} else toast.error(result.message, { id: toastId });
} catch (e) {
console.error(e);
toast.error("File terlalu besar!", { id: toastId });
toast.error("File terlalu besar! Ukuran maximum 4,3 MB", { id: toastId });
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/utils/atomics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,9 @@ export function slugify(str: string, separator = "-") {
.replace(/-+/g, separator); // remove consecutive hyphens
return str;
}

//#region file size converter
export function fileSizeToMb(fileSize: number) {
return parseFloat((fileSize / (1024 * 1024)).toFixed(2));
}
//#endregion

0 comments on commit 0b7c50e

Please sign in to comment.