scroll to bottom when new message and prevent sending empty message
This commit is contained in:
parent
ac3a0fb3a8
commit
31b97eaa59
|
|
@ -29,11 +29,10 @@ const Likes = ({ likes }: {likes: number}) => {
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const Message = ({ text = '', user, date, image = '' }: any) => {
|
const Message = ({ text = '', user, date, image = '', id }: any) => {
|
||||||
const id = '1'; // replace with real id in the future
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className="flex bg-grey-dark rounded-xl w-full max-w-3xl p-5 gap-5 shadow-md shadow-grey-dark">
|
<div className="flex bg-grey-dark rounded-xl w-full max-w-3xl p-5 gap-5 shadow-md shadow-grey-dark" id={"messageId" + id}>
|
||||||
{user && <Avatar user={user} />}
|
{user && <Avatar user={user} />}
|
||||||
<div className="flex flex-col gap-2 relative flex-grow">
|
<div className="flex flex-col gap-2 relative flex-grow">
|
||||||
<div className="flex justify-between">
|
<div className="flex justify-between">
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,6 @@ const getMessages = async () => {
|
||||||
const MessageWrapper = () => {
|
const MessageWrapper = () => {
|
||||||
const messages = useQuery(["messages"], getMessages, {
|
const messages = useQuery(["messages"], getMessages, {
|
||||||
onSuccess: (data) => {
|
onSuccess: (data) => {
|
||||||
console.log(data);
|
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
},
|
},
|
||||||
onError: (error) => {
|
onError: (error) => {
|
||||||
|
|
@ -36,7 +34,7 @@ const MessageWrapper = () => {
|
||||||
return (
|
return (
|
||||||
<main className="messages-wrapper flex flex-col p-4 gap-4 overflow-scroll w-full max-w-3xl">
|
<main className="messages-wrapper flex flex-col p-4 gap-4 overflow-scroll w-full max-w-3xl">
|
||||||
{messages.isLoading ? '' : messages.data?.map((message: any) => (
|
{messages.isLoading ? '' : messages.data?.map((message: any) => (
|
||||||
<Message user={message.author} text={message.content} image={message.image} date={message.createdAt}/>
|
<Message user={message.author} text={message.content} image={message.image} date={message.createdAt} id={message.id}/>
|
||||||
))}
|
))}
|
||||||
</main>
|
</main>
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -7,9 +7,8 @@ const sendMessage = async (data: FormData) => {
|
||||||
const token = new Cookies().get('token');
|
const token = new Cookies().get('token');
|
||||||
let contentType = 'application/json';
|
let contentType = 'application/json';
|
||||||
let body: FormData | string | undefined = undefined;
|
let body: FormData | string | undefined = undefined;
|
||||||
console.log(data.get('image'));
|
|
||||||
|
|
||||||
if (data.get('image')?.name !== "") {
|
if (data.get('image')) {
|
||||||
contentType = 'multipart/form-data';
|
contentType = 'multipart/form-data';
|
||||||
body = data;
|
body = data;
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -32,9 +31,12 @@ const NewMessage = () => {
|
||||||
const queryClient = useQueryClient();
|
const queryClient = useQueryClient();
|
||||||
|
|
||||||
const { mutate: send } = useMutation(sendMessage, {
|
const { mutate: send } = useMutation(sendMessage, {
|
||||||
onSuccess: (data) => {
|
onSuccess: () => {
|
||||||
console.log(data);
|
|
||||||
queryClient.invalidateQueries(['messages']);
|
queryClient.invalidateQueries(['messages']);
|
||||||
|
const messageWrapper = document.querySelector('.messages-wrapper');
|
||||||
|
messageWrapper?.addEventListener('DOMNodeInserted', () => {
|
||||||
|
messageWrapper?.scrollTo(0, messageWrapper.scrollHeight);
|
||||||
|
});
|
||||||
},
|
},
|
||||||
onError: (error) => {
|
onError: (error) => {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|
@ -43,6 +45,9 @@ const NewMessage = () => {
|
||||||
|
|
||||||
const handleSubmit = (e: React.FormEvent) => {
|
const handleSubmit = (e: React.FormEvent) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
if (message.trim().length === 0 && image.trim().length === 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const data = new FormData(e.target as HTMLFormElement);
|
const data = new FormData(e.target as HTMLFormElement);
|
||||||
send(data);
|
send(data);
|
||||||
setMessage('');
|
setMessage('');
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue