Skip to content

Commit

Permalink
fix(store): rename actions
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Sukharev <[email protected]>
  • Loading branch information
Antreesy committed Dec 12, 2023
1 parent e80b249 commit 869e44c
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -889,7 +889,7 @@ export default {
},

handleReply() {
this.chatExtrasStore.addMessageToBeReplied({
this.chatExtrasStore.setParentIdToReply({
token: this.token,
id: this.id,
})
Expand Down
22 changes: 11 additions & 11 deletions src/components/NewMessage/NewMessage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@
</template>
</NcButton>
</div>
<div v-if="messageToBeReplied" class="new-message-form__quote">
<Quote is-new-message-quote v-bind="messageToBeReplied" />
<div v-if="parentMessage" class="new-message-form__quote">
<Quote is-new-message-quote v-bind="parentMessage" />
</div>
<NcRichContenteditable ref="richContenteditable"
v-shortkey.once="$options.disableKeyboardShortcuts ? null : ['c']"
Expand Down Expand Up @@ -331,8 +331,8 @@ export default {
}
},

messageToBeReplied() {
const parentId = this.chatExtrasStore.getMessageToBeReplied(this.token)
parentMessage() {
const parentId = this.chatExtrasStore.getParentIdToReply(this.token)
return parentId && this.$store.getters.message(this.token, parentId)
},

Expand Down Expand Up @@ -403,14 +403,14 @@ export default {
},

text(newValue) {
this.chatExtrasStore.setCurrentMessageInput({ token: this.token, text: newValue })
this.chatExtrasStore.setChatInput({ token: this.token, text: newValue })
},

token: {
immediate: true,
handler(token) {
if (token) {
this.text = this.chatExtrasStore.getCurrentMessageInput(token)
this.text = this.chatExtrasStore.getChatInput(token)
} else {
this.text = ''
}
Expand All @@ -426,7 +426,7 @@ export default {
EventBus.$on('upload-start', this.handleUploadSideEffects)
EventBus.$on('upload-discard', this.handleUploadSideEffects)
EventBus.$on('retry-message', this.handleRetryMessage)
this.text = this.chatExtrasStore.getCurrentMessageInput(this.token)
this.text = this.chatExtrasStore.getChatInput(this.token)

if (!this.$store.getters.areFileTemplatesInitialised) {
this.$store.dispatch('getFileTemplates')
Expand Down Expand Up @@ -484,7 +484,7 @@ export default {
}
this.$nextTick(() => {
// reset or fill main input in chat view from the store
this.text = this.chatExtrasStore.getCurrentMessageInput(this.token)
this.text = this.chatExtrasStore.getChatInput(this.token)
// refocus input as the user might want to type further
this.focusInput()
})
Expand Down Expand Up @@ -516,7 +516,7 @@ export default {

if (this.upload) {
// Clear input content from store
this.chatExtrasStore.setCurrentMessageInput({ token: this.token, text: '' })
this.chatExtrasStore.setChatInput({ token: this.token, text: '' })

if (this.$store.getters.getInitialisedUploads(this.$store.getters.currentUploadId).length) {
// If dialog contains files to upload, delegate sending
Expand All @@ -539,7 +539,7 @@ export default {
// Scrolls the message list to the last added message
EventBus.$emit('smooth-scroll-chat-to-bottom')
// Also remove the message to be replied for this conversation
this.chatExtrasStore.removeMessageToBeReplied(this.token)
this.chatExtrasStore.removeParentIdToReply(this.token)

this.broadcast
? await this.broadcastMessage(temporaryMessage, options)
Expand Down Expand Up @@ -592,7 +592,7 @@ export default {

// Restore the parent/quote message
if (temporaryMessage.parent) {
this.chatExtrasStore.addMessageToBeReplied({
this.chatExtrasStore.setParentIdToReply({
token: this.token,
id: temporaryMessage.parent.id,
})
Expand Down
2 changes: 1 addition & 1 deletion src/components/Quote.vue
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ export default {
},
methods: {
handleAbortReply() {
this.chatExtrasStore.removeMessageToBeReplied(this.token)
this.chatExtrasStore.removeParentIdToReply(this.token)
EventBus.$emit('focus-chat-input')
},

Expand Down
2 changes: 1 addition & 1 deletion src/store/conversationsStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ const actions = {
try {
const response = await clearConversationHistory(token)
const chatExtrasStore = useChatExtrasStore()
chatExtrasStore.removeMessageToBeReplied(token)
chatExtrasStore.removeParentIdToReply(token)
context.dispatch('deleteMessages', token)
return response
} catch (error) {
Expand Down
2 changes: 1 addition & 1 deletion src/store/messagesStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ const actions = {
*/
createTemporaryMessage(context, { text, token, uploadId, index, file, localUrl, isVoiceMessage }) {
const chatExtrasStore = useChatExtrasStore()
const parentId = chatExtrasStore.getMessageToBeReplied(token)
const parentId = chatExtrasStore.getParentIdToReply(token)
const parent = parentId && context.getters.message(token, parentId)
const date = new Date()
let tempId = 'temp-' + date.getTime()
Expand Down
2 changes: 1 addition & 1 deletion src/store/messagesStore.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ describe('messagesStore', () => {
}

store.dispatch('processMessage', parent)
chatExtraStore.addMessageToBeReplied({ token: TOKEN, id: 123 })
chatExtraStore.setParentIdToReply({ token: TOKEN, id: 123 })

const temporaryMessage = await store.dispatch('createTemporaryMessage', {
text: 'blah',
Expand Down
30 changes: 15 additions & 15 deletions src/stores/__tests__/chatExtras.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,43 +77,43 @@ describe('chatExtrasStore', () => {
describe('reply message', () => {
it('adds reply message id to the store', () => {
// Act
chatExtrasStore.addMessageToBeReplied({ token, id: 101 })
chatExtrasStore.setParentIdToReply({ token, id: 101 })

// Assert
expect(chatExtrasStore.getMessageToBeReplied(token)).toBe(101)
expect(chatExtrasStore.getParentIdToReply(token)).toBe(101)
})

it('clears reply message', () => {
// Arrange
chatExtrasStore.addMessageToBeReplied({ token, id: 101 })
chatExtrasStore.setParentIdToReply({ token, id: 101 })

// Act
chatExtrasStore.removeMessageToBeReplied(token)
chatExtrasStore.removeParentIdToReply(token)

// Assert
expect(chatExtrasStore.getMessageToBeReplied(token)).not.toBeDefined()
expect(chatExtrasStore.getParentIdToReply(token)).not.toBeDefined()
})
})

describe('current input message', () => {
it('sets current input message', () => {
// Act
chatExtrasStore.setCurrentMessageInput({ token: 'token-1', text: 'message-1' })
chatExtrasStore.setChatInput({ token: 'token-1', text: 'message-1' })

// Assert
expect(chatExtrasStore.getCurrentMessageInput('token-1')).toStrictEqual('message-1')
expect(chatExtrasStore.getChatInput('token-1')).toStrictEqual('message-1')
})

it('clears current input message', () => {
// Arrange
chatExtrasStore.setCurrentMessageInput({ token: 'token-1', text: 'message-1' })
chatExtrasStore.setChatInput({ token: 'token-1', text: 'message-1' })

// Act
chatExtrasStore.removeCurrentMessageInput('token-1')
chatExtrasStore.removeChatInput('token-1')

// Assert
expect(chatExtrasStore.currentMessageInput['token-1']).not.toBeDefined()
expect(chatExtrasStore.getCurrentMessageInput('token-1')).toBe('')
expect(chatExtrasStore.chatInput['token-1']).not.toBeDefined()
expect(chatExtrasStore.getChatInput('token-1')).toBe('')
})
})

Expand All @@ -124,16 +124,16 @@ describe('chatExtrasStore', () => {
getUserAbsence.mockResolvedValueOnce(response)

await chatExtrasStore.getUserAbsence({ token: 'token-1', userId })
chatExtrasStore.addMessageToBeReplied({ token: 'token-1', id: 101 })
chatExtrasStore.setCurrentMessageInput({ token: 'token-1', text: 'message-1' })
chatExtrasStore.setParentIdToReply({ token: 'token-1', id: 101 })
chatExtrasStore.setChatInput({ token: 'token-1', text: 'message-1' })

// Act
chatExtrasStore.purgeChatExtras('token-1')

// Assert
expect(chatExtrasStore.absence['token-1']).not.toBeDefined()
expect(chatExtrasStore.messagesToBeReplied['token-1']).not.toBeDefined()
expect(chatExtrasStore.currentMessageInput['token-1']).not.toBeDefined()
expect(chatExtrasStore.parentToReply['token-1']).not.toBeDefined()
expect(chatExtrasStore.chatInput['token-1']).not.toBeDefined()
})
})
})
38 changes: 19 additions & 19 deletions src/stores/chatExtras.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import { getUserAbsence } from '../services/participantsService.js'
/**
* @typedef {object} State
* @property {{[key: Token]: object}} absence - The absence status per conversation.
* @property {{[key: Token]: number}} messagesToBeReplied - The parent message id to reply per conversation.
* @property {{[key: Token]: string}} currentMessageInput -The input value per conversation.
* @property {{[key: Token]: number}} parentToReply - The parent message id to reply per conversation.
* @property {{[key: Token]: string}} chatInput -The input value per conversation.
*/

/**
Expand All @@ -46,19 +46,19 @@ import { getUserAbsence } from '../services/participantsService.js'
export const useChatExtrasStore = defineStore('chatExtras', {
state: () => ({
absence: {},
messagesToBeReplied: {},
currentMessageInput: {},
parentToReply: {},
chatInput: {},
}),

getters: {
getMessageToBeReplied: (state) => (token) => {
if (state.messagesToBeReplied[token]) {
return state.messagesToBeReplied[token]
getParentIdToReply: (state) => (token) => {
if (state.parentToReply[token]) {
return state.parentToReply[token]
}
},

getCurrentMessageInput: (state) => (token) => {
return state.currentMessageInput[token] ?? ''
getChatInput: (state) => (token) => {
return state.chatInput[token] ?? ''
},
},

Expand Down Expand Up @@ -104,8 +104,8 @@ export const useChatExtrasStore = defineStore('chatExtras', {
* @param {string} payload.token The conversation token
* @param {number} payload.id The id of message
*/
addMessageToBeReplied({ token, id }) {
Vue.set(this.messagesToBeReplied, token, id)
setParentIdToReply({ token, id }) {
Vue.set(this.parentToReply, token, id)
},

/**
Expand All @@ -114,8 +114,8 @@ export const useChatExtrasStore = defineStore('chatExtras', {
*
* @param {string} token The conversation token
*/
removeMessageToBeReplied(token) {
Vue.delete(this.messagesToBeReplied, token)
removeParentIdToReply(token) {
Vue.delete(this.parentToReply, token)
},

/**
Expand All @@ -125,23 +125,23 @@ export const useChatExtrasStore = defineStore('chatExtras', {
* @param {string} payload.token The conversation token
* @param {string} payload.text The string to store
*/
setCurrentMessageInput({ token, text }) {
setChatInput({ token, text }) {
// FIXME upstream: https://github.com/nextcloud-libraries/nextcloud-vue/issues/4492
const temp = document.createElement('textarea')
temp.innerHTML = text.replace(/&/gmi, '&amp;')
const parsedText = temp.value.replace(/&amp;/gmi, '&').replace(/&lt;/gmi, '<')
.replace(/&gt;/gmi, '>').replace(/&sect;/gmi, '§')

Vue.set(this.currentMessageInput, token, parsedText)
Vue.set(this.chatInput, token, parsedText)
},

/**
* Remove a current input value from the store for a given conversation token
*
* @param {string} token The conversation token
*/
removeCurrentMessageInput(token) {
Vue.delete(this.currentMessageInput, token)
removeChatInput(token) {
Vue.delete(this.chatInput, token)
},

/**
Expand All @@ -150,9 +150,9 @@ export const useChatExtrasStore = defineStore('chatExtras', {
* @param {string} token the token of the conversation to be deleted
*/
purgeChatExtras(token) {
this.removeMessageToBeReplied(token)
this.removeParentIdToReply(token)
this.removeUserAbsence(token)
this.removeCurrentMessageInput(token)
this.removeChatInput(token)
},
},
})

0 comments on commit 869e44c

Please sign in to comment.