Skip to content
This repository has been archived by the owner on Sep 13, 2022. It is now read-only.

MM-24009: remove redux-offline #1396

Merged
merged 16 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions src/actions/channels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -652,25 +652,27 @@ export function leaveChannel(channelId: string): ActionFunc {
},
});

try {
await Client4.removeFromChannel(currentUserId, channelId);
} catch {
dispatch(batchActions([
{
type: ChannelTypes.RECEIVED_CHANNEL,
data: channel,
},
{
type: ChannelTypes.RECEIVED_MY_CHANNEL_MEMBER,
data: member,
},
]));

// The category here may not be the one in which the channel was originally located,
// much less the order in which it was placed. Treating this as a transient issue
// for the user to resolve by refreshing or leaving again.
dispatch(addChannelToInitialCategory(channel, false));
}
(async function removeFromChannelWrapper() {
hmhealey marked this conversation as resolved.
Show resolved Hide resolved
try {
await Client4.removeFromChannel(currentUserId, channelId);
} catch {
dispatch(batchActions([
{
type: ChannelTypes.RECEIVED_CHANNEL,
data: channel,
},
{
type: ChannelTypes.RECEIVED_MY_CHANNEL_MEMBER,
data: member,
},
]));

// The category here may not be the one in which the channel was originally located,
// much less the order in which it was placed. Treating this as a transient issue
// for the user to resolve by refreshing or leaving again.
dispatch(addChannelToInitialCategory(channel, false));
}
}());

return {data: true};
};
Expand Down
134 changes: 69 additions & 65 deletions src/actions/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,61 +213,63 @@ export function createPost(post: Post, files: any[] = []) {

dispatch(batchActions(actions, 'BATCH_CREATE_POST_INIT'));
hmhealey marked this conversation as resolved.
Show resolved Hide resolved

try {
const created = await Client4.createPost({...newPost, create_at: 0});
(async function createPostWrapper() {
try {
const created = await Client4.createPost({...newPost, create_at: 0});

actions = [
receivedPost(created),
{
type: PostTypes.CREATE_POST_SUCCESS,
},
{
type: ChannelTypes.INCREMENT_TOTAL_MSG_COUNT,
data: {
channelId: newPost.channel_id,
amount: 1,
actions = [
receivedPost(created),
{
type: PostTypes.CREATE_POST_SUCCESS,
},
},
{
type: ChannelTypes.DECREMENT_UNREAD_MSG_COUNT,
data: {
channelId: newPost.channel_id,
amount: 1,
{
type: ChannelTypes.INCREMENT_TOTAL_MSG_COUNT,
data: {
channelId: newPost.channel_id,
amount: 1,
},
},
},
];

if (files) {
actions.push({
type: FileTypes.RECEIVED_FILES_FOR_POST,
postId: created.id,
data: files,
});
}
{
type: ChannelTypes.DECREMENT_UNREAD_MSG_COUNT,
data: {
channelId: newPost.channel_id,
amount: 1,
},
},
];

if (files) {
actions.push({
type: FileTypes.RECEIVED_FILES_FOR_POST,
postId: created.id,
data: files,
});
}

dispatch(batchActions(actions, 'BATCH_CREATE_POST'));
} catch (error) {
const data = {
...newPost,
id: pendingPostId,
failed: true,
update_at: Date.now(),
};
actions = [{type: PostTypes.CREATE_POST_FAILURE, error}];

// If the failure was because: the root post was deleted or
// TownSquareIsReadOnly=true then remove the post
if (error.server_error_id === 'api.post.create_post.root_id.app_error' ||
error.server_error_id === 'api.post.create_post.town_square_read_only' ||
error.server_error_id === 'plugin.message_will_be_posted.dismiss_post'
) {
actions.push(removePost(data) as any);
} else {
actions.push(receivedPost(data));
}
dispatch(batchActions(actions, 'BATCH_CREATE_POST'));
} catch (error) {
const data = {
...newPost,
id: pendingPostId,
failed: true,
update_at: Date.now(),
};
actions = [{type: PostTypes.CREATE_POST_FAILURE, error}];

// If the failure was because: the root post was deleted or
// TownSquareIsReadOnly=true then remove the post
if (error.server_error_id === 'api.post.create_post.root_id.app_error' ||
error.server_error_id === 'api.post.create_post.town_square_read_only' ||
error.server_error_id === 'plugin.message_will_be_posted.dismiss_post'
) {
actions.push(removePost(data) as any);
} else {
actions.push(receivedPost(data));
}

dispatch(batchActions(actions, 'BATCH_CREATE_POST_FAILED'));
}
dispatch(batchActions(actions, 'BATCH_CREATE_POST_FAILED'));
}
}());

return {data: true};
};
Expand Down Expand Up @@ -380,21 +382,23 @@ export function deletePost(post: ExtendedPost) {
}
});
} else {
try {
dispatch({
type: PostTypes.POST_DELETED,
data: delPost,
});

await Client4.deletePost(post.id);
} catch (e) {
// Recovering from this state doesn't actually work. The deleteAndRemovePost action
// in the webapp needs to get an error in order to not call removePost, but then
// the delete modal needs to handle this to show something to the user. Since none
// of that ever worked (even with redux-offline in play), leave the behaviour here
// unresolved.
console.error('failed to delete post', e); // eslint-disable-line no-console
}
(async function deletePostWrapper() {
try {
dispatch({
type: PostTypes.POST_DELETED,
data: delPost,
});

await Client4.deletePost(post.id);
} catch (e) {
// Recovering from this state doesn't actually work. The deleteAndRemovePost action
// in the webapp needs to get an error in order to not call removePost, but then
// the delete modal needs to handle this to show something to the user. Since none
// of that ever worked (even with redux-offline in play), leave the behaviour here
// unresolved.
console.error('failed to delete post', e); // eslint-disable-line no-console
}
}());
}

