All files / src/features/AcquirerPortal/AcquirerSettings/SecuritySettings GiveAllMerchantsSecurity.tsx

100% Statements 17/17
77.77% Branches 14/18
100% Functions 4/4
100% Lines 16/16

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                                                                    2x                 101x                                     2x 78x   78x                             2x 103x       103x         103x 103x 103x 103x         103x             103x   103x                                                             1x                                                                                
import { Stack } from "@mui/material";
import { FormProvider } from "react-hook-form";
import { StorefrontIcon } from "@phosphor-icons/react";
import GiveText from "@shared/Text/GiveText";
import { useAppTheme } from "@theme/v2/Provider";
import { SettingsFormWrapperV2 } from "@features/MerchantPortal/MerchantSettings/components/atomsV2";
import { GiveWithMissingPermissionModule } from "@common/GiveWithMissingPermissionModule";
import {
  composePermission,
  useAccessControl,
} from "features/Permissions/AccessControl";
import RESOURCE_BASE, { OPERATIONS } from "@constants/permissions";
import {
  WIDTH as SIDEBAR_WIDTH,
  WIDTH_CLOSED as SIDEBAR_WIDTH_CLOSED,
  WIDTH_MOBILE_CLOSED as SIDEBAR_WIDTH_RAIL,
} from "@constants/sideMenu";
import { useDrawerState } from "componentsV2/SideMenu/DrawerContext";
import SecurityToggleCard from "./components/SecurityToggleCard";
import { useGlobalSecuritySettings } from "./hooks/useGlobalSecuritySettings";
import {
  ALL_MERCHANTS_DESCRIPTION,
  ALL_MERCHANTS_TITLE,
  DISCARD_CHANGES_LABEL,
  LOAD_ERROR_MESSAGE,
  SAVE_LABEL,
  SECTION_NAME,
  SECURITY_FORM_NAME,
  REQUIRE_EMAIL_VERIFICATION_DESCRIPTION,
  REQUIRE_EMAIL_VERIFICATION_TITLE,
  REQUIRE_PURCHASE_VERIFICATION_DESCRIPTION,
  REQUIRE_PURCHASE_VERIFICATION_TITLE,
} from "./constants";
 
const ACTION_BAR_HEIGHT = "78px";
 
// The design puts 20px above the buttons, rests them on the content column's
// right edge (64px in from the page) rather than the shared bar's
// viewport-relative 20px, and insets the bar itself to that column. The inset
// is measured off the live sidebar width so a collapse keeps it true; with no
// DrawerProvider above (unit tests) the bar stays full-bleed. Mobile keeps the
// full-bleed 20px inset, where the sidebar is an overlay.
// Bands match SideMenuDrawer: overlay, then a 92px rail, then the drawer width.
const actionBarSx = (sidebarWidth: number | null, transition: string) => ({
  paddingTop: "20px",
  paddingBottom: { xs: "20px", v2_md: "16px" },
  paddingRight: { xs: "20px", v2_md: "64px" },
  ...(sidebarWidth !== null && {
    left: {
      xs: 0,
      v2_sm: `${SIDEBAR_WIDTH_RAIL}px`,
      v2_md: `${sidebarWidth}px`,
    },
    width: {
      xs: "100vw",
      v2_sm: `calc(100vw - ${SIDEBAR_WIDTH_RAIL}px)`,
      v2_md: `calc(100vw - ${sidebarWidth}px)`,
    },
    transition,
  }),
});
 
const SectionHeading = () => {
  const { palette } = useAppTheme();
 
  return (
    <Stack direction="row" gap="16px" alignItems="center" mb="28px">
      <StorefrontIcon size={24} color={palette.icon?.["icon-primary"]} />
      <Stack gap="4px" flex={1}>
        <GiveText variant="h6" color="primary">
          {ALL_MERCHANTS_TITLE}
        </GiveText>
        <GiveText variant="bodyS" color="secondary">
          {ALL_MERCHANTS_DESCRIPTION}
        </GiveText>
      </Stack>
    </Stack>
  );
};
 
export const GiveAllMerchantsSecurity = () => {
  const permission = composePermission(
    RESOURCE_BASE.ACQUIRER,
    RESOURCE_BASE.SECURITY_CONFIG,
  );
  const isReadAllowed = useAccessControl({
    resource: permission,
    operation: OPERATIONS.READ,
  });
  const { methods, handleSave, isLoading, isError, isSaving } =
    useGlobalSecuritySettings({ isReadAllowed });
  const { transitions } = useAppTheme();
  const drawer = useDrawerState();
  const sidebarWidth = drawer
    ? drawer.open
      ? SIDEBAR_WIDTH
      : SIDEBAR_WIDTH_CLOSED
    : null;
  const isEditAllowed = useAccessControl({
    resource: permission,
    operation: OPERATIONS.UPDATE,
  });
  const {
    reset,
    formState: { isDirty },
  } = methods;
 
  return (
    // notAuthorized takes precedence over the load state: the interceptor
    // rejects a 403 GET, so a user who simply lacks access would otherwise be
    // told the page failed to load.
    <GiveWithMissingPermissionModule
      sectionName={SECTION_NAME}
      isLoading={isReadAllowed && isLoading}
      notAuthorized={!isReadAllowed}
    >
      {isError ? (
        // A control defaulting to "off" would misreport the security posture,
        // so the toggles stay off-screen until their real values are known.
        <>
          <SectionHeading />
          <GiveText variant="bodyS" color="secondary" role="alert">
            {LOAD_ERROR_MESSAGE}
          </GiveText>
        </>
      ) : (
        <FormProvider {...methods}>
          <SettingsFormWrapperV2
            isDirty={isDirty}
            formProps={{ onSubmit: methods.handleSubmit(handleSave) }}
            formActions={{
              formName: SECURITY_FORM_NAME,
              primaryAction: {
                label: SAVE_LABEL,
                disabled: isSaving || !isEditAllowed,
              },
              secondaryAction: {
                label: DISCARD_CHANGES_LABEL,
                onClick: () => reset(),
                disabled: isSaving,
              },
              actionsSx: actionBarSx(
                sidebarWidth,
                // the drawer animates its own width with these, so the bar
                // tracks it instead of snapping after it
                transitions.create(["left", "width"], {
                  easing: transitions.easing.sharp,
                  duration: transitions.duration.standard,
                }),
              ),
            }}
            // The bar only renders while dirty; reserving it otherwise is dead space.
            sx={{ paddingBottom: isDirty ? ACTION_BAR_HEIGHT : 0 }}
          >
            <SectionHeading />
 
            <Stack gap="12px">
              <SecurityToggleCard
                name="requirePurchaseVerification"
                testId="require-purchase-verification-switch"
                disabled={!isEditAllowed || isSaving}
                title={REQUIRE_PURCHASE_VERIFICATION_TITLE}
                description={REQUIRE_PURCHASE_VERIFICATION_DESCRIPTION}
              />
              <SecurityToggleCard
                name="requireEmailVerification"
                testId="require-email-verification-switch"
                disabled={!isEditAllowed || isSaving}
                title={REQUIRE_EMAIL_VERIFICATION_TITLE}
                description={REQUIRE_EMAIL_VERIFICATION_DESCRIPTION}
              />
            </Stack>
          </SettingsFormWrapperV2>
        </FormProvider>
      )}
    </GiveWithMissingPermissionModule>
  );
};