Skip to content

Commit

Permalink
action sheet: add and implement delete topic option
Browse files Browse the repository at this point in the history
  • Loading branch information
agrawal-d committed Feb 6, 2020
1 parent 4ef9a13 commit 1b112d8
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import markAllAsRead from './mark_as_read/markAllAsRead';
import markStreamAsRead from './mark_as_read/markStreamAsRead';
import markTopicAsRead from './mark_as_read/markTopicAsRead';
import deleteMessage from './messages/deleteMessage';
import deleteTopic from './messages/deleteTopic';
import getRawMessageContent from './messages/getRawMessageContent';
import getMessages from './messages/getMessages';
import getMessageHistory from './messages/getMessageHistory';
Expand Down Expand Up @@ -72,6 +73,7 @@ export {
markStreamAsRead,
markTopicAsRead,
deleteMessage,
deleteTopic,
getRawMessageContent,
getMessages,
getMessageHistory,
Expand Down
9 changes: 9 additions & 0 deletions src/api/messages/deleteTopic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* @flow strict-local */
import type { ApiResponse, Auth } from '../transportTypes';
import { apiPost } from '../apiFetch';

// Undocmumented API endpoint - deletes a topic, and all messages in it.
export default async (auth: Auth, streamId: number, topicName: string): Promise<ApiResponse> =>
apiPost(auth, `streams/${streamId}/delete_topic`, {
topic_name: topicName,
});
30 changes: 30 additions & 0 deletions src/message/messageActionSheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import * as api from '../api';
import { showToast } from '../utils/info';
import { doNarrow, startEditMessage, deleteOutboxMessage, navigateToEmojiPicker } from '../actions';
import { navigateToMessageReactionScreen } from '../nav/navActions';
import { deleteMessagesForTopic } from '../topics/topicActions';

// TODO really this belongs in a libdef.
export type ShowActionSheetWithOptions = (
Expand Down Expand Up @@ -90,6 +91,33 @@ const muteTopic = async ({ auth, message }) => {
muteTopic.title = 'Mute topic';
muteTopic.errorMessage = 'Failed to mute topic';

const deleteTopic = async ({ auth, message, dispatch, ownEmail, _ }) => {
Alert.alert(
`Are you sure you want to delete the topic '${message.subject}' ?`,
'This will also delete all messages in the topic.',
[
{
text: 'Delete topic',
onPress: async () => {
try {
dispatch(deleteMessagesForTopic(getNarrowFromMessage(message, ownEmail)));
} catch (err) {
Alert.alert(_(deleteTopic.errorMessage), err.message);
}
},
},
{
text: 'Cancel',
onPress: () => {},
style: 'cancel',
},
],
{ cancelable: true },
);
};
deleteTopic.title = 'Delete topic';
deleteTopic.errorMessage = 'Failed to delete topic';

const unmuteStream = async ({ auth, message, subscriptions }) => {
const sub = subscriptions.find(x => x.name === message.display_recipient);
if (sub) {
Expand Down Expand Up @@ -159,6 +187,7 @@ const allButtonsRaw = {
// For headers
unmuteTopic,
muteTopic,
deleteTopic,
muteStream,
unmuteStream,

Expand Down Expand Up @@ -186,6 +215,7 @@ export const constructHeaderActionButtons = ({
}: ConstructSheetParams): ButtonCode[] => {
const buttons: ButtonCode[] = [];
if (message.type === 'stream') {
buttons.push('deleteTopic');
if (isTopicMuted(message.display_recipient, message.subject, mute)) {
buttons.push('unmuteTopic');
} else {
Expand Down
21 changes: 19 additions & 2 deletions src/topics/topicActions.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/* @flow strict-local */
import type { GetState, Dispatch, Narrow, Topic, Action } from '../types';
import type { GetState, Dispatch, Message, Narrow, Topic, Action, Outbox } from '../types';
import * as api from '../api';
import { INIT_TOPICS } from '../actionConstants';
import { isStreamNarrow } from '../utils/narrow';
import { getAuth, getStreams } from '../selectors';
import { getAuth, getStreams, getMessagesForNarrow } from '../selectors';
import { deleteOutboxMessage } from '../actions';

export const initTopics = (topics: Topic[], streamId: number): Action => ({
type: INIT_TOPICS,
Expand Down Expand Up @@ -34,3 +35,19 @@ export const fetchTopicsForActiveStream = (narrow: Narrow) => async (
}
dispatch(fetchTopics(stream.stream_id));
};

export const deleteMessagesForTopic = (narrow: Narrow) => async (
dispatch: Dispatch,
getState: GetState,
) => {
const messages: $ReadOnlyArray<Message | Outbox> = getMessagesForNarrow(getState(), narrow);
console.log('agrawal here', messages, narrow);
const auth = getAuth(getState());
messages.forEach(async message => {
if (message.isOutbox) {
dispatch(deleteOutboxMessage(message.timestamp));
} else {
await api.deleteMessage(auth, message.id);
}
});
};
1 change: 1 addition & 0 deletions static/translations/messages_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"Copy to clipboard": "Copy to clipboard",
"Link copied to clipboard": "Link copied to clipboard",
"Mute topic": "Mute topic",
"Delete topic":"Delete topic",
"Unmute topic": "Unmute topic",
"Mute stream": "Mute stream",
"Unmute stream": "Unmute stream",
Expand Down

0 comments on commit 1b112d8

Please sign in to comment.