import React, {useState, useEffect, Fragment} from 'react';
import {observer, inject} from "mobx-react";
import { f7, Popup, Sheet, Popover, Page, Toolbar, Navbar, NavLeft, NavRight, NavTitle, Link, Input, Icon, List, ListItem, Actions, ActionsGroup, ActionsButton } from 'framework7-react';
import { useTranslation } from 'react-i18next';
import {Device} from '../../../utils/device';
// Utils
const sliceQuote = (text) => {
if (text) {
let sliced = text.slice(0, 100);
if (sliced.length < text.length) {
sliced += '...';
return sliced;
}
return text;
}
};
// Add comment
const AddCommentPopup = inject("storeComments")(observer(props => {
const { t } = useTranslation();
const _t = t('Common.Collaboration', {returnObjects: true});
useEffect(() => {
f7.popup.open('.add-comment-popup');
});
const userInfo = props.userInfo;
const [stateText, setText] = useState('');
return (
{
f7.popup.close('.add-comment-popup');
setTimeout(() => {
props.closeAddComment();
}, 500)
}}>{_t.textCancel}
{_t.textAddComment}
{
f7.popup.close('.add-comment-popup');
setTimeout(() => {
props.closeAddComment();
props.onAddNewComment(stateText, false)
}, 500);
}}>
{Device.android ? : _t.textDone}
{Device.android &&
{userInfo.initials}
}
{userInfo.name}
{setText(event.target.value);}}>
)
}));
const AddCommentDialog = inject("storeComments")(observer(props => {
const { t } = useTranslation();
const _t = t('Common.Collaboration', {returnObjects: true});
const userInfo = props.userInfo;
const templateInitials = `
${userInfo.initials}
`;
useEffect(() => {
f7.dialog.create({
destroyOnClose: true,
containerEl: document.getElementById('add-comment-dialog'),
content:
`
`,
on: {
opened: () => {
const cancel = document.getElementById('comment-cancel');
cancel.addEventListener('click', () => {
f7.dialog.close();
props.closeAddComment();
});
const done = document.getElementById('comment-done');
done.addEventListener('click', () => {
const value = document.getElementById('comment-text').value;
if (value.length > 0) {
f7.dialog.close();
props.closeAddComment();
props.onAddNewComment(value, false);
}
});
const area = document.getElementById('comment-text');
area.addEventListener('input', (event) => {
if (event.target.value.length === 0 && !done.classList.contains('disabled')) {
done.classList.add('disabled');
} else if (event.target.value.length > 0 && done.classList.contains('disabled')) {
done.classList.remove('disabled');
}
});
done.classList.add('disabled');
}
}
}).open();
});
return (
);
}));
const AddComment = props => {
return (
Device.phone ?
:
)
};
// Actions
const CommentActions = ({comment, onCommentMenuClick, opened, openActionComment}) => {
const { t } = useTranslation();
const _t = t('Common.Collaboration', {returnObjects: true});
return (
)
};
const ReplyActions = ({comment, reply, onCommentMenuClick, opened, openActionReply}) => {
const { t } = useTranslation();
const _t = t('Common.Collaboration', {returnObjects: true});
return (
)
};
// Edit comment
const EditCommentPopup = inject("storeComments")(observer(({storeComments, comment, onEditComment}) => {
const { t } = useTranslation();
const _t = t('Common.Collaboration', {returnObjects: true});
useEffect(() => {
f7.popup.open('.edit-comment-popup');
});
const [stateText, setText] = useState(comment.comment);
return (
{
f7.popup.close('.edit-comment-popup');
setTimeout(() => {
storeComments.openEditComment(false);
}, 500);
}}>{_t.textCancel}
{_t.textEditComment}
{
f7.popup.close('.edit-comment-popup');
setTimeout(() => {
storeComments.openEditComment(false);
onEditComment(comment, stateText);
}, 500);
}}
>
{Device.android ? : _t.textDone}
{Device.android &&
{comment.userInitials}
}
{comment.userName}
{comment.date}
{setText(event.target.value);}}>
)
}));
const EditCommentDialog = inject("storeComments")(observer(({storeComments, comment, onEditComment}) => {
const { t } = useTranslation();
const _t = t('Common.Collaboration', {returnObjects: true});
const templateInitials = `${comment.userInitials}
`;
useEffect(() => {
f7.dialog.create({
destroyOnClose: true,
containerEl: document.getElementById('edit-comment-dialog'),
content:
`
`,
on: {
opened: () => {
const cancel = document.getElementById('comment-cancel');
cancel.addEventListener('click', () => {
f7.dialog.close();
storeComments.openEditComment(false);
});
const done = document.getElementById('comment-done');
done.addEventListener('click', () => {
const value = document.getElementById('comment-text').value;
if (value.length > 0) {
onEditComment(comment, value);
f7.dialog.close();
storeComments.openEditComment(false);
}
});
const area = document.getElementById('comment-text');
area.addEventListener('input', (event) => {
if (event.target.value.length === 0 && !done.classList.contains('disabled')) {
done.classList.add('disabled');
} else if (event.target.value.length > 0 && done.classList.contains('disabled')) {
done.classList.remove('disabled');
}
});
},
open: () => {
$$('.dialog-backdrop.backdrop-in')[0].classList.add('over-popover');
},
closed: () => {
$$('.dialog-backdrop.backdrop-in')[0].classList.remove('over-popover');
}
}
}).open();
});
return (
);
}));
const EditComment = ({comment, onEditComment}) => {
return (
Device.phone ?
:
)
};
const AddReplyPopup = inject("storeComments")(observer(({storeComments, userInfo, comment, onAddReply}) => {
const { t } = useTranslation();
const _t = t('Common.Collaboration', {returnObjects: true});
useEffect(() => {
f7.popup.open('.add-reply-popup');
});
const [stateText, setText] = useState('');
return (
{
f7.popup.close('.add-reply-popup');
setTimeout(() => {
storeComments.openAddReply(false);
}, 500);
}}>{_t.textCancel}
{_t.textAddReply}
{
f7.popup.close('.add-reply-popup');
setTimeout(() => {
storeComments.openAddReply(false);
onAddReply(comment, stateText);
}, 500);
}}>
{Device.android ? : _t.textDone}
{Device.android &&
{userInfo.initials}
}
{userInfo.name}
{setText(event.target.value);}}>
)
}));
const AddReplyDialog = inject("storeComments")(observer(({storeComments, userInfo, comment, onAddReply}) => {
const { t } = useTranslation();
const _t = t('Common.Collaboration', {returnObjects: true});
const templateInitials = `${userInfo.initials}
`;
useEffect(() => {
f7.dialog.create({
destroyOnClose: true,
containerEl: document.getElementById('add-reply-dialog'),
content:
`
`,
on: {
opened: () => {
const cancel = document.getElementById('reply-cancel');
cancel.addEventListener('click', () => {
f7.dialog.close();
storeComments.openAddReply(false);
});
const done = document.getElementById('reply-done');
done.addEventListener('click', () => {
const value = document.getElementById('reply-text').value;
if (value.length > 0) {
onAddReply(comment, value);
f7.dialog.close();
storeComments.openAddReply(false);
}
});
const area = document.getElementById('reply-text');
area.addEventListener('input', (event) => {
if (event.target.value.length === 0 && !done.classList.contains('disabled')) {
done.classList.add('disabled');
} else if (event.target.value.length > 0 && done.classList.contains('disabled')) {
done.classList.remove('disabled');
}
});
done.classList.add('disabled');
},
open: () => {
$$('.dialog-backdrop.backdrop-in')[0].classList.add('over-popover');
},
closed: () => {
$$('.dialog-backdrop.backdrop-in')[0].classList.remove('over-popover');
}
}
}).open();
});
return (
);
}));
const AddReply = ({userInfo, comment, onAddReply}) => {
return (
Device.phone ?
:
)
};
const EditReplyPopup = inject("storeComments")(observer(({storeComments, comment, reply, onEditReply}) => {
const { t } = useTranslation();
const _t = t('Common.Collaboration', {returnObjects: true});
useEffect(() => {
f7.popup.open('.edit-reply-popup');
});
const [stateText, setText] = useState(reply.reply);
return (
{
f7.popup.close('.edit-reply-popup');
setTimeout(() => {
storeComments.openEditReply(false);
}, 500);
}}>{_t.textCancel}
{_t.textEditReply}
{
f7.popup.close('.edit-reply-popup');
setTimeout(() => {
storeComments.openEditReply(false);
onEditReply(comment, reply, stateText);
}, 500);
}}
>
{Device.android ? : _t.textDone}
{Device.android &&
{reply.userInitials}
}
{reply.userName}
{reply.date}
{setText(event.target.value);}}>
)
}));
const EditReplyDialog = inject("storeComments")(observer(({storeComments, comment, reply, onEditReply}) => {
const { t } = useTranslation();
const _t = t('Common.Collaboration', {returnObjects: true});
const templateInitials = `${reply.userInitials}
`;
useEffect(() => {
f7.dialog.create({
destroyOnClose: true,
containerEl: document.getElementById('edit-reply-dialog'),
content:
`
`,
on: {
opened: () => {
const cancel = document.getElementById('reply-cancel');
cancel.addEventListener('click', () => {
f7.dialog.close();
storeComments.openEditReply(false);
});
const done = document.getElementById('reply-done');
done.addEventListener('click', () => {
const value = document.getElementById('reply-text').value;
if (value.length > 0) {
onEditReply(comment, reply, value);
f7.dialog.close();
storeComments.openEditReply(false);
}
});
const area = document.getElementById('reply-text');
area.addEventListener('input', (event) => {
if (event.target.value.length === 0 && !done.classList.contains('disabled')) {
done.classList.add('disabled');
} else if (event.target.value.length > 0 && done.classList.contains('disabled')) {
done.classList.remove('disabled');
}
});
},
open: () => {
$$('.dialog-backdrop.backdrop-in')[0].classList.add('over-popover');
},
closed: () => {
$$('.dialog-backdrop.backdrop-in')[0].classList.remove('over-popover');
}
}
}).open();
});
return (
);
}));
const EditReply = ({comment, reply, onEditReply}) => {
return (
Device.phone ?
:
)
};
// View comments
const ViewComments = ({storeComments, storeAppOptions, onCommentMenuClick, onResolveComment}) => {
const { t } = useTranslation();
const _t = t('Common.Collaboration', {returnObjects: true});
const isAndroid = Device.android;
const viewMode = !storeAppOptions.canComments;
const comments = storeComments.groupCollectionFilter || storeComments.collectionComments;
const sortComments = comments.length > 0 ? [...comments].sort((a, b) => a.time > b.time ? 1 : -1) : null;
const [clickComment, setComment] = useState();
const [commentActionsOpened, openActionComment] = useState(false);
const [reply, setReply] = useState();
const [replyActionsOpened, openActionReply] = useState(false);
return (
{!sortComments ?
{_t.textNoComments}
:
{sortComments.map((comment, indexComment) => {
return (
{isAndroid &&
{comment.userInitials}
}
{comment.userName}
{comment.date}
{!viewMode &&
{onResolveComment(comment);}}>
{setComment(comment); openActionComment(true);}}
>
}
{comment.quote &&
{sliceQuote(comment.quote)}
}
{comment.replies.length > 0 &&
{comment.replies.map((reply, indexReply) => {
return (
-
{isAndroid &&
{reply.userInitials}
}
{reply.userName}
{reply.date}
{!viewMode &&
{setComment(comment); setReply(reply); openActionReply(true);}}
>
}
)
})}
}
)
})}
}
)
};
const _ViewComments = inject('storeComments', 'storeAppOptions')(observer(ViewComments));
const CommentList = inject("storeComments", "storeAppOptions")(observer(({storeComments, storeAppOptions, onCommentMenuClick, onResolveComment}) => {
const { t } = useTranslation();
const _t = t('Common.Collaboration', {returnObjects: true});
const isAndroid = Device.android;
const viewMode = !storeAppOptions.canComments;
const comments = storeComments.showComments;
const [currentIndex, setCurrentIndex] = useState(0);
const comment = comments[currentIndex];
const [commentActionsOpened, openActionComment] = useState(false);
const [reply, setReply] = useState();
const [replyActionsOpened, openActionReply] = useState(false);
const onViewPrevComment = () => {
if (currentIndex - 1 < 0) {
setCurrentIndex(comments.length - 1);
} else {
setCurrentIndex(currentIndex - 1);
}
};
const onViewNextComment = () => {
if (currentIndex + 1 === comments.length) {
setCurrentIndex(0);
} else {
setCurrentIndex(currentIndex + 1);
}
};
let arrayComments = []
comment.comment && comment.comment.replace(/((?:https?:\/\/|ftps?:\/\/|\bwww\.)(?:(?![.,?!;:()]*(?:\s|$))[^\s]){2,})|(\n+|(?:(?!(?:https?:\/\/|ftp:\/\/|\bwww\.)(?:(?![.,?!;:()]*(?:\s|$))[^\s]){2,}).)+)/gim,
(match, link, text) => {console.log(match); arrayComments.push(link ? window.open(link)} href={(link[0]==="w" ? "//" : "") + link} key={arrayComments.length}>{link}
: text)})
return (
{!viewMode &&
{onCommentMenuClick('addReply', comment);}}>{_t.textAddReply}
}
{isAndroid &&
{comment.userInitials}
}
{comment.userName}
{comment.date}
{!viewMode &&
{onResolveComment(comment);}}>
{openActionComment(true);}}
>
}
{comment.quote &&
{sliceQuote(comment.quote)}
}
{comment.replies.length > 0 &&
{comment.replies.map((reply, indexReply) => {
return (
-
{isAndroid &&
{reply.userInitials}
}
{reply.userName}
{reply.date}
{!viewMode &&
{setReply(reply); openActionReply(true);}}
>
}
)
})}
}
)
}));
const ViewCommentSheet = ({closeCurComments, onCommentMenuClick, onResolveComment}) => {
useEffect(() => {
f7.sheet.open('#view-comment-sheet');
});
const [stateHeight, setHeight] = useState('45%');
const [stateOpacity, setOpacity] = useState(1);
const [stateStartY, setStartY] = useState();
const [isNeedClose, setNeedClose] = useState(false);
const handleTouchStart = (event) => {
const touchObj = event.changedTouches[0];
setStartY(parseInt(touchObj.clientY));
};
const handleTouchMove = (event) => {
const touchObj = event.changedTouches[0];
const dist = parseInt(touchObj.clientY) - stateStartY;
if (dist < 0) { // to top
setHeight('90%');
setOpacity(1);
setNeedClose(false);
} else if (dist < 80) {
setHeight('45%');
setOpacity(1);
setNeedClose(false);
} else {
setNeedClose(true);
setOpacity(0.6);
}
};
const handleTouchEnd = (event) => {
console.log('end');
const touchObj = event.changedTouches[0];
const swipeEnd = parseInt(touchObj.clientY);
const dist = swipeEnd - stateStartY;
if (isNeedClose) {
closeCurComments();
} else if (stateHeight === '90%' && dist > 20) {
setHeight('45%');
}
};
return (
)
};
const ViewCommentPopover = ({onCommentMenuClick, onResolveComment}) => {
useEffect(() => {
f7.popover.open('#view-comment-popover', '#btn-coauth');
});
return (
)
};
const ViewCurrentComments = props => {
return (
Device.phone ?
:
)
};
export {
AddComment,
EditComment,
AddReply,
EditReply,
_ViewComments as ViewComments,
ViewCurrentComments
};