Final finishing touches on Firestore Component

This commit is contained in:
Ray 2023-11-28 08:47:12 +00:00 committed by GitHub
parent df283c8206
commit c67f881305
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 26 additions and 6 deletions

View File

@ -3,20 +3,35 @@ import Button from "react-bootstrap/Button"
import { AuthContext } from "../contexts/AuthContext" import { AuthContext } from "../contexts/AuthContext"
import { FSContext } from "../contexts/FSContext" import { FSContext } from "../contexts/FSContext"
import { useContext, useState } from "react" import { useContext, useState, useEffect } from "react"
import { collection, addDoc } from "firebase/firestore"; import { collection, addDoc } from "firebase/firestore";
const submitHandler = (event) => { const submitHandler = async (event) => {
event.preventDefault() event.preventDefault()
const userReview = {title: title, star: star, body: review }
const col = collection(db, `movies/${props.movieId}/reviews`)
const ref = await addDoc(col, userReview )
console.log( ref )
} }
export function ReviewForm(props) { export function ReviewForm(props) {
const auth = useContext(AuthContext) const auth = useContext(AuthContext)
const db = useContext(FSContext) const db = useContext(FSContext)
const[star, setStar ] = useState() const[star, setStar ] = useState(5)
const[title, setTitle ] = useState() const[title, setTitle ] = useState('')
const[review, setReview ] = useState() const[review, setReview ] = useState('')
const[valid, setValid] = useState(false)
useEffect(() => {
if( title.length > 4 && review.length > 4 ) {
setValid( true )
}
else {
setValid( false )
}
}, [star, title, review])
if (auth) { if (auth) {
return ( return (
<Form onSubmit={submitHandler} className="my-4"> <Form onSubmit={submitHandler} className="my-4">
@ -56,7 +71,12 @@ export function ReviewForm(props) {
onChange={(evt) => setReview(evt.target.value) } onChange={(evt) => setReview(evt.target.value) }
/> />
</Form.Group> </Form.Group>
<Button type="submit" variant="primary"> <Button
type="submit"
variant="primary"
className="mt-2"
disabled={ (valid) ? false: true }
>
Submit Submit
</Button> </Button>
</Form> </Form>