function roomsReducer(data, { type, payload }) {
  switch (type) {
    case 'remove-room':
      return { ...data, rooms: removeRoom(data.rooms, payload) };

    case 'add-room':
      const roomIdx = data.rooms.findIndex((room) => room.name === payload);
      const remainingRooms = removeRoom(data.rooms, roomIdx);

      const updatedRoom = roomIdx === -1 ? { name: payload } : data.rooms[roomIdx];

      return {
        ...data,
        rooms: [
          { ...updatedRoom, lastJoin: new Date().getTime() },
          ...remainingRooms,
        ],
      };

    default:
      return data;
  }
}
Reducer that returns new state of rooms list