Skip to content

Commit

Permalink
Show error/warning if deleting wasn't as successful
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Jan 15, 2021
1 parent e75c83b commit 88a9a4b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
40 changes: 33 additions & 7 deletions src/components/MessagesList/MessagesGroup/Message/Message.vue
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ import { EventBus } from '../../../../services/EventBus'
import emojiRegex from 'emoji-regex'
import { PARTICIPANT, CONVERSATION } from '../../../../constants'
import moment from '@nextcloud/moment'
import {
showError,
showSuccess,
showWarning,
TOAST_DEFAULT_TIMEOUT
} from '@nextcloud/dialogs'
export default {
name: 'Message',
Expand Down Expand Up @@ -470,13 +476,33 @@ export default {
},
async handleDelete() {
this.isDeleting = true
await this.$store.dispatch('deleteMessage', {
message: {
token: this.token,
id: this.id,
},
placeholder: t('spreed', 'Deleting message'),
})
try {
const statusCode = await this.$store.dispatch('deleteMessage', {
message: {
token: this.token,
id: this.id,
},
placeholder: t('spreed', 'Deleting message'),
})
if (statusCode === 202) {
showWarning(t('spreed', 'Message deleted successfully, but Matterbridge is configured and the message might already be distributed to other services'), {
timeout: TOAST_DEFAULT_TIMEOUT * 2,
})
} else if (statusCode === 200) {
showSuccess(t('spreed', 'Message deleted successfully'))
}
} catch (e) {
if (e?.response?.status === 400) {
showError(t('spreed', 'Message could not be deleted because it is too old'))
} else {
showError(t('spreed', 'An error occurred while deleting the message'))
console.error(e)
}
this.isDeleting = false
return
}
this.isDeleting = false
},
},
Expand Down
26 changes: 19 additions & 7 deletions src/store/messagesStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ const mutations = {
* @param {object} message the message;
* @param {string} tempMessage Placeholder message until deleting finished
*/
markMessageAsDeleted(state, { message, placeholder }) {
markMessageAsDeleting(state, { message, placeholder }) {
Vue.set(state.messages[message.token][message.id], 'messageType', 'comment_deleted')
Vue.set(state.messages[message.token][message.id], 'message', placeholder)
},
Expand Down Expand Up @@ -194,15 +194,27 @@ const actions = {
* @param {string} placeholder Placeholder message until deleting finished
*/
async deleteMessage(context, { message, placeholder }) {
context.commit('markMessageAsDeleted', { message, placeholder })
const response = await deleteMessage(message)
const messageObject = Object.assign({}, context.getters.message(message.token, message.id))
context.commit('markMessageAsDeleting', { message, placeholder })

let response
try {
response = await deleteMessage(message)
} catch (e) {
// Restore the previous message state
context.commit('addMessage', messageObject)
throw e
}

if (response.parent) {
context.commit('addMessage', response.parent)
response.parent = response.parent.id
const systemMessage = response.data.ocs.data
if (systemMessage.parent) {
context.commit('addMessage', systemMessage.parent)
systemMessage.parent = systemMessage.parent.id
}

context.commit('addMessage', response)
context.commit('addMessage', systemMessage)

return response.status
},

/**
Expand Down

0 comments on commit 88a9a4b

Please sign in to comment.