Skip to content

Commit 0465bc2

Browse files
committed
second commit
1 parent f44c9b7 commit 0465bc2

1 file changed

Lines changed: 60 additions & 3 deletions

File tree

src/components/shared/NewResourceModal.tsx

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from "react";
1+
import React, { useEffect } from "react";
22
import { useTranslation } from "react-i18next";
33
import NewEventWizard from "../events/partials/wizards/NewEventWizard";
44
import NewSeriesWizard from "../events/partials/wizards/NewSeriesWizard";
@@ -7,6 +7,7 @@ import NewAclWizard from "../users/partials/wizard/NewAclWizard";
77
import NewGroupWizard from "../users/partials/wizard/NewGroupWizard";
88
import NewUserWizard from "../users/partials/wizard/NewUserWizard";
99
import { Modal, ModalHandle } from "./modals/Modal";
10+
import { useState } from "react";
1011

1112
/**
1213
* This component renders the modal for adding new resources
@@ -23,11 +24,45 @@ const NewResourceModal = ({
2324
modalRef: React.RefObject<ModalHandle | null>
2425
}) => {
2526
const { t } = useTranslation();
27+
const [mode, setMode] = useState<"form" | "confirm">("form");
28+
const [closeRequested, setCloseRequested] = useState(false);
2629

2730
const close = () => {
2831
handleClose();
2932
};
3033

34+
const onAttemptClose = () => {
35+
if (mode === "form") {
36+
setCloseRequested(true);
37+
return false; // prevent close, show confirmation modal
38+
}
39+
if (mode === "confirm") {
40+
cancelClose(); // go back to form mode
41+
return false; // prevent modal close
42+
}
43+
return true; // default allow close
44+
};
45+
46+
// When closeRequested is set, switch to confirm mode
47+
useEffect(() => {
48+
if (closeRequested) {
49+
setMode("confirm");
50+
setCloseRequested(false); // Reset flag
51+
}
52+
}, [closeRequested]);
53+
54+
const confirmClose = () => {
55+
console.log("modalRef:", modalRef);
56+
console.log("modalRef.current:", modalRef.current);
57+
console.log("modalRef.current?.close:", modalRef.current?.close);
58+
setMode("form");
59+
modalRef.current?.close?.();
60+
handleClose();
61+
};
62+
const cancelClose = () => {
63+
setMode("form");
64+
};
65+
3166
const headerText = () => {
3267
switch (resource) {
3368
case "events": return t("EVENTS.EVENTS.NEW.CAPTION");
@@ -41,12 +76,14 @@ const NewResourceModal = ({
4176

4277
return (
4378
<Modal
44-
header={headerText()}
79+
header={mode === "confirm" ? "Confirm Close" : headerText()}
4580
classId="add-event-modal"
4681
// initialFocus={"#firstField"}
4782
ref={modalRef}
83+
closeCallback={onAttemptClose}
4884
>
49-
{resource === "events" && (
85+
<div style={{ display: mode === "form" ? "block" : "none" }}>
86+
{resource === "events" && (
5087
// New Event Wizard
5188
<NewEventWizard close={close} />
5289
)}
@@ -70,6 +107,26 @@ const NewResourceModal = ({
70107
// New User Wizard
71108
<NewUserWizard close={close} />
72109
)}
110+
</div>
111+
{mode === "confirm" && (
112+
<div style={{ padding: "1rem" }}>
113+
<p>Are you sure you want to close? Your changes will be lost.</p>
114+
<div style={{ display: "flex", justifyContent: "flex-end", gap: "1rem", marginTop: "1rem" }}>
115+
<button
116+
style={{ backgroundColor: "red", color: "white", padding: "0.5rem 1rem" }}
117+
onClick={confirmClose}
118+
>
119+
Yes, Close
120+
</button>
121+
<button
122+
style={{ backgroundColor: "gray", color: "white", padding: "0.5rem 1rem" }}
123+
onClick={cancelClose}
124+
>
125+
No, Go Back
126+
</button>
127+
</div>
128+
</div>
129+
)}
73130
</Modal>
74131
);
75132
};

0 commit comments

Comments
 (0)