All files / src/components/ProfileMenu/modals ProfileMobileDrawer.tsx

82.35% Statements 28/34
96.42% Branches 27/28
71.42% Functions 10/14
80% Lines 24/30

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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256                                                            7x             518x 518x 518x   518x 163x     518x 3x 3x     518x 518x       518x 3x             518x 3x 3x 3x           518x 106x     518x                                                                                                         472x     49x                                                                                                                                                                                                 248x                       62x                            
import { ExpandRightIcon } from "@assets/builderIcons";
import { NewLogoutIcon } from "@assets/icons";
import { ChevronRight } from "@assets/icons/RebrandedIcons";
import { Button } from "@common/Button";
import { StyledIconButton } from "@common/IconButton";
import { Text, TruncateText } from "@common/Text";
import ProfilePicture from "@components/CustomBreadcrumbsPage/ components/ProfilePicture";
import SwipeableDrawerMobile from "@components/SwipeableDrawerMobile/SwipeableDrawerMobile";
import NiceModal, { useModal } from "@ebay/nice-modal-react";
import useUserReducer from "@hooks/Reducers/useUserReducer";
import { Box, Stack, styled } from "@mui/material";
import { palette } from "@palette";
import { LOGOUT_MODAL } from "modals/modal_names";
import { useEffect, useState } from "react";
import { FormProvider } from "react-hook-form";
import { useProfileForms } from "../hooks/useProfileForms";
import TFAModalWrapper from "./ProfileModal2FAWrapper";
import { TProfileModalPage, TProfileTab } from "./types";
 
type Props = {
  sidebarList: {
    tab: TProfileTab;
    active: boolean;
  }[];
  ProfilePage: TProfileModalPage;
  setDisabled: React.Dispatch<React.SetStateAction<boolean>>;
  isDisabled: boolean;
  initialTab?: TProfileTab;
};
 
const ProfileMobileDrawer = ({
  sidebarList,
  ProfilePage,
  setDisabled,
  isDisabled,
  initialTab,
}: Props) => {
  const modal = useModal();
  const { name, email } = useUserReducer();
  const [tab, setTab] = useState<TProfileTab | null>(null);
 
  useEffect(() => {
    if (initialTab && modal.visible) setTab(initialTab);
  }, [modal.visible]);
 
  const handleLogout = () => {
    modal.hide();
    NiceModal.show(LOGOUT_MODAL);
  };
 
  const ActivePage = tab ? ProfilePage[tab]?.component : null;
  const { onSubmit, methods, isLoading, isDirty } = useProfileForms(
    tab as string,
  );
 
  const trigger2FA = () => {
    NiceModal.show(TFAModalWrapper, {
      email: email,
      isMobileView: true,
      onSuccessFn: methods?.handleSubmit?.(onSubmit),
    });
  };
 
  const handleSubmit = (e: any) => {
    e.preventDefault();
    if (tab === "Security") {
      return trigger2FA();
    } else E{
      methods?.handleSubmit?.(onSubmit, console.log).call();
    }
  };
 
  useEffect(() => {
    if (methods?.reset) methods?.reset();
  }, [tab]);
 
  return (
    <SwipeableDrawerMobile
      anchor="bottom"
      onOpen={() => undefined}
      open={modal.visible}
      onClose={() => {
        modal.hide();
        setTab(null);
      }}
      PaperProps={{
        sx: {
          background: palette.neutral[5],
          top: "96px",
          paddingBottom: "60px",
          position: "relative",
          height: "90%",
        },
      }}
    >
      {!tab && (
        <>
          <Stack direction="row" p="8px 0px" spacing={1} mb={2}>
            <ProfilePicture pointerEventsNone />
            <Stack spacing={0.5}>
              <TruncateText
                lineClamp={1}
                color={palette.neutral[80]}
                fontWeight="book"
              >
                {name}
              </TruncateText>
              <TruncateText
                lineClamp={1}
                fontSize={12}
                color={palette.neutral[70]}
                fontWeight="book"
                sx={{
                  wordBreak: "break-all",
                }}
              >
                {email}
              </TruncateText>
            </Stack>
          </Stack>
          {/* <Stack direction="row" spacing={1} alignItems="center" my={3}>
        <DarkModeIcon width={32} height={32} />
        <Text fontWeight="book" color={palette.neutral[90]} flexGrow={1}>
          Dark Mode
        </Text>
        <Switch size="medium" containerProps={{ gap: "0" }} />
      </Stack> */}
          <Stack spacing={1.5}>
            {sidebarList.map((item) => (
              <Tab
                key={item.tab}
                data-testid={item.tab}
                onClick={() => setTab(item.tab)}
              >
                <Text color={palette.neutral[80]} fontWeight="book">
                  {item.tab}
                </Text>
                <ExpandRightIcon height={24} width={24} />
              </Tab>
            ))}
          </Stack>
 
          <Box
            flexGrow={1}
            display="flex"
            alignItems="flex-end"
            justifyContent="center"
          >
            <LogoutButton
              size="medium"
              onClick={handleLogout}
              startIcon={<NewLogoutIcon fill={palette.neutral[70]} />}
            >
              Log out
            </LogoutButton>
          </Box>
        </>
      )}
 
      {tab && ActivePage && (
        <FormProvider {...methods}>
          <Box
            display="flex"
            flexDirection="column"
            justifyContent="space-between"
            height="100%"
            component={tab === "Notifications" ? "div" : "form"}
            onSubmit={tab === "Notifications" ? null : (handleSubmit as any)}
          >
            <Stack direction="row" gap={1} alignItems="center" mb={2}>
              <StyledIconButton
                onClick={() => setTab(null)}
                sx={{
                  maxWidth: "32px",
                  maxHeight: "32px",
                  borderRadius: "4px",
                  padding: "0 !important",
                }}
              >
                <ChevronRight
                  rotate={180}
                  width={28}
                  height={28}
                  stroke={palette.gray[300]}
                />
              </StyledIconButton>
              <Text
                color={palette.black[100]}
                variant="h6"
                lineHeight="21.6px"
                fontWeight="book"
                letterSpacing="-0.18px"
              >
                {tab}
              </Text>
            </Stack>
            <ActivePage setDisabled={setDisabled} />
 
            <Box flexGrow={1} pb={4} />
            {(isDirty || (!isDisabled && tab === "Notifications")) && (
              <Stack mt={2} direction="row" pb={8}>
                <Button
                  fullWidth
                  size="medium"
                  background="tertiary"
                  onClick={() => setTab(null)}
                >
                  Cancel
                </Button>
                <Button
                  size="medium"
                  background="primary"
                  type="submit"
                  data-testid="popup-submit-button"
                  fullWidth
                  form={tab === "Notifications" ? "profile-form" : undefined}
                  disabled={isLoading || (tab === "Security" && isDisabled)}
                >
                  Update
                </Button>
              </Stack>
            )}
          </Box>
        </FormProvider>
      )}
    </SwipeableDrawerMobile>
  );
};
 
const Tab = styled(Box)(() => ({
  padding: "12px 8px",
  display: "flex",
  alignItems: "center",
  justifyContent: "space-between",
  borderRadius: "8px",
  background: palette.background.dimmedWhite,
  "&:active": {
    background: palette.neutral[10],
  },
}));
 
const LogoutButton = styled(Button)(() => ({
  borderRadius: "32px",
  border: `1px solid ${palette.neutral[70]}`,
  background: palette.neutral[10],
  color: palette.neutral[80],
  fontSize: 18,
  fontWeight: "400",
  boxShadow: "none",
  "&:hover": {
    background: palette.neutral[10],
  },
}));
 
export default ProfileMobileDrawer;