import React, { useRef, useEffect, useState, useContext } from 'react';
import moment from 'moment';
import { useNavigate, Link, useParams } from 'react-router-dom';
import copy from 'copy-to-clipboard';
import { HeaderContext } from '../../context';
import ModalEditMemo from '../../component/modal_edit_memo';
import ModalConfirm from '../../component/modal_confirm';
import ModalPdf from '../../component/pdf/modal_pdf';
import Notification from '../../component/notification';
import ModalSettingNotify from '../../component/modal_setting_notify';
import Alert from '../../component/alert';
import Thumbnail from '../../component/thumbnail';
import PageLoader from '../../component/page_loader';
const ParentMeetingDetail = () => {
const navigator = useNavigate();
const params = useParams();
const { isAuthenticate } = useContext(HeaderContext);
const [notice, setNotice] = useState(-1);
const father_id = localStorage.getItem('father_id');
const [loaded, setLoaded] = useState(false);
const [loaded_children, setLoadedChildren] = useState(false);
const [submit_delete, setSubmitDelete] = useState(false);
const [submit_notify, setSubmitNotify] = useState(false);
const [_success, setSuccess] = useState('');
const [_400error, set400Error] = useState('');
const [_401error, set401Error] = useState('');
const [_404error, set404Error] = useState('');
const [show_delete_modal, setShowDeleteModal] = useState(false);
const [show_notify_all_modal, setShowNotifyAllModal] = useState(false);
const [show_memo_modal, setShowMemoModal] = useState(false);
const [show_notify_pickup_modal, setShowNotifySelectModal] = useState(false);
const [show_pdf_modal, setShowPDFModal] = useState(false);
const [meeting, setMeeting] = useState(null);
const [thumbnail, setThumbnail] = useState(null);
const isMountedRef = useRef(true);
const lineText =
`「KIKI」の招待が届いています。%0Aまずは以下より仮登録を行ってください。
%0A%0A※スマホ本体を最新の状態にアップデートしてからURLをクリックしてください。
%0A%0A${document.getElementById('inviteurl_html').value}%0A%0A▼公式サイトはこちら%0A${document.getElementById('siteurl').value}`;
useEffect(() => {
isMountedRef.current = false;
if(isAuthenticate()){
setLoaded(false);
axios.get(`/api/fathers/meetings/detail/${params?.meeting_id}`, {params: { father_id: father_id}})
.then((response) => {
if(isMountedRef.current) return;
setLoaded(true);
setNotice(response.data.notice);
if(response.data.status_code==200){
var list = response.data.params;
var total=0, num=0;
for(var i in list.approval)
{
if(list.approval[i].approval_at) num ++;
total ++;
}
setMeeting({...list, denominator:total, numerator:num});
setThumbnail(response.data.params.meeting_image[0]?.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;
}
}, []);
const handleAcceptDelete = () => {
if(isAuthenticate()){
setSubmitDelete(true);
axios.delete(`/api/fathers/meetings/delete/${params?.meeting_id}`)
.then(response => {
if(isMountedRef.current) return;
setNotice(response.data.notice);
setSubmitDelete(false);
setShowDeleteModal(false);
switch(response.data.status_code){
case 200: {
navigator('/p-account/meeting', {state: "ミーティングの削除に成功しました!" });
break;
}
case 400: set400Error('ミーティングの削除に失敗しました。'); break;
}
});
}
};
function handleFavorite(meetingId, currentFavorite) {
if(isAuthenticate()){
const formdata = new FormData();
formdata.append('meeting_id', meetingId);
formdata.append('is_favorite', currentFavorite == 1 ? 0 : 1);
axios.post('/api/fathers/meetings/registerFavorite', formdata)
const updatedItem = {
...meeting,
is_favorite: currentFavorite == 1 ? 0 : 1,
};
setMeeting(updatedItem);
}
};
const handleNotifyAllChild = () => {
if(isAuthenticate()){
setSubmitNotify(true);
axios.get('/api/fathers/meeting/approvals/listChildrenOfUnapprovel', {params:{meeting_id: params?.meeting_id}})
.then(response => {
if(isMountedRef.current) return;
setNotice(response.data.notice);
if(response.data.status_code == 200){
var list = response.data.params;
const email_list = [];
for(var i in list){
email_list.push(list[i].child.email);
}
const formdata = new FormData();
formdata.append('email', JSON.stringify(email_list));
formdata.append('meeting_id', params?.meeting_id);
axios.post('/api/fathers/meetingEditNotification', formdata)
.then(response=>{
if(isMountedRef.current) return;
setSubmitNotify(false);
setShowNotifySelectModal(false);
switch(response.data.status_code){
case 200: setSuccess('通知に成功しました!'); break;
case 400: set400Error('通知に失敗しました。'); break;
}
})
}
});
}
}
const handleUpdateMemo = (modal_memo) => {
if(isAuthenticate()){
let _tmp = meeting;
_tmp.memo = modal_memo;
setMeeting(_tmp);
const post = {
meeting_id: meeting.id,
memo: modal_memo
}
axios.put('/api/fathers/meetings/updateMemo', post).catch(err => {
set400Error(err);
});
}
}
const handlePDFOpen = (pdf) => {
var pieces = pdf.split('/');
var file_name = pieces[pieces.length-1];
window.open(`/pdf/${file_name}`, '_blank');
}
const handleLoadedChildren = (flag) => {
setLoadedChildren(flag);
}
const chareToLine = () => {
window.location.href = `http://line.naver.jp/R/msg/text/?${lineText}`;
}
const cloneMeeting = (meeting) => {
localStorage.setItem('cloneMeeting', JSON.stringify(meeting));
navigator('/p-account/meeting/new');
}
return (
{
!(loaded && loaded_children) &&
}
{
(loaded && loaded_children) && meeting &&
setShowNotifyAllModal(true)} >
- {meeting?.numerator}
- {meeting?.denominator}
{
meeting.approval?.map((v, inx) =>
{
if(v.approval_at)
return(
-
)
})
}
{ meeting?.title }
{
meeting.meeting_image.map((v, inx) =>
setThumbnail(v.image)} key={inx}>
)
}
{ meeting.text }
setShowMemoModal(false)}
handleUpdateMemo = {handleUpdateMemo}
/>
setShowDeleteModal(false)}
handleAccept={handleAcceptDelete}
loading={submit_delete}
/>
setShowNotifySelectModal(false)}
handleAccept={handleNotifyAllChild}
loading = {submit_notify}
/>
setShowPDFModal(false)}
/>
}
{
loaded && meeting &&
setShowNotifyAllModal(false)}
handleLoadedChildren = {handleLoadedChildren}
/>
}
{ _400error &&
set400Error('')}>{_400error} }
{ _success &&
setSuccess('')}>{_success} }
{ _404error &&
{
set404Error('');
navigator('/p-account/meeting', {state: ''});
}}>
{_404error}
}
)
}
export default ParentMeetingDetail;