このリポジトリは2023-09-09にアーカイブされています。 ファイルの閲覧とクローンは可能ですが、プッシュ、イシューの作成、プルリクエストはできません。
kikikan/backend/resources/js/admin/login/index.jsx

154 行
5.7 KiB
React
Raw 通常表示 履歴

2022-01-14 16:33:26 +09:00
import React, { useState, useRef, useEffect } from 'react';
2022-01-28 18:06:26 +09:00
import { useLocation, useNavigate } from 'react-router-dom';
2021-10-20 21:22:46 +09:00
import { LoadingButton } from '@material-ui/lab';
2021-10-07 04:52:20 +09:00
2021-10-22 22:09:32 +09:00
import Alert from '../../component/alert';
2021-10-07 04:52:20 +09:00
2021-10-09 05:15:18 +09:00
const AdminLogin = () => {
2021-10-07 04:52:20 +09:00
2022-01-08 21:22:42 +09:00
const location = useLocation();
2022-01-28 18:06:26 +09:00
const navigator = useNavigate();
2022-01-06 05:43:21 +09:00
2021-10-07 04:52:20 +09:00
const [email, setEmail] = useState('');
const [password, setPassword] = useState('');
2021-10-20 21:22:46 +09:00
const [submit, setSubmit] = useState(false);
2021-10-22 10:06:03 +09:00
const [_422errors, set422Errors] = useState({ email:null, password:null })
const [_400error, set400Error] = useState(null);
2021-10-22 22:09:32 +09:00
const [_success, setSuccess] = useState(null);
2021-10-07 04:52:20 +09:00
2022-01-14 16:33:26 +09:00
const isMountedRef = useRef(true);
useEffect(() => {
isMountedRef.current = false;
axios.post('/api/admin/checkSession').then(response => {
if (isMountedRef.current) return;
switch (response.data.status_code) {
case 200: {
2022-01-28 20:01:54 +09:00
if(location.search == '')
window.location.href = "/admin/meeting";
else
window.location.href = location.search.replace('?redirect_to=', '');
break;
}
default: break;
}
});
2022-01-14 16:33:26 +09:00
return () => {
isMountedRef.current = true;
}
}, [])
2021-10-07 04:52:20 +09:00
2022-01-28 19:43:59 +09:00
const loginOK = (id) => {
2022-01-28 19:50:37 +09:00
let token = {
2022-01-28 18:51:23 +09:00
type: 'admin',
2022-01-28 19:43:59 +09:00
id: id,
2022-01-28 18:51:23 +09:00
from_login: true
};
2022-01-28 19:50:37 +09:00
localStorage.setItem('admin_token', JSON.stringify(token));
2022-01-28 18:51:23 +09:00
if(location.search == '')
window.location.href = "/admin/meeting";
else
window.location.href = location.search.replace('?redirect_to=', '');
}
2022-01-14 16:33:26 +09:00
const handleSubmit = (e) => {
2021-10-07 04:52:20 +09:00
e.preventDefault();
2022-01-28 19:43:59 +09:00
set422Errors({ email:'', password:'' });
2021-10-20 21:22:46 +09:00
setSubmit(true); //show progressbar
2021-10-07 04:52:20 +09:00
const formdata = new FormData();
formdata.append('email', email);
formdata.append('password', password);
2021-10-20 21:22:46 +09:00
2022-01-14 16:33:26 +09:00
axios.post('/api/admin/login', formdata)
.then(response => {
if(isMountedRef.current) return;
setSubmit(false)
switch(response.data.status_code){
case 200: {
2022-01-28 19:43:59 +09:00
loginOK(response.data.params.id);
2022-01-14 16:33:26 +09:00
break;
}
case 422: {
window.scrollTo(0, 0);
set422Errors(response.data.error_messages);
break;
}
case 400: {
set400Error(response.data.error_message);
break;
}
2022-01-14 16:33:26 +09:00
}
if(response.data.status_code != 200){
setPassword('');
}
})
.catch(err=>console.log(err))
2021-10-07 04:52:20 +09:00
}
return (
2021-10-18 18:15:53 +09:00
<main className="l-single-main">
<div className="l-centeringbox">
<div className="l-centeringbox-wrap">
<div className="l-single-container">
<div className="l-single-inner">
<form onSubmit={handleSubmit} noValidate>
<h1 className="text-center font-weight-bold ft-25 pb-40-px">管理者ログイン</h1>
<div className="edit-set">
<label htmlFor="email" className="control-label ft-md-12">メールアドレス</label>
2021-10-24 00:13:32 +09:00
<input type="email" name="email" id="email" className={`input-default input-h60 ${ _422errors.email && "is-invalid c-input__target" } `} value={email} onChange={e=>setEmail(e.target.value)} autoFocus/>
2021-10-18 18:15:53 +09:00
{
_422errors.email &&
<span className="l-alert__text--error ft-16 ft-md-14">
{_422errors.email}
</span>
}
</div>
2021-10-07 04:52:20 +09:00
2021-10-18 18:15:53 +09:00
<div className="edit-set">
<label htmlFor="password" className="control-label ft-14 ft-md-12"> パスワード </label>
2021-10-24 00:13:32 +09:00
<input type="password" name="password" id="password" className={`input-default input-h60 ${ _422errors.password && "is-invalid c-input__target" }`} value={password} onChange={e=>setPassword(e.target.value)} />
2021-10-18 18:15:53 +09:00
{
_422errors.password &&
<span className="l-alert__text--error ft-16 ft-md-14">
{_422errors.password}
</span>
}
</div>
2021-10-26 10:07:57 +09:00
<div className="mt-5">
<LoadingButton type="submit"
loading={submit}
fullWidth
2021-11-19 18:23:20 +09:00
className="btn-edit btn-default btn-h75 bg-yellow rounded-20">
<span className={`ft-16 font-weight-bold ${!submit && 'text-black'}`}>
2021-10-26 10:07:57 +09:00
ログイン
</span>
</LoadingButton>
</div>
2021-10-07 04:52:20 +09:00
2021-10-18 18:15:53 +09:00
</form>
</div>
</div>
</div>
</div>
2021-10-22 22:09:32 +09:00
{
_400error && <Alert type="fail" hide={()=>set400Error(null)}>{_400error}</Alert>
}
2021-10-18 18:15:53 +09:00
</main>
2021-10-07 04:52:20 +09:00
)
}
2022-01-04 15:58:52 +09:00
export default AdminLogin;