diff --git a/src/app/components/NewPostForm.tsx b/src/app/components/NewPostForm.tsx new file mode 100644 index 0000000..bc56a49 --- /dev/null +++ b/src/app/components/NewPostForm.tsx @@ -0,0 +1,97 @@ +'use client'; + +import { UserContext } from "@/lib/context"; +import { db, storage } from "@/lib/firebase"; +import { addDoc, collection } from "firebase/firestore"; +import { getDownloadURL, ref, uploadBytesResumable } from "firebase/storage"; +import { useContext, useState } from "react"; +import { Hash } from "crypto"; +import toast from "react-hot-toast"; + +export default function NewPostForm() { + const [formValuePostTitle, setFormValuePostTitle] = useState(''); + const [_isValid, setIsValidPostTitle] = useState(false); + const [_loading, _setLoading] = useState(false); + + const { user, username } = useContext(UserContext); + + const handleChangePostTitle = (e: React.ChangeEvent) => { + const val = e.target.value.toLowerCase(); + const regex = /^\w*$/; + if (val.length < 5) { + setIsValidPostTitle(false); + } + if (regex.test(val)) { + setIsValidPostTitle(true); + } + setFormValuePostTitle(val); + } + + const [formValuePostDescription, setFormValuePostDescription] = useState(''); + const [_isValidPostDescription, setIsValidPostDescription] = useState(false); + + const handleChangePostDescription = (e: React.ChangeEvent) => { + const val = e.target.value.toLowerCase(); + if (val.length < 20) { + setIsValidPostDescription(false); + } + setFormValuePostDescription(val); + } + + + const [formValueImages, setFormValueImages] = useState(null); + + const handleChangeImages = (e: React.ChangeEvent) => { + const val = e.target.files; + setFormValueImages(val); + } + + const handleSubmit = async (e: React.FormEvent) => { + e.preventDefault(); + + let images = []; + + if (formValueImages !== null) { + for(let i = 0; i < formValueImages.length; i++) { + const buffer = await formValueImages.item(i)?.arrayBuffer(); + const name = formValueImages.item(i)?.name; + const storageUri = `images/${name}`; + const storageRef = ref(storage, storageUri); + if (buffer !== undefined) { + uploadBytesResumable(storageRef, buffer); + images.push(await getDownloadURL(storageRef)); + } + } + } + + const postRef = await addDoc(collection(db, 'posts'), + {uid: user.uid, post_title: formValuePostTitle, post_description: formValuePostDescription, images} + ); + + toast.success(`Succesfully created new post ID: ${postRef.id}!`); + } + + return ( +
+
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
+
+ ) +} diff --git a/src/app/post/page.tsx b/src/app/post/page.tsx new file mode 100644 index 0000000..d467b47 --- /dev/null +++ b/src/app/post/page.tsx @@ -0,0 +1,35 @@ +'use client'; + +import { signInWithGoogle } from "@/lib/auth"; +import { UserContext } from "../../lib/context"; +import { useContext } from "react"; +import Link from "next/link"; +import NewPostForm from "../components/NewPostForm"; + +export default function Post() { + const { user, username } = useContext(UserContext); + + return ( +
+ {Object.keys(user).length ? + username ? +
+

Create a new Post

+ +
+ : +
+

Please set up the username!

+ + Go username setup + +
+ : +
+

Sign in

+ +
+ } +
+ ); +}