All files / src/components/common/Modal CampaignModalActions.tsx

87.5% Statements 7/8
61.11% Branches 22/36
100% Functions 1/1
87.5% Lines 7/8

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                                                                          42x 42x 42x 42x     42x                                                                                                                                           42x   42x        
import React from "react";
import { Portal, Stack, useMediaQuery, useTheme } from "@mui/material";
import { Button } from "@common/Button";
import LoadingButton from "@common/Button/LoadingButton";
 
type Props = {
  id?: string | number;
  form: string;
  actionsPortal?: HTMLElement | null;
  direction?: "row" | "column";
  isSubmitDisabled?: boolean;
  isNotFullWidth?: boolean;
  isLoading?: boolean;
  isPrimaryActionDisabled?: boolean;
  isSecondaryActionDisabled?: boolean;
  isShowDiscard?: boolean;
  primaryBtnLabel?: React.ReactNode;
  discardBtnLabel?: React.ReactNode;
  handleDelete?: () => void;
  handleDiscard?: () => void;
};
function CampaignModalActions({
  id,
  form,
  actionsPortal = null,
  isSubmitDisabled,
  isNotFullWidth,
  isLoading,
  isPrimaryActionDisabled,
  isSecondaryActionDisabled,
  isShowDiscard,
  direction,
  primaryBtnLabel: initialPrimaryBtnLabel,
  discardBtnLabel,
  handleDelete,
  handleDiscard,
}: Props) {
  const theme = useTheme();
  const isDesktop = useMediaQuery(theme.breakpoints.up("sm"));
  const hasOtherButtons = Boolean(id) || isShowDiscard;
  const primaryBtnLabel = id ? "Save" : initialPrimaryBtnLabel || "Next step";
 
  const content = (
    <Stack
      gap={2}
      sx={{
        width: "100%",
        flexDirection: direction || "row",
        justifyContent: id ? "space-between" : "flex-end",
 
        "@media (max-width: 600px)": {
          flexDirection: direction || "column",
          alignItems: "stretch",
          width: "100%",
          gap: "4px",
        },
      }}
    >
      {hasOtherButtons && (
        <Stack
          flexGrow={1}
          direction={direction || isDesktop ? "row" : "column"}
          justifyContent="space-between"
        >
          {handleDelete ? (
            <Button
              onClick={handleDelete}
              background="tertiary"
              fullWidth={!isDesktop}
              size="medium"
              sx={{
                width: `${isNotFullWidth ? "max-content" : "auto"} !important`,
              }}
            >
              Delete
            </Button>
          ) : (
            <div />
          )}
          {handleDiscard && (
            <Button
              background="secondary"
              size="medium"
              disabled={
                (isSecondaryActionDisabled ?? isPrimaryActionDisabled) ||
                isLoading
              }
              onClick={handleDiscard}
              sx={{
                width: `${isNotFullWidth ? "max-content" : "auto"} !important`,
              }}
              fullWidth={!isDesktop}
            >
              {discardBtnLabel ? discardBtnLabel : "Discard changes"}
            </Button>
          )}
        </Stack>
      )}
 
      <LoadingButton
        background="primary"
        type="submit"
        size="medium"
        form={form}
        disabled={isPrimaryActionDisabled || isLoading}
        fullWidth={!isDesktop}
        isLoading={isLoading}
      >
        {primaryBtnLabel}
      </LoadingButton>
    </Stack>
  );
 
  Iif (actionsPortal)
    return <Portal container={actionsPortal}>{content}</Portal>;
  return content;
}
 
export default CampaignModalActions;