All files / src/components/EnterpriseSettings/Branding GiveBranding.tsx

94.44% Statements 17/18
81.25% Branches 13/16
100% Functions 4/4
94.44% Lines 17/18

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 184 185 186 187 188 189                                                        1x                       10x 10x 10x   10x       10x   40x   40x                     10x                                                           10x                               40x                                                         1x 10x 10x       10x     10x       10x                                                                          
import { Grid, Box, Stack } from "@mui/material";
import GiveBrandingLogoUploader from "./components/GiveBrandingLogoUploader";
import GiveThemeColorCard from "./components/GiveThemeColorCard";
import { useThemeContext } from "./Provider/CustomThemeProvider";
import { memo } from "react";
import { GiveBrandingHeading } from "./components/GiveBrandingComponents";
import { GiveFormActions } from "./components/GiveFormActions";
import ErrorCatcher from "@common/Error/ErrorCatcher";
import { HiddenComponent } from "@containers/HiddenComponent";
import GiveEmbedCodeSection from "./components/GiveEmbedCodeSection";
import { brandingHeadings } from "./constants";
import { useAccessControl } from "features/Permissions/AccessControl";
import RESOURCE_BASE, {
  EDIT_DENY_MESSAGE,
  OPERATIONS,
} from "@constants/permissions";
import { useCheckDocumentProcessing } from "@redux/slices/uploadProgressSlice";
import { UploadDocumentTypes } from "@redux/slices/uploadProgressSlice/types";
import useMasqueradeReducer from "@hooks/Reducers/useMasqueradeReducer";
import GiveText from "@shared/Text/GiveText";
import GiveDivider from "@shared/GiveDivider/GiveDivider";
import GiveMotionWrapper from "@shared/Motion/GiveMotionWrapper";
import GiveManageMoneyPreview from "./components/GiveManageMoneyPreview";
import { MASQUERADE_SNACKBAR_HEIGHT } from "@components/Merchants/Masquerade/constants";
import GiveBrandingSkeleton from "./GiveBrandingSkeleton";
import { useCustomThemeV2 } from "@theme/hooks/useCustomThemeV2";
import GiveTooltip from "@shared/Tooltip/GiveTooltip";
 
const ACTION_BAR_HEIGHT = "68px";
 
function GiveBranding() {
  const {
    activeGradientColor,
    onSelectGradientColor,
    colorChoices,
    isEqualDefault,
    resetValues,
    inViewRef,
    handleSaveTheme,
    isLoading,
  } = useThemeContext();
  const { isMobileView } = useCustomThemeV2();
  const { isMasqueradeMode } = useMasqueradeReducer();
 
  Iif (isLoading) {
    return <GiveBrandingSkeleton />;
  }
 
  const renderChoices = (c: (typeof colorChoices)[number], index: number) => {
    const isSelected =
      c.linearColor?.light === activeGradientColor?.light &&
      c.linearColor?.dark === activeGradientColor?.dark;
    return (
      <Grid item xs={12} sm={6} md={6} lg={3} key={c.title + index}>
        <GiveThemeColorCard
          {...c}
          onSelectGradientColor={onSelectGradientColor}
          isSelected={isSelected}
        />
      </Grid>
    );
  };
 
  const sections = [
    {
      node: <LogoSection />,
      withDivider: true,
    },
    {
      node: (
        <Stack direction="column" gap="16px" alignItems="stretch">
          <GiveBrandingHeading {...brandingHeadings["cards"]} />
          <GiveText variant="bodyM" color="secondary" mt={2}>
            Accent
          </GiveText>
          <Grid container rowGap={1} columnSpacing={1}>
            {colorChoices?.map(renderChoices)}
          </Grid>
        </Stack>
      ),
      withDivider: isMobileView,
    },
    {
      node: <GiveManageMoneyPreview />,
      hidden: isMobileView,
      withDivider: true,
    },
    {
      node: <GiveEmbedCodeSection />,
      withDivider: false,
    },
  ];
 
  return (
    <ErrorCatcher errorID="give-theme-and-branding">
      <Stack
        ref={inViewRef}
        direction="column"
        gap="50px"
        paddingBottom={
          isMasqueradeMode
            ? `calc(${ACTION_BAR_HEIGHT} + ${MASQUERADE_SNACKBAR_HEIGHT}px)`
            : ACTION_BAR_HEIGHT
        }
        alignItems="stretch"
        overflow="hidden"
      >
        <Stack direction="column" gap="32px" alignItems="stretch" flexGrow={1}>
          {sections.map(({ node, hidden, withDivider }, index) => (
            <HiddenComponent key={index} hidden={hidden || false}>
              <>
                <GiveMotionWrapper delay={50 + 70 * index}>
                  <>
                    {node}
                    {withDivider && <GiveDivider bgType="transparent" my={0} />}
                  </>
                </GiveMotionWrapper>
              </>
            </HiddenComponent>
          ))}
        </Stack>
        <GiveFormActions
          primaryAction={{
            onClick: handleSaveTheme,
            disabled: isEqualDefault,
          }}
          secondaryAction={{
            onClick: resetValues,
            disabled: isEqualDefault,
          }}
        />
      </Stack>
    </ErrorCatcher>
  );
}
 
export default memo(GiveBranding);
 
const LogoSection = () => {
  const { isMobileView } = useCustomThemeV2();
  const isUpdateLogoAllowed = useAccessControl({
    resource: RESOURCE_BASE.ENTERPRISE,
    operation: OPERATIONS.UPDATE,
  });
  const isConvertingPortraitDocument = useCheckDocumentProcessing(
    UploadDocumentTypes.themeBranding,
  );
  const isConvertingLandscapeDocument = useCheckDocumentProcessing(
    UploadDocumentTypes.themeBrandingLandscape,
  );
 
  return (
    <Stack
      direction={isMobileView ? "column" : "row"}
      alignItems={"flex-start"}
      gap="24px"
      flexGrow={1}
      mb={4}
    >
      <GiveBrandingHeading {...brandingHeadings["logo"]} />
      <Box display="flex" gap="8px" alignItems="center">
        <GiveTooltip
          title={EDIT_DENY_MESSAGE}
          disableHoverListener={isUpdateLogoAllowed}
        >
          <GiveBrandingLogoUploader
            type="landscape"
            textLabel="267&times;64"
            disabled={!isUpdateLogoAllowed || isConvertingLandscapeDocument}
            documentType={UploadDocumentTypes.themeBrandingLandscape}
          />
        </GiveTooltip>
 
        <GiveTooltip
          title={EDIT_DENY_MESSAGE}
          disableHoverListener={isUpdateLogoAllowed}
        >
          <GiveBrandingLogoUploader
            type="square"
            textLabel="64&times;64"
            disabled={!isUpdateLogoAllowed || isConvertingPortraitDocument}
            documentType={UploadDocumentTypes.themeBranding}
          />
        </GiveTooltip>
      </Box>
    </Stack>
  );
};