Newer
Older
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
import { useEffect, useState } from 'react';
import { Navigate, Link } from 'react-router-dom';
// COMPOSANTS
import Navbar from '../components/Navbar';
// AUTRES FICHIERS
import useAppDispatch from '../hooks/useAppDispatch';
import usePost from '../hooks/usePostItems';
import { addPost } from '../redux/post/thunks';
const AddPostView = () => {
const dispatch = useAppDispatch();
const [selectedImg, setSelectedImg] = useState<File[]>([]);
const [location, setLocation] = useState("");
const [caption, setCaption] = useState("");
const [redirect, setRedirect] = useState(false);
const post = usePost().items || { id: -1 }; // Initialisation de post avec un objet par défaut
useEffect(() => {
post.id = -1;
}, []);
useEffect(() => {
if (post.id !== -1) {
setRedirect(true);
}
}, [post.id]);
return (
<>
{/* HEADER */}
<Navbar />
{/* MODAL */}
<div className="max-w-[640px] mx-auto p-6 border rounded-xl mt-10 bg-white flex flex-col">
<p className="text-center font-bold text-2xl">Créer un post</p>
<form className="flex flex-col gap-4 mt-6">
{/* IMAGE */}
<div className="flex flex-col gap-1">
<label htmlFor="image-upload" className="font-bold">Ajouter une image :</label>
<input type="file" accept="image/*" id="image-upload" onChange={(test)=> {
if (test.target.files) {
setSelectedImg(selectedImg.concat(test.target.files[0]));
}
}}
/>
</div>
{/* LOCATION */}
<div className="flex flex-col gap-1">
<label htmlFor="location-input" className="font-bold">Lieu :</label>
<input className="h-10 p-2 rounded-sm border" id="location-input" placeholder="Lieu" value={location} onChange={(e)=>
setLocation(e.target.value)} />
</div>
{/* DESCRIPTION */}
<div className="flex flex-col gap-1">
<label htmlFor="caption-input" className="font-bold">Description :</label>
<input className="h-10 p-2 rounded-sm border" id="caption-input" placeholder="Description" value={caption} onChange={(e)=>
setCaption(e.target.value)} />
</div>
{/* BTNS */}
<div className="grid grid-cols-2 gap-4 mt-6">
{/* CANCEL BTN */}
<Link to="/feed">
<button
className="bg-red-600 text-white font-bold h-10 rounded-md mt-2 uppercase w-full">Annuler</button>
</Link>
{/* SUBMIT BTN */}
<button className="bg-green-700 text-white font-bold h-10 rounded-md mt-2 uppercase w-full"
onClick={(test)=> {
dispatch(addPost(selectedImg, location, caption)); //ajouter les autres éléments (accessibilityCaption, hasCommentsDisabled) ?
}}
>
Envoyer
</button>
</div>
</form>
{redirect &&
<Navigate to={'post/' + post.id} replace={true} />}
</div>
</>
);
};
export default AddPostView;