Skip to content

Commit 62c740a

Browse files
committed
merge master into feature/project-fields
1 parent 7344f41 commit 62c740a

File tree

8 files changed

+89
-35
lines changed

8 files changed

+89
-35
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: 45 additions & 29 deletions
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";
@@ -21,15 +22,7 @@ const SocketController = () => {
2122
rooms,
2223
users: { selfUser, users },
2324
app: {
24-
configs: {
25-
indicators,
26-
tenantId,
27-
apiKey,
28-
subscriptionKey,
29-
secretKey,
30-
projectName,
31-
e2e,
32-
},
25+
configs: { indicators, tenantId, apiKey, projectId, secretKey, e2e },
3326
},
3427
},
3528
dispatch,
@@ -41,13 +34,13 @@ const SocketController = () => {
4134
connect({
4235
chatAuthId: selfUser?.chatAuthId,
4336
tenantId,
44-
auth: { apiKey, subscriptionKey },
37+
projectId,
38+
auth: { apiKey },
4539
});
4640
} catch (error) {
4741
handleLogger({
4842
apiKey,
49-
subscriptionKey,
50-
projectName,
43+
projectId,
5144
chatAuthId: selfUser?.chatAuthId,
5245
error: {
5346
error,
@@ -66,8 +59,7 @@ const SocketController = () => {
6659
socket.on(types.CONNECT_ERROR, (error) => {
6760
handleLogger({
6861
apiKey,
69-
subscriptionKey,
70-
projectName,
62+
projectId,
7163
chatAuthId: selfUser?.chatAuthId,
7264
error: {
7365
error,
@@ -87,8 +79,7 @@ const SocketController = () => {
8779
} catch (error) {
8880
handleLogger({
8981
apiKey,
90-
subscriptionKey,
91-
projectName,
82+
projectId,
9283
chatAuthId: selfUser?.chatAuthId,
9384
error: {
9485
error,
@@ -113,8 +104,7 @@ const SocketController = () => {
113104
} catch (error) {
114105
handleLogger({
115106
apiKey,
116-
subscriptionKey,
117-
projectName,
107+
projectId,
118108
chatAuthId: selfUser?.chatAuthId,
119109
error: {
120110
error,
@@ -134,8 +124,7 @@ const SocketController = () => {
134124
} catch (error) {
135125
handleLogger({
136126
apiKey,
137-
subscriptionKey,
138-
projectName,
127+
projectId,
139128
chatAuthId: selfUser?.chatAuthId,
140129
error: {
141130
error,
@@ -155,8 +144,7 @@ const SocketController = () => {
155144
} catch (error) {
156145
handleLogger({
157146
apiKey,
158-
subscriptionKey,
159-
projectName,
147+
projectId,
160148
chatAuthId: selfUser?.chatAuthId,
161149
chatRoomAuthId: data.chatRoomAuthId,
162150
error: {
@@ -177,8 +165,7 @@ const SocketController = () => {
177165
} catch (error) {
178166
handleLogger({
179167
apiKey,
180-
subscriptionKey,
181-
projectName,
168+
projectId,
182169
chatAuthId: selfUser?.chatAuthId,
183170
chatRoomAuthId: data.chatRoomAuthId,
184171
error: {
@@ -198,8 +185,7 @@ const SocketController = () => {
198185
} catch (error) {
199186
handleLogger({
200187
apiKey,
201-
subscriptionKey,
202-
projectName,
188+
projectId,
203189
chatAuthId: selfUser?.chatAuthId,
204190
chatRoomAuthId: data.room.chatRoomAuthId,
205191
error: {
@@ -211,6 +197,37 @@ const SocketController = () => {
211197
});
212198
}, []);
213199

200+
useEffect(() => {
201+
socket.on(types.UNSUBSCRIBE_ROOM, (chatRoomAuthId) => {
202+
try {
203+
// Send request to server to leave room
204+
socket.emit(types.LEAVE_ROOM, chatRoomAuthId, ({ status }) => {
205+
// Check if the socket successfully leaves the room
206+
if (status === "success") {
207+
const roomId = rooms.find(
208+
(room) => room.chatRoomAuthId === chatRoomAuthId,
209+
)?.roomId;
210+
211+
// Update the context
212+
deleteRoom(chatRoomAuthId)(dispatch);
213+
roomId && deleteMessages(roomId)(dispatch);
214+
}
215+
});
216+
} catch (error) {
217+
handleLogger({
218+
apiKey,
219+
projectId,
220+
chatAuthId: selfUser?.chatAuthId,
221+
chatRoomAuthId: chatRoomAuthId,
222+
error: {
223+
error,
224+
event: types.UNSUBSCRIBE_ROOM,
225+
},
226+
});
227+
}
228+
});
229+
}, [rooms]);
230+
214231
const handleMessage = (data: DataProps) => {
215232
try {
216233
const { message: messageData, iv } = data;
@@ -271,8 +288,7 @@ const SocketController = () => {
271288
} catch (error) {
272289
handleLogger({
273290
apiKey,
274-
subscriptionKey,
275-
projectName,
291+
projectId,
276292
chatAuthId: selfUser?.chatAuthId,
277293
error: {
278294
error,

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: 10 additions & 5 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,31 +63,35 @@ 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 {
6874
chatAuthId: string | undefined;
6975
tenantId: string;
76+
projectId: string;
7077
auth: {
7178
apiKey: string;
72-
subscriptionKey: string;
7379
};
7480
}
7581

7682
type HandleLogProps = {
7783
apiKey: string;
78-
subscriptionKey: string;
79-
projectName: string;
84+
projectId: string;
8085
chatAuthId: string;
8186
chatRoomAuthId?: string;
8287
};
8388

8489
export let socket: Socket<ServerToClientEvents, ClientToServerEvents>;
8590

8691
export const connect = (props: ConnectProps) => {
87-
const { chatAuthId, tenantId, auth } = props;
92+
const { chatAuthId, tenantId, auth, projectId } = props;
8893
socket = io("https://service.commt.co", {
89-
query: { chatAuthId, tenantId },
94+
query: { chatAuthId, tenantId, projectId },
9095
auth,
9196
});
9297
};

0 commit comments

Comments
 (0)