From 62d4e032062801c8fb14329baf10851dbe5f857d Mon Sep 17 00:00:00 2001 From: Fedor Katurov Date: Mon, 9 Nov 2020 18:44:36 +0700 Subject: [PATCH] added comment form drop zone --- src/components/comment/CommentForm/index.tsx | 121 +++++++++--------- .../comment/CommentFormAttaches/index.tsx | 8 +- .../comment/CommentFormButtons/index.tsx | 6 +- .../comment/CommentFormDropzone/index.tsx | 14 ++ .../editors/SortableAudioGrid/index.tsx | 2 - .../editors/SortableImageGrid/index.tsx | 43 ++++--- src/redux/uploads/constants.ts | 5 + src/utils/hooks.ts | 36 ++++-- 8 files changed, 142 insertions(+), 93 deletions(-) create mode 100644 src/components/comment/CommentFormDropzone/index.tsx diff --git a/src/components/comment/CommentForm/index.tsx b/src/components/comment/CommentForm/index.tsx index a7ff6c2f..6481aa7e 100644 --- a/src/components/comment/CommentForm/index.tsx +++ b/src/components/comment/CommentForm/index.tsx @@ -20,6 +20,7 @@ import { getRandomPhrase } from '~/constants/phrases'; import { ERROR_LITERAL } from '~/constants/errors'; import { CommentFormAttaches } from '~/components/comment/CommentFormAttaches'; import { CommentFormAttachButtons } from '~/components/comment/CommentFormButtons'; +import { CommentFormDropzone } from '~/components/comment/CommentFormDropzone'; const mapStateToProps = (state: IState) => ({ node: selectNode(state), @@ -50,8 +51,12 @@ const CommentFormUnconnected: FC = memo( uploadUploadFiles, nodeCancelCommentEdit, }) => { + const comment = useMemo(() => comment_data[id], [comment_data, id]); + const onUpload = useCallback( (files: File[]) => { + console.log(files); + const items: IFileWithUUID[] = files.map( (file: File): IFileWithUUID => ({ file, @@ -64,28 +69,25 @@ const CommentFormUnconnected: FC = memo( const temps = items.map(file => file.temp_id); - nodeSetCommentData( - id, - assocPath(['temp_ids'], [...comment_data[id].temp_ids, ...temps], comment_data[id]) - ); + nodeSetCommentData(id, assocPath(['temp_ids'], [...comment.temp_ids, ...temps], comment)); uploadUploadFiles(items); }, - [uploadUploadFiles, comment_data, id, nodeSetCommentData] + [uploadUploadFiles, comment, id, nodeSetCommentData] ); const onInput = useCallback( text => { - nodeSetCommentData(id, assocPath(['text'], text, comment_data[id])); + nodeSetCommentData(id, assocPath(['text'], text, comment)); }, - [nodeSetCommentData, comment_data, id] + [nodeSetCommentData, comment, id] ); useEffect(() => { - const temp_ids = (comment_data && comment_data[id] && comment_data[id].temp_ids) || []; + const temp_ids = (comment && comment.temp_ids) || []; const added_files = temp_ids .map(temp_uuid => statuses[temp_uuid] && statuses[temp_uuid].uuid) .map(el => !!el && files[el]) - .filter(el => !!el && !comment_data[id].files.some(file => file && file.id === el.id)); + .filter(el => !!el && !comment.files.some(file => file && file.id === el.id)); const filtered_temps = temp_ids.filter( temp_id => @@ -95,25 +97,23 @@ const CommentFormUnconnected: FC = memo( if (added_files.length) { nodeSetCommentData(id, { - ...comment_data[id], + ...comment, temp_ids: filtered_temps, - files: [...comment_data[id].files, ...added_files], + files: [...comment.files, ...added_files], }); } }, [statuses, files]); - const comment = useMemo(() => comment_data[id], [comment_data, id]); - - const is_uploading_files = useMemo(() => comment.temp_ids.length > 0, [comment.temp_ids]); + const isUploadingNow = useMemo(() => comment.temp_ids.length > 0, [comment.temp_ids]); const onSubmit = useCallback( event => { if (event) event.preventDefault(); - if (is_uploading_files || is_sending_comment) return; + if (isUploadingNow || is_sending_comment) return; nodePostComment(id, is_before); }, - [nodePostComment, id, is_before, is_uploading_files, is_sending_comment] + [nodePostComment, id, is_before, isUploadingNow, is_sending_comment] ); const onKeyDown = useCallback>( @@ -172,56 +172,59 @@ const CommentFormUnconnected: FC = memo( ); return ( -
-
-