"use client"; import React from "react"; import { useState, useRef, useEffect } from "react"; import styles from "../../../../styles/modules/AdminArticlesCreate.module.scss"; import { PostArticle } from "../../../../types/postData"; import Markdown from "../../../Markdown"; import { Article, Category, Prisma } from "@prisma/client"; import "../../../../styles/inputs.scss"; import "../../../../styles/buttons.scss"; import Select, { GroupBase, OptionsOrGroups } from "react-select"; import { apiUrl } from "../../../global"; import urlJoin from "url-join"; import { formatTextToUrlName } from "../../../../utils"; import { isValidText } from "../../../../validators"; import { useRouter } from "next/navigation"; import ContentTable from "../../../articles/[categoryName]/[articleName]/ContentTable"; import { IContentTableEntry } from "../../../../types/contentTable"; type ArticleWithCategory = Prisma.ArticleGetPayload<{ include: { category: true } }>; export default function AdminArticlesCreate() { const router = useRouter(); const [title, setTitle] = useState(""); const [selectCategoriesOptions, setSelectCategoriesOptions] = useState([]); const [introduction, setIntroduction] = useState(""); const [markdown, setMarkdown] = useState(""); const [contentTable, setContentTable] = useState([]); const titleRef = useRef(null); const categorySelectRef = useRef(null); const introductionRef = useRef(null); const markdownTextAreaRef = useRef(null); const errorTextRef = useRef(null); function changeContentTableEntryAnchor(index: number, newAnchor: string) { setContentTable((prevArray) => { let newArray = [...prevArray]; newArray[index].anchor = newAnchor; return newArray; }); } function changeContentTableEntryTitle(index: number, newTitle: string) { setContentTable((prevArray) => { let newArray = [...prevArray]; newArray[index].anchor = newTitle; return newArray; }); } function handleFormChange() { setMarkdown(markdownTextAreaRef.current.value); setTitle(titleRef.current.value); setIntroduction(introductionRef.current.value); } async function postData() { const formData: PostArticle = { title: titleRef.current.value, introduction: introductionRef.current.value, markdown: markdown, categoryId: Number(categorySelectRef?.current?.getValue()[0]?.value), contentTable: contentTable, }; console.log(formData); const result = await fetch("/api/articles/create", { method: "POST", headers: { Accept: "application/json", "Content-Type": "application/json", }, body: JSON.stringify(formData), }); const response = await result.json(); console.log(response); errorTextRef.current.innerText = response.error ?? ""; if (response.success) { const newArticle: ArticleWithCategory = response.data; router.push(urlJoin(`/articles/`, newArticle.category.name, newArticle.name)); } } useEffect(() => { const fetchCategoryOptions = async () => { const result: Response = await fetch(urlJoin(apiUrl, `categories`), { cache: "no-cache", next: { revalidate: 60 * 1 }, }); const categories = await result.json(); let newSelectCategoriesOptions = []; categories?.forEach((c) => { newSelectCategoriesOptions.push({ value: c.id, label: c.title }); }); setSelectCategoriesOptions(newSelectCategoriesOptions); }; fetchCategoryOptions().catch((err) => { console.log(err); }); }, []); return (

Create a new article

{contentTable.map((entry: IContentTableEntry, i: number) => { return (
{ changeContentTableEntryAnchor(i, e.target.value); }} type="text" placeholder={"Anchor"} /> { changeContentTableEntryTitle(i, e.target.value); }} type="text" placeholder={"Title"} />
); })}
); }