All files / app/features/messages/requests HostRequestStatusBanner.tsx

100% Statements 32/32
100% Branches 23/23
100% Functions 3/3
100% Lines 27/27

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 1064x 4x 4x 4x 4x 4x   15x                     21x                                     21x       21x   14x 7x 7x 2x 5x 1x 4x 2x     7x   5x                                       7x 7x 4x 3x 1x         7x                         2x                
import { Box, styled, Typography } from "@mui/material";
import Button from "components/Button";
import ConfirmationDialogWrapper from "components/ConfirmationDialogWrapper";
import { useTranslation } from "i18n";
import { GLOBAL, MESSAGES } from "i18n/namespaces";
import { HostRequestStatus } from "proto/messages_pb";
 
export const StyledBanner = styled(Box)(({ theme }) => ({
  background: "var(--mui-palette-background-paper)",
  borderBottom: "1px solid var(--mui-palette-divider)",
  padding: theme.spacing(1.5, 2),
  display: "flex",
  alignItems: "center",
  justifyContent: "space-between",
  gap: theme.spacing(1),
  flexShrink: 0,
}));
 
export default function HostRequestStatusBanner({
  isHost,
  status,
  isLoading,
  onAccept,
  onDecline,
  onCancel,
  hostName,
  isOffer = false,
}: {
  isHost: boolean;
  status: HostRequestStatus;
  isLoading: boolean;
  onAccept: () => void;
  onDecline: () => void;
  onCancel: () => void;
  hostName?: string;
  isOffer?: boolean;
}) {
  const { t } = useTranslation([MESSAGES, GLOBAL]);
 
  // Public-trip offers have their own respond card + title; skip the quick banner
  // (its copy assumes a normal request's roles).
  if (isOffer) return null;
 
  if (isHost) {
    let message: string | null = null;
    if (status === HostRequestStatus.HOST_REQUEST_STATUS_ACCEPTED) {
      message = t("messages:host_request_item.host_status.accepted_waiting");
    } else if (status === HostRequestStatus.HOST_REQUEST_STATUS_CONFIRMED) {
      message = t("messages:host_request_item.host_status.confirmed");
    } else if (status === HostRequestStatus.HOST_REQUEST_STATUS_REJECTED) {
      message = t("messages:host_request_item.host_status.rejected");
    }
 
    if (!message) return null;
 
    const isRejected = status === HostRequestStatus.HOST_REQUEST_STATUS_REJECTED;
 
    return (
      <StyledBanner>
        <Typography variant="body2">{message}</Typography>
        <Button
          variant="contained"
          size="small"
          color="primary"
          onClick={isRejected ? onAccept : onDecline}
          loading={isLoading}
          sx={{ flexShrink: 0 }}
        >
          {isRejected ? t("global:accept") : t("messages:close_request_button_text")}
        </Button>
      </StyledBanner>
    );
  }
 
  // surfer view
  let surferMessage: string | null = null;
  if (status === HostRequestStatus.HOST_REQUEST_STATUS_PENDING) {
    surferMessage = t("messages:surfer_bar_pending", { name: hostName });
  } else if (status === HostRequestStatus.HOST_REQUEST_STATUS_CONFIRMED) {
    surferMessage = t("messages:host_request_item.surfer_status.confirmed", {
      name: hostName,
    });
  }
 
  if (!surferMessage) return null;
 
  return (
    <StyledBanner>
      <Typography variant="body2">{surferMessage}</Typography>
      <ConfirmationDialogWrapper
        title={t("messages:cancel_request_dialog_title")}
        message={t("messages:cancel_request_dialog_message")}
        confirmButtonLabel={t("messages:cancel_request_dialog_confirm_button")}
        cancelButtonLabel={t("messages:cancel_request_dialog_dismiss_button")}
        onConfirm={onCancel}
      >
        {(setIsOpen) => (
          <Button variant="text" size="small" color="primary" onClick={() => setIsOpen(true)} loading={isLoading}>
            {t("messages:cancel_request_button")}
          </Button>
        )}
      </ConfirmationDialogWrapper>
    </StyledBanner>
  );
}