All files / src/features/Minibuilders/Conversations/Chat ChatAction.tsx

100% Statements 6/6
66.66% Branches 6/9
100% Functions 1/1
100% Lines 6/6

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                                                21x               60x   60x       60x               60x   60x                                                                                                                                                                  
import { Button } from "@common/Button";
import { Image } from "@common/StyledImage/Image";
import { Text } from "@common/Text";
import { addSizeToImage } from "@components/UploadAvatar/UploadAvatar";
import { Stack } from "@mui/material";
import { useAppSelector } from "@redux/hooks";
import { selectUser } from "@redux/slices/auth/auth";
import ChatEditor from "./ChatEditor";
import { FormProvider } from "react-hook-form";
import { useCreateMessage } from "../hooks/useCreateMessage";
import { TMentionedUser } from "../utils/functions";
import AvatarPlaceholder from "@assets/images/avatar-placeholder.png";
import { MAX_MESSAGE_LENGTH_ERR } from "../constants";
import { palette } from "@theme/colors";
 
type Props = {
  mentionedUser?: TMentionedUser;
  merchantId: number;
  defaultMessage?: string;
  shouldHideMentionIcon?: boolean;
  onMessageSubmit?: (...props: any[]) => void;
  handleOpenConversationsModal: (open?: boolean) => void;
};
 
const ChatAction = ({
  mentionedUser,
  merchantId,
  defaultMessage,
  shouldHideMentionIcon = true,
  onMessageSubmit,
  handleOpenConversationsModal,
}: Props) => {
  const { img } = useAppSelector(selectUser);
 
  const image = img
    ? addSizeToImage(img.replace("/thumb", ""), "small")
    : AvatarPlaceholder;
 
  const { onSubmit, methods, watch, disableSubmit, isValid } = useCreateMessage(
    {
      merchantId,
      onMessageSubmit,
      handleOpenConversationsModal,
    },
  );
 
  const values = watch();
 
  return (
    <FormProvider {...methods}>
      <form onSubmit={methods.handleSubmit(onSubmit, console.log)}>
        <Stack
          direction="row"
          justifyContent="space-between"
          alignItems={"center"}
          sx={{
            borderTop: "1px solid #FFF",
            boxShadow: "0px -2px 10px 0px rgba(76, 76, 76, 0.10)",
            padding: `8px 0 8px 16px`,
            borderRadius: "unset",
            gap: 2,
          }}
        >
          <Stack
            direction="row"
            alignItems={"center"}
            sx={{ width: "100%" }}
            gap={2}
          >
            <Image
              src={image}
              sx={{
                borderRadius: "80px",
                height: 32,
                width: 32,
              }}
            />
            <Stack width="100%">
              <ChatEditor
                disabled={disableSubmit}
                name="conversationInput"
                mentionedUser={mentionedUser}
                onSubmit={onSubmit}
                defaultMessage={defaultMessage}
                shouldHideMentionIcon={shouldHideMentionIcon}
              />
              {!isValid && (
                <Text color={palette.error.hover} marginLeft={1}>
                  {MAX_MESSAGE_LENGTH_ERR}
                </Text>
              )}
            </Stack>
          </Stack>
 
          {values.conversationInput && (
            <Button
              background="tertiary"
              sx={{
                cursor: "pointer",
                borderRadius: "unset",
                minWidth: 0,
                minHeight: 0,
                height: "auto",
                boxSizing: "border-box",
                padding: "0 16px 0 0",
              }}
              tabIndex={0}
              type="submit"
              disabled={disableSubmit}
            >
              <Text
                color={
                  disableSubmit
                    ? palette.neutral.main[70]
                    : palette.givebox[600]
                }
                fontSize={14}
              >
                Post
              </Text>
            </Button>
          )}
        </Stack>
      </form>
    </FormProvider>
  );
};
 
export default ChatAction;