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

66.66% Statements 22/33
21.42% Branches 3/14
22.22% Functions 2/9
68.75% Lines 22/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  1x 1x 1x           1x 1x         1x     1x 1x     1x 1x 1x 1x   1x       174x 174x 174x     174x 298x     174x 174x                             174x       174x                                                                                                                
import { DialogProps } from "@material-ui/core";
import Alert from "components/Alert";
import Autocomplete from "components/Autocomplete";
import Button from "components/Button";
import {
  Dialog,
  DialogActions,
  DialogContent,
  DialogTitle,
} from "components/Dialog";
import useFriendList from "features/connections/friends/useFriendList";
import {
  groupChatKey,
  groupChatMessagesKey,
  groupChatsListKey,
} from "features/queryKeys";
import { Empty } from "google-protobuf/google/protobuf/empty_pb";
import { RpcError } from "grpc-web";
import { useTranslation } from "i18n";
import { GLOBAL, MESSAGES } from "i18n/namespaces";
import { User } from "proto/api_pb";
import { GroupChat } from "proto/conversations_pb";
import React from "react";
import { Controller, useForm } from "react-hook-form";
import { useMutation, useQueryClient } from "react-query";
import { service } from "service";
 
export default function InviteDialog({
  groupChat,
  ...props
}: DialogProps & { groupChat: GroupChat.AsObject }) {
  const { t } = useTranslation([GLOBAL, MESSAGES]);
  const friends = useFriendList();
  const { control, handleSubmit } = useForm<{
    selected: User.AsObject[];
  }>();
  const friendsNotInChat = friends.data?.filter(
    (friend) => !groupChat.memberUserIdsList.includes(friend?.userId ?? 0)
  );
 
  const queryClient = useQueryClient();
  const mutation = useMutation<Empty[], RpcError, User.AsObject[]>(
    (users: User.AsObject[]) =>
      service.conversations.inviteToGroupChat(groupChat.groupChatId, users),
    {
      onSuccess: () => {
        queryClient.invalidateQueries(
          groupChatMessagesKey(groupChat.groupChatId)
        );
        queryClient.invalidateQueries(groupChatsListKey);
        queryClient.invalidateQueries(groupChatKey(groupChat.groupChatId));
        Iif (props.onClose) props.onClose({}, "escapeKeyDown");
      },
    }
  );
 
  const onSubmit = handleSubmit(({ selected }) => {
    mutation.mutate(selected);
  });
 
  return (
    <Dialog {...props} aria-labelledby="invite-dialog-title">
      <DialogTitle id="invite-dialog-title">
        {t("messages:invite_dialog.title")}
      </DialogTitle>
      <DialogContent>
        <form onSubmit={onSubmit}>
          {(mutation.error || !!friends.errors.length) && (
            <Alert severity={"error"}>
              {mutation.error?.message || friends.errors.join("\n")}
            </Alert>
          )}
          <Controller
            control={control}
            defaultValue={[]}
            name="selected"
            render={({ onChange }) => (
              <Autocomplete
                id="selected-autocomplete"
                onChange={(_, value) => {
                  onChange(value);
                }}
                loading={friends.isLoading}
                options={friendsNotInChat ?? []}
                getOptionLabel={(friend) => {
                  return (
                    friend?.name ??
                    t("messages:invite_dialog.selected.option_load_error_text")
                  );
                }}
                noOptionsText={t(
                  "messages:invite_dialog.selected.no_options_text"
                )}
                label={t("messages:invite_dialog.selected.field_label")}
                multiple={true}
                freeSolo={false}
              />
            )}
          />
        </form>
      </DialogContent>
      <DialogActions>
        <Button onClick={onSubmit} loading={mutation.isLoading}>
          {t("messages:invite_dialog.invite_button_label")}
        </Button>
        <Button
          onClick={() =>
            props.onClose ? props.onClose({}, "escapeKeyDown") : null
          }
        >
          {t("global:cancel")}
        </Button>
      </DialogActions>
    </Dialog>
  );
}