"use client"; import { Button, Input, Select, SelectItem, SelectSection, } from "@heroui/react"; import { FormEvent, useEffect, useState } from "react"; import { Controller, useForm } from "react-hook-form"; import * as z from "zod"; import { zodResolver } from "@hookform/resolvers/zod"; import { close, error, loading } from "@/config/swal"; import { generateDataArticle, getDetailArticle, getGenerateKeywords, getGenerateTitle, } from "@/services/generate-article"; import { delay } from "@/utils/global"; import GetSeoScore from "./get-seo-score-form"; const writingStyle = [ { id: 1, name: "Friendly", }, { id: 1, name: "Professional", }, { id: 3, name: "Informational", }, { id: 4, name: "Neutral", }, { id: 5, name: "Witty", }, ]; const articleSize = [ { id: 1, name: "News (300 - 900 words)", value: "News", }, { id: 2, name: "Info (900 - 2000 words)", value: "Info", }, { id: 3, name: "Detail (2000 - 5000 words)", value: "Detail", }, ]; interface DiseData { id: number; articleBody: string; title: string; metaTitle: string; description: string; metaDescription: string; mainKeyword: string; additionalKeywords: string; } export default function GenerateSingleArticleForm(props: { content: (data: DiseData) => void; }) { const [selectedWritingSyle, setSelectedWritingStyle] = useState("Informational"); const [selectedArticleSize, setSelectedArticleSize] = useState("News"); const [selectedLanguage, setSelectedLanguage] = useState("id"); const [mainKeyword, setMainKeyword] = useState(""); const [title, setTitle] = useState(""); const [additionalKeyword, setAdditionalKeyword] = useState(""); const [articleIds, setArticleIds] = useState([]); const [selectedId, setSelectedId] = useState(); const [isLoading, setIsLoading] = useState(true); const generateAll = async (keyword: string | undefined) => { if (keyword) { generateTitle(keyword); generateKeywords(keyword); } }; const generateTitle = async (keyword: string | undefined) => { if (keyword) { loading(); const req = { keyword: keyword, style: selectedWritingSyle, website: "None", connectToWeb: true, lang: selectedLanguage, pointOfView: "None", clientId: "humasClientIdtest", }; const res = await getGenerateTitle(req); const data = res?.data?.data; setTitle(data); close(); } }; const generateKeywords = async (keyword: string | undefined) => { if (keyword) { const req = { keyword: keyword, style: selectedWritingSyle, website: "None", connectToWeb: true, lang: selectedLanguage, pointOfView: "0", clientId: "humasClientIdtest", }; loading(); const res = await getGenerateKeywords(req); const data = res?.data?.data; setAdditionalKeyword(data); close(); } }; const onSubmit = async () => { loading(); const request = { advConfig: "", style: selectedWritingSyle, website: "None", connectToWeb: true, lang: selectedLanguage, pointOfView: "None", title: title, imageSource: "Web", mainKeyword: mainKeyword, additionalKeywords: additionalKeyword, targetCountry: null, articleSize: selectedArticleSize, projectId: 2, createdBy: "123123", clientId: "humasClientIdtest", }; const res = await generateDataArticle(request); close(); if (res?.error) { error("Error"); } setArticleIds([...articleIds, res?.data?.data?.id]); // props.articleId(res?.data?.data?.id); }; useEffect(() => { getArticleDetail(); }, [selectedId]); const checkArticleStatus = async (data: string | null) => { if (data === null) { delay(7000).then(() => { getArticleDetail(); }); } }; const getArticleDetail = async () => { if (selectedId) { const res = await getDetailArticle(selectedId); const data = res?.data?.data; checkArticleStatus(data?.articleBody); if (data?.articleBody !== null) { setIsLoading(false); props.content(data); } else { setIsLoading(true); props.content({ id: data?.id, articleBody: "", title: "", metaTitle: "", description: "", metaDescription: "", additionalKeywords: "", mainKeyword: "", }); } } }; return (

Main Keyword

{mainKeyword == "" && (

Required

)}

Title

{/* {title == "" &&

Required

} */}

Additional Keyword

{/* {additionalKeyword == "" && (

Required

)} */} {articleIds.length < 3 && ( )}
{articleIds.length > 0 && (
{articleIds?.map((id, index) => ( ))}
)} {!isLoading && (
)}
); }