All files / app/features/profile/view Overview.tsx

88.09% Statements 37/42
61.53% Branches 8/13
71.42% Functions 5/7
87.8% Lines 36/41

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 1372x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x           2x 2x   2x   6x           2x 7x     7x             7x 7x                                         14x 14x 14x   14x   14x   14x     14x   14x               14x                                                                     2x 15x 15x   15x                          
import Alert from "components/Alert";
import Button from "components/Button";
import ProfileIncompleteDialog from "components/ProfileIncompleteDialog/ProfileIncompleteDialog";
import { useAuthContext } from "features/auth/AuthProvider";
import useAccountInfo from "features/auth/useAccountInfo";
import FlagButton from "features/FlagButton";
import FriendActions from "features/profile/actions/FriendActions";
import MessageUserButton from "features/profile/actions/MessageUserButton";
import UserOverview from "features/profile/view/UserOverview";
import { GLOBAL, PROFILE } from "i18n/namespaces";
import Link from "next/link";
import { useTranslation } from "next-i18next";
import { HostingStatus } from "proto/api_pb";
import { useState } from "react";
import {
  connectionsRoute,
  EditUserTab,
  routeToEditProfile,
  UserTab,
} from "routes";
import makeStyles from "utils/makeStyles";
 
import { useProfileUser } from "../hooks/useProfileUser";
 
const useStyles = makeStyles((theme) => ({
  flagButton: {
    alignSelf: "center",
  },
}));
 
const getEditTab = (tab: UserTab): EditUserTab | undefined => {
  switch (tab) {
    case "about":
    case "home":
      return tab;
    default:
      return undefined;
  }
};
 
function LoggedInUserActions({ tab }: { tab: UserTab }) {
  const { t } = useTranslation([GLOBAL, PROFILE]);
  return (
    <>
      <Link href={routeToEditProfile(getEditTab(tab))} passHref>
        <Button component="a" color="primary">
          {t("global:edit")}
        </Button>
      </Link>
      <Link href={connectionsRoute} passHref>
        <Button component="a" variant="outlined">
          {t("profile:my_connections")}
        </Button>
      </Link>
    </>
  );
}
 
function DefaultActions({
  setIsRequesting,
}: {
  setIsRequesting: (value: boolean) => void;
}) {
  const { t } = useTranslation([GLOBAL, PROFILE]);
  const classes = useStyles();
  const user = useProfileUser();
  const disableHosting =
    user.hostingStatus === HostingStatus.HOSTING_STATUS_CANT_HOST;
 
  const [mutationError, setMutationError] = useState("");
  const [showCantRequestDialog, setShowCantRequestDialog] =
    useState<boolean>(false);
 
  const { data: accountInfo, isLoading: isAccountInfoLoading } =
    useAccountInfo();
 
  const requestButton = () => {
    if (!accountInfo?.profileComplete) {
      setShowCantRequestDialog(true);
    } else {
      setIsRequesting(true);
    }
  };
 
  return (
    <>
      <ProfileIncompleteDialog
        open={showCantRequestDialog}
        onClose={() => setShowCantRequestDialog(false)}
        attempted_action="send_request"
      />
      <Button
        onClick={requestButton}
        disabled={isAccountInfoLoading || disableHosting}
      >
        {disableHosting
          ? t("global:hosting_status.cant_host")
          : t("profile:actions.request")}
      </Button>
 
      <MessageUserButton user={user} setMutationError={setMutationError} />
      <FriendActions user={user} setMutationError={setMutationError} />
 
      <FlagButton
        className={classes.flagButton}
        contentRef={`profile/${user.userId}`}
        authorUser={user.userId}
      />
 
      {mutationError && <Alert severity="error">{mutationError}</Alert>}
    </>
  );
}
 
export interface OverviewProps {
  setIsRequesting: (value: boolean) => void;
  tab: UserTab;
}
 
export default function Overview({ setIsRequesting, tab }: OverviewProps) {
  const currentUserId = useAuthContext().authState.userId;
  const user = useProfileUser();
 
  return (
    <UserOverview
      showHostAndMeetAvailability
      actions={
        user.userId === currentUserId ? (
          <LoggedInUserActions tab={tab} />
        ) : (
          <DefaultActions setIsRequesting={setIsRequesting} />
        )
      }
    />
  );
}