All files / components/PersonalSchedules AddToScheduleModal.jsx

100% Statements 33/33
100% Branches 9/9
100% Functions 10/10
100% Lines 32/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183                                  868x 868x 868x 868x               868x   868x                   868x 1x 1x 1x     868x 1x     868x             868x 7x 7x     868x 2x 2x     868x 1x     868x 9x             9x         9x     868x 3x 1x   2x         868x   868x       21x                                                                                                                                                                
import React, { useState } from "react";
import Modal from "react-bootstrap/Modal";
import Button from "react-bootstrap/Button";
import Form from "react-bootstrap/Form";
import PersonalScheduleSelector from "./PersonalScheduleSelector";
import { schedulesFilter } from "main/utils/PersonalScheduleUtils";
import { yyyyqToQyy } from "main/utils/quarterUtilities.jsx";
import { useBackendMutation } from "main/utils/useBackend";
import { toast } from "react-toastify";
 
export default function AddToScheduleModal({
  quarter,
  section,
  onAdd,
  schedules,
  testid = "AddToScheduleModal",
}) {
  const [showModal, setShowModal] = useState(false);
  const [selectedSchedule, setSelectedSchedule] = useState("");
  const [modalMode, setModalMode] = useState("normal");
  const [scheduleParams, setScheduleParams] = useState({
    // Stryker disable next-line StringLiteral : initial values are overwritten before use
    name: "",
    // Stryker disable next-line StringLiteral : initial values are overwritten before use
    description: "",
    quarter: quarter,
  });
 
  const filteringSchedules = schedulesFilter(schedules, quarter);
 
  const objectToAxiosParams = (personalSchedule) => ({
    url: "/api/personalschedules/post",
    method: "POST",
    params: {
      name: personalSchedule.name,
      description: personalSchedule.description,
      quarter: personalSchedule.quarter,
    },
  });
 
  const onSuccess = (data) => {
    toast(`Schedule "${data.name}" Created`);
    onAdd(section, data.id);
    handleModalClose();
  };
 
  const onError = (error) => {
    toast(`Error: ${error.response.data.message}`);
  };
 
  const mutation = useBackendMutation(
    objectToAxiosParams,
    { onSuccess, onError },
    // Stryker disable next-line all : hard to set up test for caching
    ["/api/personalschedules/all"],
  );
 
  const handleModalClose = () => {
    setShowModal(false);
    setModalMode("normal");
  };
 
  const handleModalSave = () => {
    onAdd(section, selectedSchedule);
    handleModalClose();
  };
 
  const handleModalSaveSchedule = () => {
    mutation.mutate(scheduleParams);
  };
 
  const handleCreateClick = () => {
    const timeString = new Date().toLocaleString([], {
      month: "short",
      day: "numeric",
      hour: "2-digit",
      minute: "2-digit",
    });
 
    setScheduleParams({
      ...scheduleParams,
      name: `${timeString} Schedule`,
      description: "Auto-generated schedule",
    });
    setModalMode("auto-create");
  };
 
  const onSaveButtonClick = () => {
    if (modalMode === "auto-create") {
      handleModalSaveSchedule();
    } else {
      handleModalSave();
    }
  };
 
  const showSaveButton =
    modalMode === "auto-create" || filteringSchedules.length > 0;
 
  return (
    <>
      <Button
        variant="success"
        onClick={() => setShowModal(true)}
        data-testid={`${testid}-add-to-schedule-button`}
      >
        Add
      </Button>
      <Modal
        show={showModal}
        onHide={handleModalClose}
        data-testid={`${testid}-modal`}
      >
        <Modal.Header closeButton>
          <Modal.Title>Add to Schedule</Modal.Title>
        </Modal.Header>
        <Modal.Body>
          <Form>
            {
              /* istanbul ignore next */
              filteringSchedules.length > 0 && modalMode === "normal" ? (
                <Form.Group controlId="scheduleSelect">
                  <Form.Label>Select Schedule</Form.Label>
                  <PersonalScheduleSelector
                    schedule={selectedSchedule}
                    setSchedule={setSelectedSchedule}
                    controlId="scheduleSelect"
                    filteringSchedules={filteringSchedules}
                  />
                </Form.Group>
              ) : (
                <div data-testid={`${testid}-no-schedules`}>
                  {modalMode === "auto-create" ? (
                    <p>
                      New Schedule: <strong>{scheduleParams.name}</strong> will
                      be created.
                    </p>
                  ) : (
                    <p>
                      There are no personal schedules for {yyyyqToQyy(quarter)}.
                      <Button
                        onClick={handleCreateClick}
                        style={{
                          backgroundColor: "transparent",
                          border: "none",
                          color: "#007bff",
                          padding: 0,
                          verticalAlign: "baseline",
                          textDecoration: "underline",
                        }}
                      >
                        [Create Personal Schedule]
                      </Button>
                    </p>
                  )}
                </div>
              )
            }
          </Form>
        </Modal.Body>
        <Modal.Footer>
          <Button
            variant="secondary"
            onClick={handleModalClose}
            data-testid={`${testid}-modal-close-button`}
          >
            Cancel
          </Button>
          {showSaveButton && (
            <Button
              variant="primary"
              onClick={onSaveButtonClick}
              data-testid={`${testid}-modal-save-button`}
              disabled={mutation.isLoading}
            >
              {mutation.isLoading ? "Creating..." : "Save Changes"}
            </Button>
          )}
        </Modal.Footer>
      </Modal>
    </>
  );
}