diff --git a/code/frontend/src/pages/postCreation/PostCreation.tsx b/code/frontend/src/pages/postCreation/PostCreation.tsx index f2b037e..794d3c2 100644 --- a/code/frontend/src/pages/postCreation/PostCreation.tsx +++ b/code/frontend/src/pages/postCreation/PostCreation.tsx @@ -1,6 +1,13 @@ import { useState, useEffect, useRef } from "react"; import { useNavigate } from "react-router-dom"; -import { Box, Card, CardMedia, CardActionArea, IconButton, Typography } from "@mui/material"; +import { + Box, + Card, + CardMedia, + CardActionArea, + IconButton, + Typography, +} from "@mui/material"; import Chip from "@mui/material/Chip"; import Autocomplete from "@mui/material/Autocomplete"; import TextField from "@mui/material/TextField"; @@ -12,258 +19,306 @@ import ButtonPrimary from "../../components/buttons/buttonRotkehlchen/ButtonRotk import "./postCreation.css"; const theme = createTheme({ - palette: { - primary: { - main:'#e79a0e;' - } - } + palette: { + primary: { + main: "#e79a0e;", + }, + }, }); -function PostCreation(){ - const {user} = useAuth(); - interface ImageItem { - src: string; - title: string; - } - const [options, setOptions] = useState([]); - const [tags, setTags] = useState([]); - const [description, setDescription] = useState(""); - const [selectedImage, setSelectedImage] = useState(); +function PostCreation() { + const { user } = useAuth(); + interface ImageItem { + src: string; + title: string; + } + const [isSend, setIsSend] = useState(false); + const [options, setOptions] = useState([]); + const [tags, setTags] = useState([]); + const [description, setDescription] = useState(""); + const [selectedImage, setSelectedImage] = useState(); - const [data, setData] = useState([]); - const navigate = useNavigate(); + const [data, setData] = useState([]); + const navigate = useNavigate(); - const [fileList,setFileList] = useState([]); - const inputFile = useRef(null); + const [fileList, setFileList] = useState([]); + const inputFile = useRef(null); - useEffect(() => { - let cancelled = false; - (async () => { - try { - const res = await api.get("/posts/tags"); // GET /api/posts/tags - if (!cancelled) setOptions(res.data); - } catch (err) { - console.log(err); - } - })(); - return () => { cancelled = true; }; - }, []); + useEffect(() => { + let cancelled = false; + (async () => { + try { + const res = await api.get("/posts/tags"); // GET /api/posts/tags + if (!cancelled) setOptions(res.data); + } catch (err) { + console.log(err); + } + })(); + return () => { + cancelled = true; + }; + }, []); - const handleChange = (event: React.ChangeEvent) => { - setDescription(event.target.value); - }; + const handleChange = (event: React.ChangeEvent) => { + setDescription(event.target.value); + }; - const handleTags = (event: any, newValue: string[]) => { - newValue.forEach((val) => { - if (!options.includes(val)) { - setOptions((prev) => [...prev, val]); - } - }); - setTags(newValue); - }; + const handleTags = (event: any, newValue: string[]) => { + newValue.forEach((val) => { + if (!options.includes(val)) { + setOptions((prev) => [...prev, val]); + } + }); + setTags(newValue); + }; - const handleImageUpload = (event: React.ChangeEvent) => { - const files = event.target.files; - if (files && files.length > 0) { - const fileArr = Array.from(files); - const newItems: ImageItem[] = Array.from(files).map((file) => ({ - src: URL.createObjectURL(file), - title: file.name, - } - )); - - const lastImageUrl = newItems[newItems.length - 1].src; + const handleImageUpload = (event: React.ChangeEvent) => { + const files = event.target.files; + if (files && files.length > 0) { + const fileArr = Array.from(files); + const newItems: ImageItem[] = Array.from(files).map((file) => ({ + src: URL.createObjectURL(file), + title: file.name, + })); - setData((prev) => [...prev, ...newItems]); - setSelectedImage(lastImageUrl); - setFileList(prev => [...prev, ...fileArr]); - } - }; - const onEmptyImgClick = () =>{ - if(inputFile.current){ - inputFile.current?.click(); - } - } - const handleDelete = (idx: number) =>{ - setData((prev) => prev.filter((_, i) => i !== idx)); - if((idx-1)<0){ - if(data.length == 1){setSelectedImage("");} - else{setSelectedImage(data[idx+1].src);} - } - else {setSelectedImage(data[idx-1].src);} - } + const lastImageUrl = newItems[newItems.length - 1].src; - const onSubmit = async(event: React.FormEvent) =>{ - event.preventDefault(); - if (!fileList) { - return; - } - const fData = new FormData(); - files.forEach((file, i) => { - fData.append(`images`, file, file.name); - }); - fData.append('status','PUBLIC'); - fData.append('description', description); - tags.forEach((tag) => { - fData.append("tags" ,tag); - }) + setData((prev) => [...prev, ...newItems]); + setSelectedImage(lastImageUrl); + setFileList((prev) => [...prev, ...fileArr]); + } + }; + const onEmptyImgClick = () => { + if (inputFile.current) { + inputFile.current?.click(); + } + }; + const handleDelete = (idx: number) => { + setData((prev) => prev.filter((_, i) => i !== idx)); + if (idx - 1 < 0) { + if (data.length == 1) { + setSelectedImage(""); + } else { + setSelectedImage(data[idx + 1].src); + } + } else { + setSelectedImage(data[idx - 1].src); + } + }; - try { - await api.post("/posts/upload", fData) - navigate(`/profile/${user?.username}`) - } catch (error:any) { - console.log(error); - } - }; - const onCancel= () => { - navigate(`/profile/${user?.username}`) - }; + const onSubmit = async (event: React.FormEvent) => { + if (!isSend) { + setIsSend(true); - const files = fileList ? [...fileList] : []; + event.preventDefault(); + if (!fileList) { + return; + } + const fData = new FormData(); + files.forEach((file, i) => { + fData.append(`images`, file, file.name); + }); + fData.append("status", "PUBLIC"); + fData.append("description", description); + tags.forEach((tag) => { + fData.append("tags", tag); + }); - return( - -
-
-
-

