New groups

This commit is contained in:
2458773093
2019-07-15 16:47:05 +03:00
parent fd50f03304
commit 1fabd28498
30 changed files with 809 additions and 285 deletions

View File

@@ -1,5 +1,7 @@
import api from '../api';
import api, { getLinks } from '../api';
import { me } from 'gabsocial/initial_state';
import { importFetchedAccounts } from './importer';
import { fetchRelationships } from './accounts';
export const GROUP_FETCH_REQUEST = 'GROUP_FETCH_REQUEST';
export const GROUP_FETCH_SUCCESS = 'GROUP_FETCH_SUCCESS';
@@ -21,6 +23,14 @@ export const GROUP_LEAVE_REQUEST = 'GROUP_LEAVE_REQUEST';
export const GROUP_LEAVE_SUCCESS = 'GROUP_LEAVE_SUCCESS';
export const GROUP_LEAVE_FAIL = 'GROUP_LEAVE_FAIL';
export const GROUP_MEMBERS_FETCH_REQUEST = 'GROUP_MEMBERS_FETCH_REQUEST';
export const GROUP_MEMBERS_FETCH_SUCCESS = 'GROUP_MEMBERS_FETCH_SUCCESS';
export const GROUP_MEMBERS_FETCH_FAIL = 'GROUP_MEMBERS_FETCH_FAIL';
export const GROUP_MEMBERS_EXPAND_REQUEST = 'GROUP_MEMBERS_EXPAND_REQUEST';
export const GROUP_MEMBERS_EXPAND_SUCCESS = 'GROUP_MEMBERS_EXPAND_SUCCESS';
export const GROUP_MEMBERS_EXPAND_FAIL = 'GROUP_MEMBERS_EXPAND_FAIL';
export const fetchGroup = id => (dispatch, getState) => {
if (!me) return;
@@ -98,13 +108,16 @@ export function fetchGroupRelationshipsFail(error) {
};
};
export const fetchGroups = () => (dispatch, getState) => {
export const fetchGroups = (tab) => (dispatch, getState) => {
if (!me) return;
dispatch(fetchGroupsRequest());
api(getState).get('/api/v1/groups')
.then(({ data }) => dispatch(fetchGroupsSuccess(data)))
api(getState).get('/api/v1/groups?tab=' + tab)
.then(({ data }) => {
dispatch(fetchGroupsSuccess(data, tab));
dispatch(fetchGroupRelationships(data.map(item => item.id)));
})
.catch(err => dispatch(fetchGroupsFail(err)));
};
@@ -112,9 +125,10 @@ export const fetchGroupsRequest = () => ({
type: GROUPS_FETCH_REQUEST,
});
export const fetchGroupsSuccess = groups => ({
export const fetchGroupsSuccess = (groups, tab) => ({
type: GROUPS_FETCH_SUCCESS,
groups,
tab,
});
export const fetchGroupsFail = error => ({
@@ -191,3 +205,93 @@ export function leaveGroupFail(error) {
error,
};
};
export function fetchMembers(id) {
return (dispatch, getState) => {
if (!me) return;
dispatch(fetchMembersRequest(id));
api(getState).get(`/api/v1/groups/${id}/accounts`).then(response => {
const next = getLinks(response).refs.find(link => link.rel === 'next');
dispatch(importFetchedAccounts(response.data));
dispatch(fetchMembersSuccess(id, response.data, next ? next.uri : null));
dispatch(fetchRelationships(response.data.map(item => item.id)));
}).catch(error => {
dispatch(fetchMembersFail(id, error));
});
};
};
export function fetchMembersRequest(id) {
return {
type: GROUP_MEMBERS_FETCH_REQUEST,
id,
};
};
export function fetchMembersSuccess(id, accounts, next) {
return {
type: GROUP_MEMBERS_FETCH_SUCCESS,
id,
accounts,
next,
};
};
export function fetchMembersFail(id, error) {
return {
type: GROUP_MEMBERS_FETCH_FAIL,
id,
error,
};
};
export function expandMembers(id) {
return (dispatch, getState) => {
if (!me) return;
const url = getState().getIn(['user_lists', 'groups', id, 'next']);
if (url === null) {
return;
}
dispatch(expandMembersRequest(id));
api(getState).get(url).then(response => {
const next = getLinks(response).refs.find(link => link.rel === 'next');
dispatch(importFetchedAccounts(response.data));
dispatch(expandMembersSuccess(id, response.data, next ? next.uri : null));
dispatch(fetchRelationships(response.data.map(item => item.id)));
}).catch(error => {
dispatch(expandMembersFail(id, error));
});
};
};
export function expandMembersRequest(id) {
return {
type: GROUP_MEMBERS_EXPAND_REQUEST,
id,
};
};
export function expandMembersSuccess(id, accounts, next) {
return {
type: GROUP_MEMBERS_EXPAND_SUCCESS,
id,
accounts,
next,
};
};
export function expandMembersFail(id, error) {
return {
type: GROUP_MEMBERS_EXPAND_FAIL,
id,
error,
};
};