sei-ec-remote / project-4-issues

Open an issue to receive help on project 4
0 stars 0 forks source link

Form isn't allowing input #278

Closed brendinsgit closed 11 months ago

brendinsgit commented 11 months ago

What stack are you using?

(ex: MERN(mongoose + react), DR(django + react), PEN, etc.)

What's the problem you're trying to solve?

trying to type in the form's input area

Post any code you think might be relevant (one fenced block per file)

// FishCreate is going to render a form
// this form will build a fish object in state
// the form will submit an axios POST request when submitted
// I should send a success or failure message
// on success, redirect to the new fish show page
// on failure, component should send the message and remain visible
import { useState } from 'react'
import { createFish } from '../../api/fish'
import { createFishSuccess, createFishFailure } from '../shared/AutoDismissAlert/messages'
import FishForm from '../shared/FishForm'

// to redirect to a different component(page) I can use a hook from react-router
import { useNavigate } from 'react-router-dom'

const FishCreate = (props) => {
    // pull out props for easy reference
    const { user, msgAlert } = props

    // to utilize the navigate hook from react-router-dom
    const navigate = useNavigate()

    const [fish, setFish] = useState({
        species: '',
        size: '',
        location: ''
    })

    const onChange = (e) => {
        // e is the placeholder for event
        e.persist()

        setFish(prevFish => {
            const updatedSpecies = e.target.species
            let updatedValue = e.target.value

            // the above is enough for string inputs
            // but I have a number and a boolean to handle
            if (e.target.type === 'number') {
                // if the target type is a number - updateValue must be a number
                updatedValue = parseInt(e.target.value)
            }

            // build the fish object, grab the attribute species from the field and assign it the respective value.
            const updatedFish = { [updatedSpecies] : updatedValue }

            // keep all the old fish stuff and add the new fish stuff(each keypress)
            return {
                ...prevFish, ...updatedFish
            }
        })
    }

    const onSubmit = (e) => {
        // I'm still using a form - the default behavior of a form is to refresh the page
        e.preventDefault()

        // I'm making an api call here
        // so I want to handle the promise with then and catch
        // first I want to send the create request
        createFish(user, fish)
            // then navigate the user to the show page if successful
            .then(res => { navigate(`/fish/${res.data.fish.id}`)})
            // send a success message
            .then(() => {
                msgAlert({
                    heading: 'Oh Yeah!',
                    message: createFishSuccess,
                    variant: 'success'
                })
            })
            // if it fails, keep the user on the create page and send a message
            .catch(() => {
                msgAlert({
                    heading: 'Oh no!',
                    message: createFishFailure,
                    variant: 'danger'
                })
            })
    }

    return (
        <FishForm 
            fish={fish} 
            handleChange={onChange}
            handleSubmit={onSubmit}
            heading="Add a new fish!"
        />
    )
}

export default FishCreate

If you see an error message, post it here. If you don't, what unexpected behavior are you seeing?

What is your best guess as to the source of the problem?

There's an issue somewhere with my onChange I think

What things have you already tried to solve the problem?

I tried changing the variable information in the onChange function but it didn't work

Paste a link to your repository here

asands94 commented 11 months ago

change your e.target.species to e.target.name. This refers to the name field on your form that all your form inputs have

brendinsgit commented 11 months ago

I still can't enter anything into the form input except for the size. For some reason it lets me type 1 letter 'e' in there

asands94 commented 11 months ago

Show your code for the form

brendinsgit commented 11 months ago
import { Form, Button, Container } from 'react-bootstrap'

const FishForm = (props) => {

    const { fish, handleChange, handleSubmit, heading } = props

    return (
        <Container className="justify-content-center">
            <h3>{heading}</h3>
            <Form onSubmit={handleSubmit}>
                <Form.Group className="m-2">
                    <Form.Label>Species:</Form.Label>
                    <Form.Control 
                        placeholder="What is your fish's species?"
                        id="species"
                        species="species"
                        value={ fish.species }
                        onChange={handleChange}
                    />
                </Form.Group>
                <Form.Group className="m-2">
                    <Form.Label>Location:</Form.Label>
                    <Form.Control 
                        placeholder="Where was this fish caught?"
                        id="location"
                        species="location"
                        value={ fish.location }
                        onChange={handleChange}
                    />
                </Form.Group>
                <Form.Group className="m-2">
                    <Form.Label>Size:</Form.Label>
                    <Form.Control 
                        type="number"
                        placeholder="How big is your fish?"
                        id="size"
                        species="size"
                        value={ fish.size }
                        onChange={handleChange}
                    />
                </Form.Group>
                <Button className="m-2" type="submit">Submit</Button>
            </Form>
        </Container>
    )

}

export default FishForm
asands94 commented 11 months ago

You need to add a name field to your forms. the name needs to be the fields in your use state so name="species" for the species input for example

brendinsgit commented 11 months ago

Ok, that part was a little confusing for me. It's letting me change and the submit button seems to be working except I get a 'something went wrong please try again' error when I click on it. I think the issue must be within the same files but I'm stumped on it to be honest

asands94 commented 11 months ago

Can you see your post or is it not showing up?

brendinsgit commented 11 months ago

It's not creating the fish it's just giving me a failure message

asands94 commented 11 months ago

If you check your backend terminal it should give you some more details on why it's failing