// islands/QuizConfigure.tsx import { useEffect, useState } from "preact/hooks"; import { QuizConfig } from "../utils/types.ts"; import { AsyncYesNoPop } from "./AsyncYesNoPop.tsx"; export interface ArrangementProps { quizConfig: QuizConfig; // kvObj: KvObj; quizOrder: { index: number; slots: string[] }; quizMode: string; tabs: string[]; } const NEW_TAB_VALUE = "__new_tab__"; export default function ArrangeQuiz( { quizConfig, quizOrder, quizMode, tabs }: ArrangementProps, ) { // Theme classes are defined in styles.css with @media (prefers-color-scheme: dark) const [ask, popupElement] = AsyncYesNoPop(); const [thisQuizConfig, setQuizConfig] = useState(quizConfig); const [orderedQuizStr, setOrderQuizStr] = useState( JSON.stringify(thisQuizConfig), ); const [thisQuizConfigStr, setQuizConfigStr] = useState( JSON.stringify({ theUpdate: null, quiz: quizConfig, //kvObj: kvObj, quizOrder: quizOrder, }), ); const [selectedSlot, setSelectedSlot] = useState(0); const [thisQuizOder, setThisQuizOrder] = useState(quizOrder); const [randomOrder, setRandomOrder] = useState(true); const [modeType, setModeType] = useState(quizMode); //const [isLoading, setIsLoading] = useState(true); // Tab selection state const [selectedTabIndex, setSelectedTabIndex] = useState< number | typeof NEW_TAB_VALUE >(-1); const [newTabName, setNewTabName] = useState(""); const isNewTab = selectedTabIndex === NEW_TAB_VALUE; const isExistingTab = typeof selectedTabIndex === "number" && selectedTabIndex >= 0; const [count, setCount] = useState(0); const handleClick = () => { setCount((prev) => { const newCount = prev + 1; return newCount; }); }; const handleSaveQuiz = async (event: Event) => { //console.log("Clicked save"); //if (changeMode && changeMode.mode) { // setQuizConfigStr(JSON.stringify({ theUpdate:changeMode.mode, quiz: thisQuizConfig, kvObj:kvObj,quizOrder:thisQuizOder })); // handleClick(); event.preventDefault(); //value={JSON.stringify({ theUpdate:null, quiz: quiz , kvObj:quizProps[index] ? cloneObjectExcept(quizProps[index],["value"]) as KvObj: null})} const form = event.currentTarget as HTMLFormElement; const userConfirmed = await ask( "Do you want to save this change?", Date.now(), ); if (userConfirmed) { form.submit(); } //const userConfirmed = window.confirm("Do you wanted to save changes?"); // if (userConfirmed) { // (event.currentTarget as HTMLFormElement).submit(); // } //} else { // event.preventDefault(); //} }; useEffect(() => { //console.log("setting mode!!",quizConfig.quiz_mode) if (quizConfig.quiz_mode) { setModeType(quizConfig.quiz_mode); } }, []); useEffect(() => { // Initialize selectedTabIndex to 0 when tabs are available and no tab is selected if (tabs.length > 0 && selectedTabIndex === -1) { setSelectedTabIndex(0); } }, [tabs, selectedTabIndex]); return ( <> {popupElement}

Arrange Quizzes

Dashboard Quiz List

{/* Step 1: Select a quiz */}
{/* Step 2: Select a tab (only shown after quiz is selected) */} {thisQuizOder.index != -1 && (
)} {/* Step 3a: New tab name input (only shown if "New Tab" is selected) */} {thisQuizOder.index != -1 && isNewTab && (
setNewTabName(e.target.value)} placeholder="Enter new tab name" className="w-full px-3 py-2 rounded-md theme-input border theme-border" />
)} {/* Step 3b: Position selector (only shown if existing tab is selected) */} {thisQuizOder.index != -1 && isExistingTab && (
)} {/* Save button for moving to new tab */} {thisQuizOder.index != -1 && isNewTab && newTabName.trim().length > 0 && (
)} {/* Save button for reordering position within tab */} {thisQuizOder.index != -1 && isExistingTab && thisQuizOder.index !== selectedSlot && (
)} {/* Save button for moving to different existing tab (same position) */} {thisQuizOder.index != -1 && isExistingTab && thisQuizOder.index === selectedSlot && (
)}
); }