All files / app/features FlagButton.tsx

97.43% Statements 38/39
88.88% Branches 8/9
100% Functions 12/12
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            3x 3x 3x             3x 3x 3x 3x     3x 3x 3x 3x 3x 3x   3x   19x                                                   3x         449x 449x 449x             449x             449x   2x     1x         449x       1x 1x 1x 1x     449x 2x     449x                   5x                                                           3x       19x           2x                           190x                                                         1x                      
import {
  FormControl,
  FormHelperText,
  IconButton,
  InputLabel,
  Select,
} from "@material-ui/core";
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 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,
    errors,
    reset: resetForm,
  } = 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);
  });
 
  return (
    <>
      {report && (
        <Snackbar severity="success">
          {t("report.content.success_message")}
        </Snackbar>
      )}
      <IconButton
        aria-label={t("report.flag.button_aria_label")}
        className={className}
        onClick={() => setIsOpen(true)}
        color="primary"
      >
        <FlagIcon />
      </IconButton>
      <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={({ onChange, value }) => (
                  <Select
                    className={classes.field}
                    native
                    value={value}
                    label={t("report.flag.reason_label")}
                    id="content-report-reason"
                    onChange={(event) => onChange(event.target.value)}
                  >
                    {[
                      "",
                      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"
              label={t("report.flag.description_label")}
              helperText={t("report.flag.description_helper")}
              name="description"
              inputRef={register}
              fullWidth
              multiline
              minRows={4}
              maxRows={6}
            />
          </DialogContent>
          <DialogActions>
            <Button type="submit" loading={isLoading} onClick={onSubmit}>
              {t("submit")}
            </Button>
            <Button
              onClick={() => handleClose({}, "button")}
              variant="outlined"
            >
              {t("cancel")}
            </Button>
          </DialogActions>
        </form>
      </Dialog>
    </>
  );
}