diff --git a/src/components/AChat/AChatForm.vue b/src/components/AChat/AChatForm.vue index 46807b123..36eb30a4d 100644 --- a/src/components/AChat/AChatForm.vue +++ b/src/components/AChat/AChatForm.vue @@ -7,6 +7,7 @@ @@ -252,7 +255,6 @@ - @@ -484,8 +486,9 @@ export default { 'Windows 10': this.$t('chats.message_windows_10') }[detect().os] || this.$t('chats.message') - if (this.$route.query.replyToId) { - this.replyMessageId = this.$route.query.replyToId + const draftMessage = this.$store.getters['draftMessage/draftReplyTold'](this.partnerId) + if (draftMessage) { + this.replyMessageId = draftMessage } }, methods: { @@ -510,7 +513,15 @@ export default { return } }, + cancelReplyMessage() { + this.replyMessageId = -1 + this.$store.commit('draftMessage/deleteReplyTold', { + replyToId: this.replyMessageId, + partnerId: this.partnerId + }) + }, sendMessage(message) { + this.$store.dispatch('draftMessage/deleteDraft', { partnerId: this.partnerId }) const replyToId = this.replyMessageId > -1 ? this.replyMessageId : undefined return this.$store @@ -667,6 +678,10 @@ export default { this.replyMessageId = message.id this.$refs.chatForm.focus() + this.$store.commit('draftMessage/saveReplyToId', { + replyToId: message.id, + partnerId: this.partnerId + }) }, copyMessageToClipboard({ message }) { this.closeActionsMenu() diff --git a/src/store/index.js b/src/store/index.js index 9c6bb58a6..0edd934fc 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -27,6 +27,7 @@ import bitcoinModule from './modules/btc' import nodesModule from './modules/nodes' import delegatesModule from './modules/delegates' import nodesPlugin from './modules/nodes/nodes-plugin' +import draftMessage from '@/store/modules/draft-message' import snackbar from './modules/snackbar' import language from './modules/language' import chat from './modules/chat' @@ -133,6 +134,7 @@ const store = { }, logout({ dispatch }) { dispatch('reset') + dispatch('draftMessage/resetState', null, { root: true }) }, unlock({ state, dispatch }) { // user updated an app, F5 or something @@ -228,6 +230,7 @@ const store = { delegates: delegatesModule, // Voting for delegates screen nodes: nodesModule, // ADAMANT nodes snackbar, + draftMessage, language, chat, options, diff --git a/src/store/modules/draft-message/index.ts b/src/store/modules/draft-message/index.ts new file mode 100644 index 000000000..f6699450a --- /dev/null +++ b/src/store/modules/draft-message/index.ts @@ -0,0 +1,83 @@ +import { MutationTree, GetterTree, ActionTree } from 'vuex' +import { RootState } from '@/store/types' + +export interface DraftState { + drafts: { [key: string]: { replyToId?: string; message?: string } } +} + +const state = (): DraftState => ({ + drafts: {} +}) + +const mutations: MutationTree = { + saveReplyToId(state, payload: { partnerId: string; replyToId: string }) { + if (state.drafts[payload.partnerId]) { + state.drafts[payload.partnerId].replyToId = payload.replyToId + } else { + state.drafts[payload.partnerId] = { + replyToId: payload.replyToId + } + } + }, + + saveMessage(state, payload: { partnerId: string; message: string }) { + if (state.drafts[payload.partnerId]) { + state.drafts[payload.partnerId].message = payload.message + } else { + state.drafts[payload.partnerId] = { + message: payload.message + } + } + }, + + deleteReplyTold(state, payload: { partnerId: string; replyToId: string }) { + delete state.drafts[payload.partnerId].replyToId + }, + + deleteMessage(state, payload: { partnerId: string; message: string }) { + delete state.drafts[payload.partnerId].message + }, + + reset(state) { + state.drafts = {} + } +} + +const getters: GetterTree = { + draftMessage: (state) => (partnerId: string) => { + const objMessage = state.drafts[partnerId] + if (objMessage) { + return objMessage.message + } else { + return '' + } + }, + + draftReplyTold: (state) => (partnerId: string) => { + const objMessage = state.drafts[partnerId] + if (objMessage) { + return objMessage.replyToId + } else { + return '' + } + } +} + +const actions: ActionTree = { + resetState(context) { + context.commit('reset') + }, + + deleteDraft(context, payload: { partnerId: string }) { + context.commit('deleteReplyTold', payload) + context.commit('deleteMessage', payload) + } +} + +export default { + state, + mutations, + getters, + actions, + namespaced: true +} diff --git a/src/store/types.ts b/src/store/types.ts index 30d59aaa5..172d6783d 100644 --- a/src/store/types.ts +++ b/src/store/types.ts @@ -1,3 +1,5 @@ +import { DraftState } from '@/store/modules/draft-message' + export interface RootState { address: string balance: number @@ -25,4 +27,5 @@ export interface RootState { identicon: any notification: any rate: any + draftMessage: DraftState }