"use client"; import { useEffect, useState } from "react"; import { Input, Textarea, Select, SelectItem, Chip, Button, Divider, } from "@nextui-org/react"; import { Plus, ArrowUpFromLine } from "lucide-react"; import { priorities, testTypes, templates } from "@/config/selection"; import StepsEditor from "./steps-editor"; import AttachmentsEditor from "./attachments-editor"; import Config from "@/config/config"; const apiServer = Config.apiServer; type CaseType = { id: number; title: string; state: number; priority: number; type: number; automationStatus: number; description: string; template: number; preConditions: string; expectedResults: string; folderId: number; Steps: StepType[]; Attachments: AttachmentType[]; }; type CaseStepType = { createdAt: Date; updatedAt: Date; CaseId: number; StepId: number; }; export type StepType = { id: number; step: string; result: string; createdAt: Date; updatedAt: Date; caseSteps: CaseStepType; }; type CaseAttachmentType = { createdAt: Date; updatedAt: Date; CaseId: number; AttachmentId: number; }; export type AttachmentType = { id: number; title: string; detail: string; path: string; createdAt: Date; updatedAt: Date; caseSteps: CaseAttachmentType; }; const defaultTestCase = { id: 0, title: "", state: 0, priority: 0, type: 0, automationStatus: 0, description: "", template: 0, preConditions: "", expectedResults: "", folderId: 0, }; /** * fetch case */ async function fetchCase(url: string) { try { const response = await fetch(url, { method: "GET", headers: { "Content-Type": "application/json", }, }); if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status}`); } const data = await response.json(); return data; } catch (error) { console.error("Error fetching data:", error.message); } } /** * create step */ async function fetchCreateStep(newStepNo: number, parentCaseId: number) { const fetchOptions = { method: "POST", headers: { "Content-Type": "application/json", }, }; const url = `${apiServer}/steps?newStepNo=${newStepNo}&parentCaseId=${parentCaseId}`; try { const response = await fetch(url, fetchOptions); if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status}`); } return await response.json(); } catch (error) { console.error("Error deleting project:", error); throw error; } } /** * delete step */ async function fetchDeleteStep(stepId: number, parentCaseId: number) { const fetchOptions = { method: "DELETE", headers: { "Content-Type": "application/json", }, }; const url = `${apiServer}/steps/${stepId}?parentCaseId=${parentCaseId}`; try { const response = await fetch(url, fetchOptions); if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status}`); } } catch (error) { console.error("Error deleting project:", error); throw error; } } /** * Update folder */ async function updateCase(updateCaseData: CaseType) { const fetchOptions = { method: "PUT", headers: { "Content-Type": "application/json", }, body: JSON.stringify(updateCaseData), }; const url = `${apiServer}/cases/${updateCaseData.id}`; try { const response = await fetch(url, fetchOptions); if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status}`); } const data = await response.json(); return data; } catch (error) { console.error("Error updating project:", error); throw error; } } export default function Page({ params, }: { params: { projectId: string; folderId: string; caseId: string }; }) { const [testCase, setTestCase] = useState(defaultTestCase); const [isTitleInvalid, setIsTitleInvalid] = useState(false); const [isUpdating, setIsUpdating] = useState(false); const url = `${apiServer}/cases?caseId=${params.caseId}`; const onPlusClick = async (newStepNo: number) => { const newStep = await fetchCreateStep(newStepNo, params.caseId); if (newStep) { newStep.caseSteps = { stepNo: newStepNo }; const updatedSteps = testCase.Steps.map((step) => { if (step.caseSteps.stepNo >= newStepNo) { return { ...step, caseSteps: { ...step.caseSteps, stepNo: step.caseSteps.stepNo + 1, }, }; } return step; }); updatedSteps.push(newStep); setTestCase({ ...testCase, Steps: updatedSteps, }); } }; const onDeleteClick = async (stepId: number) => { // find deletedStep's stepNo const deletedStep = testCase.Steps.find((step) => step.id === stepId); if (!deletedStep) { return; } const deletedStepNo = deletedStep.caseSteps.stepNo; // delete request await fetchDeleteStep(stepId, params.caseId); const updatedSteps = testCase.Steps.map((step) => { if (step.caseSteps.stepNo > deletedStepNo) { return { ...step, caseSteps: { ...step.caseSteps, stepNo: step.caseSteps.stepNo - 1, }, }; } return step; }).filter((step) => step.id !== stepId); setTestCase({ ...testCase, Steps: updatedSteps, }); }; const handleFileUpload = async (event) => { const files = event.target.files; try { const formData = new FormData(); for (let i = 0; i < files.length; i++) { formData.append("files", files[i]); } const url = `${apiServer}/attachments`; const response = await fetch(url, { method: "POST", body: formData, }); if (!response.ok) { throw new Error("Network response was not ok"); } const responseData = await response.json(); } catch (error) { console.error("Error uploading files:", error); } }; useEffect(() => { async function fetchDataEffect() { try { const data = await fetchCase(url); setTestCase(data); } catch (error) { console.error("Error in effect:", error.message); } } fetchDataEffect(); }, []); return (
Basic
{ setTestCase({ ...testCase, title: e.target.value }); }} className="mt-3" />