All files / app/features/profile/actions MessageUserButton.tsx

92.59% Statements 25/27
100% Branches 4/4
71.42% Functions 5/7
92.59% Lines 25/27

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 743x 3x 3x 3x 3x 3x 3x   3x 3x   3x   19x             27x 27x 27x 2x     2x           2x   1x     1x           27x     27x   27x 3x 1x   2x                                          
import { useMutation } from "@tanstack/react-query";
import Button from "components/Button";
import ProfileIncompleteDialog from "components/ProfileIncompleteDialog/ProfileIncompleteDialog";
import useAccountInfo from "features/auth/useAccountInfo";
import { useTranslation } from "i18n";
import { PROFILE } from "i18n/namespaces";
import { useRouter } from "next/router";
import { User } from "proto/api_pb";
import { useState } from "react";
import { service } from "service";
 
import { routeToCreateMessage, routeToGroupChat } from "../../../routes";
 
export default function MessageUserButton({
  user,
  setMutationError,
}: {
  user: User.AsObject;
  setMutationError: (value: string) => void;
}) {
  const { t } = useTranslation(PROFILE);
  const router = useRouter();
  const { mutate, isPending } = useMutation<number | false, Error>({
    mutationFn: () => service.conversations.getDirectMessage(user.userId),
 
    onMutate() {
      setMutationError("");
    },
    onError(e) {
      setMutationError(e.message);
    },
    onSuccess(data) {
      if (!data) {
        //no existing thread
        router.push(routeToCreateMessage(user.username));
      } else {
        //has thread
        router.push(routeToGroupChat(data));
      }
    },
  });
 
  const [showCantMessageDialog, setShowCantMessageDialog] =
    useState<boolean>(false);
 
  const { data: accountInfo, isLoading: isAccountInfoLoading } =
    useAccountInfo();
 
  const onClick = () => {
    if (!accountInfo?.profileComplete) {
      setShowCantMessageDialog(true);
    } else {
      mutate();
    }
  };
 
  return (
    <>
      <ProfileIncompleteDialog
        open={showCantMessageDialog}
        onClose={() => setShowCantMessageDialog(false)}
        attempted_action="send_message"
      />
      <Button
        loading={isPending}
        onClick={onClick}
        disabled={isAccountInfoLoading}
      >
        {t("actions.message_label")}
      </Button>
    </>
  );
}