import React, { useEffect, useState } from 'react'; import AutoExpandingTextArea from "@/components/Low/AutoExpandingTextarea"; import { Card, CardContent } from "@/components/ui/card"; import { AiOutlineUnorderedList, AiOutlinePlus, AiOutlineDelete } from 'react-icons/ai'; import Header from "../../Shared/Header"; import GenLoader from "../Shared/GenLoader"; import useSectionEdit from "../../Hooks/useSectionEdit"; import useExamEditorStore from "@/stores/examEditor"; import { InteractiveSpeakingExercise } from "@/interfaces/exam"; import { BsFileText } from "react-icons/bs"; import { RiVideoLine } from 'react-icons/ri'; import { FaChevronLeft, FaChevronRight } from 'react-icons/fa6'; import { Module } from '@/interfaces'; interface Props { sectionId: number; exercise: InteractiveSpeakingExercise; module?: Module; } const Speaking1: React.FC = ({ sectionId, exercise, module = "speaking" }) => { const {dispatch} = useExamEditorStore(); const [local, setLocal] = useState(() => { const defaultPrompts = [ { text: "Hello my name is {avatar}, what is yours?", video_url: "" }, { text: "Do you work or do you study?", video_url: "" }, ...exercise.prompts.slice(2) ]; return { ...exercise, prompts: defaultPrompts }; }); const [currentVideoIndex, setCurrentVideoIndex] = useState(0); const { generating, genResult , state} = useExamEditorStore( (state) => state.modules[module].sections.find((section) => section.sectionId === sectionId)! ); const { editing, setEditing, handleSave, handleDiscard, handleEdit, handlePractice } = useSectionEdit({ sectionId, onSave: () => { setEditing(false); dispatch({ type: "UPDATE_SECTION_STATE", payload: { sectionId: sectionId, update: local, module } }); if (genResult) { dispatch({ type: "UPDATE_SECTION_SINGLE_FIELD", payload: { sectionId, module: module, field: "genResult", value: undefined } }); } }, onDiscard: () => { setLocal({ ...exercise, prompts: [ { text: "Hello my name is {avatar}, what is yours?", video_url: "" }, { text: "Do you work or do you study?", video_url: "" }, ...exercise.prompts.slice(2) ] }); }, onPractice: () => { const updatedExercise = { ...state, isPractice: !local.isPractice }; setLocal((prev) => ({...prev, isPractice: !local.isPractice})) dispatch({ type: 'UPDATE_SECTION_STATE', payload: { sectionId, update: updatedExercise, module: module } }); }, }); useEffect(() => { if (genResult && generating === "speakingScript") { setEditing(true); setLocal(prev => ({ ...prev, first_title: genResult.result[0].first_topic, second_title: genResult.result[0].second_topic, prompts: [ prev.prompts[0], prev.prompts[1], ...genResult.result[0].prompts.map((item: any) => ({ text: item, video_url: "" })) ] })); dispatch({ type: "UPDATE_SECTION_SINGLE_FIELD", payload: { sectionId, module: module, field: "generating", value: undefined } }); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [genResult, generating]); const addPrompt = () => { setLocal(prev => ({ ...prev, prompts: [...prev.prompts, { text: "", video_url: "" }] })); }; const removePrompt = (index: number) => { if (index < 2) return; setLocal(prev => ({ ...prev, prompts: prev.prompts.filter((_, i) => i !== index) })); }; const updatePrompt = (index: number, text: string) => { if (index < 2) return; setLocal(prev => { const newPrompts = [...prev.prompts]; newPrompts[index] = { ...newPrompts[index], text }; return { ...prev, prompts: newPrompts }; }); }; const isUnedited = local.prompts.length === 2; useEffect(() => { if (genResult && generating === "video") { setLocal({ ...local, prompts: genResult.result[0].prompts }); dispatch({ type: "UPDATE_SECTION_STATE", payload: { sectionId, update: { ...local, prompts: genResult.result[0].prompts }, module } }); dispatch({ type: "UPDATE_SECTION_SINGLE_FIELD", payload: { sectionId, module: module, field: "generating", value: undefined } }); dispatch({ type: "UPDATE_SECTION_SINGLE_FIELD", payload: { sectionId, module: module, field: "genResult", value: undefined } }); } // eslint-disable-next-line react-hooks/exhaustive-deps }, [genResult, generating]); const handlePrevVideo = () => { setCurrentVideoIndex((prev) => (prev > 0 ? prev - 1 : prev)); }; const handleNextVideo = () => { setCurrentVideoIndex((prev) => (prev < local.prompts.length - 1 ? prev + 1 : prev) ); }; return ( <>
{generating && generating === "speakingScript" ? ( ) : ( <> {editing ? ( <>

Titles

First Title

setLocal(prev => ({ ...prev, first_title: text }))} className="w-full p-3 border border-gray-300 rounded-md focus:ring-2 focus:ring-blue-500 focus:border-transparent min-h-[80px] transition-all" placeholder="Enter the first title" />

Second Title

setLocal(prev => ({ ...prev, second_title: text }))} className="w-full p-3 border border-gray-300 rounded-md focus:ring-2 focus:ring-blue-500 focus:border-transparent min-h-[80px] transition-all" placeholder="Enter the second title" />

Questions

{local.prompts.length === 2 ? (

No questions added yet

) : ( local.prompts.slice(2).map((prompt, index) => (

Question {index + 1}

updatePrompt(index + 2, text)} className="w-full p-3 border border-gray-200 rounded-md focus:ring-2 focus:ring-blue-500 focus:border-transparent min-h-[80px] transition-all bg-white" placeholder={`Enter question ${index + 1}`} />
)) )}
) : isUnedited ? (

Generate or edit the questions!

) : (
{local.prompts.every((p) => p.video_url !== "") && (

Videos

{currentVideoIndex + 1} / {local.prompts.length}
)} {generating && generating === "video" && }

Titles

First Title

{local.first_title || 'No first title'}

Second Title

{local.second_title || 'No second title'}

Questions

{local.prompts.slice(2) .filter(prompt => prompt.text !== "") .map((prompt, index) => (

Question {index + 1}

{prompt.text}

)) }
)} )} ); }; export default Speaking1;