Skip to content

Commit

Permalink
CW-2354 Added handler action for deletion messages
Browse files Browse the repository at this point in the history
  • Loading branch information
pvm-code committed Dec 15, 2023
1 parent ef1c5c4 commit d8d1b8f
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/pages/common/components/ChatComponent/ChatComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,8 @@ export default function ChatComponent({
(messageId: string) => {
if (isChatChannel) {
chatMessagesData.deleteChatMessage(messageId);
} else {
discussionMessagesData.deleteDiscussionMessage(messageId);
}
},
[isChatChannel, chatMessagesData.deleteChatMessage],
Expand Down
1 change: 1 addition & 0 deletions src/shared/components/DeleteModal/DeleteModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const DeleteModal: FC<PropsWithChildren<ReportModalProps>> = (props) => {
return;
}

onDeleteOutside?.(entity.id);
setLoading(false);
notify("The message has deleted!");
onClose();
Expand Down
13 changes: 12 additions & 1 deletion src/shared/hooks/useCases/useDiscussionMessagesById.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from "react";
import { useEffect, useState, useCallback } from "react";
import { useDispatch, useSelector } from "react-redux";
import { useUpdateEffect } from "react-use";
import classNames from 'classnames';
Expand Down Expand Up @@ -47,6 +47,7 @@ interface Return extends State {
fetchDiscussionMessages: () => void;
fetchRepliedMessages: (messageId: string, endDate: Date) => Promise<void>;
addDiscussionMessage: (discussionMessage: DiscussionMessage) => void;
deleteDiscussionMessage: (discussionMessageId: string) => void;
isEndOfList: Record<string, boolean> | null;
rawData: DiscussionMessage[] | null;
}
Expand Down Expand Up @@ -123,6 +124,15 @@ export const useDiscussionMessagesById = ({
);
};

const deleteDiscussionMessage = useCallback((discussionMessageId: string) => {
dispatch(
cacheActions.deleteDiscussionMessageById({
discussionId,
discussionMessageId
})
)
}, []);

const fetchRepliedMessages = async (messageId: string, endDate: Date): Promise<void> => {
if (state.data?.find((item) => item.id === discussionId)) {
return Promise.resolve();
Expand Down Expand Up @@ -327,5 +337,6 @@ export const useDiscussionMessagesById = ({
fetchDiscussionMessages,
fetchRepliedMessages,
addDiscussionMessage,
deleteDiscussionMessage,
};
};
7 changes: 7 additions & 0 deletions src/store/states/cache/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ export const addDiscussionMessageByDiscussionId = createStandardAction(
discussionMessage: DiscussionMessageWithParsedText;
}>();

export const deleteDiscussionMessageById = createStandardAction(
CacheActionType.DELETE_DISCUSSION_MESSAGE_BY_ID,
)<{
discussionId: string;
discussionMessageId: string;
}>();

export const getProposalStateById = createAsyncAction(
CacheActionType.GET_PROPOSAL_STATE_BY_ID,
CacheActionType.GET_PROPOSAL_STATE_BY_ID_SUCCESS,
Expand Down
1 change: 1 addition & 0 deletions src/store/states/cache/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export enum CacheActionType {
UPDATE_DISCUSSION_STATES = "@CACHE/UPDATE_DISCUSSION_STATES",
UPDATE_DISCUSSION_STATE_BY_DISCUSSION_ID = "@CACHE/UPDATE_DISCUSSION_STATE_BY_DISCUSSION_ID",
ADD_DISCUSSION_MESSAGE_BY_DISCUSSION_ID = "@CACHE/ADD_DISCUSSION_MESSAGE_BY_DISCUSSION_ID",
DELETE_DISCUSSION_MESSAGE_BY_ID = "@CACHE/DELETE_DISCUSSION_MESSAGE_BY_ID",
UPDATE_DISCUSSION_STATE_BY_DISCUSSION_MESSAGES_ACTUAL_ID = "@CACHE/UPDATE_DISCUSSION_STATE_BY_DISCUSSION_MESSAGES_ACTUAL_ID",
GET_PROPOSAL_STATE_BY_ID = "@CACHE/GET_PROPOSAL_STATE_BY_ID",
GET_PROPOSAL_STATE_BY_ID_SUCCESS = "@CACHE/GET_PROPOSAL_STATE_BY_ID_SUCCESS",
Expand Down
14 changes: 14 additions & 0 deletions src/store/states/cache/reducer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,18 @@ export const reducer = createReducer<CacheState, Action>(INITIAL_CACHE_STATE)
data: updatedDiscussionMessages,
};
}),
)
.handleAction(actions.deleteDiscussionMessageById, (state, { payload }) =>
produce(state, (nextState) => {
const { discussionMessageId, discussionId } = payload;

const updatedDiscussionMessages = (
state.discussionMessagesStates[discussionId]?.data ?? []
).filter((message) => message.id !== discussionMessageId);

nextState.discussionMessagesStates[discussionId] = {
...state.discussionMessagesStates[discussionId],
data: updatedDiscussionMessages,
};
}),
);

0 comments on commit d8d1b8f

Please sign in to comment.