All files / app/features/profile/view/leaveReference/formSteps ReferenceStepHeader.tsx

70% Statements 21/30
23.07% Branches 3/13
66.66% Functions 2/3
70% Lines 21/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 951x 1x 1x 1x 1x 1x 1x 1x 1x 1x                 1x         4x         4x 4x 4x     4x         4x               4x 4x       4x 4x                                                                                    
import { styled, Typography } from "@mui/material";
import HeaderButton from "components/HeaderButton";
import { BackIcon } from "components/Icons";
import { useProfileUser } from "features/profile/hooks/useProfileUser";
import { useTranslation } from "i18n";
import { GLOBAL, PROFILE } from "i18n/namespaces";
import { useRouter } from "next/router";
import { ReferenceType } from "proto/references_pb";
import { ReferenceStep, referenceTypeRoute, routeToUser } from "routes";
import { theme } from "theme";
 
interface ReferenceStepHeaderProps {
  name?: string;
  referenceType?: string;
  isSubmitStep?: boolean;
  step?: ReferenceStep;
}
 
const StyledHeader = styled("div")({
  display: "flex",
  alignItems: "center",
});
 
export default function ReferenceStepHeader({
  name,
  referenceType,
  step,
}: ReferenceStepHeaderProps) {
  const { t } = useTranslation([GLOBAL, PROFILE]);
  const router = useRouter();
  const user = useProfileUser();
 
  const isFirstStep =
    step === "did-stay" ||
    (step === "private-feedback" &&
      referenceType ===
        referenceTypeRoute[ReferenceType.REFERENCE_TYPE_FRIEND]);
 
  const handleBackClick = () => {
    if (isFirstStep && user.username) {
      router.push(routeToUser(user.username));
    } else {
      router.back();
    }
  };
 
  const returnHeaderText = () => {
    Iif (step === "submit") {
      return t("profile:leave_reference.reference_submit_heading");
    }
 
    if (step === "did-stay") {
      return referenceType ===
        referenceTypeRoute[ReferenceType.REFERENCE_TYPE_SURFED]
        ? t("profile:leave_reference.reference_form_heading_did_stay_surfed", {
            name,
          })
        : t("profile:leave_reference.reference_form_heading_did_stay_hosted", {
            name,
          });
    }
    Iif (
      referenceType === referenceTypeRoute[ReferenceType.REFERENCE_TYPE_FRIEND]
    ) {
      return t("profile:leave_reference.reference_form_heading_friend", {
        name,
      });
    }
    Iif (
      referenceType === referenceTypeRoute[ReferenceType.REFERENCE_TYPE_SURFED]
    ) {
      return t("profile:leave_reference.reference_form_heading_surfed", {
        name,
      });
    }
    return t("profile:leave_reference.reference_form_heading_hosted", {
      name,
    });
  };
 
  return (
    <StyledHeader>
      <HeaderButton
        onClick={handleBackClick}
        aria-label={t("profile:leave_reference.previous_step")}
      >
        <BackIcon />
      </HeaderButton>
      <Typography variant="h2" sx={{ marginInlineStart: theme.spacing(2) }}>
        {returnHeaderText()}
      </Typography>
    </StyledHeader>
  );
}