diff --git a/src/message/messageActionSheet.js b/src/message/messageActionSheet.js index c9423033819..bb8df3c0edd 100644 --- a/src/message/messageActionSheet.js +++ b/src/message/messageActionSheet.js @@ -146,7 +146,7 @@ const muteTopic = async ({ auth, stream, topic, subscriptions }) => { muteTopic.title = 'Mute topic'; muteTopic.errorMessage = 'Failed to mute topic'; -const deleteTopic = async ({ auth, stream, topic, dispatch, _ }) => { +const deleteTopic = async ({ auth, stream, topic, subscriptions, dispatch, _ }) => { const alertTitle = _('Are you sure you want to delete the topic “{topic}”?', { topic }); const AsyncAlert = async (): Promise => new Promise((resolve, reject) => { @@ -173,7 +173,10 @@ const deleteTopic = async ({ auth, stream, topic, dispatch, _ }) => { ); }); if (await AsyncAlert()) { - await dispatch(deleteMessagesForTopic(stream, topic)); + const sub = subscriptions.find(x => x.name === stream); + if (sub) { + await dispatch(deleteMessagesForTopic(sub.stream_id, topic)); + } } }; deleteTopic.title = 'Delete topic'; diff --git a/src/topics/topicActions.js b/src/topics/topicActions.js index 521410a8f42..04c24aec2a0 100644 --- a/src/topics/topicActions.js +++ b/src/topics/topicActions.js @@ -40,25 +40,24 @@ export const fetchTopicsForStream = (narrow: Narrow) => async ( dispatch(fetchTopics(stream.stream_id)); }; -export const deleteMessagesForTopic = (streamName: string, topic: string) => async ( +export const deleteMessagesForTopic = (streamId: number, topic: string) => async ( dispatch: Dispatch, getState: GetState, ) => { const state = getState(); const outbox = getOutbox(state); + const currentStream: Stream | void = getStreams(state).find( + (stream: Stream) => stream.stream_id === streamId, + ); + outbox.forEach((outboxMessage: Outbox) => { if ( outboxMessage.type === 'stream' - && streamNameOfStreamMessage(outboxMessage) === streamName + && streamNameOfStreamMessage(outboxMessage) === currentStream?.name && outboxMessage.subject === topic ) { dispatch(deleteOutboxMessage(outboxMessage.id)); } }); - const currentStream: Stream | void = getStreams(state).find( - (stream: Stream) => stream.name === streamName, - ); - if (currentStream) { - await api.deleteTopic(getAuth(state), currentStream.stream_id, topic); - } + await api.deleteTopic(getAuth(state), streamId, topic); };