FitTrackee/fittrackee_client/src/components/User/ProfileEdit.jsx

279 lines
11 KiB
React
Raw Normal View History

2018-06-12 11:47:01 +02:00
import { format } from 'date-fns'
2018-01-01 16:59:46 +01:00
import React from 'react'
import { Helmet } from 'react-helmet'
2019-09-16 10:26:02 +02:00
import { withTranslation } from 'react-i18next'
2018-01-01 16:59:46 +01:00
import { connect } from 'react-redux'
import TimezonePicker from 'react-timezone'
2018-01-01 16:59:46 +01:00
2018-06-12 11:47:01 +02:00
import { handleProfileFormSubmit } from '../../actions/user'
2018-05-01 13:03:40 +02:00
import { history } from '../../index'
import { languages } from '../NavBar/LanguageDropdown'
2018-01-01 16:59:46 +01:00
class ProfileEdit extends React.Component {
2018-06-12 11:47:01 +02:00
constructor(props, context) {
super(props, context)
this.state = {
2019-08-28 15:35:22 +02:00
formData: {},
2018-06-12 11:47:01 +02:00
}
}
2018-01-01 16:59:46 +01:00
componentDidMount() {
2018-06-12 11:47:01 +02:00
this.initForm()
}
componentDidUpdate(prevProps) {
if (prevProps.user !== this.props.user) {
this.initForm()
}
}
initForm() {
const { user } = this.props
const formData = {}
2019-08-28 15:35:22 +02:00
Object.keys(user).map(k =>
user[k] === null
? (formData[k] = '')
: k === 'birth_date'
? (formData[k] = format(new Date(user[k]), 'yyyy-MM-DD'))
: (formData[k] = user[k])
)
2018-06-12 11:47:01 +02:00
this.setState({ formData })
2018-01-01 16:59:46 +01:00
}
2018-06-12 11:47:01 +02:00
handleFormChange(e) {
const { formData } = this.state
if (e.target.name === 'weekm') {
formData.weekm = e.target.value === 'Monday'
} else {
formData[e.target.name] = e.target.value
}
2018-06-12 11:47:01 +02:00
this.setState(formData)
}
2019-08-28 15:35:22 +02:00
render() {
2019-09-16 10:26:02 +02:00
const { onHandleProfileFormSubmit, message, t, user } = this.props
2018-06-12 11:47:01 +02:00
const { formData } = this.state
2018-01-01 16:59:46 +01:00
return (
<div>
<Helmet>
2019-09-16 10:26:02 +02:00
<title>FitTrackee - {t('user:Profile Edition')}</title>
2018-01-01 16:59:46 +01:00
</Helmet>
2019-08-28 15:35:22 +02:00
{message !== '' && <code>{message}</code>}
2018-06-12 11:47:01 +02:00
{formData.isAuthenticated && (
<div className="container">
2019-09-16 10:26:02 +02:00
<h1 className="page-title">{t('user:Profile Edition')}</h1>
2018-06-12 11:47:01 +02:00
<div className="row">
<div className="col-md-2" />
<div className="col-md-8">
<div className="card">
2019-08-28 15:35:22 +02:00
<div className="card-header">{user.username}</div>
2018-06-12 11:47:01 +02:00
<div className="card-body">
<div className="row">
<div className="col-md-12">
2019-08-28 15:35:22 +02:00
<form
onSubmit={event => {
event.preventDefault()
onHandleProfileFormSubmit(formData)
}}
>
<div className="form-group">
<label>
2019-09-16 10:26:02 +02:00
{t('user:Email')}:
2019-08-28 15:35:22 +02:00
<input
name="email"
className="form-control input-lg"
type="text"
value={formData.email}
readOnly
/>
</label>
</div>
<div className="form-group">
<label>
2019-09-16 10:26:02 +02:00
{t('user:Registration Date')}:
2019-08-28 15:35:22 +02:00
<input
name="createdAt"
className="form-control input-lg"
type="text"
value={formData.created_at}
disabled
/>
</label>
</div>
<div className="form-group">
<label>
2019-09-16 10:26:02 +02:00
{t('user:Password')}:
2019-08-28 15:35:22 +02:00
<input
name="password"
className="form-control input-lg"
type="password"
onChange={e => this.handleFormChange(e)}
/>
</label>
</div>
<div className="form-group">
<label>
2019-09-16 10:26:02 +02:00
{t('user:Password Confirmation')}:
2019-08-28 15:35:22 +02:00
<input
name="password_conf"
className="form-control input-lg"
type="password"
onChange={e => this.handleFormChange(e)}
/>
</label>
</div>
<hr />
2018-06-12 11:47:01 +02:00
<div className="form-group">
2019-08-28 15:35:22 +02:00
<label>
2019-09-16 10:26:02 +02:00
{t('user:First Name')}:
2019-08-28 15:35:22 +02:00
<input
name="first_name"
className="form-control input-lg"
type="text"
value={formData.first_name}
onChange={e => this.handleFormChange(e)}
/>
</label>
</div>
<div className="form-group">
<label>
2019-09-16 10:26:02 +02:00
{t('user:Last Name')}:
2019-08-28 15:35:22 +02:00
<input
name="last_name"
className="form-control input-lg"
type="text"
value={formData.last_name}
onChange={e => this.handleFormChange(e)}
/>
</label>
</div>
<div className="form-group">
<label>
2019-09-16 10:26:02 +02:00
{t('user:Birth Date')}
2019-08-28 15:35:22 +02:00
<input
name="birth_date"
className="form-control input-lg"
type="date"
value={formData.birth_date}
onChange={e => this.handleFormChange(e)}
/>
</label>
</div>
<div className="form-group">
<label>
2019-09-16 10:26:02 +02:00
{t('user:Location')}:
2019-08-28 15:35:22 +02:00
<input
name="location"
className="form-control input-lg"
type="text"
value={formData.location}
onChange={e => this.handleFormChange(e)}
/>
</label>
</div>
<div className="form-group">
<label>
2019-09-16 10:26:02 +02:00
{t('user:Bio')}:
2019-08-28 15:35:22 +02:00
<textarea
name="bio"
className="form-control input-lg"
maxLength="200"
value={formData.bio}
onChange={e => this.handleFormChange(e)}
/>
</label>
</div>
<div className="form-group">
<label>
{t('user:Language')}:
<select
name="language"
className="form-control input-lg"
value={formData.language}
onChange={e => this.handleFormChange(e)}
>
{languages.map(lang => (
<option value={lang.name} key={lang.name}>
{lang.name}
</option>
))}
</select>
</label>
</div>
2019-08-28 15:35:22 +02:00
<div className="form-group">
<label>
2019-09-16 10:26:02 +02:00
{t('user:Timezone')}:
2019-08-28 15:35:22 +02:00
<TimezonePicker
className="form-control timezone-custom-height"
onChange={tz => {
const e = {
target: {
name: 'timezone',
value: tz ? tz : 'Europe/Paris',
},
2018-06-12 11:47:01 +02:00
}
2019-08-28 15:35:22 +02:00
this.handleFormChange(e)
}}
value={formData.timezone}
/>
</label>
</div>
<div className="form-group">
<label>
2019-09-16 10:26:02 +02:00
{t('user:First day of week')}:
<select
name="weekm"
className="form-control input-lg"
value={formData.weekm ? 'Monday' : 'Sunday'}
onChange={e => this.handleFormChange(e)}
>
2019-09-16 10:26:02 +02:00
<option value="Sunday">
{t('user:Sunday')}
</option>
<option value="Monday">
{t('user:Monday')}
</option>
</select>
</label>
</div>
2019-08-28 15:35:22 +02:00
<input
type="submit"
className="btn btn-primary btn-lg btn-block"
2019-09-16 10:26:02 +02:00
value={t('common:Submit')}
2019-08-28 15:35:22 +02:00
/>
<input
type="submit"
className="btn btn-secondary btn-lg btn-block"
onClick={() => history.push('/profile')}
2019-09-16 10:26:02 +02:00
value={t('common:Cancel')}
2019-08-28 15:35:22 +02:00
/>
</form>
</div>
2018-01-01 16:59:46 +01:00
</div>
</div>
</div>
</div>
2018-06-12 11:47:01 +02:00
<div className="col-md-2" />
2018-01-01 16:59:46 +01:00
</div>
</div>
2018-06-12 11:47:01 +02:00
)}
2018-01-01 16:59:46 +01:00
</div>
)
}
}
2019-09-16 13:08:58 +02:00
export default withTranslation()(
2019-09-16 10:26:02 +02:00
connect(
state => ({
location: state.router.location,
message: state.message,
user: state.user,
}),
dispatch => ({
onHandleProfileFormSubmit: formData => {
dispatch(handleProfileFormSubmit(formData))
},
})
)(ProfileEdit)
)