mattermost-mobile/service/actions/teams.js
enahum e2f3f7b803 project folder structure (#107)
* project folder structure

* Moving store to be called from app folder
2016-12-09 10:34:17 -05:00

185 lines
5.6 KiB
JavaScript

// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
import Client from 'service/client';
import {batchActions} from 'redux-batched-actions';
import {Constants, TeamsTypes} from 'service/constants';
import {bindClientFunc, forceLogoutIfNecessary} from './helpers';
export function selectTeam(team) {
return async (dispatch, getState) => {
dispatch({
type: TeamsTypes.SELECT_TEAM,
data: team.id
}, getState);
};
}
export function fetchTeams() {
return bindClientFunc(
Client.getAllTeams,
TeamsTypes.FETCH_TEAMS_REQUEST,
[TeamsTypes.RECEIVED_ALL_TEAMS, TeamsTypes.FETCH_TEAMS_SUCCESS],
TeamsTypes.FETCH_TEAMS_FAILURE
);
}
export function getAllTeamListings() {
return bindClientFunc(
Client.getAllTeamListings,
TeamsTypes.TEAM_LISTINGS_REQUEST,
[TeamsTypes.RECEIVED_TEAM_LISTINGS, TeamsTypes.TEAM_LISTINGS_SUCCESS],
TeamsTypes.TEAM_LISTINGS_FAILURE
);
}
export function createTeam(userId, team) {
return async (dispatch, getState) => {
try {
dispatch({type: TeamsTypes.CREATE_TEAM_REQUEST}, getState);
const created = await Client.createTeam(team);
const member = {
team_id: created.id,
user_id: userId,
roles: `${Constants.TEAM_ADMIN_ROLE} ${Constants.TEAM_USER_ROLE}`,
delete_at: 0,
msg_count: 0,
mention_count: 0
};
dispatch(batchActions([
{
type: TeamsTypes.CREATED_TEAM,
data: created
},
{
type: TeamsTypes.RECEIVED_MY_TEAM_MEMBERS,
data: [member]
},
{
type: TeamsTypes.SELECT_TEAM,
data: created.id
},
{
type: TeamsTypes.CREATE_TEAM_SUCCESS
}
]), getState);
} catch (err) {
forceLogoutIfNecessary(err, dispatch);
dispatch({type: TeamsTypes.CREATE_TEAM_FAILURE, error: err}, getState);
}
};
}
export function updateTeam(team) {
return bindClientFunc(
Client.updateTeam,
TeamsTypes.UPDATE_TEAM_REQUEST,
[TeamsTypes.UPDATED_TEAM, TeamsTypes.UPDATE_TEAM_SUCCESS],
TeamsTypes.UPDATE_TEAM_FAILURE,
team
);
}
export function getMyTeamMembers() {
return bindClientFunc(
Client.getMyTeamMembers,
TeamsTypes.MY_TEAM_MEMBERS_REQUEST,
[TeamsTypes.RECEIVED_MY_TEAM_MEMBERS, TeamsTypes.MY_TEAM_MEMBERS_SUCCESS],
TeamsTypes.MY_TEAM_MEMBERS_FAILURE
);
}
export function getTeamMember(teamId, userId) {
return async (dispatch, getState) => {
try {
dispatch({type: TeamsTypes.TEAM_MEMBERS_REQUEST}, getState);
const member = await Client.getTeamMember(teamId, userId);
dispatch(batchActions([
{
type: TeamsTypes.RECEIVED_MEMBERS_IN_TEAM,
data: [member]
},
{
type: TeamsTypes.TEAM_MEMBERS_SUCCESS
}
]), getState);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch({type: TeamsTypes.TEAM_MEMBERS_FAILURE, error}, getState);
}
};
}
export function getTeamMembersByIds(teamId, userIds) {
return bindClientFunc(
Client.getTeamMemberByIds,
TeamsTypes.TEAM_MEMBERS_REQUEST,
[TeamsTypes.RECEIVED_MEMBERS_IN_TEAM, TeamsTypes.TEAM_MEMBERS_SUCCESS],
TeamsTypes.TEAM_MEMBERS_FAILURE,
teamId,
userIds
);
}
export function getTeamStats(teamId) {
return bindClientFunc(
Client.getTeamStats,
TeamsTypes.TEAM_STATS_REQUEST,
[TeamsTypes.RECEIVED_TEAM_STATS, TeamsTypes.TEAM_STATS_SUCCESS],
TeamsTypes.TEAM_STATS_FAILURE,
teamId
);
}
export function addUserToTeam(teamId, userId) {
return async (dispatch, getState) => {
try {
dispatch({type: TeamsTypes.ADD_TEAM_MEMBER_REQUEST}, getState);
await Client.addUserToTeam(teamId, userId);
const member = {
team_id: teamId,
user_id: userId
};
dispatch(batchActions([
{
type: TeamsTypes.RECEIVED_MEMBER_IN_TEAM,
data: member
},
{
type: TeamsTypes.ADD_TEAM_MEMBER_SUCCESS
}
]), getState);
} catch (err) {
forceLogoutIfNecessary(err, dispatch);
dispatch({type: TeamsTypes.ADD_TEAM_MEMBER_FAILURE, error: err}, getState);
}
};
}
export function removeUserFromTeam(teamId, userId) {
return async (dispatch, getState) => {
try {
dispatch({type: TeamsTypes.REMOVE_TEAM_MEMBER_REQUEST}, getState);
await Client.removeUserFromTeam(teamId, userId);
const member = {
team_id: teamId,
user_id: userId
};
dispatch(batchActions([
{
type: TeamsTypes.REMOVE_MEMBER_FROM_TEAM,
data: member
},
{
type: TeamsTypes.REMOVE_TEAM_MEMBER_SUCCESS
}
]), getState);
} catch (err) {
forceLogoutIfNecessary(err, dispatch);
dispatch({type: TeamsTypes.REMOVE_TEAM_MEMBER_FAILURE, error: err}, getState);
}
};
}