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

94.44% Statements 34/36
91.3% Branches 21/23
100% Functions 9/9
93.75% Lines 30/32

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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 3053x 3x 3x 3x 3x 3x   3x 3x   14x                   14x                               5x                   1x                                 5x                   2x                                               12x 12x 12x     12x 7x                                               5x                                 5x                                     2x                                     1x                                                 23x                             23x   23x                       11x 5x                                                             6x                                               1x                        
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";
 
import FieldButton from "./FieldButton";
import { StyledBanner } from "./HostRequestStatusBanner";
 
const StyledCard = styled(Box)(({ theme }) => ({
  background: "var(--mui-palette-grey-50)",
  borderRadius: theme.shape.borderRadius * 2,
  padding: theme.spacing(2),
  display: "flex",
  flexDirection: "column",
  gap: theme.spacing(1.5),
  alignSelf: "flex-start",
}));
 
const StyledButtonRow = styled(Box)(({ theme }) => ({
  display: "flex",
  flexWrap: "wrap",
  gap: theme.spacing(1),
  justifyContent: "flex-end",
}));
 
function WithdrawOfferButton({
  isLoading,
  onConfirm,
  name,
}: {
  isLoading: boolean;
  onConfirm: () => void;
  name?: string;
}) {
  const { t } = useTranslation([MESSAGES]);
  return (
    <ConfirmationDialogWrapper
      title={t("messages:withdraw_invitation_dialog_title")}
      message={t("messages:withdraw_invitation_dialog_message", { name })}
      confirmButtonLabel={t("messages:withdraw_invitation_dialog_confirm_button")}
      cancelButtonLabel={t("messages:withdraw_invitation_dialog_dismiss_button")}
      onConfirm={onConfirm}
    >
      {(setIsOpen) => (
        <FieldButton isLoading={isLoading} callback={() => setIsOpen(true)} variant="outlined">
          {t("messages:withdraw_invitation_button")}
        </FieldButton>
      )}
    </ConfirmationDialogWrapper>
  );
}
 
function CancelAcceptedOfferButton({
  isLoading,
  onConfirm,
  name,
}: {
  isLoading: boolean;
  onConfirm: () => void;
  name?: string;
}) {
  const { t } = useTranslation([MESSAGES]);
  return (
    <ConfirmationDialogWrapper
      title={t("messages:cancel_accepted_invitation_stay_dialog_title", { name })}
      message={t("messages:cancel_accepted_invitation_stay_dialog_message", { name })}
      confirmButtonLabel={t("messages:cancel_accepted_invitation_stay_dialog_confirm_button")}
      cancelButtonLabel={t("messages:cancel_accepted_invitation_stay_dialog_dismiss_button")}
      onConfirm={onConfirm}
    >
      {(setIsOpen) => (
        <Button variant="text" size="small" color="primary" onClick={() => setIsOpen(true)} loading={isLoading}>
          {t("messages:cancel_accepted_invitation_stay_button")}
        </Button>
      )}
    </ConfirmationDialogWrapper>
  );
}
 
