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

62 lines
2 KiB
JavaScript
Raw Normal View History

2020-08-24 19:26:42 -07:00
import api from '../api';
2020-08-25 10:38:21 -07:00
import { importFetchedChats } from 'soapbox/actions/importer';
2020-08-25 15:54:10 -07:00
import { getSettings, changeSetting } from 'soapbox/actions/settings';
import { Map as ImmutableMap } from 'immutable';
2020-08-24 19:26:42 -07:00
export const CHATS_FETCH_REQUEST = 'CHATS_FETCH_REQUEST';
export const CHATS_FETCH_SUCCESS = 'CHATS_FETCH_SUCCESS';
export const CHATS_FETCH_FAIL = 'CHATS_FETCH_FAIL';
export function fetchChats() {
return (dispatch, getState) => {
dispatch({ type: CHATS_FETCH_REQUEST });
return api(getState).get('/api/v1/pleroma/chats').then(({ data }) => {
2020-08-25 10:38:21 -07:00
dispatch(importFetchedChats(data));
2020-08-24 19:26:42 -07:00
dispatch({ type: CHATS_FETCH_SUCCESS, data });
}).catch(error => {
dispatch({ type: CHATS_FETCH_FAIL, error });
});
};
}
2020-08-25 15:54:10 -07:00
export function openChat(chatId) {
return (dispatch, getState) => {
const panes = getSettings(getState()).getIn(['chats', 'panes']);
const idx = panes.findIndex(pane => pane.get('chat_id') === chatId);
if (idx > -1) {
return dispatch(changeSetting(['chats', 'panes', idx, 'state'], 'open'));
} else {
const newPane = ImmutableMap({ chat_id: chatId, state: 'open' });
return dispatch(changeSetting(['chats', 'panes'], panes.push(newPane)));
}
};
}
2020-08-25 16:11:48 -07:00
export function closeChat(chatId) {
return (dispatch, getState) => {
const panes = getSettings(getState()).getIn(['chats', 'panes']);
const idx = panes.findIndex(pane => pane.get('chat_id') === chatId);
if (idx > -1) {
return dispatch(changeSetting(['chats', 'panes'], panes.delete(idx)));
} else {
return false;
}
};
}
2020-08-25 16:45:05 -07:00
export function toggleChat(chatId) {
return (dispatch, getState) => {
const panes = getSettings(getState()).getIn(['chats', 'panes']);
const [idx, pane] = panes.findEntry(pane => pane.get('chat_id') === chatId);
if (idx > -1) {
const state = pane.get('state') === 'minimized' ? 'open' : 'minimized';
return dispatch(changeSetting(['chats', 'panes', idx, 'state'], state));
} else {
return false;
}
};
}