added zod for post requests

This commit is contained in:
daniel31x13
2024-09-14 16:00:19 -04:00
parent a5b1952e0d
commit 1cf7421b76
24 changed files with 350 additions and 180 deletions
+14 -24
View File
@@ -14,6 +14,7 @@ import Modal from "../Modal";
import { useTranslation } from "next-i18next";
import { useCollections } from "@/hooks/store/collections";
import { useUploadFile } from "@/hooks/store/links";
import { PostLinkSchemaType } from "@/lib/shared/schemaValidation";
type Props = {
onClose: Function;
@@ -25,27 +26,16 @@ export default function UploadFileModal({ onClose }: Props) {
const initial = {
name: "",
url: "",
description: "",
type: "url",
tags: [],
preview: "",
image: "",
pdf: "",
readable: "",
monolith: "",
textContent: "",
icon: "",
iconWeight: "",
color: "",
collection: {
id: undefined,
name: "",
ownerId: data?.user.id as number,
},
} as LinkIncludingShortenedCollectionAndTags;
} as PostLinkSchemaType;
const [link, setLink] =
useState<LinkIncludingShortenedCollectionAndTags>(initial);
const [link, setLink] = useState<PostLinkSchemaType>(initial);
const [file, setFile] = useState<File>();
const uploadFile = useUploadFile();
@@ -55,11 +45,11 @@ export default function UploadFileModal({ onClose }: Props) {
const { data: collections = [] } = useCollections();
const setCollection = (e: any) => {
if (e?.__isNew__) e.value = null;
if (e?.__isNew__) e.value = undefined;
setLink({
...link,
collection: { id: e?.value, name: e?.label, ownerId: e?.ownerId },
collection: { id: e?.value, name: e?.label },
});
};
@@ -77,6 +67,7 @@ export default function UploadFileModal({ onClose }: Props) {
const currentCollection = collections.find(
(e) => e.id == Number(router.query.id)
);
if (
currentCollection &&
currentCollection.ownerId &&
@@ -87,13 +78,12 @@ export default function UploadFileModal({ onClose }: Props) {
collection: {
id: currentCollection.id,
name: currentCollection.name,
ownerId: currentCollection.ownerId,
},
});
} else
setLink({
...initial,
collection: { name: "Unorganized", ownerId: data?.user.id as number },
collection: { name: "Unorganized" },
});
}, [router, collections]);
@@ -166,12 +156,12 @@ export default function UploadFileModal({ onClose }: Props) {
</div>
<div className="sm:col-span-2 col-span-5">
<p className="mb-2">{t("collection")}</p>
{link.collection.name && (
{link.collection?.name && (
<CollectionSelection
onChange={setCollection}
defaultValue={{
label: link.collection.name,
value: link.collection.id,
value: link.collection?.id,
label: link.collection?.name || "Unorganized",
}}
/>
)}
@@ -193,16 +183,16 @@ export default function UploadFileModal({ onClose }: Props) {
<p className="mb-2">{t("tags")}</p>
<TagSelection
onChange={setTags}
defaultValue={link.tags.map((e) => ({
label: e.name,
defaultValue={link.tags?.map((e) => ({
value: e.id,
label: e.name,
}))}
/>
</div>
<div className="sm:col-span-2">
<p className="mb-2">{t("description")}</p>
<textarea
value={unescapeString(link.description) as string}
value={unescapeString(link.description || "") || ""}
onChange={(e) =>
setLink({ ...link, description: e.target.value })
}