Skip to content

Commit

Permalink
feat: add websocket for delete message operation (#139)
Browse files Browse the repository at this point in the history
* feat: add websocket for delete message operation

* feat: add mutation for delete chat message
  • Loading branch information
spaenleh authored Mar 10, 2022
1 parent f8cff60 commit 9b111d9
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 5 deletions.
15 changes: 13 additions & 2 deletions src/api/chat.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { PartialChatMessage, QueryClientConfig, UUID } from '../types';
import { PartialChatMessage, PartialNewChatMessage, QueryClientConfig, UUID } from '../types';
import configureAxios, {
fallbackToPublic,
verifyAuthentication,
} from './axios';
import {
buildDeleteItemChatMessageRoute,
buildGetItemChatRoute,
buildGetPublicItemChatRoute,
buildPostItemChatMessageRoute,
Expand All @@ -18,7 +19,7 @@ export const getItemChat = async (id: UUID, { API_HOST }: QueryClientConfig) =>
);

export const postItemChatMessage = async (
{ chatId, body }: PartialChatMessage,
{ chatId, body }: PartialNewChatMessage,
{ API_HOST }: QueryClientConfig,
) =>
verifyAuthentication(() =>
Expand All @@ -28,3 +29,13 @@ export const postItemChatMessage = async (
})
.then(({ data }) => data),
);

export const deleteItemChatMessage = async (
{ chatId, messageId }: PartialChatMessage,
{ API_HOST }: QueryClientConfig,
) =>
verifyAuthentication(() =>
axios
.delete(`${API_HOST}/${buildDeleteItemChatMessageRoute(chatId, messageId)}`)
.then(({ data }) => data),
);
3 changes: 3 additions & 0 deletions src/api/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export const buildGetPublicItemChatRoute = (id: UUID) =>
`${PUBLIC_PREFIX}/${buildGetItemChatRoute(id)}`;
export const buildPostItemChatMessageRoute = (id: UUID) =>
`${ITEMS_ROUTE}/${id}/chat`;
export const buildDeleteItemChatMessageRoute = (chatId: UUID, messageId: UUID) =>
`${ITEMS_ROUTE}/${chatId}/chat/${messageId}`;

export const buildGetMemberBy = (email: string) =>
`${MEMBERS_ROUTE}/search?email=${email.toLowerCase()}`;
Expand Down Expand Up @@ -260,6 +262,7 @@ export const API_ROUTES = {
buildGetPublicChildrenRoute,
buildGetItemChatRoute,
buildPostItemChatMessageRoute,
buildDeleteItemChatMessageRoute,
buildRecycleItemRoute,
buildRecycleItemsRoute,
buildGetPublicItemsWithTag,
Expand Down
1 change: 1 addition & 0 deletions src/config/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export const MUTATION_KEYS = {
EDIT_ITEM_MEMBERSHIP: 'editItemMembership',
DELETE_ITEM_MEMBERSHIP: 'deleteItemMembership',
POST_ITEM_CHAT_MESSAGE: 'postChatMessage',
DELETE_ITEM_CHAT_MESSAGE: 'deleteChatMessage',
RECYCLE_ITEM: 'recycleItem',
RECYCLE_ITEMS: 'recycleItems',
RESTORE_ITEMS: 'restoreItems',
Expand Down
21 changes: 19 additions & 2 deletions src/mutations/chat.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { QueryClient } from 'react-query';
import * as Api from '../api';
import { buildItemChatKey, MUTATION_KEYS } from '../config/keys';
import { postItemChatMessageRoutine } from '../routines';
import { deleteItemChatMessageRoutine, postItemChatMessageRoutine } from '../routines';
import { QueryClientConfig } from '../types';

const { POST_ITEM_CHAT_MESSAGE } = MUTATION_KEYS;
const { POST_ITEM_CHAT_MESSAGE, DELETE_ITEM_CHAT_MESSAGE } = MUTATION_KEYS;

export default (queryClient: QueryClient, queryConfig: QueryClientConfig) => {
queryClient.setMutationDefaults(POST_ITEM_CHAT_MESSAGE, {
Expand All @@ -23,4 +23,21 @@ export default (queryClient: QueryClient, queryConfig: QueryClientConfig) => {
}
},
});

queryClient.setMutationDefaults(DELETE_ITEM_CHAT_MESSAGE, {
mutationFn: (chatMsg) => Api.deleteItemChatMessage(chatMsg, queryConfig),
onError: (error) => {
queryConfig.notifier?.({
type: deleteItemChatMessageRoutine.FAILURE,
payload: { error },
});
},
onSettled: (_data, _error, { chatId }) => {
// invalidate keys only if websockets are disabled
// otherwise the cache is updated automatically
if (!queryConfig.enableWebsocket) {
queryClient.invalidateQueries(buildItemChatKey(chatId));
}
},
});
};
4 changes: 4 additions & 0 deletions src/routines/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ import createRoutine from './utils';
export const postItemChatMessageRoutine = createRoutine(
'POST_ITEM_CHAT_MESSAGE',
);

export const deleteItemChatMessageRoutine = createRoutine(
'DELETE_ITEM_CHAT_MESSAGE',
);
8 changes: 7 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,17 @@ export enum PERMISSION_LEVELS {
ADMIN = 'admin',
}

export type PartialChatMessage = {
export type PartialNewChatMessage = {
chatId: string;
body: string;
};

export type PartialChatMessage = {
chatId: string;
messageId: string;
body?: string;
};

export type ChatMessage = {
id: string,
chatId: string;
Expand Down
5 changes: 5 additions & 0 deletions src/ws/hooks/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ export const configureWsChatHooks = (
queryClient.setQueryData(chatKey, mutation);
break;
}
case OPS.DELETE: {
const mutation = current.update('messages', (messages) => messages.filter(m => m.id !== event.message.id));
queryClient.setQueryData(chatKey, mutation);
break;
}
default:
break;
}
Expand Down

0 comments on commit 9b111d9

Please sign in to comment.