diff options
author | Maksim Sukharev <antreesy.web@gmail.com> | 2023-07-17 15:00:39 +0200 |
---|---|---|
committer | Maksim Sukharev <antreesy.web@gmail.com> | 2023-08-03 13:51:06 +0200 |
commit | 159ed26b00d659d837f9386156a4caefed744d93 (patch) | |
tree | 804c945bae90779f803d258b2797342680b7e719 /src | |
parent | 9d4018205eaad667ee7a80af0821146172c05797 (diff) |
replace Vuex settingsStore.js to Pinia settings.js
Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
(cherry picked from commit ec5dd4240268b7cc361b1311240b1880ce5641c2)
Diffstat (limited to 'src')
-rw-r--r-- | src/components/NewMessage/NewMessage.vue | 6 | ||||
-rw-r--r-- | src/components/SettingsDialog/SettingsDialog.vue | 24 | ||||
-rw-r--r-- | src/store/settingsStore.js | 80 | ||||
-rw-r--r-- | src/store/storeConfig.js | 2 | ||||
-rw-r--r-- | src/stores/__tests__/settings.spec.js | 43 | ||||
-rw-r--r-- | src/stores/settings.js | 57 |
6 files changed, 114 insertions, 98 deletions
diff --git a/src/components/NewMessage/NewMessage.vue b/src/components/NewMessage/NewMessage.vue index 303de1c29..a7e6b24cf 100644 --- a/src/components/NewMessage/NewMessage.vue +++ b/src/components/NewMessage/NewMessage.vue @@ -183,6 +183,7 @@ import { CONVERSATION, PARTICIPANT, PRIVACY } from '../../constants.js' import { EventBus } from '../../services/EventBus.js' import { shareFile } from '../../services/filesSharingServices.js' import { searchPossibleMentions } from '../../services/mentionsService.js' +import { useSettingsStore } from '../../stores/settings.js' import { fetchClipboardContent } from '../../utils/clipboard.js' import { isDarkTheme } from '../../utils/isDarkTheme.js' @@ -261,8 +262,11 @@ export default { setup() { const { openViewer } = useViewer() + const settingsStore = useSettingsStore() + return { openViewer, + settingsStore, supportTypingStatus, } }, @@ -371,7 +375,7 @@ export default { }, showTypingStatus() { return this.hasTypingIndicator && this.supportTypingStatus - && this.$store.getters.getTypingStatusPrivacy() === PRIVACY.PUBLIC + && this.settingsStore.typingStatusPrivacy === PRIVACY.PUBLIC }, }, diff --git a/src/components/SettingsDialog/SettingsDialog.vue b/src/components/SettingsDialog/SettingsDialog.vue index 5e8ed454f..8f5136616 100644 --- a/src/components/SettingsDialog/SettingsDialog.vue +++ b/src/components/SettingsDialog/SettingsDialog.vue @@ -168,6 +168,7 @@ import NcTextField from '@nextcloud/vue/dist/Components/NcTextField.js' import MediaDevicesPreview from '../MediaDevicesPreview.vue' import { PRIVACY } from '../../constants.js' +import { useSettingsStore } from '../../stores/settings.js' const supportTypingStatus = getCapabilities()?.spreed?.config?.chat?.['typing-privacy'] !== undefined @@ -184,7 +185,10 @@ export default { }, setup() { + const settingsStore = useSettingsStore() + return { + settingsStore, supportTypingStatus, } }, @@ -220,19 +224,11 @@ export default { }, readStatusPrivacyIsPublic() { - return this.readStatusPrivacy === PRIVACY.PUBLIC - }, - - readStatusPrivacy() { - return this.$store.getters.getReadStatusPrivacy() + return this.settingsStore.readStatusPrivacy === PRIVACY.PUBLIC }, typingStatusPrivacyIsPublic() { - return this.typingStatusPrivacy === PRIVACY.PUBLIC - }, - - typingStatusPrivacy() { - return this.$store.getters.getTypingStatusPrivacy() + return this.settingsStore.typingStatusPrivacy === PRIVACY.PUBLIC }, settingsUrl() { @@ -280,9 +276,8 @@ export default { async toggleReadStatusPrivacy() { this.privacyLoading = true try { - await this.$store.dispatch( - 'updateReadStatusPrivacy', - this.readStatusPrivacyIsPublic ? PRIVACY.PRIVATE : PRIVACY.PUBLIC, + await this.settingsStore.updateReadStatusPrivacy( + this.readStatusPrivacyIsPublic ? PRIVACY.PRIVATE : PRIVACY.PUBLIC ) showSuccess(t('spreed', 'Your privacy setting has been saved')) } catch (exception) { @@ -294,8 +289,7 @@ export default { async toggleTypingStatusPrivacy() { this.privacyLoading = true try { - await this.$store.dispatch( - 'updateTypingStatusPrivacy', + await this.settingsStore.updateTypingStatusPrivacy( this.typingStatusPrivacyIsPublic ? PRIVACY.PRIVATE : PRIVACY.PUBLIC ) showSuccess(t('spreed', 'Your privacy setting has been saved')) diff --git a/src/store/settingsStore.js b/src/store/settingsStore.js deleted file mode 100644 index 20a761d8f..000000000 --- a/src/store/settingsStore.js +++ /dev/null @@ -1,80 +0,0 @@ -/** - * @copyright Copyright (c) 2020 Joas Schilling <coding@schilljs.com> - * - * @license AGPL-3.0-or-later - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * - */ - -import { loadState } from '@nextcloud/initial-state' - -import { PRIVACY } from '../constants.js' -import { setReadStatusPrivacy, setTypingStatusPrivacy } from '../services/settingsService.js' - -const state = { - readStatusPrivacy: loadState('spreed', 'read_status_privacy', PRIVACY.PRIVATE), - typingStatusPrivacy: loadState('spreed', 'typing_privacy', PRIVACY.PRIVATE), -} - -const getters = { - getReadStatusPrivacy: (state) => () => { - return state.readStatusPrivacy - }, - getTypingStatusPrivacy: (state) => () => { - return state.typingStatusPrivacy - }, -} - -const mutations = { - /** - * Updates the token - * - * @param {object} state current store state; - * @param {string} privacy The token of the active conversation - */ - updateReadStatusPrivacy(state, privacy) { - state.readStatusPrivacy = privacy - }, - updateTypingStatusPrivacy(state, privacy) { - state.typingStatusPrivacy = privacy - }, -} - -const actions = { - - /** - * Update the read status privacy for the user - * - * @param {object} context default store context; - * @param {number} privacy The new selected privacy - */ - async updateReadStatusPrivacy(context, privacy) { - await setReadStatusPrivacy(privacy) - context.commit('updateReadStatusPrivacy', privacy) - }, - - /** - * Update the typing status privacy for the user - * - * @param {object} context default store context; - * @param {number} privacy The new selected privacy - */ - async updateTypingStatusPrivacy(context, privacy) { - await setTypingStatusPrivacy(privacy) - context.commit('updateTypingStatusPrivacy', privacy) - }, -} - -export default { state, mutations, getters, actions } diff --git a/src/store/storeConfig.js b/src/store/storeConfig.js index 1bf210658..ad9c53628 100644 --- a/src/store/storeConfig.js +++ b/src/store/storeConfig.js @@ -34,7 +34,6 @@ import participantsStore from './participantsStore.js' import pollStore from './pollStore.js' import quoteReplyStore from './quoteReplyStore.js' import reactionsStore from './reactionsStore.js' -import settingsStore from './settingsStore.js' import sharedItemStore from './sharedItemsStore.js' import sidebarStore from './sidebarStore.js' import soundsStore from './soundsStore.js' @@ -55,7 +54,6 @@ export default { newGroupConversationStore, participantsStore, quoteReplyStore, - settingsStore, sidebarStore, soundsStore, talkHashStore, diff --git a/src/stores/__tests__/settings.spec.js b/src/stores/__tests__/settings.spec.js new file mode 100644 index 000000000..9b7eccb8b --- /dev/null +++ b/src/stores/__tests__/settings.spec.js @@ -0,0 +1,43 @@ +import { setActivePinia, createPinia } from 'pinia' + +import { PRIVACY } from '../../constants.js' +import { useSettingsStore } from '../settings.js' + +jest.mock('@nextcloud/initial-state', + () => ({ + loadState: jest.fn().mockReturnValue(0), + })) + +jest.mock('../../services/settingsService', + () => ({ + setReadStatusPrivacy: jest.fn().mockReturnValue('success'), + setTypingStatusPrivacy: jest.fn().mockReturnValue('success'), + })) + +describe('settingsStore', () => { + beforeEach(() => { + // creates a fresh pinia and make it active, so it's automatically picked + // up by any useStore() call without having to pass it to it: + // `useStore(pinia)` + setActivePinia(createPinia()) + }) + + it('shows correct loaded values for statuses', () => { + const settingsStore = useSettingsStore() + + expect(settingsStore.readStatusPrivacy).toBe(PRIVACY.PUBLIC) + expect(settingsStore.typingStatusPrivacy).toBe(PRIVACY.PUBLIC) + }) + + it('toggles statuses correctly', async () => { + const settingsStore = useSettingsStore() + + expect(settingsStore.readStatusPrivacy).toBe(PRIVACY.PUBLIC) + await settingsStore.updateReadStatusPrivacy(PRIVACY.PRIVATE) + expect(settingsStore.readStatusPrivacy).toBe(PRIVACY.PRIVATE) + + expect(settingsStore.typingStatusPrivacy).toBe(PRIVACY.PUBLIC) + await settingsStore.updateTypingStatusPrivacy(PRIVACY.PRIVATE) + expect(settingsStore.typingStatusPrivacy).toBe(PRIVACY.PRIVATE) + }) +}) diff --git a/src/stores/settings.js b/src/stores/settings.js new file mode 100644 index 000000000..64bb5c582 --- /dev/null +++ b/src/stores/settings.js @@ -0,0 +1,57 @@ +/** + * @copyright Copyright (c) 2020 Joas Schilling <coding@schilljs.com> + * + * @author Maksim Sukharev <antreesy.web@gmail.com> + * + * @license AGPL-3.0-or-later + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +import { defineStore } from 'pinia' + +import { loadState } from '@nextcloud/initial-state' + +import { PRIVACY } from '../constants.js' +import { setReadStatusPrivacy, setTypingStatusPrivacy } from '../services/settingsService.js' + +export const useSettingsStore = defineStore('settings', { + state: () => ({ + readStatusPrivacy: loadState('spreed', 'read_status_privacy', PRIVACY.PRIVATE), + typingStatusPrivacy: loadState('spreed', 'typing_privacy', PRIVACY.PRIVATE), + }), + + actions: { + /** + * Update the read status privacy for the user + * + * @param {number} privacy The new selected privacy + */ + async updateReadStatusPrivacy(privacy) { + await setReadStatusPrivacy(privacy) + this.readStatusPrivacy = privacy + }, + + /** + * Update the typing status privacy for the user + * + * @param {number} privacy The new selected privacy + */ + async updateTypingStatusPrivacy(privacy) { + await setTypingStatusPrivacy(privacy) + this.typingStatusPrivacy = privacy + }, + }, +}) |