From a621ebcc2d7d30fbcce7564b3017a365a9d304b6 Mon Sep 17 00:00:00 2001 From: Christa Date: Tue, 2 Jul 2024 11:25:27 -0400 Subject: [PATCH] fixed bugs related editTreatment --- src/app/editor/components/EditElement.tsx | 205 ++++++++++------------ src/app/editor/components/EditStage.tsx | 134 +++++++------- src/app/editor/components/ElementCard.tsx | 67 ++++--- src/app/editor/components/StageCard.tsx | 121 ++++++------- src/app/editor/components/Timeline.tsx | 2 +- 5 files changed, 256 insertions(+), 273 deletions(-) diff --git a/src/app/editor/components/EditElement.tsx b/src/app/editor/components/EditElement.tsx index 925b116..560973a 100644 --- a/src/app/editor/components/EditElement.tsx +++ b/src/app/editor/components/EditElement.tsx @@ -1,12 +1,12 @@ -import React, { useState } from "react"; -import { useForm } from "react-hook-form"; +import React, { useState } from 'react' +import { useForm } from 'react-hook-form' import { TreatmentType, ElementType, elementSchema, -} from "@/../deliberation-empirica/server/src/preFlight/validateTreatmentFile"; -import { zodResolver } from "@hookform/resolvers/zod"; -import * as zod from "zod"; +} from '@/../deliberation-empirica/server/src/preFlight/validateTreatmentFile' +import { zodResolver } from '@hookform/resolvers/zod' +import * as zod from 'zod' export function EditElement({ treatment, @@ -14,14 +14,11 @@ export function EditElement({ stageIndex, elementIndex, }: { - treatment: TreatmentType; - editTreatment: (treatment : TreatmentType) => void; - stageIndex: number; - elementIndex: number; + treatment: TreatmentType + editTreatment: (treatment: TreatmentType) => void + stageIndex: number + elementIndex: number }) { - //const [isValid, setIsValid] = useState(true) - //var element: ElementType = treatment.gameStages[stageIndex].elements[elementIndex]; // if elementIndex is undefine, is the whole thing undefined? or does it error? - const { register, watch, @@ -32,97 +29,73 @@ export function EditElement({ elementIndex !== undefined ? { defaultValues: { - name: treatment.gameStages[stageIndex]?.elements?.[elementIndex]?.name, - type: treatment.gameStages[stageIndex]?.elements?.[elementIndex]?.type, - file: treatment.gameStages[stageIndex]?.elements?.[elementIndex]?.file, + name: treatment.gameStages[stageIndex]?.elements?.[elementIndex] + ?.name, + type: treatment.gameStages[stageIndex]?.elements?.[elementIndex] + ?.type, + file: treatment.gameStages[stageIndex]?.elements?.[elementIndex] + ?.file, }, resolver: zodResolver(elementSchema), - mode: "onChange", + mode: 'onChange', } : {} - ); - + ) - console.log(typeof editTreatment); // Here it's a function function saveEdits() { - //console.log(typeof editTreatment); // here it's undefined try { - const updatedTreatment = JSON.parse(JSON.stringify(treatment)); // deep copy - if (isValid) { - //console.log("HEEERREEE") - if (stageIndex === undefined) { - throw new Error("No stage index given"); - } - if (elementIndex === undefined) { - updatedTreatment.gameStages[stageIndex].elements.push({ - name: watch("name"), - type: watch("type"), - file: watch("file"), - //TODO: to add more fields here - }); - } else { - updatedTreatment.gameStages[stageIndex].elements[elementIndex].name = - watch("name"); - updatedTreatment.gameStages[stageIndex].elements[elementIndex].type = - watch("type"); - updatedTreatment.gameStages[stageIndex].elements[elementIndex].file = - watch("file"); - // TODO: add more fields here - } - console.log(typeof editTreatment); - editTreatment(updatedTreatment); + const updatedTreatment = JSON.parse(JSON.stringify(treatment)) // deep copy + + if (stageIndex === undefined) { + throw new Error('No stage index given') + } + + if (elementIndex === undefined) { + updatedTreatment.gameStages[stageIndex].elements.push({ + name: watch('name'), + type: watch('type'), + file: watch('file'), + //TODO: to add more fields here + }) } else { - throw new Error("Form is not valid"); + updatedTreatment.gameStages[stageIndex].elements[elementIndex].name = + watch('name') + updatedTreatment.gameStages[stageIndex].elements[elementIndex].type = + watch('type') + updatedTreatment.gameStages[stageIndex].elements[elementIndex].file = + watch('file') + // TODO: add more fields here } + editTreatment(updatedTreatment) } catch (error) { - console.error(error); + console.error(error) } } - // const parseResult = elementSchema.safeParse({ - // name: watch("name"), - // type: watch("type"), - // file: watch("file"), - // url: watch("url"), - // params: watch("params"), - // style: watch("style"), - // buttonText: watch("buttonText"), - // startTime: watch("startTime"), - // endTime: watch("endTime") - // }) - // if (!parseResult.success) { - // console.log(parseResult.error) - // window.alert( parseResult.error ) - // setIsValid(false); - // return; - // } - - //setIsValid(true); - function deleteElement() { const confirm = window.confirm( - "Are you sure you want to delete the element?" - ); + 'Are you sure you want to delete the element?' + ) if (confirm) { - const updatedTreatment = JSON.parse(JSON.stringify(treatment)); // deep copy - updatedTreatment.gameStages[stageIndex].elements.splice(elementIndex, 1); // delete in place - editTreatment(updatedTreatment); + const updatedTreatment = JSON.parse(JSON.stringify(treatment)) // deep copy + updatedTreatment.gameStages[stageIndex].elements.splice(elementIndex, 1) // delete in place + editTreatment(updatedTreatment) } } // FORM QUESTIONS - const htmlElements = []; + const htmlElements = [] htmlElements.push( -
+