All files / app/features/messages/groupchats MuteDialog.tsx

83.33% Statements 30/36
55.55% Branches 10/18
83.33% Functions 5/6
96.55% Lines 28/29

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            1x 1x 1x           1x 1x   1x 1x 1x 1x 1x 1x       1x       167x 167x 167x     2x 2x 2x 2x 2x 2x               1x 1x 1x         167x       167x   167x                         2x                                                                                                                
import {
  DialogProps,
  FormControl,
  FormControlLabel,
  Radio,
  RadioGroup,
} from "@material-ui/core";
import Alert from "components/Alert";
import Button from "components/Button";
import {
  Dialog,
  DialogActions,
  DialogContent,
  DialogTitle,
} from "components/Dialog";
import { groupChatKey, groupChatsListKey } from "features/queryKeys";
import { RpcError } from "grpc-web";
import { useTranslation } from "i18n";
import { GLOBAL, MESSAGES } from "i18n/namespaces";
import React, { useState } from "react";
import { useMutation, useQueryClient } from "react-query";
import { service } from "service";
import dayjs from "utils/dayjs";
 
type DurationChoice = "1h" | "8h" | "1d" | "1w" | "1m" | "forever";
 
export default function MuteDialog({
  groupChatId,
  ...props
}: DialogProps & { groupChatId: number }) {
  const { t } = useTranslation([GLOBAL, MESSAGES]);
  const queryClient = useQueryClient();
  const muteMutation = useMutation<void, RpcError, DurationChoice>(
    async (duration) => {
      let d;
      Iif (duration === "1h") d = dayjs.duration({ hours: 1 });
      else Iif (duration === "8h") d = dayjs.duration({ hours: 8 });
      else Iif (duration === "1d") d = dayjs.duration({ days: 1 });
      else Iif (duration === "1w") d = dayjs.duration({ weeks: 1 });
      else Iif (duration === "1m") d = dayjs.duration({ months: 1 });
      await service.conversations.muteChat({
        groupChatId,
        forDuration: d,
        forever: !d,
      });
    },
    {
      onSuccess: () => {
        queryClient.invalidateQueries(groupChatsListKey);
        queryClient.invalidateQueries(groupChatKey(groupChatId));
        if (props.onClose) props.onClose({}, "escapeKeyDown");
      },
    }
  );
 
  const [selected, setSelected] = useState<DurationChoice | undefined>(
    undefined
  );
 
  const handleSubmit = () => (selected ? muteMutation.mutate(selected) : null);
 
  return (
    <Dialog {...props} aria-labelledby="mute-dialog-title">
      <DialogTitle id="mute-dialog-title">
        {t("messages:chat_view.mute.dialog_title")}
      </DialogTitle>
      <DialogContent>
        {muteMutation.error && (
          <Alert severity="error">{muteMutation.error.message}</Alert>
        )}
        <FormControl component="fieldset">
          <RadioGroup
            aria-labelledby="mute-dialog-title"
            value={selected ?? null}
            onChange={(e, val) => setSelected(val as DurationChoice)}
          >
            <FormControlLabel
              value="1h"
              control={<Radio />}
              label={t("messages:chat_view.mute.1_hour_label")}
            />
            <FormControlLabel
              value="8h"
              control={<Radio />}
              label={t("messages:chat_view.mute.8_hours_label")}
            />
            <FormControlLabel
              value="1d"
              control={<Radio />}
              label={t("messages:chat_view.mute.1_day_label")}
            />
            <FormControlLabel
              value="1w"
              control={<Radio />}
              label={t("messages:chat_view.mute.1_week_label")}
            />
            <FormControlLabel
              value="1m"
              control={<Radio />}
              label={t("messages:chat_view.mute.1_month_label")}
            />
            <FormControlLabel
              value="forever"
              control={<Radio />}
              label={t("messages:chat_view.mute.forever_label")}
            />
          </RadioGroup>
        </FormControl>
      </DialogContent>
      <DialogActions>
        <Button
          variant="outlined"
          onClick={() =>
            props.onClose ? props.onClose({}, "escapeKeyDown") : null
          }
          loading={muteMutation.isLoading}
        >
          {t("global:cancel")}
        </Button>
        <Button
          onClick={handleSubmit}
          loading={muteMutation.isLoading}
          disabled={!selected}
        >
          {t("messages:chat_view.mute.button_label")}
        </Button>
      </DialogActions>
    </Dialog>
  );
}