// Public-trip offers: the traveller accepts/declines (and can cancel their stay after
// accepting, which is sent as a decline); the offering host can
// confirm once accepted, and withdraw at any point.
function OfferRespondButtons({
  isHost,
  status,
  isLoading,
  handleStatus,
  name,
}: {
  isHost: boolean;
  status: HostRequestStatus;
  isLoading: boolean;
  handleStatus: (status: HostRequestStatus) => () => void;
  name?: string;
}) {
  const { t } = useTranslation([MESSAGES, GLOBAL]);
  const isAccepted = status === HostRequestStatus.HOST_REQUEST_STATUS_ACCEPTED;
  const isConfirmed = status === HostRequestStatus.HOST_REQUEST_STATUS_CONFIRMED;
 
  // Traveller (the trip owner) accepts or declines the offer.
  if (!isHost) {
    if (status === HostRequestStatus.HOST_REQUEST_STATUS_PENDING) {
      return (
        <StyledCard>
          <div>
            <Typography variant="subtitle2">{t("messages:invitation_respond_box_title", { name })}</Typography>
            <Typography variant="body2" color="text.secondary">
              {t("messages:invitation_respond_box_description", { name })}
            </Typography>
          </div>
          <StyledButtonRow>
            <FieldButton
              isLoading={isLoading}
              callback={handleStatus(HostRequestStatus.HOST_REQUEST_STATUS_REJECTED)}
              variant="outlined"
            >
              {t("messages:close_request_button_text")}
            </FieldButton>
            <FieldButton callback={handleStatus(HostRequestStatus.HOST_REQUEST_STATUS_ACCEPTED)} isLoading={isLoading}>
              {t("global:accept")}
            </FieldButton>
          </StyledButtonRow>
        </StyledCard>
      );
    }
    Eif (isAccepted || isConfirmed) {
      return (
        <StyledBanner>
          <Typography variant="body2">{t("messages:invitation_accept_confirmation", { name })}</Typography>
          <CancelAcceptedOfferButton
            isLoading={isLoading}
            onConfirm={handleStatus(HostRequestStatus.HOST_REQUEST_STATUS_REJECTED)}
            name={name}
          />
        </StyledBanner>
      );
    }
    return null;
  }
 
  // Offering host: withdraw while pending, confirm (or withdraw) once accepted,
  // and still withdraw after confirming.
  if (status === HostRequestStatus.HOST_REQUEST_STATUS_PENDING) {
    return (
      <StyledCard>
        <div>
          <Typography variant="subtitle2">{t("messages:invitation_sent_box_title")}</Typography>
          <Typography variant="body2" color="text.secondary">
            {t("messages:invitation_sent_box_description", { name })}
          </Typography>
        </div>
        <StyledButtonRow>
          <WithdrawOfferButton
            isLoading={isLoading}
            onConfirm={handleStatus(HostRequestStatus.HOST_REQUEST_STATUS_CANCELLED)}
            name={name}
          />
        </StyledButtonRow>
      </StyledCard>
    );
  }
  if (isConfirmed) {
    return (
      <StyledCard>
        <div>
          <Typography variant="subtitle2">{t("messages:invitation_host_confirmed_box_title", { name })}</Typography>
          <Typography variant="body2" color="text.secondary">
            {t("messages:invitation_host_confirmed_box_description", { name })}
          </Typography>
        </div>
        <StyledButtonRow>
          <WithdrawOfferButton
            isLoading={isLoading}
            onConfirm={handleStatus(HostRequestStatus.HOST_REQUEST_STATUS_CANCELLED)}
            name={name}
          />
        </StyledButtonRow>
      </StyledCard>
    );
  }
  Eif (isAccepted) {
    return (
      <StyledCard>
        <div>
          <Typography variant="subtitle2">{t("messages:invitation_host_accepted_box_title", { name })}</Typography>
          <Typography variant="body2" color="text.secondary">
            {t("messages:invitation_host_accepted_box_description")}
          </Typography>
        </div>
        <StyledButtonRow>
          <WithdrawOfferButton
            isLoading={isLoading}
            onConfirm={handleStatus(HostRequestStatus.HOST_REQUEST_STATUS_CANCELLED)}
            name={name}
          />
          <FieldButton callback={handleStatus(HostRequestStatus.HOST_REQUEST_STATUS_CONFIRMED)} isLoading={isLoading}>
            {t("messages:confirm_request_button_text")}
          </FieldButton>
        </StyledButtonRow>
      </StyledCard>
    );
  }
  return null;
}
 
export default function HostRequestRespondButtons({
  isHost,
  status,
  isLoading,
  handleStatus,
  name,
  isOffer = false,
}: {
  isHost: boolean;
  status: HostRequestStatus;
  isLoading: boolean;
  handleStatus: (status: HostRequestStatus) => () => void;
  name?: string;
  isOffer?: boolean;
}) {
  const { t } = useTranslation([MESSAGES, GLOBAL]);
 
  if (isOffer) {
    return (
      <OfferRespondButtons
        isHost={isHost}
        status={status}
        isLoading={isLoading}
        handleStatus={handleStatus}
        name={name}
      />
    );
  }
 
  if (isHost) {
    if (status !== HostRequestStatus.HOST_REQUEST_STATUS_PENDING) return null;
 
    return (
      <StyledCard>
        <div>
          <Typography variant="subtitle2">{t("messages:respond_box_title")}</Typography>
          <Typography
            variant="body2"
            sx={{
              color: "text.secondary",
            }}
          >
            {t("messages:respond_box_description", { name })}
          </Typography>
        </div>
        <StyledButtonRow>
          <FieldButton
            isLoading={isLoading}
            callback={handleStatus(HostRequestStatus.HOST_REQUEST_STATUS_REJECTED)}
            variant="outlined"
          >
            {t("messages:close_request_button_text")}
          </FieldButton>
          <FieldButton callback={handleStatus(HostRequestStatus.HOST_REQUEST_STATUS_ACCEPTED)} isLoading={isLoading}>
            {t("global:accept")}
          </FieldButton>
        </StyledButtonRow>
      </StyledCard>
    );
  }
 
  if (status !== HostRequestStatus.HOST_REQUEST_STATUS_ACCEPTED) return null;
 
  return (
    <StyledCard>
      <div>
        <Typography variant="subtitle2">{t("messages:surfer_confirm_box_title", { name })}</Typography>
        <Typography
          variant="body2"
          sx={{
            color: "text.secondary",
          }}
        >
          {t("messages:surfer_confirm_box_description")}
        </Typography>
      </div>
      <StyledButtonRow>
        <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={handleStatus(HostRequestStatus.HOST_REQUEST_STATUS_CANCELLED)}
        >
          {(setIsOpen) => (
            <FieldButton isLoading={isLoading} callback={() => setIsOpen(true)} variant="outlined">
              {t("messages:cancel_request_button")}
            </FieldButton>
          )}
        </ConfirmationDialogWrapper>
        <FieldButton callback={handleStatus(HostRequestStatus.HOST_REQUEST_STATUS_CONFIRMED)} isLoading={isLoading}>
          {t("messages:confirm_request_button_text")}
        </FieldButton>
      </StyledButtonRow>
    </StyledCard>
  );
}