Adding remove messages to persistence layer (#5066)

This commit is contained in:
Joseph Insalaco 2024-06-26 20:44:40 -04:00 committed by GitHub
parent 8687163cca
commit f8bc6cf998
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 54 additions and 4 deletions

View file

@ -49,5 +49,12 @@ export const Actions = {
roomId, roomId,
field, field,
order order
}) }),
removeMessages: (roomId, name, amount) => ({
type: Types.REMOVE_MESSAGES,
roomId,
name,
amount
}),
} }

View file

@ -42,5 +42,9 @@ export const Dispatch = {
sortGames: (roomId, field, order) => { sortGames: (roomId, field, order) => {
store.dispatch(Actions.sortGames(roomId, field, order)); store.dispatch(Actions.sortGames(roomId, field, order));
} },
removeMessages: (roomId, name, amount) => {
store.dispatch(Actions.removeMessages(roomId, name, amount));
},
} }

View file

@ -28,6 +28,7 @@ export const roomsReducer = (state = initialState, action: any) => {
...initialState ...initialState
}; };
} }
case Types.UPDATE_ROOMS: { case Types.UPDATE_ROOMS: {
const rooms = { const rooms = {
...state.rooms ...state.rooms
@ -52,6 +53,7 @@ export const roomsReducer = (state = initialState, action: any) => {
return { ...state, rooms }; return { ...state, rooms };
} }
case Types.JOIN_ROOM: { case Types.JOIN_ROOM: {
const { roomInfo } = action; const { roomInfo } = action;
const { joined, rooms, sortGamesBy, sortUsersBy } = state; const { joined, rooms, sortGamesBy, sortUsersBy } = state;
@ -87,6 +89,7 @@ export const roomsReducer = (state = initialState, action: any) => {
}, },
} }
} }
case Types.LEAVE_ROOM: { case Types.LEAVE_ROOM: {
const { roomId } = action; const { roomId } = action;
const { joined, messages } = state; const { joined, messages } = state;
@ -109,6 +112,7 @@ export const roomsReducer = (state = initialState, action: any) => {
messages: _messages, messages: _messages,
} }
} }
case Types.ADD_MESSAGE: { case Types.ADD_MESSAGE: {
const { roomId, message } = action; const { roomId, message } = action;
const { messages } = state; const { messages } = state;
@ -134,6 +138,7 @@ export const roomsReducer = (state = initialState, action: any) => {
} }
} }
// @TODO improve this reducer, likely by improving the store model // @TODO improve this reducer, likely by improving the store model
case Types.UPDATE_GAMES: { case Types.UPDATE_GAMES: {
const { roomId, games } = action; const { roomId, games } = action;
const { rooms, sortGamesBy } = state; const { rooms, sortGamesBy } = state;
@ -196,6 +201,7 @@ export const roomsReducer = (state = initialState, action: any) => {
} }
} }
} }
case Types.USER_JOINED: { case Types.USER_JOINED: {
const { roomId, user } = action; const { roomId, user } = action;
const { rooms, sortUsersBy } = state; const { rooms, sortUsersBy } = state;
@ -220,6 +226,7 @@ export const roomsReducer = (state = initialState, action: any) => {
} }
}; };
} }
case Types.USER_LEFT: { case Types.USER_LEFT: {
const { roomId, name } = action; const { roomId, name } = action;
const { rooms } = state; const { rooms } = state;
@ -238,6 +245,7 @@ export const roomsReducer = (state = initialState, action: any) => {
} }
}; };
} }
case Types.SORT_GAMES: { case Types.SORT_GAMES: {
const { field, order, roomId } = action; const { field, order, roomId } = action;
const { rooms } = state; const { rooms } = state;
@ -264,6 +272,36 @@ export const roomsReducer = (state = initialState, action: any) => {
sortGamesBy sortGamesBy
} }
} }
case Types.REMOVE_MESSAGES: {
const { name, amount, roomId } = action;
const { messages } = state;
let amountRemoved = 0;
return {
...state,
messages: {
...messages,
[roomId]: messages[roomId]
.reverse()
.filter(({ message }) => {
if (amount === amountRemoved) {
return true;
}
const keep = message.indexOf(`${name}:`) !== 0;
if (!keep) {
amountRemoved++;
}
return keep;
})
.reverse()
}
}
}
default: default:
return state; return state;
} }

View file

@ -7,7 +7,8 @@ export const Types = {
UPDATE_GAMES: '[Rooms] Update Games', UPDATE_GAMES: '[Rooms] Update Games',
USER_JOINED: '[Rooms] User Joined', USER_JOINED: '[Rooms] User Joined',
USER_LEFT: '[Rooms] User Left', USER_LEFT: '[Rooms] User Left',
SORT_GAMES: '[Rooms] Sort Games' SORT_GAMES: '[Rooms] Sort Games',
REMOVE_MESSAGES: '[Rooms] Remove Messages',
}; };
export const MAX_ROOM_MESSAGES = 1000; export const MAX_ROOM_MESSAGES = 1000;

View file

@ -50,7 +50,7 @@ export class RoomPersistence {
} }
static removeMessages(roomId: number, name: string, amount: number): void { static removeMessages(roomId: number, name: string, amount: number): void {
console.log('removeMessages', roomId, name, amount); RoomsDispatch.removeMessages(roomId, name, amount);
}; };
static gameCreated(roomId: number) { static gameCreated(roomId: number) {