Download TravelYaari-react/src/admin/UpdateProduct.js from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 7.19 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/TravelYaari-react/src/admin/UpdateProduct.js
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/TravelYaari-react/src/admin/UpdateProduct.js
-
curl -L -o UpdateProduct.js https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/1e92f2d0b9e054abc23a53595fd89ad3e2b4a34e/TravelYaari-react/src/admin/UpdateProduct.js
7.19 kB
| import React, { useState, useEffect } from 'react'; | |
| import Layout from '../core/Layout'; | |
| import { isAuthenticated } from '../auth'; | |
| /* eslint-disable no-unused-vars */ | |
| import { Link, Redirect } from 'react-router-dom'; | |
| import { getProduct, getCategories, updateProduct } from './apiAdmin'; | |
| const UpdateProduct = ({ match }) => { | |
| const [values, setValues] = useState({ | |
| name: '', | |
| description: '', | |
| price: '', | |
| categories: [], | |
| youtubelink:'', | |
| category: '', | |
| shipping: '', | |
| quantity: '', | |
| photo: '', | |
| loading: false, | |
| error: false, | |
| createdProduct: '', | |
| redirectToProfile: false, | |
| formData: '' | |
| }); | |
| const [categories, setCategories] = useState([]); | |
| const { user, token } = isAuthenticated(); | |
| const { | |
| name, | |
| description, | |
| price, | |
| // categories, | |
| category, | |
| shipping, | |
| youtubelink, | |
| quantity, | |
| loading, | |
| error, | |
| createdProduct, | |
| redirectToProfile, | |
| formData | |
| } = values; | |
| const init = productId => { | |
| getProduct(productId).then(data => { | |
| if (data.error) { | |
| setValues({ ...values, error: data.error }); | |
| } else { | |
| // populate the state | |
| setValues({ | |
| ...values, | |
| name: data.name, | |
| description: data.description, | |
| price: data.price, | |
| youtubelink:data.youtubelink, | |
| category: data.category._id, | |
| shipping: data.shipping, | |
| quantity: data.quantity, | |
| formData: new FormData() | |
| }); | |
| // load categories | |
| initCategories(); | |
| } | |
| }); | |
| }; | |
| // load categories and set form data | |
| const initCategories = () => { | |
| getCategories().then(data => { | |
| if (data.error) { | |
| setValues({ ...values, error: data.error }); | |
| } else { | |
| setCategories(data); | |
| } | |
| }); | |
| }; | |
| useEffect(() => { | |
| init(match.params.productId); | |
| // eslint-disable-next-line react-hooks/exhaustive-deps | |
| }, []); | |
| const handleChange = name => event => { | |
| const value = name === 'photo' ? event.target.files[0] : event.target.value; | |
| formData.set(name, value); | |
| setValues({ ...values, [name]: value }); | |
| }; | |
| const clickSubmit = 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: '', | |
| photo: '', | |
| price: '', | |
| quantity: '', | |
| youtubelink:'', | |
| loading: false, | |
| error: false, | |
| redirectToProfile: true, | |
| createdProduct: data.name | |
| }); | |
| } | |
| }); | |
| }; | |
| const newPostForm = () => ( | |
| <form className="mb-3" onSubmit={clickSubmit}> | |
| <h4>Post Photo</h4> | |
| <div className="form-group"> | |
| <label className="btn btn-secondary"> | |
| <input onChange={handleChange('photo')} type="file" name="photo" accept="image/*" /> | |
| </label> | |
| </div> | |
| <div className="form-group"> | |
| <label className="text-muted">Name</label> | |
| <input onChange={handleChange('name')} type="text" className="form-control" value={name} /> | |
| </div> | |
| <div className="form-group"> | |
| <label className="text-muted">youtube link (Id only)</label> | |
| <input onChange={handleChange('youtubelink')} type="text" className="form-control" value={youtubelink} placeholder=" not this -https://youtu.be/a4pi2zKbf8Q but this only- a4pi2zKbf8Q" /> | |
| </div> | |
| <div className="form-group"> | |
| <label className="text-muted">Description</label> | |
| <textarea onChange={handleChange('description')} className="form-control" value={description} /> | |
| </div> | |
| <div className="form-group"> | |
| <label className="text-muted">Price</label> | |
| <input onChange={handleChange('price')} type="number" className="form-control" value={price} /> | |
| </div> | |
| <div className="form-group"> | |
| <label className="text-muted">Category</label> | |
| <select onChange={handleChange('category')} className="form-control"> | |
| <option>Please select</option> | |
| {categories && | |
| categories.map((c, i) => ( | |
| <option key={i} value={c._id}> | |
| {c.name} | |
| </option> | |
| ))} | |
| </select> | |
| </div> | |
| <div className="form-group"> | |
| <label className="text-muted">Shipping</label> | |
| <select onChange={handleChange('shipping')} className="form-control"> | |
| <option>Please select</option> | |
| <option value="0">No</option> | |
| <option value="1">Yes</option> | |
| </select> | |
| </div> | |
| <div className="form-group"> | |
| <label className="text-muted">Quantity</label> | |
| <input onChange={handleChange('quantity')} type="number" className="form-control" value={quantity} /> | |
| </div> | |
| <button className="btn btn-outline-primary">Update Product</button> | |
| </form> | |
| ); | |
| const showError = () => ( | |
| <div className="alert alert-danger" style={{ display: error ? '' : 'none' }}> | |
| {error} | |
| </div> | |
| ); | |
| const showSuccess = () => ( | |
| <div className="alert alert-info" style={{ display: createdProduct ? '' : 'none' }}> | |
| <h2>{`${createdProduct}`} is updated!</h2> | |
| </div> | |
| ); | |
| const showLoading = () => | |
| loading && ( | |
| <div className="alert alert-success"> | |
| <h2>Loading...</h2> | |
| </div> | |
| ); | |
| const redirectUser = () => { | |
| if (redirectToProfile) { | |
| if (!error) { | |
| return <Redirect to="/admin/products" />; | |
| } | |
| } | |
| }; | |
| return ( | |
| <Layout title="Add a new product" description={`G'day ${user.name}, ready to add a new product?`}> | |
| <div className="row"> | |
| <div className="col-md-8 offset-md-2"> | |
| {showLoading()} | |
| {showSuccess()} | |
| {showError()} | |
| {newPostForm()} | |
| {redirectUser()} | |
| </div> | |
| </div> | |
| </Layout> | |
| ); | |
| }; | |
| export default UpdateProduct; | |