All files / src/components DeleteModal.tsx

94.44% Statements 17/18
100% Branches 2/2
100% Functions 4/4
94.11% Lines 16/17

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        4x 4x 4x 4x                   4x 51x   51x 1x 1x 1x 1x 1x       1x       51x               1x                                           1x                    
/*---------------------------------------------------------------------------------------------
 * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
 * See LICENSE.md in the project root for license terms and full copyright notice.
 *--------------------------------------------------------------------------------------------*/
import { Button, MiddleTextTruncation, Modal, ModalButtonBar, Text } from "@itwin/itwinui-react";
import React, { useState } from "react";
import "./DeleteModal.scss";
import { handleError, LoadingSpinner } from "./utils";
 
export interface DeleteModalProps {
  entityName: string;
  show: boolean;
  setShow: React.Dispatch<React.SetStateAction<boolean>>;
  onDelete: () => Promise<void>;
  refresh: () => Promise<void>;
}
 
export const DeleteModal = ({ entityName, show, setShow, onDelete, refresh }: DeleteModalProps) => {
  const [isLoading, setIsLoading] = useState<boolean>(false);
 
  const deleteCallback = async () => {
    try {
      setIsLoading(true);
      await onDelete();
      setShow(false);
      await refresh();
    } catch (error: any) {
      handleError(error.status);
    } finally {
      setIsLoading(false);
    }
  };
 
  return (
    <Modal
      title="Confirm"
      data-testid="ec3-delete-modal"
      modalRootId="ec3-widget-react"
      isOpen={show}
      isDismissible={!isLoading}
      onClose={() => {
        setShow(false);
      }}
    >
      <div className="ec3w-delete-modal-body-text">
        <Text variant="leading">Are you sure you want to delete</Text>
        <strong>
          <MiddleTextTruncation text={`${entityName}?`} />
        </strong>
      </div>
      <ModalButtonBar>
        {isLoading && (
          <div className="ec3w-loading-delete">
            <LoadingSpinner />
          </div>
        )}
        <Button styleType="high-visibility" onClick={deleteCallback} disabled={isLoading} data-testid="ec3-delete-modal-button">
          Delete
        </Button>
        <Button
          data-testid="ec3-delete-modal-cancel-button"
          styleType="default"
          onClick={() => {
            setShow(false);
          }}
          disabled={isLoading}
        >
          Cancel
        </Button>
      </ModalButtonBar>
    </Modal>
  );
};