All files / app/features/profile/view/leaveReference LeaveReferencePage.tsx

100% Statements 51/51
85.71% Branches 36/42
100% Functions 5/5
100% Lines 49/49

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 1881x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   4x                                   4x                   22x                     31x 31x   31x     10x               31x         31x   31x   31x               28x             28x 20x         8x 8x 8x   8x     8x   4x       8x   8x               7x               6x   6x 6x   6x               5x               5x               5x                                                
import { styled, useMediaQuery } from "@mui/material";
import { useQuery } from "@tanstack/react-query";
import Alert from "components/Alert";
import CenteredSpinner from "components/CenteredSpinner/CenteredSpinner";
import { useListAvailableReferences } from "features/profile/hooks/referencesHooks";
import { ProfileUserProvider } from "features/profile/hooks/useProfileUser";
import ReferenceForm from "features/profile/view/leaveReference/ReferenceForm";
import UserOverview from "features/profile/view/UserOverview";
import { hasGivenHostRequestReferenceKey } from "features/queryKeys";
import { useUser } from "features/userQueries/useUsers";
import { useTranslation } from "i18n";
import { GLOBAL, PROFILE } from "i18n/namespaces";
import { User } from "proto/api_pb";
import { ReferenceType } from "proto/references_pb";
import React from "react";
import { ReferenceStep, referenceTypeRoute } from "routes";
import { service } from "service";
import { ReferenceTypeStrings } from "service/references";
import { theme } from "theme";
 
const StyledRoot = styled("div")(({ theme }) => ({
  padding: theme.spacing(1),
  [theme.breakpoints.up("sm")]: {
    display: "grid",
    gridTemplateColumns: "2fr 3fr",
    gap: theme.spacing(3),
    margin: theme.spacing(0, 3),
    padding: 0,
    paddingTop: theme.spacing(3),
    paddingBottom: theme.spacing(3),
  },
  [theme.breakpoints.up("md")]: {
    gridTemplateColumns: "2fr 4fr",
    maxWidth: "61.5rem",
    margin: "0 auto",
  },
}));
 
const StyledFormWrapper = styled("div")(({ theme }) => ({
  [theme.breakpoints.down("md")]: {
    margin: 0,
    width: "100%",
  },
  flexGrow: 1,
  margin: theme.spacing(2),
  padding: theme.spacing(2),
}));
 
export default function LeaveReferencePage({
  referenceType,
  userId,
  hostRequestId,
  step = "did-stay",
}: {
  referenceType: string;
  userId: number;
  hostRequestId?: number;
  step?: ReferenceStep;
}) {
  const { t } = useTranslation([GLOBAL, PROFILE]);
  const isMobile = useMediaQuery(theme.breakpoints.down("md"));
 
  const { data: statusRes } = useQuery({
    queryKey: [hasGivenHostRequestReferenceKey, hostRequestId],
    queryFn: () =>
      service.references.getHostRequestReferenceStatus(hostRequestId!),
    enabled: !!hostRequestId,
  });
 
  const {
    data: user,
    isLoading: isUserLoading,
    error: userError,
  } = useUser(userId);
  const {
    data: availableReferences,
    isLoading: isAvailableReferencesLoading,
    error: availableReferencesError,
  } = useListAvailableReferences(userId);
 
  const referenceTypeValid = referenceType in ReferenceTypeStrings;
 
  if (!referenceTypeValid) {
    return (
      <Alert severity="error">
        {t("profile:leave_reference.invalid_reference_type")}
      </Alert>
    );
  }
 
  Iif (userError || availableReferencesError) {
    return (
      <Alert severity="error">
        {userError || availableReferencesError?.message || ""}
      </Alert>
    );
  }
  if (isUserLoading || isAvailableReferencesLoading) {
    return <CenteredSpinner />;
  }
 
  // Compute availability booleans
  const isFriendType =
    referenceType === referenceTypeRoute[ReferenceType.REFERENCE_TYPE_FRIEND];
  const canWriteFriendRef = !!availableReferences?.canWriteFriendReference;
  const isFriendsWithUser = user?.friends === User.FriendshipStatus.FRIENDS;
  const canWriteFriendReferenceForUser =
    isFriendType && canWriteFriendRef && isFriendsWithUser;
 
  const canWriteHostRequestReference =
    !!hostRequestId &&
    !!availableReferences?.availableWriteReferencesList?.some(
      ({ hostRequestId: availableId }) => availableId === hostRequestId,
    );
 
  const canWriteReference =
    canWriteFriendReferenceForUser || canWriteHostRequestReference;
 
  if (isFriendType && !isFriendsWithUser) {
    return (
      <Alert severity="error">
        {t("profile:leave_reference.friend_reference_requires_friendship")}
      </Alert>
    );
  }
 
  if (isFriendType && isFriendsWithUser && !canWriteFriendRef) {
    return (
      <Alert severity="info">
        {t("profile:leave_reference.already_wrote_friend_reference")}
      </Alert>
    );
  }
 
  const alreadyWroteThisStay = !!hostRequestId && !!statusRes?.hasGiven;
 
  const isExpired = !!hostRequestId && !!statusRes && statusRes.isExpired;
  const didntStay = !!hostRequestId && !!statusRes && statusRes.didntStay;
 
  if (alreadyWroteThisStay) {
    return (
      <Alert severity="info">
        {t("profile:leave_reference.already_wrote_reference_for_stay")}
      </Alert>
    );
  }
 
  Iif (!!hostRequestId && didntStay) {
    return (
      <Alert severity="error">
        {t("profile:leave_reference.cant_write_reference_didnt_stay")}
      </Alert>
    );
  }
 
  Iif (!!hostRequestId && isExpired) {
    return (
      <Alert severity="error">
        {t("profile:leave_reference.cant_write_reference_expired")}
      </Alert>
    );
  }
 
  if (!canWriteReference) {
    return (
      <Alert severity="error">
        {t("profile:leave_reference.reference_type_not_available")}
      </Alert>
    );
  }
 
  return (
    <StyledRoot>
      <ProfileUserProvider user={user!}>
        {!isMobile && <UserOverview showHostAndMeetAvailability={false} />}
        <StyledFormWrapper>
          <ReferenceForm
            hostRequestId={hostRequestId}
            referenceType={referenceType}
            userId={userId}
            step={step}
          />
        </StyledFormWrapper>
      </ProfileUserProvider>
    </StyledRoot>
  );
}