return {data: true};
Expand Down
56 changes: 30 additions & 26 deletions src/actions/preferences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,21 @@ export function deletePreferences(userId: string, preferences: PreferenceType[])
const myPreferences = getMyPreferencesSelector(state);
const currentPreferences = preferences.map((pref) => myPreferences[getPreferenceKey(pref.category, pref.name)]);

try {
dispatch({
type: PreferenceTypes.DELETED_PREFERENCES,
data: preferences,
});

await Client4.deletePreferences(userId, preferences);
} catch {
dispatch({
type: PreferenceTypes.RECEIVED_PREFERENCES,
data: currentPreferences,
});
}
(async function deletePreferencesWrapper() {
try {
dispatch({
type: PreferenceTypes.DELETED_PREFERENCES,
data: preferences,
});

await Client4.deletePreferences(userId, preferences);
} catch {
dispatch({
type: PreferenceTypes.RECEIVED_PREFERENCES,
data: currentPreferences,
});
}
}());

return {data: true};
};
Expand Down Expand Up @@ -120,19 +122,21 @@ export function setCustomStatusInitialisationState(initializationState: Record<s

export function savePreferences(userId: string, preferences: PreferenceType[]) {
return async (dispatch: DispatchFunc) => {
try {
dispatch({
type: PreferenceTypes.RECEIVED_PREFERENCES,
data: preferences,
});

await Client4.savePreferences(userId, preferences);
} catch {
dispatch({
type: PreferenceTypes.DELETED_PREFERENCES,
data: preferences,
});
}
(async function savePreferencesWrapper() {
try {
dispatch({
type: PreferenceTypes.RECEIVED_PREFERENCES,
data: preferences,
});

await Client4.savePreferences(userId, preferences);
} catch {
dispatch({
type: PreferenceTypes.DELETED_PREFERENCES,
data: preferences,
});
}
}());

return {data: true};
};
Expand Down