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

97.77% Statements 44/45
89.28% Branches 25/28
100% Functions 15/15
97.36% Lines 37/38

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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280                                                                7x                     85x           7x 482x     7x 993x 993x 993x 993x 993x   993x 318x               993x   993x 60x 240x 60x       180x     60x     993x 400x     993x   993x 354x                     639x 3x             639x 3x 3x 3x           639x                                                                                                                                                                                                     7x             586x                               503x             503x                     503x                   127x                    
import { CloseIcon } from "@assets/rebrandIcons";
import { Button } from "@common/Button";
import { StyledIconButton } from "@common/IconButton";
import { BaseModal } from "@common/Modal";
import NiceModal from "@ebay/nice-modal-react";
import {
  Box,
  DialogActions,
  DialogContent,
  DialogTitle,
  Grid,
  styled,
} from "@mui/material";
import { palette } from "@palette";
import { useCustomTheme } from "@theme/hooks/useCustomTheme";
import { useEffect, useMemo, useState } from "react";
import { FormProvider } from "react-hook-form";
import { useProfileForms } from "../hooks/useProfileForms";
import { ProfileAccountDetails } from "../Tabs/ProfileAccountDetails";
import ProfileMobileDrawer from "./ProfileMobileDrawer";
import ProfileModalSidebar from "./ProfileModalSidebar";
 
import LoadingButton from "@common/Button/LoadingButton";
import useNiceModal from "@common/Modal/ModalFactory/hooks/useNiceModal";
import { useAppSelector } from "@redux/hooks";
import { selectUser } from "@redux/slices/auth/auth";
import TFAModalWrapper from "./ProfileModal2FAWrapper";
import AccessibilityTab from "./Tabs/AccessibilityTab";
import NotificationTab from "./Tabs/NotificationTab";
import SecurityTab from "./Tabs/SecurityTab";
import { ProfileModalPageProps, TProfileTab } from "./types";
 
const ProfilePage = {
  "Account Details": {
    component: ProfileAccountDetails,
  },
  Security: {
    component: SecurityTab,
  },
  Accessibility: {
    component: AccessibilityTab,
  },
  Notifications: {
    component: (props: ProfileModalPageProps) => <NotificationTab {...props} />,
  },
};
 
type ModalProps = { initialTab?: TProfileTab };
 
const ProfileModal = NiceModal.create((props: ModalProps) => {
  return <ProfileModalBody {...props} />;
});
 
