Reduced scope of try-catch around client calls (#134)

This commit is contained in:
Harrison Healey 2016-12-13 11:49:39 -05:00 committed by enahum
parent 228b1be76f
commit fcd86b8923
6 changed files with 592 additions and 493 deletions

View file

@ -14,44 +14,18 @@ import Client from 'service/client';
export function createChannel(channel, userId) {
return async (dispatch, getState) => {
dispatch(batchActions([
{
type: ChannelTypes.CREATE_CHANNEL_REQUEST
},
{
type: ChannelTypes.CHANNEL_MEMBERS_REQUEST
}
]), getState);
let created;
try {
dispatch(batchActions([
{
type: ChannelTypes.CREATE_CHANNEL_REQUEST
},
{
type: ChannelTypes.CHANNEL_MEMBERS_REQUEST
}
]), getState);
const created = await Client.createChannel(channel);
const member = {
channel_id: created.id,
user_id: userId,
roles: `${Constants.CHANNEL_USER_ROLE} ${Constants.CHANNEL_ADMIN_ROLE}`,
last_viewed_at: 0,
msg_count: 0,
mention_count: 0,
notify_props: {desktop: 'default', mark_unread: 'all'},
last_update_at: created.create_at
};
dispatch(batchActions([
{
type: ChannelTypes.RECEIVED_CHANNEL,
data: created
},
{
type: ChannelTypes.CREATE_CHANNEL_SUCCESS
},
{
type: ChannelTypes.RECEIVED_MY_CHANNEL_MEMBER,
data: member
},
{
type: ChannelTypes.CHANNEL_MEMBERS_SUCCESS
}
]), getState);
created = await Client.createChannel(channel);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch(batchActions([
@ -64,44 +38,46 @@ export function createChannel(channel, userId) {
error
}
]), getState);
return;
}
const member = {
channel_id: created.id,
user_id: userId,
roles: `${Constants.CHANNEL_USER_ROLE} ${Constants.CHANNEL_ADMIN_ROLE}`,
last_viewed_at: 0,
msg_count: 0,
mention_count: 0,
notify_props: {desktop: 'default', mark_unread: 'all'},
last_update_at: created.create_at
};
dispatch(batchActions([
{
type: ChannelTypes.RECEIVED_CHANNEL,
data: created
},
{
type: ChannelTypes.CREATE_CHANNEL_SUCCESS
},
{
type: ChannelTypes.RECEIVED_MY_CHANNEL_MEMBER,
data: member
},
{
type: ChannelTypes.CHANNEL_MEMBERS_SUCCESS
}
]), getState);
};
}
export function createDirectChannel(userId, otherUserId) {
return async (dispatch, getState) => {
dispatch({type: ChannelTypes.CREATE_CHANNEL_REQUEST}, getState);
let created;
try {
dispatch({type: ChannelTypes.CREATE_CHANNEL_REQUEST}, getState);
const created = await Client.createDirectChannel(otherUserId);
const member = {
channel_id: created.id,
user_id: userId,
roles: `${Constants.CHANNEL_USER_ROLE} ${Constants.CHANNEL_ADMIN_ROLE}`,
last_viewed_at: 0,
msg_count: 0,
mention_count: 0,
notify_props: {desktop: 'default', mark_unread: 'all'},
last_update_at: created.create_at
};
dispatch(batchActions([
{
type: ChannelTypes.RECEIVED_CHANNEL,
data: created
},
{
type: ChannelTypes.RECEIVED_MY_CHANNEL_MEMBER,
data: member
},
{
type: PreferencesTypes.RECEIVED_PREFERENCES,
data: [{category: Preferences.CATEGORY_DIRECT_CHANNEL_SHOW, name: otherUserId, value: 'true'}]
},
{
type: ChannelTypes.CREATE_CHANNEL_SUCCESS
}
]), getState);
created = await Client.createDirectChannel(otherUserId);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch(batchActions([
@ -114,119 +90,145 @@ export function createDirectChannel(userId, otherUserId) {
error
}
]), getState);
return;
}
const member = {
channel_id: created.id,
user_id: userId,
roles: `${Constants.CHANNEL_USER_ROLE} ${Constants.CHANNEL_ADMIN_ROLE}`,
last_viewed_at: 0,
msg_count: 0,
mention_count: 0,
notify_props: {desktop: 'default', mark_unread: 'all'},
last_update_at: created.create_at
};
dispatch(batchActions([
{
type: ChannelTypes.RECEIVED_CHANNEL,
data: created
},
{
type: ChannelTypes.RECEIVED_MY_CHANNEL_MEMBER,
data: member
},
{
type: PreferencesTypes.RECEIVED_PREFERENCES,
data: [{category: Preferences.CATEGORY_DIRECT_CHANNEL_SHOW, name: otherUserId, value: 'true'}]
},
{
type: ChannelTypes.CREATE_CHANNEL_SUCCESS
}
]), getState);
};
}
export function updateChannel(channel) {
return async (dispatch, getState) => {
try {
dispatch({type: ChannelTypes.UPDATE_CHANNEL_REQUEST}, getState);
dispatch({type: ChannelTypes.UPDATE_CHANNEL_REQUEST}, getState);
const updated = await Client.updateChannel(channel);
dispatch(batchActions([
{
type: ChannelTypes.RECEIVED_CHANNEL,
data: updated
},
{
type: ChannelTypes.UPDATE_CHANNEL_SUCCESS
}
]), getState);
let updated;
try {
updated = await Client.updateChannel(channel);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch({type: ChannelTypes.UPDATE_CHANNEL_FAILURE, error}, getState);
return;
}
dispatch(batchActions([
{
type: ChannelTypes.RECEIVED_CHANNEL,
data: updated
},
{
type: ChannelTypes.UPDATE_CHANNEL_SUCCESS
}
]), getState);
};
}
export function updateChannelNotifyProps(userId, teamId, channelId, props) {
return async (dispatch, getState) => {
dispatch({type: ChannelTypes.NOTIFY_PROPS_REQUEST}, getState);
const data = {
user_id: userId,
channel_id: channelId,
...props
};
let notifyProps;
try {
dispatch({type: ChannelTypes.NOTIFY_PROPS_REQUEST}, getState);
const data = {
user_id: userId,
channel_id: channelId,
...props
};
const notifyProps = await Client.updateChannelNotifyProps(teamId, data);
dispatch(batchActions([
{
type: ChannelTypes.RECEIVED_CHANNEL_PROPS,
data: notifyProps,
channel_id: channelId
},
{
type: ChannelTypes.NOTIFY_PROPS_SUCCESS
}
]), getState);
notifyProps = await Client.updateChannelNotifyProps(teamId, data);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch({type: ChannelTypes.NOTIFY_PROPS_FAILURE, error}, getState);
return;
}
dispatch(batchActions([
{
type: ChannelTypes.RECEIVED_CHANNEL_PROPS,
data: notifyProps,
channel_id: channelId
},
{
type: ChannelTypes.NOTIFY_PROPS_SUCCESS
}
]), getState);
};
}
export function getChannel(teamId, channelId) {
return async (dispatch, getState) => {
dispatch({type: ChannelTypes.CHANNEL_REQUEST}, getState);
let data;
try {
dispatch({type: ChannelTypes.CHANNEL_REQUEST}, getState);
const data = await Client.getChannel(teamId, channelId);
dispatch(batchActions([
{
type: ChannelTypes.RECEIVED_CHANNEL,
data: data.channel
},
{
type: ChannelTypes.CHANNEL_SUCCESS
},
{
type: ChannelTypes.RECEIVED_MY_CHANNEL_MEMBER,
data: data.member
}
]), getState);
data = await Client.getChannel(teamId, channelId);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch({type: ChannelTypes.CHANNELS_FAILURE, error}, getState);
return;
}
dispatch(batchActions([
{
type: ChannelTypes.RECEIVED_CHANNEL,
data: data.channel
},
{
type: ChannelTypes.CHANNEL_SUCCESS
},
{
type: ChannelTypes.RECEIVED_MY_CHANNEL_MEMBER,
data: data.member
}
]), getState);
};
}
export function fetchMyChannelsAndMembers(teamId) {
return async (dispatch, getState) => {
dispatch(batchActions([
{
type: ChannelTypes.CHANNELS_REQUEST
},
{
type: ChannelTypes.CHANNEL_MEMBERS_REQUEST
}
]), getState);
let channels;
let channelMembers;
try {
dispatch(batchActions([
{
type: ChannelTypes.CHANNELS_REQUEST
},
{
type: ChannelTypes.CHANNEL_MEMBERS_REQUEST
}
]), getState);
const channelsRequest = Client.getChannels(teamId);
const channelMembersRequest = Client.getMyChannelMembers(teamId);
const channels = Client.getChannels(teamId);
const channelMembers = Client.getMyChannelMembers(teamId);
dispatch(batchActions([
{
type: ChannelTypes.RECEIVED_CHANNELS,
data: await channels
},
{
type: ChannelTypes.CHANNELS_SUCCESS
},
{
type: ChannelTypes.RECEIVED_MY_CHANNEL_MEMBERS,
data: await channelMembers
},
{
type: ChannelTypes.CHANNEL_MEMBERS_SUCCESS
}
]), getState);
channels = await channelsRequest;
channelMembers = await channelMembersRequest;
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch(batchActions([
@ -239,209 +241,243 @@ export function fetchMyChannelsAndMembers(teamId) {
error
}
]), getState);
return;
}
dispatch(batchActions([
{
type: ChannelTypes.RECEIVED_CHANNELS,
data: await channels
},
{
type: ChannelTypes.CHANNELS_SUCCESS
},
{
type: ChannelTypes.RECEIVED_MY_CHANNEL_MEMBERS,
data: await channelMembers
},
{
type: ChannelTypes.CHANNEL_MEMBERS_SUCCESS
}
]), getState);
};
}
export function leaveChannel(teamId, channelId) {
return async (dispatch, getState) => {
try {
dispatch({type: ChannelTypes.LEAVE_CHANNEL_REQUEST}, getState);
dispatch({type: ChannelTypes.LEAVE_CHANNEL_REQUEST}, getState);
try {
await Client.leaveChannel(teamId, channelId);
dispatch(batchActions([
{
type: ChannelTypes.LEAVE_CHANNEL,
channel_id: channelId
},
{
type: ChannelTypes.LEAVE_CHANNEL_SUCCESS
}
]), getState);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch({type: ChannelTypes.LEAVE_CHANNEL_FAILURE, error}, getState);
return;
}
dispatch(batchActions([
{
type: ChannelTypes.LEAVE_CHANNEL,
channel_id: channelId
},
{
type: ChannelTypes.LEAVE_CHANNEL_SUCCESS
}
]), getState);
};
}
export function joinChannel(userId, teamId, channelId, channelName) {
return async (dispatch, getState) => {
try {
dispatch({type: ChannelTypes.JOIN_CHANNEL_REQUEST}, getState);
dispatch({type: ChannelTypes.JOIN_CHANNEL_REQUEST}, getState);
let channel;
let channel;
try {
if (channelId) {
channel = await Client.joinChannel(teamId, channelId);
} else if (channelName) {
channel = await Client.joinChannelByName(teamId, channelName);
}
const channelMember = {
channel_id: channel.id,
user_id: userId,
roles: `${Constants.CHANNEL_USER_ROLE}`,
last_viewed_at: 0,
msg_count: 0,
mention_count: 0,
notify_props: {desktop: 'default', mark_unread: 'all'},
last_update_at: new Date().getTime()
};
dispatch(batchActions([
{
type: ChannelTypes.RECEIVED_CHANNEL,
data: channel
},
{
type: ChannelTypes.RECEIVED_MY_CHANNEL_MEMBER,
data: channelMember
},
{
type: ChannelTypes.JOIN_CHANNEL_SUCCESS
}
]), getState);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch({type: ChannelTypes.JOIN_CHANNEL_FAILURE, error}, getState);
return;
}
const channelMember = {
channel_id: channel.id,
user_id: userId,
roles: `${Constants.CHANNEL_USER_ROLE}`,
last_viewed_at: 0,
msg_count: 0,
mention_count: 0,
notify_props: {desktop: 'default', mark_unread: 'all'},
last_update_at: new Date().getTime()
};
dispatch(batchActions([
{
type: ChannelTypes.RECEIVED_CHANNEL,
data: channel
},
{
type: ChannelTypes.RECEIVED_MY_CHANNEL_MEMBER,
data: channelMember
},
{
type: ChannelTypes.JOIN_CHANNEL_SUCCESS
}
]), getState);
};
}
export function deleteChannel(teamId, channelId) {
return async (dispatch, getState) => {
try {
dispatch({type: ChannelTypes.DELETE_CHANNEL_REQUEST}, getState);
dispatch({type: ChannelTypes.DELETE_CHANNEL_REQUEST}, getState);
try {
await Client.deleteChannel(teamId, channelId);
dispatch(batchActions([
{
type: ChannelTypes.RECEIVED_CHANNEL_DELETED,
channel_id: channelId
},
{
type: ChannelTypes.DELETE_CHANNEL_SUCCESS
}
]), getState);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch({type: ChannelTypes.DELETE_CHANNEL_FAILURE, error}, getState);
return;
}
dispatch(batchActions([
{
type: ChannelTypes.RECEIVED_CHANNEL_DELETED,
channel_id: channelId
},
{
type: ChannelTypes.DELETE_CHANNEL_SUCCESS
}
]), getState);
};
}
export function updateLastViewedAt(teamId, channelId, active) {
return async (dispatch, getState) => {
try {
dispatch({type: ChannelTypes.UPDATE_LAST_VIEWED_REQUEST}, getState);
dispatch({type: ChannelTypes.UPDATE_LAST_VIEWED_REQUEST}, getState);
try {
// this API should return the timestamp that was set
await Client.updateLastViewedAt(teamId, channelId, active);
dispatch(batchActions([
{
type: ChannelTypes.RECEIVED_LAST_VIEWED,
channel_id: channelId,
last_viewed_at: new Date().getTime()
},
{
type: ChannelTypes.UPDATE_LAST_VIEWED_SUCCESS
}
]), getState);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch({type: ChannelTypes.UPDATE_LAST_VIEWED_FAILURE, error}, getState);
return;
}
dispatch(batchActions([
{
type: ChannelTypes.RECEIVED_LAST_VIEWED,
channel_id: channelId,
last_viewed_at: new Date().getTime()
},
{
type: ChannelTypes.UPDATE_LAST_VIEWED_SUCCESS
}
]), getState);
};
}
export function getMoreChannels(teamId, offset, limit = Constants.CHANNELS_CHUNK_SIZE) {
return async (dispatch, getState) => {
dispatch({type: ChannelTypes.MORE_CHANNELS_REQUEST}, getState);
let channels;
try {
dispatch({type: ChannelTypes.MORE_CHANNELS_REQUEST}, getState);
const channels = Client.getMoreChannels(teamId, offset, limit);
dispatch(batchActions([
{
type: ChannelTypes.RECEIVED_MORE_CHANNELS,
data: await channels
},
{
type: ChannelTypes.MORE_CHANNELS_SUCCESS
}
]), getState);
channels = await Client.getMoreChannels(teamId, offset, limit);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch({type: ChannelTypes.MORE_CHANNELS_FAILURE, error}, getState);
return;
}
dispatch(batchActions([
{
type: ChannelTypes.RECEIVED_MORE_CHANNELS,
data: await channels
},
{
type: ChannelTypes.MORE_CHANNELS_SUCCESS
}
]), getState);
};
}
export function getChannelStats(teamId, channelId) {
return async (dispatch, getState) => {
try {
dispatch({type: ChannelTypes.CHANNEL_STATS_REQUEST}, getState);
dispatch({type: ChannelTypes.CHANNEL_STATS_REQUEST}, getState);
const stat = await Client.getChannelStats(teamId, channelId);
dispatch(batchActions([
{
type: ChannelTypes.RECEIVED_CHANNEL_STATS,
data: stat
},
{
type: ChannelTypes.CHANNEL_STATS_SUCCESS
}
]), getState);
let stat;
try {
stat = await Client.getChannelStats(teamId, channelId);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch({type: ChannelTypes.CHANNEL_STATS_FAILURE, error}, getState);
return;
}
dispatch(batchActions([
{
type: ChannelTypes.RECEIVED_CHANNEL_STATS,
data: stat
},
{
type: ChannelTypes.CHANNEL_STATS_SUCCESS
}
]), getState);
};
}
export function addChannelMember(teamId, channelId, userId) {
return async (dispatch, getState) => {
try {
dispatch({type: ChannelTypes.ADD_CHANNEL_MEMBER_REQUEST}, getState);
dispatch({type: ChannelTypes.ADD_CHANNEL_MEMBER_REQUEST}, getState);
try {
await Client.addChannelMember(teamId, channelId, userId);
dispatch(batchActions([
{
type: UsersTypes.RECEIVED_PROFILE_IN_CHANNEL,
data: {user_id: userId},
id: channelId
},
{
type: ChannelTypes.ADD_CHANNEL_MEMBER_SUCCESS
}
]), getState);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch({type: ChannelTypes.ADD_CHANNEL_MEMBER_FAILURE, error}, getState);
return;
}
dispatch(batchActions([
{
type: UsersTypes.RECEIVED_PROFILE_IN_CHANNEL,
data: {user_id: userId},
id: channelId
},
{
type: ChannelTypes.ADD_CHANNEL_MEMBER_SUCCESS
}
]), getState);
};
}
export function removeChannelMember(teamId, channelId, userId) {
return async (dispatch, getState) => {
try {
dispatch({type: ChannelTypes.REMOVE_CHANNEL_MEMBER_REQUEST}, getState);
dispatch({type: ChannelTypes.REMOVE_CHANNEL_MEMBER_REQUEST}, getState);
try {
await Client.addChannelMember(teamId, channelId, userId);
dispatch(batchActions([
{
type: UsersTypes.RECEIVED_PROFILE_NOT_IN_CHANNEL,
data: {user_id: userId},
id: channelId
},
{
type: ChannelTypes.REMOVE_CHANNEL_MEMBER_SUCCESS
}
]), getState);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch({type: ChannelTypes.REMOVE_CHANNEL_MEMBER_FAILURE, error}, getState);
return;
}
dispatch(batchActions([
{
type: UsersTypes.RECEIVED_PROFILE_NOT_IN_CHANNEL,
data: {user_id: userId},
id: channelId
},
{
type: ChannelTypes.REMOVE_CHANNEL_MEMBER_SUCCESS
}
]), getState);
};
}

View file

@ -44,18 +44,21 @@ export function bindClientFunc(clientFunc, request, success, failure, ...args) {
return async (dispatch, getState) => {
dispatch(requestData(request), getState);
let data = null;
try {
const data = await clientFunc(...args);
if (Array.isArray(success)) {
success.forEach((s) => {
dispatcher(s, data, dispatch, getState);
});
} else {
dispatcher(success, data, dispatch, getState);
}
data = await clientFunc(...args);
} catch (err) {
forceLogoutIfNecessary(err, dispatch);
dispatch(requestFailure(failure, err), getState);
return;
}
if (Array.isArray(success)) {
success.forEach((s) => {
dispatcher(s, data, dispatch, getState);
});
} else {
dispatcher(success, data, dispatch, getState);
}
};
}

View file

@ -30,23 +30,25 @@ export function editPost(teamId, post) {
export function deletePost(teamId, post) {
return async (dispatch, getState) => {
try {
dispatch({type: PostsTypes.DELETE_POST_REQUEST}, getState);
dispatch({type: PostsTypes.DELETE_POST_REQUEST}, getState);
try {
await Client.deletePost(teamId, post.channel_id, post.id);
dispatch(batchActions([
{
type: PostsTypes.POST_DELETED,
data: post
},
{
type: PostsTypes.DELETE_POST_SUCCESS
}
]), getState);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch({type: PostsTypes.DELETE_POST_FAILURE, error}, getState);
return;
}
dispatch(batchActions([
{
type: PostsTypes.POST_DELETED,
data: post
},
{
type: PostsTypes.DELETE_POST_SUCCESS
}
]), getState);
};
}
@ -61,111 +63,131 @@ export function removePost(post) {
export function getPost(teamId, channelId, postId) {
return async (dispatch, getState) => {
dispatch({type: PostsTypes.GET_POST_REQUEST}, getState);
let post;
try {
dispatch({type: PostsTypes.GET_POST_REQUEST}, getState);
const post = await Client.getPost(teamId, channelId, postId);
dispatch(batchActions([
{
type: PostsTypes.RECEIVED_POSTS,
data: post,
channelId
},
{
type: PostsTypes.GET_POST_SUCCESS
}
]), getState);
post = await Client.getPost(teamId, channelId, postId);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch({type: PostsTypes.GET_POST_FAILURE, error}, getState);
return;
}
dispatch(batchActions([
{
type: PostsTypes.RECEIVED_POSTS,
data: post,
channelId
},
{
type: PostsTypes.GET_POST_SUCCESS
}
]), getState);
};
}
export function getPosts(teamId, channelId, offset = 0, limit = Constants.POST_CHUNK_SIZE) {
return async (dispatch, getState) => {
dispatch({type: PostsTypes.GET_POSTS_REQUEST}, getState);
let posts;
try {
dispatch({type: PostsTypes.GET_POSTS_REQUEST}, getState);
const posts = await Client.getPosts(teamId, channelId, offset, limit);
dispatch(batchActions([
{
type: PostsTypes.RECEIVED_POSTS,
data: posts,
channelId
},
{
type: PostsTypes.GET_POSTS_SUCCESS
}
]), getState);
posts = await Client.getPosts(teamId, channelId, offset, limit);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch({type: PostsTypes.GET_POSTS_FAILURE, error}, getState);
return;
}
dispatch(batchActions([
{
type: PostsTypes.RECEIVED_POSTS,
data: posts,
channelId
},
{
type: PostsTypes.GET_POSTS_SUCCESS
}
]), getState);
};
}
export function getPostsSince(teamId, channelId, since) {
return async (dispatch, getState) => {
dispatch({type: PostsTypes.GET_POSTS_SINCE_REQUEST}, getState);
let posts;
try {
dispatch({type: PostsTypes.GET_POSTS_SINCE_REQUEST}, getState);
const posts = await Client.getPostsSince(teamId, channelId, since);
dispatch(batchActions([
{
type: PostsTypes.RECEIVED_POSTS,
data: posts,
channelId
},
{
type: PostsTypes.GET_POSTS_SINCE_SUCCESS
}
]), getState);
posts = await Client.getPostsSince(teamId, channelId, since);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch({type: PostsTypes.GET_POSTS_SINCE_FAILURE, error}, getState);
return;
}
dispatch(batchActions([
{
type: PostsTypes.RECEIVED_POSTS,
data: posts,
channelId
},
{
type: PostsTypes.GET_POSTS_SINCE_SUCCESS
}
]), getState);
};
}
export function getPostsBefore(teamId, channelId, postId, offset = 0, limit = Constants.POST_CHUNK_SIZE) {
return async (dispatch, getState) => {
dispatch({type: PostsTypes.GET_POSTS_BEFORE_REQUEST}, getState);
let posts;
try {
dispatch({type: PostsTypes.GET_POSTS_BEFORE_REQUEST}, getState);
const posts = await Client.getPostsBefore(teamId, channelId, postId, offset, limit);
dispatch(batchActions([
{
type: PostsTypes.RECEIVED_POSTS,
data: posts,
channelId
},
{
type: PostsTypes.GET_POSTS_BEFORE_SUCCESS
}
]), getState);
posts = await Client.getPostsBefore(teamId, channelId, postId, offset, limit);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch({type: PostsTypes.GET_POSTS_BEFORE_FAILURE, error}, getState);
return;
}
dispatch(batchActions([
{
type: PostsTypes.RECEIVED_POSTS,
data: posts,
channelId
},
{
type: PostsTypes.GET_POSTS_BEFORE_SUCCESS
}
]), getState);
};
}
export function getPostsAfter(teamId, channelId, postId, offset = 0, limit = Constants.POST_CHUNK_SIZE) {
return async (dispatch, getState) => {
dispatch({type: PostsTypes.GET_POSTS_AFTER_REQUEST}, getState);
let posts;
try {
dispatch({type: PostsTypes.GET_POSTS_AFTER_REQUEST}, getState);
const posts = await Client.getPostsAfter(teamId, channelId, postId, offset, limit);
dispatch(batchActions([
{
type: PostsTypes.RECEIVED_POSTS,
data: posts,
channelId
},
{
type: PostsTypes.GET_POSTS_AFTER_SUCCESS
}
]), getState);
posts = await Client.getPostsAfter(teamId, channelId, postId, offset, limit);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch({type: PostsTypes.GET_POSTS_AFTER_FAILURE, error}, getState);
return;
}
dispatch(batchActions([
{
type: PostsTypes.RECEIVED_POSTS,
data: posts,
channelId
},
{
type: PostsTypes.GET_POSTS_AFTER_SUCCESS
}
]), getState);
};
}

View file

@ -23,20 +23,21 @@ export function savePreferences(preferences) {
try {
await Client.savePreferences(preferences);
dispatch(batchActions([
{
type: PreferencesTypes.RECEIVED_PREFERENCES,
data: preferences
},
{
type: PreferencesTypes.SAVE_PREFERENCES_SUCCESS
}
]), getState);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch({type: PreferencesTypes.SAVE_PREFERENCES_FAILURE, error}, getState);
return;
}
dispatch(batchActions([
{
type: PreferencesTypes.RECEIVED_PREFERENCES,
data: preferences
},
{
type: PreferencesTypes.SAVE_PREFERENCES_SUCCESS
}
]), getState);
};
}
@ -46,20 +47,21 @@ export function deletePreferences(preferences) {
try {
await Client.deletePreferences(preferences);
dispatch(batchActions([
{
type: PreferencesTypes.DELETED_PREFERENCES,
data: preferences
},
{
type: PreferencesTypes.DELETE_PREFERENCES_SUCCESS
}
]), getState);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch({type: PreferencesTypes.DELETE_PREFERENCES_FAILURE, error}, getState);
return;
}
dispatch(batchActions([
{
type: PreferencesTypes.DELETED_PREFERENCES,
data: preferences
},
{
type: PreferencesTypes.DELETE_PREFERENCES_SUCCESS
}
]), getState);
};
}

View file

@ -35,39 +35,43 @@ export function getAllTeamListings() {
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({type: TeamsTypes.CREATE_TEAM_REQUEST}, getState);
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);
let created;
try {
created = await Client.createTeam(team);
} catch (err) {
forceLogoutIfNecessary(err, dispatch);
dispatch({type: TeamsTypes.CREATE_TEAM_FAILURE, error: err}, getState);
return;
}
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);
};
}
@ -92,22 +96,26 @@ export function getMyTeamMembers() {
export function getTeamMember(teamId, userId) {
return async (dispatch, getState) => {
dispatch({type: TeamsTypes.TEAM_MEMBERS_REQUEST}, getState);
let member;
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);
member = await Client.getTeamMember(teamId, userId);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch({type: TeamsTypes.TEAM_MEMBERS_FAILURE, error}, getState);
return;
}
dispatch(batchActions([
{
type: TeamsTypes.RECEIVED_MEMBERS_IN_TEAM,
data: [member]
},
{
type: TeamsTypes.TEAM_MEMBERS_SUCCESS
}
]), getState);
};
}
@ -134,52 +142,58 @@ export function getTeamStats(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({type: TeamsTypes.ADD_TEAM_MEMBER_REQUEST}, getState);
dispatch(batchActions([
{
type: TeamsTypes.RECEIVED_MEMBER_IN_TEAM,
data: member
},
{
type: TeamsTypes.ADD_TEAM_MEMBER_SUCCESS
}
]), getState);
try {
await Client.addUserToTeam(teamId, userId);
} catch (err) {
forceLogoutIfNecessary(err, dispatch);
dispatch({type: TeamsTypes.ADD_TEAM_MEMBER_FAILURE, error: err}, getState);
return;
}
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);
};
}
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({type: TeamsTypes.REMOVE_TEAM_MEMBER_REQUEST}, getState);
dispatch(batchActions([
{
type: TeamsTypes.REMOVE_MEMBER_FROM_TEAM,
data: member
},
{
type: TeamsTypes.REMOVE_TEAM_MEMBER_SUCCESS
}
]), getState);
try {
await Client.removeUserFromTeam(teamId, userId);
} catch (err) {
forceLogoutIfNecessary(err, dispatch);
dispatch({type: TeamsTypes.REMOVE_TEAM_MEMBER_FAILURE, error: err}, getState);
return;
}
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);
};
}

View file

@ -12,38 +12,48 @@ import {bindClientFunc, forceLogoutIfNecessary} from './helpers';
export function login(loginId, password, mfaToken = '') {
return async (dispatch, getState) => {
dispatch({type: UsersTypes.LOGIN_REQUEST}, getState);
Client.login(loginId, password, mfaToken).
then(async (data) => {
// TODO: uncomment when PLT-4167 is merged
// let teamMembers;
let preferences;
try {
const preferences = Client.getMyPreferences();
// TODO: uncomment when PLT-4167 is merged
// const teamMembersRequest = Client.getMyTeamMembers();
const preferencesRequest = Client.getMyPreferences();
// TODO: uncomment when PLT-4167 is merged
// const teamMembers = Client.getMyTeamMembers();
dispatch(batchActions([
{
type: UsersTypes.RECEIVED_ME,
data
},
{
type: PreferencesTypes.RECEIVED_PREFERENCES,
data: await preferences
},
// TODO: uncomment when PLT-4167 is merged
// {
// type: TeamsTypes.RECEIVED_MY_TEAM_MEMBERS,
// data: await teamMembers
// },
{
type: UsersTypes.LOGIN_SUCCESS
}
]), getState);
// teamMembers = await teamMembersRequest;
preferences = await preferencesRequest;
} catch (err) {
dispatch({type: UsersTypes.LOGIN_FAILURE, error: err}, getState);
return;
}
dispatch(batchActions([
{
type: UsersTypes.RECEIVED_ME,
data
},
{
type: PreferencesTypes.RECEIVED_PREFERENCES,
data: await preferences
},
// TODO: uncomment when PLT-4167 is merged
// {
// type: TeamsTypes.RECEIVED_MY_TEAM_MEMBERS,
// data: await teamMembers
// },
{
type: UsersTypes.LOGIN_SUCCESS
}
]), getState);
}).
catch((err) => {
dispatch({type: UsersTypes.LOGIN_FAILURE, error: err}, getState);
return;
});
};
}
@ -80,79 +90,91 @@ export function getProfilesByIds(userIds) {
export function getProfilesInTeam(teamId, offset, limit = Constants.PROFILE_CHUNK_SIZE) {
return async (dispatch, getState) => {
dispatch({type: UsersTypes.PROFILES_IN_TEAM_REQUEST}, getState);
let profiles;
try {
dispatch({type: UsersTypes.PROFILES_IN_TEAM_REQUEST}, getState);
const profiles = await Client.getProfilesInTeam(teamId, offset, limit);
dispatch(batchActions([
{
type: UsersTypes.RECEIVED_PROFILES_IN_TEAM,
data: profiles,
id: teamId
},
{
type: UsersTypes.RECEIVED_PROFILES,
data: profiles
},
{
type: UsersTypes.PROFILES_IN_TEAM_SUCCESS
}
]), getState);
profiles = await Client.getProfilesInTeam(teamId, offset, limit);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch({type: UsersTypes.PROFILES_IN_TEAM_FAILURE, error}, getState);
return;
}
dispatch(batchActions([
{
type: UsersTypes.RECEIVED_PROFILES_IN_TEAM,
data: profiles,
id: teamId
},
{
type: UsersTypes.RECEIVED_PROFILES,
data: profiles
},
{
type: UsersTypes.PROFILES_IN_TEAM_SUCCESS
}
]), getState);
};
}
export function getProfilesInChannel(teamId, channelId, offset, limit = Constants.PROFILE_CHUNK_SIZE) {
return async (dispatch, getState) => {
dispatch({type: UsersTypes.PROFILES_IN_CHANNEL_REQUEST}, getState);
let profiles;
try {
dispatch({type: UsersTypes.PROFILES_IN_CHANNEL_REQUEST}, getState);
const profiles = await Client.getProfilesInChannel(teamId, channelId, offset, limit);
dispatch(batchActions([
{
type: UsersTypes.RECEIVED_PROFILES_IN_CHANNEL,
data: profiles,
id: channelId
},
{
type: UsersTypes.RECEIVED_PROFILES,
data: profiles
},
{
type: UsersTypes.PROFILES_IN_CHANNEL_SUCCESS
}
]), getState);
profiles = await Client.getProfilesInChannel(teamId, channelId, offset, limit);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch({type: UsersTypes.PROFILES_IN_CHANNEL_FAILURE, error}, getState);
return;
}
dispatch(batchActions([
{
type: UsersTypes.RECEIVED_PROFILES_IN_CHANNEL,
data: profiles,
id: channelId
},
{
type: UsersTypes.RECEIVED_PROFILES,
data: profiles
},
{
type: UsersTypes.PROFILES_IN_CHANNEL_SUCCESS
}
]), getState);
};
}
export function getProfilesNotInChannel(teamId, channelId, offset, limit = Constants.PROFILE_CHUNK_SIZE) {
return async (dispatch, getState) => {
dispatch({type: UsersTypes.PROFILES_NOT_IN_CHANNEL_REQUEST}, getState);
let profiles;
try {
dispatch({type: UsersTypes.PROFILES_NOT_IN_CHANNEL_REQUEST}, getState);
const profiles = await Client.getProfilesNotInChannel(teamId, channelId, offset, limit);
dispatch(batchActions([
{
type: UsersTypes.RECEIVED_PROFILES_NOT_IN_CHANNEL,
data: profiles,
id: channelId
},
{
type: UsersTypes.RECEIVED_PROFILES,
data: profiles
},
{
type: UsersTypes.PROFILES_NOT_IN_CHANNEL_SUCCESS
}
]), getState);
profiles = await Client.getProfilesNotInChannel(teamId, channelId, offset, limit);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch({type: UsersTypes.PROFILES_NOT_IN_CHANNEL_FAILURE, error}, getState);
return;
}
dispatch(batchActions([
{
type: UsersTypes.RECEIVED_PROFILES_NOT_IN_CHANNEL,
data: profiles,
id: channelId
},
{
type: UsersTypes.RECEIVED_PROFILES,
data: profiles
},
{
type: UsersTypes.PROFILES_NOT_IN_CHANNEL_SUCCESS
}
]), getState);
};
}