Skip to content

Commit 3bdb961

Browse files
authored
Merge pull request #8 from commt/unsubscribe-logic
Unsubscribe Logic
2 parents 89f744b + 3d4adb6 commit 3bdb961

File tree

8 files changed

+74
-2
lines changed

8 files changed

+74
-2
lines changed

src/context/actions/messagesAction.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,8 @@ export const addMoreMessages =
2929
(data: AddMoreMessagesProps) => (dispatch: Dispatch<CommtContextActions>) => {
3030
dispatch({ type: "ADD_MORE_MESSAGES", payload: data });
3131
};
32+
33+
export const deleteMessages =
34+
(roomId: string) => (dispatch: Dispatch<CommtContextActions>) => {
35+
dispatch({ type: "DELETE_MESSAGES", payload: roomId });
36+
};

src/context/actions/roomsActions.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ export const addRoom =
2424
dispatch({ type: "ADD_ROOM", payload: room });
2525
};
2626

27+
export const deleteRoom =
28+
(chatRoomAuthId: string) => (dispatch: Dispatch<CommtContextActions>) => {
29+
dispatch({ type: "DELETE_ROOM", payload: chatRoomAuthId });
30+
};
31+
2732
export const updateLastMessage =
2833
(data: UpdateLastMessageProps) =>
2934
(dispatch: Dispatch<CommtContextActions>) => {

src/context/reducers/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export const InitialState: CommtContextData = {
3939
type RoomsActions =
4040
| { type: "SET_ROOMS"; payload: RoomProps[] }
4141
| { type: "ADD_ROOM"; payload: RoomProps }
42+
| { type: "DELETE_ROOM"; payload: string }
4243
| { type: "UPDATE_LAST_MESSAGE"; payload: UpdateLastMessageProps }
4344
| { type: "UPDATE_READ_TOKEN"; payload: ReadTokenProps }
4445
| { type: "UPDATE_UNREAD_MSG_COUNT"; payload: UpdateUnreadMsgCountProps };
@@ -51,7 +52,8 @@ type UsersActions =
5152
type MessagesActions =
5253
| { type: "SET_MESSAGES"; payload: IMessagesData }
5354
| { type: "ADD_MESSAGE"; payload: AddMessageProps }
54-
| { type: "ADD_MORE_MESSAGES"; payload: AddMoreMessagesProps };
55+
| { type: "ADD_MORE_MESSAGES"; payload: AddMoreMessagesProps }
56+
| { type: "DELETE_MESSAGES"; payload: string };
5557

5658
type AppActions =
5759
| { type: "TOGGLE_THEME"; payload: DefaultTheme }

src/context/reducers/messagesReducer.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ export function messagesReducer(
5757
};
5858
}
5959

60+
case "DELETE_MESSAGES": {
61+
const roomId = action.payload;
62+
63+
const messages = { ...state };
64+
delete messages[roomId];
65+
66+
return messages;
67+
}
68+
6069
default: {
6170
return state;
6271
}

src/context/reducers/roomsReducer.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ export function roomsReducer(state: RoomProps[], action: CommtContextActions) {
2222
return [...state, action.payload];
2323
}
2424

25+
case "DELETE_ROOM": {
26+
const rooms = [...state];
27+
const roomIndexToBeDeleted = rooms.findIndex(
28+
(r) => r.chatRoomAuthId === action.payload,
29+
); // action.payload represents the chatRoomAuthId of the room to be deleted from the state
30+
rooms.splice(roomIndexToBeDeleted, 1); // Delete that particular index from the instance of the rooms
31+
32+
return rooms;
33+
}
34+
2535
case "UPDATE_LAST_MESSAGE": {
2636
const { roomId, lastMessage } = action.payload;
2737
return state.map((room) =>

src/utils/SocketController.tsx

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import { connect, socket, DataProps, MessageInfoProps } from "./socket";
33
import { CommtContext } from "../context/Context";
44
import * as types from "./emitTypes";
55
import { updateUserOnline } from "../context/actions/usersActions";
6-
import { addMessage } from "../context/actions/messagesAction";
6+
import { addMessage, deleteMessages } from "../context/actions/messagesAction";
77
import {
88
addRoom,
9+
deleteRoom,
910
updateLastMessage,
1011
updateReadToken,
1112
} from "../context/actions/roomsActions";
@@ -211,6 +212,38 @@ const SocketController = () => {
211212
});
212213
}, []);
213214

215+
useEffect(() => {
216+
socket.on(types.UNSUBSCRIBE_ROOM, (chatRoomAuthId) => {
217+
try {
218+
// Send request to server to leave room
219+
socket.emit(types.LEAVE_ROOM, chatRoomAuthId, ({ status }) => {
220+
// Check if the socket successfully leaves the room
221+
if (status === "success") {
222+
const roomId = rooms.find(
223+
(room) => room.chatRoomAuthId === chatRoomAuthId,
224+
)?.roomId;
225+
226+
// Update the context
227+
deleteRoom(chatRoomAuthId)(dispatch);
228+
roomId && deleteMessages(roomId)(dispatch);
229+
}
230+
});
231+
} catch (error) {
232+
handleLogger({
233+
apiKey,
234+
subscriptionKey,
235+
projectName,
236+
chatAuthId: selfUser?.chatAuthId,
237+
chatRoomAuthId: chatRoomAuthId,
238+
error: {
239+
error,
240+
event: types.UNSUBSCRIBE_ROOM,
241+
},
242+
});
243+
}
244+
});
245+
}, [rooms]);
246+
214247
const handleMessage = (data: DataProps) => {
215248
try {
216249
const { message: messageData, iv } = data;

src/utils/emitTypes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ export const SEND_READ_TOKEN = "send_read_token";
1616
export const RECEIVE_READ_TOKEN = "receive_read_token";
1717
export const CREATE_NEW_ROOM = "create_new_room";
1818
export const CONNECT_ERROR = "connect_error";
19+
export const LEAVE_ROOM = "leave_room";
20+
export const UNSUBSCRIBE_ROOM = "unsubscribe_room";

src/utils/socket.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ interface ServerToClientEvents {
4141
[types.RECEIVE_READ_TOKEN]: (data: ReadTokenProps) => void;
4242
[types.JOIN_NEW_ROOM]: (data: { room: RoomProps }) => void;
4343
[types.CONNECT_ERROR]: (err: Error) => void;
44+
[types.UNSUBSCRIBE_ROOM]: (chatRoomAuthId: string) => void;
4445
}
4546

4647
interface ClientToServerEvents {
@@ -62,6 +63,11 @@ interface ClientToServerEvents {
6263
data: CreateRoomProps,
6364
callback: ({ room }: { room: RoomProps }) => void,
6465
) => void;
66+
67+
[types.LEAVE_ROOM]: (
68+
chatRoomAuthId: string,
69+
callback: ({ status }: { status: string }) => void,
70+
) => void;
6571
}
6672

6773
interface ConnectProps {

0 commit comments

Comments
 (0)