-
Notifications
You must be signed in to change notification settings - Fork 442
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(store): migrate getters, actions and tests
Signed-off-by: Maksim Sukharev <[email protected]>
- Loading branch information
Showing
2 changed files
with
155 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
* @copyright Copyright (c) 2023 Maksim Sukharev <[email protected]> | ||
* | ||
* @author Maksim Sukharev <[email protected]> | ||
* @author Marco Ambrosini <[email protected]> | ||
* | ||
* @license AGPL-3.0-or-later | ||
* | ||
|
@@ -25,11 +26,42 @@ import Vue from 'vue' | |
|
||
import { getUserAbsence } from '../services/participantsService.js' | ||
|
||
/** | ||
* @typedef {string} Token | ||
*/ | ||
|
||
/** | ||
* @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. | ||
*/ | ||
|
||
/** | ||
* Store for conversation extra chat features apart from messages | ||
* | ||
* @param {string} id store name | ||
* @param {State} options.state store state structure | ||
*/ | ||
export const useChatExtrasStore = defineStore('chatExtras', { | ||
state: () => ({ | ||
absence: {}, | ||
messagesToBeReplied: {}, | ||
currentMessageInput: {}, | ||
}), | ||
|
||
getters: { | ||
getMessageToBeReplied: (state) => (token) => { | ||
if (state.messagesToBeReplied[token]) { | ||
return state.messagesToBeReplied[token] | ||
} | ||
}, | ||
|
||
getCurrentMessageInput: (state) => (token) => { | ||
return state.currentMessageInput[token] ?? '' | ||
}, | ||
}, | ||
|
||
actions: { | ||
/** | ||
* Fetch an absence status for user and save to store | ||
|
@@ -64,5 +96,63 @@ export const useChatExtrasStore = defineStore('chatExtras', { | |
Vue.delete(this.absence, token) | ||
} | ||
}, | ||
|
||
/** | ||
* Add a reply message id to the store | ||
* | ||
* @param {object} payload action payload | ||
* @param {string} payload.token The conversation token | ||
* @param {number} payload.id The id of message | ||
*/ | ||
addMessageToBeReplied({ token, id }) { | ||
Vue.set(this.messagesToBeReplied, token, id) | ||
}, | ||
|
||
/** | ||
* Removes a reply message id from the store | ||
* (after posting message or dismissing the operation) | ||
* | ||
* @param {string} token The conversation token | ||
*/ | ||
removeMessageToBeReplied(token) { | ||
Vue.delete(this.messagesToBeReplied, token) | ||
}, | ||
|
||
/** | ||
* Add a current input value to the store for a given conversation token | ||
* | ||
* @param {object} payload action payload | ||
* @param {string} payload.token The conversation token | ||
* @param {string} payload.text The string to store | ||
*/ | ||
setCurrentMessageInput({ token, text }) { | ||
// FIXME upstream: https://github.com/nextcloud-libraries/nextcloud-vue/issues/4492 | ||
const temp = document.createElement('textarea') | ||
temp.innerHTML = text.replace(/&/gmi, '&') | ||
const parsedText = temp.value.replace(/&/gmi, '&').replace(/</gmi, '<') | ||
.replace(/>/gmi, '>').replace(/§/gmi, '§') | ||
|
||
Vue.set(this.currentMessageInput, 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) | ||
}, | ||
|
||
/** | ||
* Clears store for a deleted conversation | ||
* | ||
* @param {string} token the token of the conversation to be deleted | ||
*/ | ||
purgeChatExtras(token) { | ||
this.removeMessageToBeReplied(token) | ||
this.removeUserAbsence(token) | ||
this.removeCurrentMessageInput(token) | ||
}, | ||
}, | ||
}) |