All files / src/components/Playbooks SavePlaybookOverlay.tsx

0% Statements 0/37
0% Branches 0/17
0% Functions 0/7
0% Lines 0/37

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                                                                                                                                                                                                                                                 
import { useEffect, useState } from "react";
import styles from "./index.module.css";
import Overlay from "../Overlay/index.js";
import { useDispatch, useSelector } from "react-redux";
import {
  currentPlaybookSelector,
  setCurrentPlaybookKey,
} from "../../store/features/playbook/playbookSlice.ts";
import { Toast } from "../Toast.tsx";
import useIsExisting from "../../hooks/playbooks/useIsExisting.ts";
import CustomInput from "../Inputs/CustomInput.tsx";
import { InputTypes } from "../../types/inputs/inputTypes.ts";
 
const SavePlaybookOverlay = ({ isOpen, close, saveCallback }) => {
  const currentPlaybook = useSelector(currentPlaybookSelector);
  const dispatch = useDispatch();
  const [name, setName] = useState(currentPlaybook?.name);
  const [description, setDescription] = useState(currentPlaybook?.description);
  const [validationError, setValidationError] = useState("");
  const isExisting = useIsExisting();
 
  const handleSubmit = () => {
    Iif (!name && !currentPlaybook?.name) {
      setValidationError("Please enter a name");
      return;
    }
    dispatch(setCurrentPlaybookKey({ key: "description", value: description }));
    dispatch(setCurrentPlaybookKey({ key: "name", value: name }));
    saveCallback();
    close();
  };
 
  const handleDescriptionChange = (e) => {
    const val = e.target.value;
    setDescription(val);
  };
 
  const handleClose = () => {
    dispatch(setCurrentPlaybookKey({ key: "description", value: description }));
    dispatch(setCurrentPlaybookKey({ key: "name", value: name }));
    close();
  };
 
  useEffect(() => {
    setName(currentPlaybook?.name);
  }, [currentPlaybook?.name]);
 
  return (
    <div style={{ zIndex: "200" }}>
      <Overlay close={handleClose} visible={isOpen}>
        <div className={styles["dashboardSaveOverlay"]}>
          <div
            style={!isExisting ? { gap: "10px" } : {}}
            className={styles["dashboardSaveOverlay__content"]}>
            <div className={styles["panel__description"]}>
              {isExisting
                ? `Update "${currentPlaybook?.name}" playbook? `
                : currentPlaybook?.name
                ? `Save this playbook as "${currentPlaybook?.name}"?`
                : "Please enter a name"}
            </div>
            <div className={styles["panel__description"]}>
              {isExisting && "NOTE: This action is irreversible"}
            </div>
            {!isExisting && !currentPlaybook?.name && (
              <div>
                <CustomInput
                  inputType={InputTypes.TEXT}
                  label="Name"
                  handleChange={(val) => setName(val)}
                  value={name}
                  placeholder={"Enter Playbook name"}
                  className="!w-[300px]"
                />
              </div>
            )}
            {!isExisting && !currentPlaybook?.description && (
              <div>
                <label className="text-xs font-bold text-gray-500">
                  Description
                </label>
                <textarea
                  className={
                    "w-full border border-gray-300 p-1 rounded text-xs resize-none text-[#676666] h-32"
                  }
                  placeholder="Add Playbook description"
                  value={description}
                  onChange={handleDescriptionChange}
                />
              </div>
            )}
          </div>
          <div className={styles["actions"]}>
            <button className={styles["submitButton"]} onClick={handleClose}>
              Cancel
            </button>
 
            <button
              className={styles["submitButton"]}
              onClick={handleSubmit}
              style={{
                marginLeft: "12px",
              }}>
              Save
            </button>
          </div>
        </div>
      </Overlay>
      <Toast
        open={!!validationError}
        severity="error"
        message={validationError}
        handleClose={() => setValidationError("")}
        anchorOrigin={{ vertical: "bottom", horizontal: "left" }}
      />
    </div>
  );
};
 
export default SavePlaybookOverlay;