send picture

This commit is contained in:
Guillaume Dorce 2022-09-24 18:22:31 +02:00
parent 0baa655a66
commit ac3a0fb3a8
4 changed files with 34 additions and 8 deletions

View File

@ -13,7 +13,7 @@ const Avatar = ({ user }: any) => {
} }
}); });
return <img src={avatar} alt="avatar" className='rounded-full w-16 h-16 cursor-pointer' />; return <div className='avatar'><img src={avatar} alt="avatar" className='rounded-full w-16 h-16 cursor-pointer' /></div>;
}; };
export default Avatar; export default Avatar;

View File

@ -35,7 +35,7 @@ const Message = ({ text = '', user, date, image = '' }: any) => {
<> <>
<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">
{user && <Avatar user={user} />} {user && <Avatar user={user} />}
<div className="flex flex-col gap-2 relative"> <div className="flex flex-col gap-2 relative flex-grow">
<div className="flex justify-between"> <div className="flex justify-between">
<div className="text-red-light text-xl username"> <div className="text-red-light text-xl username">
{user.firstName} {user.lastName} {user.firstName} {user.lastName}

View File

@ -1,25 +1,34 @@
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { useState } from 'react'; import { useState } from 'react';
import { Cookies } from 'react-cookie'; import { Cookies } from 'react-cookie';
import { FaPlus } from 'react-icons/fa';
const sendMessage = async (message: string) => { const sendMessage = async (data: FormData) => {
const token = new Cookies().get('token'); const token = new Cookies().get('token');
let contentType = 'application/json';
let body: FormData | string | undefined = undefined;
console.log(data.get('image'));
if (data.get('image')?.name !== "") {
contentType = 'multipart/form-data';
body = data;
} else {
body = JSON.stringify(data);
}
const response = await fetch('/api/posts/new', { const response = await fetch('/api/posts/new', {
method: 'POST', method: 'POST',
mode: 'cors', mode: 'cors',
headers: { headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`, Authorization: `Bearer ${token}`,
}, },
body: JSON.stringify({ content: message }), body,
}); });
return response.json(); return response.json();
}; };
const NewMessage = () => { const NewMessage = () => {
const [message, setMessage] = useState(''); const [message, setMessage] = useState('');
const [image, setImage] = useState('');
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const { mutate: send } = useMutation(sendMessage, { const { mutate: send } = useMutation(sendMessage, {
@ -34,19 +43,32 @@ const NewMessage = () => {
const handleSubmit = (e: React.FormEvent) => { const handleSubmit = (e: React.FormEvent) => {
e.preventDefault(); e.preventDefault();
send(message); const data = new FormData(e.target as HTMLFormElement);
send(data);
setMessage(''); setMessage('');
setImage('');
}; };
return ( return (
<footer className="new-message bg-grey-dark rounded-xl w-full max-w-3xl p-3 gap-5 shadow-md shadow-grey-dark -mt-2"> <footer className="new-message bg-grey-dark rounded-xl w-full max-w-3xl p-3 gap-5 shadow-md shadow-grey-dark -mt-2">
<form onSubmit={handleSubmit} className="flex gap-2"> <form onSubmit={handleSubmit} className="flex gap-2">
<div className="file">
<label htmlFor="image" className="cursor-pointer block p-2">
<div className="rounded-full text-grey-dark bg-red-light text-lg p-2">
<FaPlus className="" />
</div>
</label>
<input type="file" name="image" id="image" accept="image/*" className="hidden" value={image} onChange={e => setImage(e.target.value)} />
</div>
<input <input
type="text" type="text"
value={message} value={message}
onChange={(e) => setMessage(e.target.value)} onChange={(e) => setMessage(e.target.value)}
placeholder="Message" placeholder="Message"
className="bg-grey-dark text-white rounded-xl p-2.5 w-full placeholder-red-light" className="bg-grey-dark text-white rounded-xl p-2.5 w-full placeholder-red-light"
id='content'
name='content'
/> />
<button <button
type="submit" type="submit"

View File

@ -11,6 +11,10 @@ export default defineConfig({
target: 'http://localhost:5000', target: 'http://localhost:5000',
changeOrigin: true, changeOrigin: true,
}, },
'/uploads': {
target: 'http://localhost:5000',
changeOrigin: true,
},
}, },
}, },
resolve: { resolve: {