All files / app/features FlagButton.tsx

97.43% Statements 38/39
85.71% Branches 6/7
100% Functions 11/11
100% Lines 37/37

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            10x 10x 10x             10x 10x 10x 10x     10x 10x 10x 10x 10x 10x   10x 10x   32x                                                   310x         320x 320x 320x             320x             320x   2x     1x         320x       1x 1x 1x 1x     320x 2x     320x 5x                           5x                                                               3x                                                   120x                                                       1x                                        
import {
  FormControl,
  FormHelperText,
  IconButton,
  InputLabel,
  Select,
} from "@mui/material";
import Alert from "components/Alert";
import Button from "components/Button";
import {
  Dialog,
  DialogActions,
  DialogContent,
  DialogContentText,
  DialogTitle,
} from "components/Dialog";
import { FlagIcon } from "components/Icons";
import Snackbar from "components/Snackbar";
import TextField from "components/TextField";
import { Empty } from "google-protobuf/google/protobuf/empty_pb";
import { RpcError } from "grpc-web";
import { useTranslation } from "i18n";
import { GLOBAL } from "i18n/namespaces";
import { useState } from "react";
import { Controller, useForm } from "react-hook-form";
import { useMutation } from "react-query";
import { service } from "service";
import { ReportInput } from "service/reporting";
import { theme } from "theme";
import makeStyles from "utils/makeStyles";
 
const useStyles = makeStyles((theme) => ({
  typeButton: {
    display: "block",
    margin: "0 auto",
    "& + &": {
      marginBlockStart: theme.spacing(2),
    },
  },
  field: {
    "& + &": {
      marginBlockStart: theme.spacing(2),
    },
  },
  formControl: {
    "& .MuiOutlinedInput-root": {
      borderRadius: theme.shape.borderRadius * 3,
    },
  },
}));
 
export interface FlagButtonProps {
  contentRef: string;
  authorUser: string | number;
  className?: string;
}
 
export default function FlagButton({
  contentRef,
  authorUser,
  className,
}: FlagButtonProps) {
  const { t } = useTranslation(GLOBAL);
  const classes = useStyles();
  const [isOpen, setIsOpen] = useState(false);
  const {
    control,
    handleSubmit,
    register,
    reset: resetForm,
    formState: { errors },
  } = useForm<ReportInput>();
  const {
    data: report,
    error,
    isLoading,
    mutate: reportContent,
    reset: resetMutation,
  } = useMutation<Empty, RpcError, ReportInput>(
    (formData) =>
      service.reporting.reportContent({ ...formData, contentRef, authorUser }),
    {
      onSuccess: () => {
        setIsOpen(false);
      },
    }
  );
 
  const handleClose = (
    event: unknown,
    reason: "backdropClick" | "escapeKeyDown" | "button"
  ) => {
    Iif (reason !== "button") return;
    resetForm();
    resetMutation();
    setIsOpen(false);
  };
 
  const onSubmit = handleSubmit((data) => {
    reportContent(data);
  });
 
  const handleFlagButtonClick = (event: { preventDefault: () => void }) => {
    event.preventDefault();
  };
 
  return (
    <>
      {report && (
        <Snackbar severity="success">
          {t("report.content.success_message")}
        </Snackbar>
      )}
      <div onClick={handleFlagButtonClick}>
        <IconButton
          aria-label={t("report.flag.button_aria_label")}
          className={className}
          onClick={() => setIsOpen(true)}
          color="primary"
          size="large"
        >
          <FlagIcon />
        </IconButton>
      </div>
      <Dialog
        aria-labelledby="content-reporter"
        open={isOpen}
        onClose={handleClose}
      >
        <DialogTitle id="content-reporter">
          {t("report.flag.title")}
        </DialogTitle>
        <form onSubmit={onSubmit}>
          <DialogContent>
            {error && <Alert severity="error">{error.message}</Alert>}
            <DialogContentText>{t("report.flag.explainer")}</DialogContentText>
            <FormControl
              variant="outlined"
              fullWidth
              className={classes.formControl}
              margin="normal"
            >
              <InputLabel htmlFor="content-report-reason">
                {t("report.flag.reason_label")}
              </InputLabel>
              <Controller
                control={control}
                defaultValue={""}
                rules={{
                  validate: (v) => !!v || t("report.flag.reason_required"),
                }}
                name="reason"
                render={({ field }) => (
                  <Select
                    {...field}
                    variant="standard"
                    className={classes.field}
                    native
                    value={field.value}
                    label={t("report.flag.reason_label")}
                    id="content-report-reason"
                    onChange={field.onChange}
                  >
                    {[
                      "",
                      t("report.flag.reason.spam"),
                      t("report.flag.reason.dating"),
                      t("report.flag.reason.external"),
                      t("report.flag.reason.commercial"),
                      t("report.flag.reason.harassment"),
                      t("report.flag.reason.fake"),
                      t("report.flag.reason.freeloading"),
                      t("report.flag.reason.guidelines_breach"),
                      t("report.flag.reason.other"),
                    ].map((option) => (
                      <option value={option} key={option}>
                        {option}
                      </option>
                    ))}
                  </Select>
                )}
              />
              <FormHelperText error={!!errors?.reason}>
                {errors?.reason?.message || t("report.flag.reason_helper")}
              </FormHelperText>
            </FormControl>
            <TextField
              className={classes.field}
              id="content-report-description"
              {...register("description")}
              label={t("report.flag.description_label")}
              helperText={t("report.flag.description_helper")}
              fullWidth
              multiline
              minRows={4}
              maxRows={6}
            />
          </DialogContent>
          <DialogActions>
            <Button type="submit" loading={isLoading} onClick={onSubmit}>
              {t("submit")}
            </Button>
            <Button
              onClick={() => handleClose({}, "button")}
              variant="outlined"
              sx={{
                color: theme.palette.common.black,
                borderColor: theme.palette.grey[300],
 
                "&:hover": {
                  borderColor: theme.palette.grey[300],
                  backgroundColor: "#3135390A",
                },
              }}
            >
              {t("cancel")}
            </Button>
          </DialogActions>
        </form>
      </Dialog>
    </>
  );
}