2021-12-18 16:05:52 +09:00
|
|
|
import { useRef, useEffect, useState } from 'react';
|
2021-11-17 23:56:11 +09:00
|
|
|
import { CircularProgress } from '@material-ui/core';
|
|
|
|
import { LoadingButton } from '@material-ui/lab';
|
2021-11-16 02:31:44 +09:00
|
|
|
import Notification from '../notification';
|
2021-11-17 23:56:11 +09:00
|
|
|
import Alert from '../../component/alert';
|
|
|
|
import { useHistory } from 'react-router';
|
2021-10-01 17:45:39 +09:00
|
|
|
|
|
|
|
const ProfileEdit = () => {
|
2021-11-17 23:56:11 +09:00
|
|
|
|
|
|
|
const history = useHistory();
|
|
|
|
const [notice, setNotice] = useState(localStorage.getItem('notice'));
|
|
|
|
const father_id = document.getElementById('father_id').value;
|
|
|
|
|
|
|
|
const [company, setCompany] = useState('');
|
2021-10-08 13:07:58 +09:00
|
|
|
const [email, setEmail] = useState('');
|
|
|
|
const [tel, setTel] = useState('');
|
|
|
|
const [profile, setProfile] = useState('');
|
2021-11-18 18:44:21 +09:00
|
|
|
const [params, setParams] = useState(null);
|
2021-10-08 13:07:58 +09:00
|
|
|
|
2021-11-17 23:56:11 +09:00
|
|
|
const [_success, setSuccess] = useState('');
|
|
|
|
const [_400error, set400Error] = useState('');
|
2021-11-30 18:08:59 +09:00
|
|
|
const [_404error, set404Error] = useState('');
|
2021-11-17 23:56:11 +09:00
|
|
|
const [_422errors, set422Errors] = useState({
|
|
|
|
company: '',
|
|
|
|
email:'',
|
|
|
|
tel:'',
|
|
|
|
profile:''
|
2021-10-08 13:07:58 +09:00
|
|
|
})
|
2021-11-17 23:56:11 +09:00
|
|
|
const [loaded, setLoaded] = useState(false);
|
|
|
|
const [submit, setSubmit] = useState(false);
|
2021-10-08 13:07:58 +09:00
|
|
|
|
2021-12-18 16:05:52 +09:00
|
|
|
const isMountedRef = useRef(true);
|
|
|
|
|
2021-10-08 13:07:58 +09:00
|
|
|
useEffect(() => {
|
2021-12-18 16:05:52 +09:00
|
|
|
isMountedRef.current = false;
|
2021-11-17 23:56:11 +09:00
|
|
|
setLoaded(false);
|
|
|
|
axios.get(`/api/fathers/detail/${father_id}`)
|
|
|
|
.then(response => {
|
|
|
|
setLoaded(true);
|
|
|
|
setNotice(response.data.notice);
|
2021-10-08 13:07:58 +09:00
|
|
|
if(response.data.status_code==200) {
|
2021-11-18 18:44:21 +09:00
|
|
|
setParams(response.data.params);
|
2021-11-17 23:56:11 +09:00
|
|
|
setCompany(response.data.params?.company);
|
|
|
|
setEmail(response.data.params?.email);
|
|
|
|
setTel(response.data.params?.tel);
|
2021-12-01 08:42:43 +09:00
|
|
|
setProfile(response.data.params.profile ? response.data.params.profile: '');
|
2021-11-18 12:34:24 +09:00
|
|
|
}
|
|
|
|
else {
|
2021-11-18 17:33:59 +09:00
|
|
|
set400Error("失敗しました。");
|
2021-11-18 12:34:24 +09:00
|
|
|
}
|
2021-11-30 18:08:59 +09:00
|
|
|
})
|
|
|
|
.catch(err=>{
|
|
|
|
setLoaded(true);
|
|
|
|
setNotice(err.response.data.notice);
|
|
|
|
if(err.response.status==404){
|
|
|
|
set404Error(err.response.data.message);
|
|
|
|
}
|
|
|
|
})
|
2021-10-08 13:07:58 +09:00
|
|
|
}, []);
|
|
|
|
|
2021-11-17 23:56:11 +09:00
|
|
|
const handleSubmit = (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
set422Errors({ company:'', email: '', tel: '', profile:'' });
|
|
|
|
const request = {
|
|
|
|
company: company,
|
|
|
|
email: email,
|
|
|
|
tel: tel,
|
|
|
|
profile: profile
|
|
|
|
}
|
|
|
|
setSubmit(true);
|
|
|
|
axios.put(`/api/fathers/updateProfile/${father_id}`, request)
|
|
|
|
.then(response => {
|
|
|
|
setNotice(response.data.notice);
|
|
|
|
setSubmit(false);
|
|
|
|
switch(response.data.status_code){
|
|
|
|
case 200:{
|
|
|
|
history.push({
|
|
|
|
pathname: '/p-account/profile',
|
|
|
|
state: response.data.success_messages
|
2021-10-08 13:07:58 +09:00
|
|
|
});
|
2021-11-17 23:56:11 +09:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 400: set400Error(response.data.error_messages); break;
|
|
|
|
case 422: set422Errors(response.data.error_messages); break;
|
2021-10-08 13:07:58 +09:00
|
|
|
}
|
2021-11-17 23:56:11 +09:00
|
|
|
});
|
2021-10-08 13:07:58 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-01 17:45:39 +09:00
|
|
|
return (
|
2021-10-03 02:08:39 +09:00
|
|
|
<div className="l-content">
|
|
|
|
<div className="l-content-w560">
|
|
|
|
<div className="l-content__ttl">
|
|
|
|
<div className="l-content__ttl__left">
|
|
|
|
<h2>プロフィール編集</h2>
|
|
|
|
</div>
|
2021-11-17 23:56:11 +09:00
|
|
|
<Notification notice={notice}/>
|
2021-10-03 02:08:39 +09:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="l-content-wrap">
|
2021-11-17 23:56:11 +09:00
|
|
|
<section className="edit-container">
|
|
|
|
{
|
|
|
|
!loaded &&
|
|
|
|
<CircularProgress className="css-loader"/>
|
|
|
|
}
|
|
|
|
{
|
2021-11-18 18:44:21 +09:00
|
|
|
loaded && params &&
|
2021-10-03 02:08:39 +09:00
|
|
|
<div className="edit-wrap">
|
|
|
|
<div className="edit-content">
|
2021-11-17 23:56:11 +09:00
|
|
|
<form className="edit-form" onSubmit={handleSubmit}>
|
2021-10-03 02:08:39 +09:00
|
|
|
|
|
|
|
<div className="edit-set">
|
2021-11-17 23:56:11 +09:00
|
|
|
<label className="control-label" htmlFor="nameSei">会社名</label>
|
|
|
|
<input type="text" name="nameSei" value={ company } onChange={e=>setCompany(e.target.value)}
|
|
|
|
className={`input-default input-nameSei input-h60 input-w480 ${ _422errors.company && "is-invalid c-input__target" }`} id="company" />
|
|
|
|
{
|
|
|
|
_422errors.company &&
|
|
|
|
<span className="l-alert__text--error ft-16 ft-md-14">
|
|
|
|
{ _422errors.company }
|
|
|
|
</span>
|
|
|
|
}
|
2021-10-03 02:08:39 +09:00
|
|
|
</div>
|
2021-11-17 23:56:11 +09:00
|
|
|
|
2021-10-03 02:08:39 +09:00
|
|
|
<div className="edit-set">
|
|
|
|
<label className="control-label" htmlFor="mail">メールアドレス</label>
|
2021-10-08 13:07:58 +09:00
|
|
|
<input type="email" name="mail" value={ email } onChange={e=>setEmail(e.target.value)}
|
2021-11-17 23:56:11 +09:00
|
|
|
className={`input-default input-mail input-h60 input-w480 ${ _422errors.email && "is-invalid c-input__target" } `} id="mail" />
|
|
|
|
{
|
|
|
|
_422errors.email &&
|
|
|
|
<span className="l-alert__text--error ft-16 ft-md-14">
|
|
|
|
{ _422errors.email }
|
|
|
|
</span>
|
|
|
|
}
|
2021-10-03 02:08:39 +09:00
|
|
|
</div>
|
2021-11-17 23:56:11 +09:00
|
|
|
|
2021-10-03 02:08:39 +09:00
|
|
|
<div className="edit-set">
|
|
|
|
<label className="control-label" htmlFor="tel">電話番号</label>
|
2021-10-08 13:07:58 +09:00
|
|
|
<input type="tel" name="tel" value={ tel } onChange={e=>setTel(e.target.value)}
|
2021-11-17 23:56:11 +09:00
|
|
|
className={`input-default input-tel input-h60 input-w480 ${ _422errors.tel && "is-invalid c-input__target" } `} id="tel" />
|
|
|
|
{
|
|
|
|
_422errors.tel &&
|
|
|
|
<span className="l-alert__text--error ft-16 ft-md-14">
|
|
|
|
{ _422errors.tel }
|
|
|
|
</span>
|
|
|
|
}
|
2021-10-03 02:08:39 +09:00
|
|
|
</div>
|
2021-11-17 23:56:11 +09:00
|
|
|
|
2021-10-03 02:08:39 +09:00
|
|
|
<div className="edit-set">
|
|
|
|
<label className="control-label" htmlFor="profile_textarea">プロフィール</label>
|
2021-10-08 13:07:58 +09:00
|
|
|
<textarea name="profile" value={ profile } onChange={e=>setProfile(e.target.value)} rows="8"
|
2021-11-17 23:56:11 +09:00
|
|
|
className={`textarea-default ${ _422errors.profile && "is-invalid c-input__target" } `} id="profile_textarea" />
|
|
|
|
{
|
|
|
|
_422errors.profile &&
|
|
|
|
<span className="l-alert__text--error ft-16 ft-md-14">
|
|
|
|
{ _422errors.profile }
|
|
|
|
</span>
|
|
|
|
}
|
2021-10-03 02:08:39 +09:00
|
|
|
</div>
|
|
|
|
|
2021-11-17 23:56:11 +09:00
|
|
|
<LoadingButton type="submit"
|
|
|
|
loading={submit}
|
|
|
|
fullWidth
|
|
|
|
className="btn-edit btn-default btn-h75 bg-yellow rounded-20">
|
|
|
|
<span className={`ft-16 font-weight-bold ${!submit && 'text-black'}`}>
|
|
|
|
プロフィールを更新
|
|
|
|
</span>
|
|
|
|
</LoadingButton>
|
2021-10-03 02:08:39 +09:00
|
|
|
|
2021-11-17 23:56:11 +09:00
|
|
|
</form>
|
2021-10-01 17:45:39 +09:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-11-17 23:56:11 +09:00
|
|
|
}
|
2021-11-18 19:04:25 +09:00
|
|
|
{ _400error && <Alert type="fail" hide={()=>set400Error('')}>{_400error}</Alert> }
|
|
|
|
{ _success && <Alert type="success" hide={()=>setSuccess('') }>{_success}</Alert> }
|
2021-11-30 18:08:59 +09:00
|
|
|
{ _404error &&
|
|
|
|
<Alert type="fail" hide={()=>{
|
|
|
|
set404Error('');
|
|
|
|
history.push({
|
|
|
|
pathname: "/p-account/profile"
|
|
|
|
});
|
|
|
|
}}>
|
|
|
|
{_404error}
|
|
|
|
</Alert>
|
|
|
|
}
|
2021-11-18 19:04:25 +09:00
|
|
|
</section>
|
2021-10-01 17:45:39 +09:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-10-03 02:08:39 +09:00
|
|
|
</div>
|
2021-10-01 17:45:39 +09:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-10-03 02:08:39 +09:00
|
|
|
export default ProfileEdit;
|