|
|
|
@ -1,16 +1,25 @@
@@ -1,16 +1,25 @@
|
|
|
|
|
import { hasImageUrl, ICommentFullInfo } from '@/shared' |
|
|
|
|
import { MessageType } from './../../../shared/enums/chat.enums' |
|
|
|
|
import { appEvents, hasImageUrl, IComment, ICommentFullInfo } from '@/shared' |
|
|
|
|
import { commentsService } from '@/services/domain' |
|
|
|
|
import { useEffect, useState } from 'react' |
|
|
|
|
import { useCallback, useEffect, useState } from 'react' |
|
|
|
|
import { useSelector } from 'react-redux' |
|
|
|
|
import { selectId } from '@/store/account' |
|
|
|
|
import _ from 'lodash' |
|
|
|
|
import { GiftedChat } from 'react-native-gifted-chat' |
|
|
|
|
import { transformComments } from '../transforms' |
|
|
|
|
import { transformComment, transformComments } from '../transforms' |
|
|
|
|
import { CommentActionEnum } from '../enums/comment-actions.enum' |
|
|
|
|
import { getCommentsMenuOptions } from '../configs/comments-menu-options.config' |
|
|
|
|
import { |
|
|
|
|
deleteComment, |
|
|
|
|
editComment, |
|
|
|
|
readComments, |
|
|
|
|
} from '@/api/comments/requests' |
|
|
|
|
|
|
|
|
|
export const useTaskComments = (taskId: number) => { |
|
|
|
|
const accountId = useSelector(selectId) |
|
|
|
|
const [comments, setComments] = useState([]) |
|
|
|
|
const [commentText, setCommentText] = useState('') |
|
|
|
|
const [editableComment, setEditableComment] = useState(null) |
|
|
|
|
const [isLoading, setLoad] = useState<boolean>(true) |
|
|
|
|
const [isSending, setSending] = useState(false) |
|
|
|
|
|
|
|
|
@ -33,25 +42,94 @@ export const useTaskComments = (taskId: number) => {
@@ -33,25 +42,94 @@ export const useTaskComments = (taskId: number) => {
|
|
|
|
|
useEffect(() => { |
|
|
|
|
if (taskId) loadComments() |
|
|
|
|
}, [taskId]) |
|
|
|
|
|
|
|
|
|
const onEditComment = (comment: IComment) => { |
|
|
|
|
setEditableComment({ |
|
|
|
|
...comment, |
|
|
|
|
type: MessageType.Text, |
|
|
|
|
}) |
|
|
|
|
setCommentText((comment.content as any)?.message) |
|
|
|
|
} |
|
|
|
|
const sendComment = async () => { |
|
|
|
|
if (_.isEmpty(commentText)) return |
|
|
|
|
setSending(true) |
|
|
|
|
try { |
|
|
|
|
const comment = await commentsService.addTaskComment({ |
|
|
|
|
taskId, |
|
|
|
|
content: commentText.trim(), |
|
|
|
|
}) |
|
|
|
|
if (editableComment) { |
|
|
|
|
const respEdit = await editComment( |
|
|
|
|
{ content: commentText }, |
|
|
|
|
editableComment.id, |
|
|
|
|
) |
|
|
|
|
console.log('respEdit', respEdit) |
|
|
|
|
setComments(prev => { |
|
|
|
|
return [...prev].map(it => { |
|
|
|
|
if (it.id !== editableComment.id) return it |
|
|
|
|
return transformComment( |
|
|
|
|
{ |
|
|
|
|
...it, |
|
|
|
|
content: commentText, |
|
|
|
|
}, |
|
|
|
|
accountId, |
|
|
|
|
) |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|
setCommentText('') |
|
|
|
|
setEditableComment(null) |
|
|
|
|
} else { |
|
|
|
|
const comment = await commentsService.addTaskComment({ |
|
|
|
|
taskId, |
|
|
|
|
content: commentText.trim(), |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
setComments(prev => { |
|
|
|
|
return [...prev, ...transformComments([comment], accountId)] |
|
|
|
|
}) |
|
|
|
|
setCommentText(null) |
|
|
|
|
setComments(prev => { |
|
|
|
|
return [...prev, ...transformComments([comment], accountId)] |
|
|
|
|
}) |
|
|
|
|
setCommentText(null) |
|
|
|
|
} |
|
|
|
|
} catch (e) { |
|
|
|
|
console.log('ADD COMMENT ERROR', e) |
|
|
|
|
} finally { |
|
|
|
|
setSending(false) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
const onDeleteComment = (item: IComment) => { |
|
|
|
|
setTimeout( |
|
|
|
|
() => |
|
|
|
|
appEvents.emit('openConfirmModal', { |
|
|
|
|
title: 'Ви впевнені, що хочете \n видалити коментар?', |
|
|
|
|
buttonToHighlight: 'allow', |
|
|
|
|
allowBtnAction: async () => { |
|
|
|
|
const temp = comments.filter(el => el.id !== item.id) |
|
|
|
|
setComments(temp) |
|
|
|
|
await deleteComment(item.id) |
|
|
|
|
}, |
|
|
|
|
notAllowBtnAction: () => {}, |
|
|
|
|
}), |
|
|
|
|
300, |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const actions = { |
|
|
|
|
[CommentActionEnum.DELETE]: onDeleteComment, |
|
|
|
|
[CommentActionEnum.EDIT]: onEditComment, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const onLongPress = useCallback( |
|
|
|
|
(id: any) => { |
|
|
|
|
const item = comments.find(el => el.id === id) |
|
|
|
|
if (!item?.isMy) return |
|
|
|
|
appEvents.emit('openActionSheet', { |
|
|
|
|
items: getCommentsMenuOptions({ |
|
|
|
|
canEdit: item?.isMy, |
|
|
|
|
onPress: (actionType: CommentActionEnum) => |
|
|
|
|
actions[actionType](item), |
|
|
|
|
}), |
|
|
|
|
}) |
|
|
|
|
}, |
|
|
|
|
[comments, taskId], |
|
|
|
|
) |
|
|
|
|
const cancelEdit = () => { |
|
|
|
|
setEditableComment(null) |
|
|
|
|
setCommentText('') |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return { |
|
|
|
|
isLoading, |
|
|
|
@ -60,5 +138,8 @@ export const useTaskComments = (taskId: number) => {
@@ -60,5 +138,8 @@ export const useTaskComments = (taskId: number) => {
|
|
|
|
|
setCommentText, |
|
|
|
|
sendComment, |
|
|
|
|
isSending, |
|
|
|
|
onLongPress, |
|
|
|
|
editableComment, |
|
|
|
|
cancelEdit, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|