mirror of
https://github.com/alexmickelson/canvasManagement.git
synced 2026-03-25 23:28:33 -06:00
popup modal to protect lecture override
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
"use client";
|
"use client";
|
||||||
import { ReactNode, useCallback, DragEvent, useEffect } from "react";
|
import { ReactNode, useCallback, DragEvent, useEffect, useState } from "react";
|
||||||
import { DraggableItem, DraggingContext } from "./draggingContext";
|
import { DraggableItem, DraggingContext } from "./draggingContext";
|
||||||
import { useUpdateQuizMutation } from "@/hooks/localCourse/quizHooks";
|
import { useUpdateQuizMutation } from "@/hooks/localCourse/quizHooks";
|
||||||
import { useLocalCourseSettingsQuery } from "@/hooks/localCourse/localCoursesHooks";
|
import { useLocalCourseSettingsQuery } from "@/hooks/localCourse/localCoursesHooks";
|
||||||
@@ -20,6 +20,8 @@ import {
|
|||||||
useLectureUpdateMutation,
|
useLectureUpdateMutation,
|
||||||
} from "@/hooks/localCourse/lectureHooks";
|
} from "@/hooks/localCourse/lectureHooks";
|
||||||
import { getLectureForDay } from "@/models/local/lectureUtils";
|
import { getLectureForDay } from "@/models/local/lectureUtils";
|
||||||
|
import Modal, { useModal } from "@/components/Modal";
|
||||||
|
import { Spinner } from "@/components/Spinner";
|
||||||
|
|
||||||
export default function DraggingContextProvider({
|
export default function DraggingContextProvider({
|
||||||
children,
|
children,
|
||||||
@@ -33,6 +35,10 @@ export default function DraggingContextProvider({
|
|||||||
const updatePageMutation = useUpdatePageMutation();
|
const updatePageMutation = useUpdatePageMutation();
|
||||||
const { data: settings } = useLocalCourseSettingsQuery();
|
const { data: settings } = useLocalCourseSettingsQuery();
|
||||||
const { data: weeks } = useLecturesByWeekQuery();
|
const { data: weeks } = useLecturesByWeekQuery();
|
||||||
|
const [isLoading, setIsLoading] = useState(false);
|
||||||
|
const [modalText, setModalText] = useState("");
|
||||||
|
const modal = useModal();
|
||||||
|
const [modalCallback, setModalCallback] = useState<() => void>(() => {});
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleDrop = () => {
|
const handleDrop = () => {
|
||||||
@@ -166,7 +172,28 @@ export default function DraggingContextProvider({
|
|||||||
const existingLecture = getLectureForDay(weeks, dayAsDate);
|
const existingLecture = getLectureForDay(weeks, dayAsDate);
|
||||||
if (existingLecture) {
|
if (existingLecture) {
|
||||||
console.log("attempting to drop on existing lecture");
|
console.log("attempting to drop on existing lecture");
|
||||||
|
setModalText(
|
||||||
|
`Are you sure you want to replace ${existingLecture?.name} with ${lecture.name}? This will delete ${existingLecture.name}.`
|
||||||
|
);
|
||||||
|
|
||||||
|
setModalCallback(() => async () => { // because sometimes setStates receive a function
|
||||||
|
console.log("running callback");
|
||||||
|
setIsLoading(true);
|
||||||
|
await updateLectureMutation.mutateAsync({
|
||||||
|
previousDay: lecture.date,
|
||||||
|
lecture: {
|
||||||
|
...lecture,
|
||||||
|
date: getDateOnlyMarkdownString(dayAsDate),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
setModalText("");
|
||||||
|
setModalCallback(() => {});
|
||||||
|
modal.closeModal();
|
||||||
|
setIsLoading(false);
|
||||||
|
});
|
||||||
|
modal.openModal();
|
||||||
} else {
|
} else {
|
||||||
|
console.log("updating lecture on unique day");
|
||||||
updateLectureMutation.mutate({
|
updateLectureMutation.mutate({
|
||||||
previousDay: lecture.date,
|
previousDay: lecture.date,
|
||||||
lecture: {
|
lecture: {
|
||||||
@@ -249,6 +276,7 @@ export default function DraggingContextProvider({
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
[
|
[
|
||||||
|
modal,
|
||||||
setIsDragging,
|
setIsDragging,
|
||||||
settings.defaultDueTime.hour,
|
settings.defaultDueTime.hour,
|
||||||
settings.defaultDueTime.minute,
|
settings.defaultDueTime.minute,
|
||||||
@@ -267,6 +295,30 @@ export default function DraggingContextProvider({
|
|||||||
itemDropOnModule,
|
itemDropOnModule,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
|
<Modal modalControl={modal} buttonText={""} buttonClass="hidden">
|
||||||
|
{({ closeModal }) => (
|
||||||
|
<div>
|
||||||
|
<div className="text-center">{modalText}</div>
|
||||||
|
<br />
|
||||||
|
<div className="flex justify-around gap-3">
|
||||||
|
<button
|
||||||
|
onClick={() => {
|
||||||
|
console.log("deleting");
|
||||||
|
modalCallback();
|
||||||
|
}}
|
||||||
|
disabled={isLoading}
|
||||||
|
className="btn-danger"
|
||||||
|
>
|
||||||
|
Yes
|
||||||
|
</button>
|
||||||
|
<button onClick={closeModal} disabled={isLoading}>
|
||||||
|
No
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
{isLoading && <Spinner />}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
</Modal>
|
||||||
{children}
|
{children}
|
||||||
</DraggingContext.Provider>
|
</DraggingContext.Provider>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
"use client";
|
"use client";
|
||||||
import React, { ReactNode, useState } from "react";
|
import React, { ReactNode, useCallback, useMemo, useState } from "react";
|
||||||
|
|
||||||
export interface ModalControl {
|
export interface ModalControl {
|
||||||
isOpen: boolean;
|
isOpen: boolean;
|
||||||
@@ -10,14 +10,17 @@ export interface ModalControl {
|
|||||||
export function useModal() {
|
export function useModal() {
|
||||||
const [isOpen, setIsOpen] = useState(false);
|
const [isOpen, setIsOpen] = useState(false);
|
||||||
|
|
||||||
const openModal = () => setIsOpen(true);
|
const openModal = useCallback(() => setIsOpen(true), []);
|
||||||
const closeModal = () => setIsOpen(false);
|
const closeModal = useCallback(() => setIsOpen(false), []);
|
||||||
|
|
||||||
return {
|
return useMemo(
|
||||||
isOpen,
|
() => ({
|
||||||
openModal,
|
isOpen,
|
||||||
closeModal,
|
openModal,
|
||||||
};
|
closeModal,
|
||||||
|
}),
|
||||||
|
[closeModal, isOpen, openModal]
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function Modal({
|
export default function Modal({
|
||||||
|
|||||||
Reference in New Issue
Block a user