Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chat: after deleting a message it disappears only after refresh #2354 #2398

Merged
merged 3 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
5 changes: 5 additions & 0 deletions src/shared/components/DeleteModal/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@
.delete-modal__button-container__cancel {
margin-right: 0.75rem;
}

.delete-modal__button-container__send__loader {
left: 0;
transform: unset;
}
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,
};
}),
);
Loading