Download TravelYaari-react/src/user/Profile.js from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 3.17 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/f5071caa249115f3b590492f1c8eb471e02aae4b/TravelYaari-react/src/user/Profile.js
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset@f5071caa249115f3b590492f1c8eb471e02aae4b/TravelYaari-react/src/user/Profile.js
-
curl -L -o Profile.js https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/f5071caa249115f3b590492f1c8eb471e02aae4b/TravelYaari-react/src/user/Profile.js
3.17 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 { read, update, updateUser } from './apiUser'; | |
| const Profile = ({ match }) => { | |
| const [values, setValues] = useState({ | |
| name: '', | |
| email: '', | |
| password: '', | |
| error: false, | |
| success: false | |
| }); | |
| const { token } = isAuthenticated(); | |
| const { name, email, password, error, success } = values; | |
| const init = userId => { | |
| // console.log(userId); | |
| read(userId, token).then(data => { | |
| if (data.error) { | |
| setValues({ ...values, error: true }); | |
| } else { | |
| setValues({ ...values, name: data.name, email: data.email }); | |
| } | |
| }); | |
| }; | |
| useEffect(() => { | |
| init(match.params.userId); | |
| // eslint-disable-next-line react-hooks/exhaustive-deps | |
| }, []); | |
| const handleChange = name => e => { | |
| setValues({ ...values, error: false, [name]: e.target.value }); | |
| }; | |
| const clickSubmit = e => { | |
| e.preventDefault(); | |
| update(match.params.userId, token, { name, email, password }).then(data => { | |
| if (data.error) { | |
| // console.log(data.error); | |
| alert(data.error); | |
| } else { | |
| updateUser(data, () => { | |
| setValues({ | |
| ...values, | |
| name: data.name, | |
| email: data.email, | |
| success: true | |
| }); | |
| }); | |
| } | |
| }); | |
| }; | |
| const redirectUser = success => { | |
| if (success) { | |
| return <Redirect to="/cart" />; | |
| } | |
| }; | |
| const profileUpdate = (name, email, password) => ( | |
| <form> | |
| <div className="form-group"> | |
| <label className="text-muted">Name</label> | |
| <input type="text" onChange={handleChange('name')} className="form-control" value={name} /> | |
| </div> | |
| <div className="form-group"> | |
| <label className="text-muted">Email</label> | |
| <input type="email" onChange={handleChange('email')} className="form-control" value={email} /> | |
| </div> | |
| <div className="form-group"> | |
| <label className="text-muted">Password</label> | |
| <input type="password" onChange={handleChange('password')} className="form-control" value={password} /> | |
| </div> | |
| <button onClick={clickSubmit} className="btn btn-primary"> | |
| Submit | |
| </button> | |
| </form> | |
| ); | |
| return ( | |
| <Layout title="Profile" description="Update your profile" className="container mt-5 border rounded p-3 shadow-lg bg-light"> | |
| <h2 className="mb-4 p-2 bg-success text-white text-center font-weight-bold rounded shadow">Profile update</h2> | |
| {profileUpdate(name, email, password)} | |
| {redirectUser(success)} | |
| </Layout> | |
| ); | |
| }; | |
| export default Profile; | |