Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset modal after adding event #442

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions client/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ import Modal from "features/modal/Modal";
import RejectionModal from "features/modal/RejectionModal";
import WelcomeUserModal from "features/modal/WelcomeUserModal";
import { useAuthContext } from "contexts/AuthContext";
import FormProvider from "contexts/FormContext";
import LandingPage from "pages/LandingPage";
import CalendarPage from "pages/CalendarPage";
import { AdminDashboard } from "pages/AdminDashboard";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import { Routes, Route } from "react-router-dom";

function App() {
const auth = useAuthContext();
Expand Down Expand Up @@ -38,7 +39,14 @@ function App() {
<>
<Routes>
<Route path="/" element={<LandingPage />} />
<Route path="/calendar" element={<CalendarPage />} />
<Route
path="/calendar"
element={
<FormProvider>
<CalendarPage />
</FormProvider>
}
/>
<Route path="/adminDashboard" element={<AdminDashboard />} />
</Routes>
{isAuthenticated && (
Expand Down
8 changes: 8 additions & 0 deletions client/src/contexts/FormContext/useProvideForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ const useProvideForm = () => {
const [formCreateEventErrors, setFormCreateEventErrors] = useState([]);
const [formScheduleEventErrors, setFormScheduleEventErrors] = useState([]);

const resetForm = () => {
if (currentStep === 4) {
setFormData({ recurring: { rate: "noRecurr", days: [] } });
setCurrentStep(1);
}
};

const handleNewStep = async direction => {
const newStep = direction === "next" ? currentStep + 1 : currentStep - 1;

Expand Down Expand Up @@ -62,6 +69,7 @@ const useProvideForm = () => {
setFormCreateEventErrors,
setFormScheduleEventErrors,
setCurrentStep,
resetForm,
};
};

Expand Down
2 changes: 1 addition & 1 deletion client/src/features/form/FormCreateEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default function FormCreateEvent() {
<div className="flex flex-col">
{formCreateEventErrors.map((error, index) => {
return (
<div className="alert alert-error shadow-lg text-red-700" key="index">
<div className="alert alert-error shadow-lg text-red-700" key={index}>
<div>
<svg
xmlns="http://www.w3.org/2000/svg"
Expand Down
2 changes: 1 addition & 1 deletion client/src/features/form/FormScheduleEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function FormScheduleEvent() {
<div>
{formScheduleEventErrors.map((error, index) => {
return (
<div className="alert alert-error shadow-lg text-red-700" key="index">
<div className="alert alert-error shadow-lg text-red-700" key={index}>
<div>
<svg
xmlns="http://www.w3.org/2000/svg"
Expand Down
9 changes: 2 additions & 7 deletions client/src/features/form/FormSuccess.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { useFormContext } from "contexts/FormContext";

export default function FormSuccess() {
const form = useFormContext();
const { resetForm } = useFormContext();
return (
<div className="container md:mt-10">
<div className="flex flex-col items-center">
Expand All @@ -14,12 +14,7 @@ export default function FormSuccess() {

{/* A button to add another event if so desired */}
<button
onClick={() => {
form.setFormData({
recurring: { rate: "noRecurr", days: [] },
});
form.setCurrentStep(1);
}}
onClick={resetForm}
className="h-10 mt-10 px-5 font-semibold text-green-700 transition-colors duration-150 border border-gray-300 rounded-lg focus:shadow-outline hover:bg-green-500 hover:text-green-100"
>
New Event
Expand Down
9 changes: 6 additions & 3 deletions client/src/features/form/UserForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { useFormModalContext } from "contexts/FormModalContext";
// This is the code for the form where you add events to the calendar
const UserForm = () => {
// Specifically extract the currentStep and totalSteps from userFormContext
const { currentStep, totalSteps } = useFormContext();
const { currentStep, totalSteps, resetForm } = useFormContext();

const modal = useFormModalContext();
const { handleClose } = useFormModalContext();

// Called to display different parts of the form based on the latest step.
const displayStep = step => {
Expand All @@ -39,7 +39,10 @@ const UserForm = () => {
<div className="flex flex-col items-center">
<button
className="w-auto h-12 mt-5 px-2 border-solid border-2 border-gray outline-none rounded font-semibold text-xl hover:bg-teal-600 active:bg-teal-700 focus:outline-none focus:ring focus:ring-teal-300"
onClick={modal.handleClose}
onClick={() => {
handleClose();
resetForm();
}}
>
Close
</button>
Expand Down
9 changes: 7 additions & 2 deletions client/src/features/modal/Modal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { AnimatePresence } from "framer-motion";
import Backdrop from "./Backdrop";
import { motion } from "framer-motion";

Copy link
Contributor

@cblanken cblanken Sep 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optionally you could add a check for the onBackdropClick type with prop-types. That way you get an error on page load if an invalid onBackdropClick prop is passed in instead of at runtime.

Suggested change
import PropTypes from "prop-types";
// Modal component definition
Modal.propTypes = {
onBackdropClick: PropTypes.func,
};

Like this
image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a redesign for the event modal is in the works like @intelagense mentioned then this may not be that important. Just my two cents.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a redesign for the event modal is in the works like @intelagense mentioned then this may not be that important. Just my two cents.

I'm not sure the redesign will be coming anytime soon.

const Modal = ({ children, context }) => {
const Modal = ({ children, context, onBackdropClick }) => {
const dropIn = {
hidden: {
y: "-100vh",
Expand Down Expand Up @@ -32,7 +32,12 @@ const Modal = ({ children, context }) => {
onExitComplete={() => null}
>
{context.isOpen && (
<Backdrop onClick={context.handleClose}>
<Backdrop
onClick={() => {
context.handleClose();
if (typeof onBackdropClick === "function") onBackdropClick();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this conditional should silently pass if a "function" typed onBackdropClick isn't passed as a prop.

If someone is adding more modals later but accidentally passes an onBackdropClick of some type other than "function", this if will essentially hide that error.

Instead, it might be better to just check for the existence of onBackdropClick. Then if a non-function prop is passed to Modal it will easily be caught during testing.

Suggested change
if (typeof onBackdropClick === "function") onBackdropClick();
if (onBackdropClick) onBackdropClick();

}}
>
<motion.div
className="modal overflow-y-auto"
onClick={e => e.stopPropagation()}
Expand Down
9 changes: 5 additions & 4 deletions client/src/pages/CalendarPage.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import useDate from "hooks/useDate";
import Calendar from "../features/calendar/Calendar";
import CalendarHeader from "../features/calendarHeader";
import FormProvider from "contexts/FormContext";
import { useFormContext } from "contexts/FormContext";
import Modal from "features/modal/Modal";
import UserForm from "features/form/UserForm";
import { useAuthContext } from "contexts/AuthContext";
Expand All @@ -16,6 +16,7 @@ function CalendarPage() {
const date = useDate();
const formModal = useFormModalContext();
const modal = useModalContext();
const { resetForm } = useFormContext();
const canScrollMonthRef = useRef(true);

const handleWheelScroll = e => {
Expand All @@ -33,7 +34,7 @@ function CalendarPage() {
};

return (
<FormProvider>
<>
<main
onWheel={handleWheelScroll}
className="flex flex-col gap-3 p-3 shadow-sm min-h-screen max-w-[1920px] mx-auto"
Expand All @@ -45,15 +46,15 @@ function CalendarPage() {
<Modal context={modal}>
<EventModal />
</Modal>
<Modal context={formModal}>
<Modal context={formModal} onBackdropClick={resetForm}>
{auth?.user ? (
<UserForm />
) : (
<RejectionModal handleClose={formModal.handleClose} />
)}
</Modal>
</div>
</FormProvider>
</>
);
}
export default CalendarPage;
4 changes: 3 additions & 1 deletion cypress/e2e/create-event-form.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ describe("Event Creation Form", () => {
cy.contains("button", "Test Title");

tgt.calendar.button.addEvent().click();
cy.contains("button", "New Event").click();
tgt.createForm.input.title().should("exist");
});

Expand Down Expand Up @@ -281,5 +280,8 @@ describe("Event Creation Form", () => {
"have.length.of.at.least",
2
);

tgt.calendar.button.addEvent().click();
tgt.createForm.input.title().should("exist");
});
});