export const ProfileModalBody = ({ initialTab }: ModalProps) => {
  const { open, onClose, SlideProps } = useNiceModal();
  const { isMobileView } = useCustomTheme();
  const [sidebarList, setSidebarList] = useState(initialSidebarList);
  const [isDisabled, setDisabled] = useState(true);
  const { email } = useAppSelector(selectUser);
 
  const activeTab = useMemo(
    () => sidebarList.find((item) => item.active)?.tab || "Account Details",
    [sidebarList],
  );
  const {
    onSubmit,
    methods = {},
    isLoading,
    isDirty,
  } = useProfileForms(activeTab);
 
  const handleSelectTab = (tab: TProfileTab) => {
    const updatedTabs = sidebarList.map((item) => {
      if (item.tab === tab)
        return {
          ...item,
          active: true,
        };
      return { ...item, active: false };
    });
 
    setSidebarList(updatedTabs);
  };
 
  useEffect(() => {
    if (initialTab && open) handleSelectTab(initialTab);
  }, [open]);
 
  const ActivePage = ProfilePage[activeTab];
 
  if (isMobileView) {
    return (
      <ProfileMobileDrawer
        sidebarList={sidebarList}
        ProfilePage={ProfilePage}
        setDisabled={setDisabled}
        isDisabled={isDisabled}
        initialTab={initialTab}
      />
    );
  }
 
  const trigger2FA = () => {
    NiceModal.show(TFAModalWrapper, {
      email: email,
      isMobileView: isMobileView,
      onSuccessFn: methods?.handleSubmit?.(onSubmit),
    });
  };
 
  const handleSubmit = (e: any) => {
    e.preventDefault();
    if (activeTab === "Security") {
      return trigger2FA();
    } else E{
      methods?.handleSubmit?.(onSubmit, console.log).call();
    }
  };
 
  return (
    <StyledModal
      open={open}
      TransitionProps={{
        onExited: SlideProps.onExit,
      }}
      onClose={onClose}
      testId="profile-modal"
      PaperProps={{
        style: {
          background: "rgba(251, 251, 251, 0.8)",
          boxShadow: "0px 4px 50px 0px rgba(0, 0, 0, 0.25)",
          borderRadius: "12px",
          width: "800px",
          maxWidth: "800px",
        },
        sx: {
          "&::-webkit-scrollbar": {
            display: "none",
          },
          "&::-webkit-scrollbar-track": {
            display: "none",
          },
          "& .MuiDialogTitle-root + .MuiDialogContent-root": {
            paddingTop: "0 !important",
          },
        },
      }}
    >
      <FormProvider {...methods}>
        <Box
          component={activeTab === "Notifications" ? "div" : "form"}
          onSubmit={
            activeTab === "Notifications" ? null : (handleSubmit as any)
          }
        >
          <Grid container>
            <Grid item xs={3.5}>
              <ProfileModalSidebar
                sidebarList={sidebarList}
                handleSelectTab={handleSelectTab}
              />
            </Grid>
            <Grid item xs={8.5}>
              <Box
                display="flex"
                flexDirection="column"
                height="100%"
                sx={{ background: palette.neutral[5] }}
              >
                <ModalTitleContainer>
                  <Box component="span" flexGrow={1}>
                    {activeTab}
                  </Box>
                  <CloseButton onClick={onClose}>
                    <CloseIcon width={24} height={24} />
                  </CloseButton>
                </ModalTitleContainer>
                <ContentContainer>
                  <ActivePage.component setDisabled={setDisabled} />
                </ContentContainer>
                {(isDirty ||
                  (!isDisabled && activeTab === "Notifications")) && (
                  <ModalActionsContainer>
                    <Button
                      size="medium"
                      background="tertiary"
                      onClick={onClose}
                      disabled={isLoading}
                    >
                      Cancel
                    </Button>
                    <LoadingButton
                      isLoading={isLoading}
                      disabled={
                        isLoading || (activeTab === "Security" && isDisabled)
                      }
                      size="medium"
                      background="primary"
                      form={
                        activeTab === "Notifications"
                          ? "profile-form"
                          : undefined
                      }
                      type="submit"
                    >
                      {isMobileView ? "Save" : "Update"}
                    </LoadingButton>
                  </ModalActionsContainer>
                )}
              </Box>
            </Grid>
          </Grid>
        </Box>
      </FormProvider>
    </StyledModal>
  );
};
 
const initialSidebarList = [
  { tab: "Account Details", active: true },
  { tab: "Security", active: false },
  { tab: "Accessibility", active: false },
  { tab: "Notifications", active: false },
] as { tab: TProfileTab; active: boolean }[];
 
const StyledModal = styled(BaseModal)(() => ({
  ".MuiDialogActions-root": {
    backgroundColor: "#FBFBFB",
  },
 
  "@media (max-width: 600px)": {
    "& .MuiDialog-paper": {
      top: "50px !important",
      height: "90%",
      borderRadius: "8px",
      maxHeight: "90%",
      padding: "0 5px",
    },
  },
}));
 
const CloseButton = styled(StyledIconButton)(() => ({
  maxWidth: "24px",
  maxHeight: "24px",
  borderRadius: "4px",
  padding: "0 !important",
}));
 
const ModalTitleContainer = styled(DialogTitle)(() => ({
  padding: "16px",
  display: "flex",
  alignItems: "center",
  justifyContent: "space-between",
  color: palette.black[100],
  fontSize: "20px",
  lineHeight: "27px",
  fontWeight: 350,
}));
 
export const ContentContainer = styled(DialogContent)(() => ({
  padding: "0 16px 8px !important",
  flexGrow: 1,
  "& > div": {
    height: "65vh",
    maxHeight: "65vh",
    overflowY: "auto",
  },
}));
 
const ModalActionsContainer = styled(DialogActions)(() => ({
  display: "flex",
  alignItems: "center",
  justifyContent: "flex-end",
  gap: "24px",
  background: "transparent",
  padding: "12px 16px",
}));
 
export default ProfileModal;