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

0% Statements 0/39
0% Branches 0/8
0% Functions 0/13
0% Lines 0/38

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               
import { ThumbDown, ThumbsUpDown, ThumbUp } from "@mui/icons-material";
import {
  Box,
  Checkbox,
  FormControl,
  FormControlLabel,
  FormGroup,
  styled,
  TextField,
  ToggleButton,
  ToggleButtonGroup,
  Typography,
} from "@mui/material";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import Alert from "components/Alert";
import Button from "components/Button";
import { hostRequestKey } from "features/queryKeys";
import { RpcError } from "grpc-web";
import { useTranslation } from "i18n";
import { MESSAGES } from "i18n/namespaces";
import { HostRequestQuality } from "proto/requests_pb";
import { useState } from "react";
import { service } from "service";
 
type DeclineReason =
  | "didnt_read_profile"
  | "dont_want_to_host"
  | "not_available"
  | "other";
 
const StyledCard = styled(Box)(({ theme }) => ({
  background: "var(--mui-palette-grey-50)",
  borderRadius: theme.shape.borderRadius * 2,
  padding: theme.spacing(2, 2, 1.5),
  display: "flex",
  flexDirection: "column",
  gap: theme.spacing(2),
}));
 
const StyledActions = styled(Box)({
  display: "flex",
  justifyContent: "flex-end",
  gap: 8,
});
 
export default function HostRequestFeedbackCard({
  hostRequestId,
}: {
  hostRequestId: number;
}) {
  const { t } = useTranslation(MESSAGES);
  const queryClient = useQueryClient();
 
  const [quality, setQuality] = useState<HostRequestQuality | null>(null);
  const [declineReasons, setDeclineReasons] = useState<Set<DeclineReason>>(
    new Set(),
  );
  const [otherText, setOtherText] = useState("");
 
  const toggleReason = (reason: DeclineReason) => {
    setDeclineReasons((prev) => {
      const next = new Set(prev);
      if (next.has(reason)) {
        next.delete(reason);
      } else {
        next.add(reason);
      }
      return next;
    });
  };
 
  const {
    mutate: submitFeedback,
    isPending,
    error,
  } = useMutation<
    void,
    RpcError,
    { quality: HostRequestQuality; declineReason: string }
  >({
    mutationFn: (data) =>
      service.requests.sendHostRequestFeedback(
        hostRequestId,
        data.quality,
        data.declineReason,
      ),
    onSuccess: () => {
      queryClient.invalidateQueries({
        queryKey: hostRequestKey(hostRequestId),
      });
    },
  });
 
  const handleSkip = () => {
    submitFeedback({
      quality: HostRequestQuality.HOST_REQUEST_QUALITY_UNSPECIFIED,
      declineReason: "",
    });
  };
 
  const handleSubmit = () => {
    const reasons = Array.from(declineReasons).map((r) =>
      r === "other" ? otherText : r,
    );
    submitFeedback({
      quality: quality ?? HostRequestQuality.HOST_REQUEST_QUALITY_UNSPECIFIED,
      declineReason: reasons.join(","),
    });
  };
 
  return (
    <StyledCard>
      {error && <Alert severity="error">{error.message}</Alert>}
      <FormControl component="fieldset">
        <Typography variant="subtitle2" gutterBottom>
          {t("private_feedback_card.quality_label")}
        </Typography>
        <ToggleButtonGroup
          exclusive
          value={quality}
          onChange={(_e, val) => setQuality(val)}
          aria-label={t("private_feedback_card.quality_label")}
          size="small"
          sx={{
            gap: 1,
            flexWrap: "wrap",
            "& .MuiToggleButton-root": {
              border: "1px solid var(--mui-palette-divider) !important",
            },
            "& .MuiToggleButton-root.Mui-selected": {
              border: "1px solid var(--mui-palette-primary-main) !important",
            },
          }}
        >
          <ToggleButton
            value={HostRequestQuality.HOST_REQUEST_QUALITY_LOW}
            sx={{
              borderRadius: "2rem !important",
              px: { xs: 1, sm: 2 },
              fontSize: { xs: "0.7rem", sm: "0.8125rem" },
              gap: 0.75,
              background: "var(--mui-palette-grey-200)",
            }}
          >
            <ThumbDown fontSize="inherit" />
            {t("private_feedback_card.quality_low")}
          </ToggleButton>
          <ToggleButton
            value={HostRequestQuality.HOST_REQUEST_QUALITY_OKAY}
            sx={{
              borderRadius: "2rem !important",
              px: { xs: 1, sm: 2 },
              fontSize: { xs: "0.7rem", sm: "0.8125rem" },
              gap: 0.75,
              background: "var(--mui-palette-grey-200)",
            }}
          >
            <ThumbsUpDown fontSize="inherit" />
            {t("private_feedback_card.quality_okay")}
          </ToggleButton>
          <ToggleButton
            value={HostRequestQuality.HOST_REQUEST_QUALITY_HIGH}
            sx={{
              borderRadius: "2rem !important",
              px: { xs: 1, sm: 2 },
              fontSize: { xs: "0.7rem", sm: "0.8125rem" },
              gap: 0.75,
              background: "var(--mui-palette-grey-200)",
            }}
          >
            <ThumbUp fontSize="inherit" />
            {t("private_feedback_card.quality_high")}
          </ToggleButton>
        </ToggleButtonGroup>
      </FormControl>
      <FormControl component="fieldset">
        <Typography variant="subtitle2" gutterBottom>
          {t("private_feedback_card.decline_reason_label")}
        </Typography>
        <FormGroup>
          {(
            [
              "didnt_read_profile",
              "dont_want_to_host",
              "not_available",
              "other",
            ] as DeclineReason[]
          ).map((reason) => (
            <FormControlLabel
              key={reason}
              control={
                <Checkbox
                  size="small"
                  checked={declineReasons.has(reason)}
                  onChange={() => toggleReason(reason)}
                />
              }
              label={t(`private_feedback_card.reason_${reason}`)}
            />
          ))}
        </FormGroup>
        {declineReasons.has("other") && (
          <TextField
            multiline
            minRows={2}
            maxRows={4}
            fullWidth
            value={otherText}
            onChange={(e) => setOtherText(e.target.value)}
            placeholder={t("private_feedback_card.reason_other_placeholder")}
            size="small"
            sx={{ mt: 1 }}
          />
        )}
      </FormControl>
      <Typography variant="caption" color="text.secondary">
        {t("private_feedback_card.privacy_notice")}
      </Typography>
      <StyledActions>
        <Button
          variant="text"
          size="small"
          onClick={handleSkip}
          disabled={isPending}
        >
          {t("private_feedback_card.skip_button")}
        </Button>
        <Button
          variant="contained"
          size="small"
          onClick={handleSubmit}
          loading={isPending}
        >
          {t("private_feedback_card.submit_button")}
        </Button>
      </StyledActions>
    </StyledCard>
  );
}