mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-19 04:57:52 +00:00
send and receive message
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
import { createAction, createFeatureSelector, createReducer, createSelector, on, props } from "@ngrx/store";
|
||||
import { TableRoom } from "./rooom-local-data-source.service";
|
||||
import { RoomOutPutDTO } from "../../dto/room/roomOutputDTO";
|
||||
|
||||
|
||||
export interface ChatRoom {
|
||||
[roomId: string]: TableRoom[];
|
||||
}
|
||||
|
||||
export interface RoomRemoteDataSourceState {
|
||||
chatRooms: ChatRoom;
|
||||
}
|
||||
|
||||
export const initialState: RoomRemoteDataSourceState = {
|
||||
chatRooms: {
|
||||
// add more rooms as needed
|
||||
}
|
||||
};
|
||||
|
||||
export const addMessage = createAction(
|
||||
'[Chat] Add Message',
|
||||
props<{ roomId: string; message: any }>()
|
||||
);
|
||||
|
||||
export const addRoom = createAction(
|
||||
'[Chat] Add Room',
|
||||
props<RoomOutPutDTO>()
|
||||
);
|
||||
|
||||
const _chatReducer = createReducer(
|
||||
initialState,
|
||||
on(addMessage, (state, { roomId, message }) => ({
|
||||
...state,
|
||||
chatRooms: {
|
||||
...state.chatRooms,
|
||||
[roomId]: [...(state.chatRooms[roomId] || []), message]
|
||||
}
|
||||
})),
|
||||
on(addRoom, (state, roomData: RoomOutPutDTO) => ({
|
||||
...state,
|
||||
chatRooms: {
|
||||
...state.chatRooms,
|
||||
[roomData.data.roomName]: roomData.data
|
||||
}
|
||||
}))
|
||||
);
|
||||
|
||||
export function chatReducer(state, action) {
|
||||
return _chatReducer(state, action);
|
||||
}
|
||||
|
||||
// Create a feature selector
|
||||
export const selectChatState = createFeatureSelector<RoomRemoteDataSourceState>('chat');
|
||||
|
||||
// Create a selector for a specific room's messages
|
||||
export const selectMessagesByRoom = (roomId: string) => createSelector(
|
||||
selectChatState,
|
||||
(state: RoomRemoteDataSourceState) => state.chatRooms[roomId] || []
|
||||
);
|
||||
|
||||
// Create a selector for the list of rooms
|
||||
export const selectAllRooms = createSelector(
|
||||
selectChatState,
|
||||
(state: RoomRemoteDataSourceState) => Object.keys(state.chatRooms)
|
||||
);
|
||||
Reference in New Issue
Block a user