Trying to get a response when clicking and store the selected option from the selected value genre, not sure how to add a select new state variable or a post method. When the option is clicked the click handler is not called with the selected options so that we can add it to the state. My genre is also seen as undefined when I don't click on the the text type.
Feedback.js
import React, { useState } from "react";
import axios from "axios";
import { ToastContainer, toast } from "react-toastify";
import "react-toastify/dist/ReactToastify.min.css";
import Layout from "./Layout";
const Feedback = () => {
const [values, setValues] = useState({
name: "",
artist: "",
songTitle: "",
email: "",
message: "",
phone: "",
genre: "",
uploadedFiles: [],
buttonText: "Submit",
uploadPhotosButtonText: "Upload files",
});
// destructure state variables
const {
name,
artist,
songTitle,
label,
email,
message,
phone,
genre,
uploadedFiles,
buttonText,
uploadPhotosButtonText,
} = values;
// destructure env variables
const {
REACT_APP_API,
REACT_APP_CLOUDINARY_CLOUD_NAME,
REACT_APP_CLOUDINARY_UPLOAD_SECRET,
} = process.env;
// event handler
const handleChange = (name) => (event) => {
setValues({ ...values, [name]: event.target.value });
};
const handleSubmit = (event) => {
event.preventDefault();
setValues({ ...values, buttonText: "...sending" });
// send to backend for email
console.table({ name, email, phone, message, uploadedFiles, genre });
axios({
method: "POST",
url: `${REACT_APP_API}/feedback`,
data: {
name,
artist,
songTitle,
label,
email,
genre,
phone,
message,
uploadedFiles,
},
})
.then((response) => {
// console.log('feedback submit response', response);
if (response.data.success) toast.success("Thanks for your feedback");
setValues({
...values,
name: "",
artist: "",
songTitle: "",
label: "",
phone: "",
email: "",
genre: "",
message: "",
uploadedFiles: [],
buttonText: "Submitted",
uploadPhotosButtonText: "Uploaded",
});
})
.catch((error) => {
// console.log('feedback submit error', error.response);
if (error.response.data.error) toast.error("Failed! Try again!");
});
};
function onChangeInput(value) {
console.log(value);
}
const uploadWidget = () => {
window.cloudinary.openUploadWidget(
{
cloud_name: REACT_APP_CLOUDINARY_CLOUD_NAME,
upload_preset: REACT_APP_CLOUDINARY_UPLOAD_SECRET,
tags: ["ebooks"],
},
function (error, result) {
// console.log(result);
setValues({
...values,
uploadedFiles: result,
uploadPhotosButtonText: `${
result ? result.length : 0
} Photos uploaded`,
});
}
);
};
const feedbackForm = () => (
<React.Fragment>
<div className="form-group">
<button
onClick={() => uploadWidget()}
className="btn btn-outline-secondary btn-block p-5"
>
{uploadPhotosButtonText}
</button>
</div>
<form onSubmit={handleSubmit}>
<div className="form-group">
<label className="text-muted">Description</label>
<textarea
onChange={handleChange("message")}
type="text"
className="form-control"
value={message}
required
></textarea>
</div>
<div className="form-group">
<label className="text-muted">Your Name</label>
<input
className="form-control"
type="text"
onChange={handleChange("name")}
value={name}
required
/>
</div>
<div className="form-group">
<label className="text-muted">Your Email</label>
<input
className="form-control"
type="email"
onChange={handleChange("email")}
value={email}
required
/>
</div>
<div className="form-group">
<label className="text-muted">Your Phone</label>
<input
className="form-control"
type="number"
onChange={handleChange("phone")}
value={phone}
required
/>
</div>
<div className="form-group">
<label className="text-muted">Artist Name</label>
<input
className="form-control"
type="text"
onChange={handleChange("artist")}
value={artist}
required
/>
</div>
<div className="form-group">
<label className="text-muted">Song Title</label>
<input
className="form-control"
type="text"
onChange={handleChange("songTitle")}
value={songTitle}
required
/>
</div>
<div className="form-group">
<label className="text-muted">Record Label Name</label>
<input
className="form-control"
type="text"
onChange={handleChange("label")}
value={label}
/>
</div>
<div className="form-group">
<label className="text-muted">Music Genre:</label>
<select
className="form-control"
value={genre}
type="select"
onChange={handleChange("genre")}
>
<option value="steak">Steak</option>
<option value="sandwich">Sandwich</option>
<option value="dumpling">Dumpling</option>
</select>
</div>
<button className="btn btn-outline-primary btn-block">
{buttonText}
</button>
</form>
</React.Fragment>
);
return (
<Layout>
<ToastContainer />
<div className="container text-center">
<h1 className="p-5">Feedback Online</h1>
</div>
<div className="container col-md-8 offset-md-2">{feedbackForm()}</div>
<br />
<br />
<br />
</Layout>
);
};
export default Feedback;
Related
Im trying to display my data in diffrent Form inputs, but in some of them, i dont know how to reach my data.
//DashShowDetails.js:
import { useState, useEffect } from 'react';
import { Button, Form } from 'react-bootstrap';
import { Input, Label } from 'reactstrap';
function DashShowDetails(props) {
const { data } = props;
const [edit, setEdit] = useState(false);
const [formData, setFormData] = useState({
id: data._id,
showId: data.showId,
showName: data.name,
showGenres: data.genres,
showStatus: data.status,
showPremiered: data.premiered,
showEnded: data.ended,
showRating: data.rating,
showSummery: data.summary,
showImage: data.image,
});
const onClick = () => {
setEdit(current => !current)
}
const onChange = (e) => {
setFormData((prevState) => ({
...prevState,
[e.target.name]: e.target.value
}))
}
const onSubmit = async (e) => {
e.preventDefault()
let show = formData;
dispatch(updateDashShow(show));
setEdit(current => !current);
setFormData((prevState) => ({
...prevState,
}))
}
useEffect(() => {
}, [edit]);
return (
<div>
<div>
<h5>{data.name}</h5>
<Button onClick={onClick}>Edit Show</Button>
</div>
<div>
{edit === true && (
<div>
<h3>Edit {data.name}</h3>
<Form onSubmit={onSubmit} >
<div>
<Label>Show ID:</Label>
<Input type="text" name="showId" value={data.showId} onChange={onChange} />
</div>
<div>
<Label>Show Name:</Label>
<Input type="text" name="showName" defaultValue={data.name} onChange={onChange} />
</div>
<div>
<Label>Show Genres:</Label>
<Input type="text" name="showGenres" defaultValue={data.genres} onChange={onChange} />
</div>
<div>
<Label>Show Status:</Label>
<Input type="select" name="showStatus" select={data.status} onChange={onChange}>
<option value="running">Running</option>
<option value="ended">Ended</option>
</Input>
</div>
<div>
<Label>Premiered Date:</Label>
<Input type="date" name="showPremiered" value={data.premiered} onChange={onChange} />
</div>
<div>
<Label>Ended Date:</Label>
<Input type="date" name="showEnded" value={data.ended} onChange={onChange} />
</div>
<div>
<Label>Show Rating:</Label>
<Input type="array" name="showRating" value={data.rating} onChange={onChange} />
</div>
<div>
<Label>Show Summery:</Label>
<Input type="textarea" name="showSummery" value={data.summery} onChange={onChange} />
</div>
<div>
<Label>Image:</Label>
</div>
<Button type="submit">Update Show</Button>
<Button>Delete Show</Button>
</Form>
</div>
)}
</div>
</div>
);
}
export default DashShowDetails;
My output when i click 'Edit Show' is:
How can i display my Data in inputs: Premiered Date, Show Rating, Show Summery & Image?
My Schema file in Node.js: (sending new data to MoongoDB)
const showSchema = new mongoose.Schema({
showId: {type: Number},
name: {type: String},
genres: {type: Array},
status: {type: String},//Boolean
premiered: {type: Date},
ended: {type: Date},
rating: {type: Array},
summary: {type: String},
image: {type: Array},//Link?File?
})
module.exports = mongoose.model('shows', showSchema);
I am a new learner in react, I tried a simple login form which takes user input and passes it to backend. This application is also written to create tokens if the login credentials are correct. In order to take user input I have added the onChange() function and called it for each input field, however the it is still not editable. I couldn't find the error. I also have added onSubmit() function implementation. I did try various other ways of calling the onChange function, however it wasnt successful.
const [formData, setFormData] = useState({
clientEmail: "",
clientPassword: "",
errorMsg: false,
loadingSpinner: false,
});
// destructure form data
const { clientEmail, clientPassword, errorMsg, loadingSpinner } = formData;
const handleChange = (evt) => {
setFormData({
...formData,
[evt.target.name]: evt.target.value,
errorMsg: "",
});
};
const handleSubmit = (evt) => {
evt.preventDefault();
//form validation
if (isEmpty(clientEmail) || isEmpty(clientPassword)) {
setFormData({
...formData,
errorMsg: "All field are Required",
});
} else if (!isEmail(clientEmail)) {
setFormData({
...formData,
errorMsg: "Invalid Email new",
});
} else {
const { clientEmail, clientPassword} = formData;
const data = {
clientEmail,
clientPassword,
};
setFormData({
...formData,
loadingSpinner: true,
});
ClientLoginUser(data)
.then((response) => {
console.log(response);
setClientAuthentication(response.data.token, response.data.clients);
if (isClientAuthenticated()) {
console.log("client Successful");
history.push("./clientDashboard");
}
})
.catch((err) => {
console.log("client login api controller error: ", err);
setFormData({
...formData,
errorMsg:err.response.data.errorMessage,
loading:false
});
});
}
};
const showLoginForm = () => (
<Fragment>
<div className="card px-5 py-5">
<div className="card-body">
<h5 className="card-title text-center pb-3">Client Login</h5>
<form className="login-form"
onSubmit={handleSubmit}
noValidate>
{/* Email */}
<div className="form-group">
<input
type="email"
className="form-control"
name="email"
value={clientEmail}
placeholder="Email"
onChange={handleChange}
/>
</div>
{/* Password */}
<div className="form-group">
<input
type="password"
className="form-control"
name="password"
value={clientPassword}
placeholder="Password"
onChange={handleChange}
/>
</div>
{/* Submit button */}
<div className="form-group pt-3">
<button
type="submit"
className="btn btn-primary btn-lg btn-block"
>
Login
</button>
</div>
</form>
</div>
</div>
</Fragment>
);
/****************************
* Render
****************************/
return (
<div className="login-container">
<GridWrapper>
<div className="container-fluid">
<div className="row px-4 vh-100">
<div className="col-12 col-md-8 my-auto pl-5">
<img
src="/images/welcomeLogo.png"
className="img-fluid"
alt="Logo"
/>
</div>
<div className="col-12 col-md-3 align-self-center">
{errorMsg && showErrorMsg(errorMsg)}
{loadingSpinner && (
<div className="text-center pb-5">{showLoading()}</div>
)}
{showLoginForm()}
</div>
</div>
</div>
</GridWrapper>
</div>
);
};
// return <p>ClientLogin Component</p>;
The name in input is not exactly the same as in formData state
you got this state
const [formData, setFormData] = useState({
clientEmail: "",
clientPassword: "",
errorMsg: false,
loadingSpinner: false,
});
which contain data like clientEmail
<input
type="email"
className="form-control"
name="email"
value={clientEmail}
placeholder="Email"
onChange={handleChange}
/>
which contains name="email"
in handleChange Function
const handleChange = (evt) => {
setFormData({
...formData,
[evt.target.name]: evt.target.value,
errorMsg: "",
});
};
you contains [evt.target.name]: evt.target.value
thats mean you are trying to assign new Values to formData.email not to formData.clientEmail
there are two solution
first you can change
const [formData, setFormData] = useState({
email: "",
...
});
or you can
<input
type="email"
className="form-control"
name="clientEmail"
value={clientEmail}
placeholder="Email"
onChange={handleChange}
/>
<div>
<label>Last Name</label>
<input
type="text"
name="lastName"
defaultValue={userData.lastName}
onChange={(ev) => this.setState({ lastName: ev.target.value })}
/>
</div>
I have some input fields on the screen.When the page loads first time , I am displaying readonly values from the redux store.On click of edit button , I want fields to become editable with the existing values.( i have state which corresponds to each field of the form).DefaultValue is from redux store .but onClick of save of new fields , I am only getting vlaues for fields which I have modified ,otherfields coming as blank.Any help pls?
/* eslint-disable react/destructuring-assignment */
/* eslint-disable react/forbid-prop-types */
/* eslint-disable no-nested-ternary */
import React, { Component } from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { eventsActions } from '../../store/actions/index';
class UserAccount extends Component {
//console.log(this.props);
constructor(props) {
console.log('props --- ', props.userData);
super();
this.state = {
username: props.userData.username,
firstName: props.userData.firstName,
lastName: props.userData.lastName,
// profilePic: '',
email: props.userData.email,
addressUnit: props.userData.addressUnit,
addressStreet: props.userData.addressStreet,
addressCity: props.userData.addressCity,
addressZip: props.userData.addressZIP,
addressState: props.userData.addressState,
isEditing: false,
};
this.toggleEdit = this.toggleEdit.bind(this);
}
componentDidMount() {
const { loadUserDetails } = this.props;
loadUserDetails();
}
toggleEdit() {
console.log('toggleEdit is called here ----');
this.setState({ isEditing: !this.state.isEditing });
}
handleChange = (event) => {
console.log('event ---- ', event);
this.setState({ [event.target.name]: event.target.value });
};
updateUser = (e) => {
console.log('updateUser is called ---');
e.preventDefault();
const {
username,
firstName,
lastName,
email,
addressUnit,
addressStreet,
addressCity,
addressZip,
addressState,
} = this.state;
const { editUser } = this.props;
console.log('for updating user --- ', this.state);
editUser(this.state);
};
render() {
const { userData } = this.props;
const {
username,
firstName,
lastName,
email,
addressUnit,
addressStreet,
addressCity,
addressZip,
addressState,
isEditing,
} = this.state;
console.log('userData --- ', userData);
console.log('isEditing ---- ', isEditing);
return (
<div>
{isEditing ? (
<div>
<div>
<label>First Name</label>
<input
type="text"
name="firstName"
// ref={firstName}
value={firstName}
onChange={this.handleChange}
/>
</div>
<div>
<label>Last Name</label>
<input
type="text"
name="lastName"
defaultValue={userData.lastName}
onChange={(ev) => this.setState({ lastName: ev.target.value })}
/>
</div>
<div>
<label>Email</label>
<input
type="text"
name="email"
value={userData.email}
onChange={(ev) => this.setState({ email: ev.target.value })}
/>
</div>
<div>
<label>UserName</label>
<input
type="text"
name="username"
value={userData.username}
onChange={(ev) => this.setState({ username: ev.target.value })}
/>
</div>
<div>
<label>Address Unit</label>
<input
type="text"
name="addressUnit"
value={userData.addressUnit}
onChange={(ev) =>
this.setState({ addressUnit: ev.target.value })
}
/>
</div>
<div>
<label>Address Street</label>
<input
type="text"
name="addressStreet"
value={userData.addressStreet}
onChange={(ev) =>
this.setState({ addressStreet: ev.target.value })
}
/>
</div>
<div>
<label>Address City</label>
<input
type="text"
name="addressCity"
value={userData.addressCity}
onChange={(ev) =>
this.setState({ addressCity: ev.target.value })
}
/>
</div>
<div>
<label>Address State</label>
<input
type="text"
name="addressState"
value={userData.addressState}
onChange={(ev) =>
this.setState({ addressState: ev.target.value })
}
/>
</div>
<div>
<label>Address Zip</label>
<input
type="text"
name="addressZip"
value={userData.addressZIP}
onChange={(ev) =>
this.setState({ addressZip: ev.target.value })
}
/>
</div>
<button type="submit" onClick={this.updateUser}>
Save
</button>
</div>
) : (
<div>
<form>
<div>
<label>First Name:</label>
<label> {userData.firstName}</label>
</div>
<div>
<label>Last Name:</label>
<label> {userData.lastName}</label>
</div>
<div>
<label>Email:</label>
<label> {userData.email}</label>
</div>
<div>
<label>UserName:</label>
<label> {userData.username}</label>
</div>
<div>
<label>Address Unit:</label>
<label> {userData.addressUnit}</label>
</div>
<div>
<label>Address Street:</label>
<label> {userData.addressStreet}</label>
</div>
<div>
<label>Address City:</label>
<label> {userData.addressCity}</label>
</div>
<div>
<label>Address State:</label>
<label> {userData.addressState}</label>
</div>
<div>
<label>Address Zip:</label>
<label> {userData.addressZIP}</label>
</div>
<button type="submit" onClick={this.toggleEdit}>
edit
</button>
</form>
</div>
)}
</div>
);
}
}
const mapStateToProps = (state) => {
console.log('state in UserProfile in mapStatetoProps --- ', state);
return { userData: state.events.user };
};
const mapDispatchToProps = (dispatch) => {
return {
loadUserDetails: (userId) => {
console.log('disptach is called for loadUserDetails --');
dispatch(eventsActions.fetchUserDetails(userId));
},
};
};
UserAccount.propTypes = {
loadUserDetails: PropTypes.func.isRequired,
userData: PropTypes.object.isRequired,
};
export default connect(mapStateToProps, mapDispatchToProps)(UserAccount);
You could e.g. initialize your state with values from redux store.
state = {
lastName: this.props.userData.lastName,
firstName: this.props.userData.lastName,
}; // and so on
Then, even if not modified, state will store the correct data.
Note: Remember to add value prop to the input:
<input onChange={} value={this.state.lastName} />
If your data comes from async request:
componentDidUpdate(prevProps) {
if (!prevProps.userData && this.props.userData){
this.setState({ firstName: this.props.userData.firstName });
}
}
The below code is to update a product in admin panel. I have manage to do everything except that on a button pressed for update i want to fetch the url from the above address bar. I thought of using match.params.productId. Here produtId is what i wanted ti get. But i dont know why the match object returns undefined..PLease help me to get rid of this.
import React, { useState, useEffect } from "react";
import Base from "../core/Base";
import { Link } from "react-router-dom";
import {
getAllCategories,
getProduct,
updateProduct
} from "./helper/adminapicall";
import { isAuthenticated } from "../auth/helper/index";
const UpdateProduct = ({ match }) => {
const { user, token } = isAuthenticated();
const [values, setValues] = useState({
name: "",
description: "",
price: "",
stock: "",
photo: "",
categories: [],
category: "",
loading: false,
error: "",
createdProduct: "",
getaRedirect: false,
formData: ""
});
const {
name,
description,
price,
stock,
categories,
category,
loading,
error,
createdProduct,
getaRedirect,
formData
} = values;
const preload = (productId) => {
getProduct(productId).then((data) => {
console.log(data);
if (data.error) {
setValues({ ...values, error: data.error });
} else {
preloadCategories();
setValues({
...values,
name: data.name,
description: data.description,
price: data.price,
category: data.category._id,
stock: data.stock,
formData: new FormData()
});
}
});
};
const preloadCategories = () => {
getAllCategories().then((data) => {
if (data.error) {
setValues({ ...values, error: data.error });
} else {
setValues({
categories: data,
formData: new FormData()
});
}
});
};
useEffect(() => {
//console.log(match);
preload(match.params.productId);
}, []);
//TODO: work on it
const onSubmit = (event) => {
event.preventDefault();
setValues({ ...values, error: "", loading: true });
updateProduct(match.params.productId, user._id, token, formData).then(
(data) => {
if (data.error) {
setValues({ ...values, error: data.error });
} else {
setValues({
...values,
name: "",
description: "",
price: "",
photo: "",
stock: "",
loading: false,
createdProduct: data.name
});
}
}
);
};
const handleChange = (name) => (event) => {
const value = name === "photo" ? event.target.files[0] : event.target.value;
formData.set(name, value);
setValues({ ...values, [name]: value });
};
const successMessage = () => (
<div
className="alert alert-success mt-3"
style={{ display: createdProduct ? "" : "none" }}
>
<h4>{createdProduct} updated successfully</h4>
</div>
);
const createProductForm = () => (
<form>
<span>Post photo</span>
<div className="form-group">
<label className="btn btn-block btn-success">
<input
onChange={handleChange("photo")}
type="file"
name="photo"
accept="image"
placeholder="choose a file"
/>
</label>
</div>
<div className="form-group">
<input
onChange={handleChange("name")}
name="photo"
className="form-control"
placeholder="Name"
value={name}
/>
</div>
<div className="form-group">
<textarea
onChange={handleChange("description")}
name="photo"
className="form-control"
placeholder="Description"
value={description}
/>
</div>
<div className="form-group">
<input
onChange={handleChange("price")}
type="number"
className="form-control"
placeholder="Price"
value={price}
/>
</div>
<div className="form-group">
<select
onChange={handleChange("category")}
className="form-control"
placeholder="Category"
>
<option>Select</option>
{categories &&
categories.map((cate, index) => (
<option key={index} value={cate._id}>
{cate.name}
</option>
))}
</select>
</div>
<div className="form-group">
<input
onChange={handleChange("stock")}
type="number"
className="form-control"
placeholder="Stock"
value={stock}
/>
</div>
<button
type="submit"
onClick={onSubmit}
className="btn btn-outline-success mb-3"
>
Update Product
</button>
</form>
);
return (
<Base
title="Add a product here!"
description="Welcome to product creation section"
className="container bg-info p-4"
>
<Link to="/admin/dashboard" className="btn btn-md btn-dark mb-3">
Admin Home
</Link>
<div className="row bg-dark text-white rounded">
<div className="col-md-8 offset-md-2">
{successMessage()}
{createProductForm()}
</div>
</div>
</Base>
);
};
export default UpdateProduct;
I have list of input tags in my react app. I want to get the value of all inputs when I click on submit button.
I can get the value of one input, but not able to get all the values in one go.
keys.map(i => <Input fullWidth defaultValue={i} />)
<Button>Submit</Button>
get the list of values
Sample application for multiple inputs
const App = () => {
const [people, setPeople] = React.useState([
{ firstName: "Herry", lastName: "Sejal" },
{ firstName: "John", lastName: "Siri" },
{ firstName: "Bukky", lastName: "Sera" }
]);
const [person, setPerson] = React.useState({ firstName: "", lastName: "" });
const onChange = e => {
setPerson({ ...person, [e.target.name]: e.target.value });
};
const onSubmit = e => {
e.preventDefault();
const newVal = [...people, person];
setPeople(newVal);
setPerson({ firstName: "", lastName: "" });
};
return (
<div className="container">
<div className="row">
<div className="col">
<h2>Add a person: </h2>
<hr />
<form onSubmit={onSubmit}>
<div className="form-group">
<input
type="text"
className="form-control"
name="firstName"
placeholder="First Name.."
value={person.firstName}
onChange={onChange}
/>
</div>
<div className="form-group">
<input
type="text"
className="form-control"
name="lastName"
placeholder="Last Name.."
value={person.lastName}
onChange={onChange}
/>
</div>
<button className="btn btn-success" type="submit">
Add person
</button>
</form>
</div>
<div className="col">
<h2>People: </h2>
<hr />
{people.map(p => (
<div key={Math.random() * 1000000000}>
<p>
{p.firstName} {p.lastName}
</p>
</div>
))}
</div>
</div>
</div>
);
};