import React, { useRef, useEffect, useState } from 'react'; import { useNavigate, Link } from 'react-router-dom'; import IconButton from "@material-ui/core/IconButton"; import PhotoCameraOutlinedIcon from '@mui/icons-material/PhotoCameraOutlined'; import Alert from '../../component/alert'; import PageLoader from '../../component/page_loader'; import Notification from '../../component/notification'; const ParentProfileDetail = () => { const navigator = useNavigate(); const father_id = localStorage.getItem('kiki_acc_id'); const [notice, setNotice] = useState(localStorage.getItem('notice')); const [image, setImage] = useState(''); const [profile, setProfile] = useState({company:'', email:'', tel:'', profile:''}); const [loaded, setLoaded] = useState(false); const [_400error, set400Error] = useState(''); const [_404error, set404Error] = useState(''); const [_422errors, set422Errors] = useState({ image: '' }); const [_success, setSuccess] = useState(''); const [submit_image, setSubmitImage] = useState(false); const isMountedRef = useRef(true); useEffect(() => { isMountedRef.current = false; setLoaded(false); axios.get('/api/fathers/detail/'+father_id) .then(response => { if(isMountedRef.current) return; setLoaded(true); setNotice(response.data.notice); if(response.data.status_code==200){ setProfile(response.data.params); setImage(response.data.params.image); } else { set400Error("失敗しました。"); } }) .catch(err=>{ if(isMountedRef.current) return; setLoaded(true); setNotice(err.response.data.notice); if(err.response.status==404){ set404Error(err.response.data.message); } }) return () => { isMountedRef.current = true; } },[]); useEffect(() => { if(localStorage.getItem('image_upload_success')){ setSuccess(localStorage.getItem('image_upload_success')); localStorage.removeItem('image_upload_success'); } }) const handleLogout = () => { axios.get('/p-account/logout') .then(() => { window.location.href = '/p-account/login'; }) } const handleImageChange = (e) => { e.preventDefault(); set422Errors({image: ''}); let reader = new FileReader(); let _file = e.target.files[0]; reader.readAsDataURL(_file); reader.onloadend = () => { set422Errors({image: ''}); setSubmitImage(true); axios.put(`/api/fathers/updateImage/${father_id}`, {image: reader.result}) .then(response => { if(isMountedRef.current) return; setNotice(response.data.notice); setSubmitImage(false); switch(response.data.status_code){ case 200: { localStorage.setItem('image_upload_success', response.data.success_messages); window.location.reload(true); break; } case 400: set400Error(response.data.error_messages); break; case 422: window.scrollTo(0, 0); set422Errors(response.data.error_messages); break; } }); }; }; return (
{ profile.company }