bigbuffet-rw/app/soapbox/reducers/chats.js

50 lines
1.4 KiB
JavaScript
Raw Normal View History

2022-01-10 14:01:24 -08:00
import { Map as ImmutableMap, fromJS } from 'immutable';
2020-08-27 14:09:03 -07:00
import {
CHATS_FETCH_SUCCESS,
CHATS_FETCH_REQUEST,
CHATS_EXPAND_SUCCESS,
CHATS_EXPAND_REQUEST,
2020-08-27 14:09:03 -07:00
CHAT_FETCH_SUCCESS,
CHAT_READ_SUCCESS,
CHAT_READ_REQUEST,
} from 'soapbox/actions/chats';
import { normalizeChat } from 'soapbox/actions/importer/normalizer';
import { STREAMING_CHAT_UPDATE } from 'soapbox/actions/streaming';
2020-08-24 19:26:42 -07:00
const importChat = (state, chat) => state.setIn(['items', chat.id], fromJS(normalizeChat(chat)));
2020-08-25 10:38:21 -07:00
const importChats = (state, chats, next) =>
state.withMutations(mutable => {
if (next !== undefined) mutable.set('next', next);
chats.forEach(chat => importChat(mutable, chat));
mutable.set('loading', false);
});
2020-08-25 10:38:21 -07:00
const initialState = ImmutableMap({
next: null,
isLoading: false,
items: ImmutableMap({}),
});
2020-08-24 19:26:42 -07:00
2020-08-25 18:33:49 -07:00
export default function chats(state = initialState, action) {
2020-08-24 19:26:42 -07:00
switch(action.type) {
case CHATS_FETCH_REQUEST:
case CHATS_EXPAND_REQUEST:
return state.set('loading', true);
case CHATS_FETCH_SUCCESS:
case CHATS_EXPAND_SUCCESS:
return importChats(state, action.chats, action.next);
case STREAMING_CHAT_UPDATE:
2020-08-27 13:07:15 -07:00
return importChats(state, [action.chat]);
case CHAT_FETCH_SUCCESS:
2020-08-27 13:07:15 -07:00
return importChats(state, [action.chat]);
2020-08-27 14:09:03 -07:00
case CHAT_READ_REQUEST:
return state.setIn([action.chatId, 'unread'], 0);
case CHAT_READ_SUCCESS:
return importChats(state, [action.chat]);
2020-08-24 19:26:42 -07:00
default:
return state;
}
2021-08-03 12:22:51 -07:00
}