2022-02-03 04:39:18 +09:00
|
|
|
import React, { useState, useRef, useEffect, useContext } from 'react';
|
2022-01-04 15:58:52 +09:00
|
|
|
import { useNavigate } from 'react-router-dom';
|
2021-11-09 16:53:39 +09:00
|
|
|
import { LoadingButton } from '@material-ui/lab';
|
|
|
|
|
2022-02-03 04:39:18 +09:00
|
|
|
import { HeaderContext } from '../../context';
|
2021-12-31 12:33:35 +09:00
|
|
|
import Notification from '../../component/notification';
|
2021-11-09 16:53:39 +09:00
|
|
|
import Alert from '../../component/alert';
|
|
|
|
|
|
|
|
|
2021-10-31 12:26:19 +09:00
|
|
|
const ChildProfilePasswordEdit = () => {
|
2021-12-20 18:01:50 +09:00
|
|
|
|
2022-01-04 17:44:38 +09:00
|
|
|
const navigator = useNavigate();
|
2022-02-03 04:39:18 +09:00
|
|
|
const { isAuthenticate } = useContext(HeaderContext);
|
2022-01-04 17:44:38 +09:00
|
|
|
|
2022-01-28 22:29:58 +09:00
|
|
|
const child_id = localStorage.getItem('child_id');
|
2022-01-28 20:41:31 +09:00
|
|
|
const [notice, setNotice] = useState(-1);
|
2021-11-11 14:33:17 +09:00
|
|
|
|
2021-11-09 16:53:39 +09:00
|
|
|
const [password, setPassword] = useState('');
|
|
|
|
const [password_confirmation, setConfirmPassword] = useState('');
|
|
|
|
|
|
|
|
const [_422errors, set422Errors] = useState({
|
|
|
|
password:'',
|
|
|
|
password_confirmation:''
|
|
|
|
});
|
|
|
|
const [_400error, set400Error] = useState('');
|
|
|
|
const [_success, setSuccess] = useState('');
|
|
|
|
const [submit, setSubmit] = useState(false);
|
|
|
|
|
2021-11-11 14:33:17 +09:00
|
|
|
|
2022-01-14 16:33:26 +09:00
|
|
|
const isMountedRef = useRef(true);
|
|
|
|
useEffect(() => {
|
|
|
|
isMountedRef.current = false;
|
|
|
|
return () => {
|
|
|
|
isMountedRef.current = true;
|
|
|
|
}
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
|
|
const handleSubmit = (e) => {
|
2021-11-09 16:53:39 +09:00
|
|
|
e.preventDefault();
|
2022-02-03 04:39:18 +09:00
|
|
|
if(isAuthenticate()){
|
|
|
|
set422Errors({
|
|
|
|
password:'',
|
|
|
|
password_confirmation:''
|
|
|
|
});
|
|
|
|
setSubmit(true);
|
|
|
|
const post = {
|
|
|
|
password: password,
|
|
|
|
password_confirmation: password_confirmation
|
2022-01-14 16:33:26 +09:00
|
|
|
}
|
2022-02-03 04:39:18 +09:00
|
|
|
|
|
|
|
axios.put(`/api/children/updatePassword/${child_id}`, post)
|
|
|
|
.then(response => {
|
|
|
|
if(isMountedRef.current) return;
|
|
|
|
|
|
|
|
setSubmit(false);
|
|
|
|
setNotice(response.data.notice);
|
|
|
|
switch(response.data.status_code){
|
|
|
|
case 200: {
|
|
|
|
navigator('/c-account/profile', { state: response.data.success_messages });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 400: set400Error(response.data.error_messages); break;
|
|
|
|
case 422: window.scrollTo(0, 0); set422Errors(response.data.error_messages); break;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-11-09 16:53:39 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-20 18:01:50 +09:00
|
|
|
|
2021-11-09 16:53:39 +09:00
|
|
|
return (
|
|
|
|
<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-11 14:33:17 +09:00
|
|
|
<Notification notice={notice}/>
|
2021-11-09 16:53:39 +09:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="l-content-wrap">
|
2021-11-18 12:14:22 +09:00
|
|
|
<section className="edit-container">
|
|
|
|
<div className="edit-wrap">
|
|
|
|
<div className="edit-content">
|
|
|
|
<form onSubmit={handleSubmit} className="edit-form">
|
2021-11-09 16:53:39 +09:00
|
|
|
<div className="edit-set">
|
2021-12-20 18:01:50 +09:00
|
|
|
<label htmlFor="password" className="control-label ft-14 ft-md-12">
|
|
|
|
新しいパスワード
|
2021-11-09 16:53:39 +09:00
|
|
|
</label>
|
2021-12-20 18:01:50 +09:00
|
|
|
<input type="password" name="password" id="password" placeholder='半角英数字8文字以上' className={`input-default input-h60 ${ _422errors.password && "is-invalid c-input__target" }`}
|
2021-11-09 16:53:39 +09:00
|
|
|
value={password} onChange={e=>setPassword(e.target.value)} autoFocus/>
|
2021-12-20 18:01:50 +09:00
|
|
|
{
|
|
|
|
_422errors.password &&
|
2021-11-09 16:53:39 +09:00
|
|
|
<span className="l-alert__text--error ft-16 ft-md-14">
|
|
|
|
{ _422errors.password }
|
2021-12-20 18:01:50 +09:00
|
|
|
</span>
|
2021-11-09 16:53:39 +09:00
|
|
|
}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="edit-set">
|
2021-12-20 18:01:50 +09:00
|
|
|
<label htmlFor="password_confirmation" className="control-label ft-14 ft-md-12">
|
2021-11-09 16:53:39 +09:00
|
|
|
確認用新しいパスワード
|
|
|
|
</label>
|
2021-12-20 18:01:50 +09:00
|
|
|
<input type="password" name="password_confirmation" id="password_confirmation" className={`input-default input-h60 ${ _422errors.password_confirmation && "is-invalid c-input__target" }`}
|
2021-11-09 16:53:39 +09:00
|
|
|
value={password_confirmation} onChange={e=>setConfirmPassword(e.target.value)}/>
|
2021-12-20 18:01:50 +09:00
|
|
|
{
|
|
|
|
_422errors.password_confirmation &&
|
2021-11-09 16:53:39 +09:00
|
|
|
<span className="l-alert__text--error ft-16 ft-md-14">
|
|
|
|
{ _422errors.password_confirmation }
|
2021-12-20 18:01:50 +09:00
|
|
|
</span>
|
2021-11-09 16:53:39 +09:00
|
|
|
}
|
|
|
|
</div>
|
2021-12-20 18:01:50 +09:00
|
|
|
|
|
|
|
<LoadingButton type="submit" fullWidth
|
2021-11-18 12:14:22 +09:00
|
|
|
loading = {submit}
|
|
|
|
className="btn-edit btn-default btn-h75 bg-yellow rounded-20">
|
|
|
|
<span className={`ft-16 font-weight-bold ${!submit && 'text-black'}`}>パスワードを更新</span>
|
|
|
|
</LoadingButton>
|
|
|
|
{ _400error && <Alert type="fail" hide={()=>set400Error('')}>{_400error}</Alert> }
|
|
|
|
{ _success && <Alert type="success" hide={()=>setSuccess('')}>{_success}</Alert> }
|
2021-11-09 16:53:39 +09:00
|
|
|
</form>
|
2021-12-20 18:01:50 +09:00
|
|
|
|
2021-11-09 16:53:39 +09:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-12-20 18:01:50 +09:00
|
|
|
</section>
|
2021-11-09 16:53:39 +09:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-10-31 12:26:19 +09:00
|
|
|
export default ChildProfilePasswordEdit;
|