fixing dynamic expands

This commit is contained in:
2024-09-27 11:55:40 -06:00
parent a462d66a7e
commit 5667c8ba9e
3 changed files with 105 additions and 73 deletions

View File

@@ -1,8 +1,8 @@
"use client"; "use client";
import { useState } from "react";
import { CalendarMonthModel } from "./calendarMonthUtils"; import { CalendarMonthModel } from "./calendarMonthUtils";
import { DayOfWeek } from "@/models/local/localCourse"; import { DayOfWeek } from "@/models/local/localCourse";
import Day from "./day/Day"; import Day from "./day/Day";
import { Expandable } from "@/components/Expandable";
export const CalendarMonth = ({ month }: { month: CalendarMonthModel }) => { export const CalendarMonth = ({ month }: { month: CalendarMonthModel }) => {
const weekInMilliseconds = 604_800_000; const weekInMilliseconds = 604_800_000;
@@ -10,33 +10,30 @@ export const CalendarMonth = ({ month }: { month: CalendarMonthModel }) => {
new Date(month.year, month.month, 1) < new Date(month.year, month.month, 1) <
new Date(Date.now() - weekInMilliseconds); new Date(Date.now() - weekInMilliseconds);
const [isCollapsed, setIsCollapsed] = useState(isInPast);
const monthName = new Date(month.year, month.month - 1, 1).toLocaleString( const monthName = new Date(month.year, month.month - 1, 1).toLocaleString(
"default", "default",
{ month: "long" } { month: "long" }
); );
const toggleCollapse = () => setIsCollapsed(!isCollapsed);
const weekDaysList: DayOfWeek[] = Object.values(DayOfWeek); const weekDaysList: DayOfWeek[] = Object.values(DayOfWeek);
return ( return (
<> <>
<Expandable
defaultExpanded={!isInPast}
ExpandableElement={({ setIsExpanded, isExpanded }) => (
<div className="flex justify-center"> <div className="flex justify-center">
<h3 <h3
className={ className={
"text-2xl transition-all duration-500 " + "text-2xl transition-all duration-500 " +
"hover:text-slate-50 underline hover:scale-105 `" "hover:text-slate-50 underline hover:scale-105 `"
} }
onClick={toggleCollapse} onClick={() => setIsExpanded((e) => !e)}
role="button" role="button"
> >
{monthName} {monthName}
</h3> </h3>
</div> </div>
)}
<div
id={monthName}
className={"collapsible " + (isCollapsed ? "" : "expand")}
> >
<div className="grid grid-cols-7 text-center fw-bold"> <div className="grid grid-cols-7 text-center fw-bold">
{weekDaysList.map((day) => ( {weekDaysList.map((day) => (
@@ -49,7 +46,7 @@ export const CalendarMonth = ({ month }: { month: CalendarMonthModel }) => {
{month.daysByWeek.map((week, weekIndex) => ( {month.daysByWeek.map((week, weekIndex) => (
<CalendarWeek key={weekIndex} week={week} monthNumber={month.month} /> <CalendarWeek key={weekIndex} week={week} monthNumber={month.month} />
))} ))}
</div> </Expandable>
</> </>
); );
}; };

View File

@@ -1,18 +1,14 @@
"use client"; "use client";
import { useAssignmentsQueries } from "@/hooks/localCourse/assignmentHooks"; import { useAssignmentsQueries } from "@/hooks/localCourse/assignmentHooks";
import { import { usePagesQueries } from "@/hooks/localCourse/pageHooks";
usePagesQueries, import { useQuizzesQueries } from "@/hooks/localCourse/quizHooks";
} from "@/hooks/localCourse/pageHooks";
import {
useQuizzesQueries,
} from "@/hooks/localCourse/quizHooks";
import { IModuleItem } from "@/models/local/IModuleItem"; import { IModuleItem } from "@/models/local/IModuleItem";
import { import {
getDateFromString, getDateFromString,
getDateFromStringOrThrow, getDateFromStringOrThrow,
getDateOnlyMarkdownString, getDateOnlyMarkdownString,
} from "@/models/local/timeUtils"; } from "@/models/local/timeUtils";
import { Fragment, useRef, useState } from "react"; import { Fragment } from "react";
import Modal from "../../../../components/Modal"; import Modal from "../../../../components/Modal";
import NewItemForm from "./NewItemForm"; import NewItemForm from "./NewItemForm";
import { ModuleCanvasStatus } from "./ModuleCanvasStatus"; import { ModuleCanvasStatus } from "./ModuleCanvasStatus";
@@ -23,6 +19,7 @@ import DropTargetStyling from "../../../../components/DropTargetStyling";
import Link from "next/link"; import Link from "next/link";
import { getModuleItemUrl } from "@/services/urlUtils"; import { getModuleItemUrl } from "@/services/urlUtils";
import { useCourseContext } from "../context/courseContext"; import { useCourseContext } from "../context/courseContext";
import { Expandable } from "../../../../components/Expandable";
export default function ExpandableModule({ export default function ExpandableModule({
moduleName, moduleName,
@@ -35,7 +32,7 @@ export default function ExpandableModule({
const { data: quizzes } = useQuizzesQueries(moduleName); const { data: quizzes } = useQuizzesQueries(moduleName);
const { data: pages } = usePagesQueries(moduleName); const { data: pages } = usePagesQueries(moduleName);
const [expanded, setExpanded] = useState(false); // const [expanded, setExpanded] = useState(false);
const moduleItems: { const moduleItems: {
type: "assignment" | "quiz" | "page"; type: "assignment" | "quiz" | "page";
@@ -65,7 +62,7 @@ export default function ExpandableModule({
"item due date in expandable module" "item due date in expandable module"
).getTime() ).getTime()
); );
const expandRef = useRef<HTMLDivElement | null>(null); // const expandRef = useRef<HTMLDivElement | null>(null);
return ( return (
<div <div
@@ -75,10 +72,12 @@ export default function ExpandableModule({
> >
<DropTargetStyling draggingClassName="shadow-[0_0px_10px_0px] shadow-blue-500/50 "> <DropTargetStyling draggingClassName="shadow-[0_0px_10px_0px] shadow-blue-500/50 ">
<div className=" p-3 "> <div className=" p-3 ">
<Expandable
ExpandableElement={({ setIsExpanded, isExpanded }) => (
<div <div
className="font-bold flex flex-row justify-between " className="font-bold flex flex-row justify-between "
role="button" role="button"
onClick={() => setExpanded((e) => !e)} onClick={() => setIsExpanded((e) => !e)}
> >
<div>{moduleName}</div> <div>{moduleName}</div>
<div className="flex flex-row"> <div className="flex flex-row">
@@ -87,22 +86,21 @@ export default function ExpandableModule({
</ClientOnly> </ClientOnly>
<ExpandIcon <ExpandIcon
style={{ style={{
...(expanded ? { rotate: "-90deg" } : {}), ...(isExpanded ? { rotate: "-90deg" } : {}),
}} }}
/> />
</div> </div>
</div> </div>
<div )}
ref={expandRef}
className={` overflow-hidden transition-all `}
style={{
maxHeight: expanded ? expandRef?.current?.scrollHeight : "0",
}}
> >
<>
<Modal buttonText="New Item"> <Modal buttonText="New Item">
{({ closeModal }) => ( {({ closeModal }) => (
<div> <div>
<NewItemForm moduleName={moduleName} onCreate={closeModal} /> <NewItemForm
moduleName={moduleName}
onCreate={closeModal}
/>
<br /> <br />
<button onClick={closeModal}>close</button> <button onClick={closeModal}>close</button>
</div> </div>
@@ -118,7 +116,8 @@ export default function ExpandableModule({
/> />
))} ))}
</div> </div>
</div> </>
</Expandable>
</div> </div>
</DropTargetStyling> </DropTargetStyling>
</div> </div>

View File

@@ -0,0 +1,36 @@
"use client";
import { ReactNode, Dispatch, SetStateAction, useState, useRef } from "react";
export function Expandable({
children,
ExpandableElement,
defaultExpanded = false,
}: {
children: ReactNode;
ExpandableElement: (props: {
setIsExpanded: Dispatch<SetStateAction<boolean>>;
isExpanded: boolean;
}) => ReactNode;
defaultExpanded?: boolean;
}) {
const [isExpanded, setIsExpanded] = useState(defaultExpanded);
const expandRef = useRef<HTMLDivElement | null>(null);
return (
<>
<ExpandableElement
setIsExpanded={setIsExpanded}
isExpanded={isExpanded}
/>
<div
ref={expandRef}
className={` overflow-hidden transition-all `}
style={{
maxHeight: isExpanded ? expandRef?.current?.scrollHeight : "0",
}}
>
{children}
</div>
</>
);
}