You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
	
	
		
			61 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			TypeScript
		
	
			
		
		
	
	
			61 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			TypeScript
		
	
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
 | 
						|
import { OpenGroupV2InfoJoinable } from '../../session/apis/open_group_api/opengroupV2/ApiUtil';
 | 
						|
 | 
						|
export type DefaultRoomsState = {
 | 
						|
  rooms: Array<OpenGroupV2InfoJoinable>;
 | 
						|
  inProgress: boolean;
 | 
						|
};
 | 
						|
 | 
						|
export const initialDefaultRoomState: DefaultRoomsState = {
 | 
						|
  rooms: [],
 | 
						|
  inProgress: false,
 | 
						|
};
 | 
						|
 | 
						|
/**
 | 
						|
 * Payload to dispatch to update the base64 data of a default room
 | 
						|
 */
 | 
						|
export type Base64Update = {
 | 
						|
  base64Data: string;
 | 
						|
  roomId: string;
 | 
						|
};
 | 
						|
 | 
						|
/**
 | 
						|
 * This slice is the one holding the default joinable rooms fetched once in a while from the default opengroup v2 server.
 | 
						|
 */
 | 
						|
const defaultRoomsSlice = createSlice({
 | 
						|
  name: 'defaultRooms',
 | 
						|
  initialState: initialDefaultRoomState,
 | 
						|
  reducers: {
 | 
						|
    updateDefaultRooms(state, action) {
 | 
						|
      window?.log?.info('updating default rooms', action.payload);
 | 
						|
      const rooms = action.payload as Array<OpenGroupV2InfoJoinable>;
 | 
						|
      return { ...state, rooms: rooms };
 | 
						|
    },
 | 
						|
    updateDefaultRoomsInProgress(state, action) {
 | 
						|
      const inProgress = action.payload as boolean;
 | 
						|
      return { ...state, inProgress };
 | 
						|
    },
 | 
						|
    updateDefaultBase64RoomData(state, action: PayloadAction<Base64Update>) {
 | 
						|
      const payload = action.payload;
 | 
						|
      const newRoomsState = state.rooms.map(room => {
 | 
						|
        if (room.id === payload.roomId) {
 | 
						|
          return {
 | 
						|
            ...room,
 | 
						|
            base64Data: payload.base64Data,
 | 
						|
          };
 | 
						|
        }
 | 
						|
        return room;
 | 
						|
      });
 | 
						|
      return { ...state, rooms: newRoomsState };
 | 
						|
    },
 | 
						|
  },
 | 
						|
});
 | 
						|
 | 
						|
const { actions, reducer } = defaultRoomsSlice;
 | 
						|
export const {
 | 
						|
  updateDefaultRooms,
 | 
						|
  updateDefaultBase64RoomData,
 | 
						|
  updateDefaultRoomsInProgress,
 | 
						|
} = actions;
 | 
						|
export const defaultRoomReducer = reducer;
 |