Create Post

-
-
- {selectedImage? - Add an Image: - } -
-
-

Description*

- -
- *': { scrollSnapAlign: 'center' }, - '::-webkit-scrollbar': { display: 'none' }, - }} - > - {/* Upload card */} - - - + try { + await api.post("/posts/upload", fData); + navigate(`/profile/${user?.username}`); + } catch (error: any) { + console.log(error); + } + } + }; + const onCancel = () => { + navigate(`/profile/${user?.username}`); + }; - {/* Image cards */} - {data.map((item, idx) => ( - setSelectedImage(item.src)} - > - {/* Delete button */} - { - e.stopPropagation(); - handleDelete(idx); - }} - sx={{ - position: 'absolute', - top: 2, - right: 2, - backgroundColor: 'background.paper', - zIndex: 1, - }} - > - - + const files = fileList ? [...fileList] : []; - - - - - ))} - + return ( + +
+
+ +

Create Post

+
+
+ {selectedImage ? ( + Add an Image + ) : ( + + )} +
+
+

Description*

+ +
+ *": { scrollSnapAlign: "center" }, + "::-webkit-scrollbar": { display: "none" }, + }} + > + {/* Upload card */} + + + - - value.map((tags: string, index: number) => { - const { key, ...itemProps } = getItemProps({ index }); - return ( - - ); - }) - } - renderInput={(params) => ( - - )} - /> -
- - -
-
- -
-
-
- ); + {/* Image cards */} + {data.map((item, idx) => ( + setSelectedImage(item.src)} + > + {/* Delete button */} + { + e.stopPropagation(); + handleDelete(idx); + }} + sx={{ + position: "absolute", + top: 2, + right: 2, + backgroundColor: "background.paper", + zIndex: 1, + }} + > + + + + + + + + ))} + + + + value.map((tags: string, index: number) => { + const { key, ...itemProps } = getItemProps({ index }); + return ( + + ); + }) + } + renderInput={(params) => ( + + )} + /> +
+ + +
+
+ +
+
+
+ ); } -export default PostCreation; \ No newline at end of file +export default PostCreation;