Skip to content

Commit

Permalink
replace Vuex settingsStore.js to Pinia
Browse files Browse the repository at this point in the history
Signed-off-by: Maksim Sukharev <antreesy.web@gmail.com>
  • Loading branch information
Antreesy committed Jul 3, 2023
1 parent 8a97a0e commit 19d3eca
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 106 deletions.
9 changes: 5 additions & 4 deletions src/components/NewMessage/NewMessage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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/settingsStore.js'
import { fetchClipboardContent } from '../../utils/clipboard.js'
import { isDarkTheme } from '../../utils/isDarkTheme.js'

Expand All @@ -194,7 +195,6 @@ const picker = getFilePickerBuilder(t('spreed', 'File to share'))
.build()

const disableKeyboardShortcuts = OCP.Accessibility.disableKeyboardShortcuts()
const supportTypingStatus = getCapabilities()?.spreed?.config?.chat?.['typing-privacy'] !== undefined

export default {
name: 'NewMessage',
Expand Down Expand Up @@ -261,9 +261,10 @@ export default {

setup() {
const { openViewer } = useViewer()
const settingsStore = useSettingsStore()
return {
openViewer,
supportTypingStatus,
settingsStore,
}
},

Expand Down Expand Up @@ -370,8 +371,8 @@ export default {
return !this.hasText && this.canUploadFiles && !this.broadcast
},
showTypingStatus() {
return this.hasTypingIndicator && this.supportTypingStatus
&& this.$store.getters.getTypingStatusPrivacy() === PRIVACY.PUBLIC
return this.hasTypingIndicator && this.settingsStore.supportTypingStatus
&& this.settingsStore.typingStatusPrivacy === PRIVACY.PUBLIC
},
},

Expand Down
30 changes: 10 additions & 20 deletions src/components/SettingsDialog/SettingsDialog.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
@update:checked="toggleReadStatusPrivacy">
{{ t('spreed', 'Share my read-status and show the read-status of others') }}
</NcCheckboxRadioSwitch>
<NcCheckboxRadioSwitch v-if="supportTypingStatus"
<NcCheckboxRadioSwitch v-if="settingsStore.supportTypingStatus"
id="typing_status_privacy"
:checked="typingStatusPrivacyIsPublic"
:disabled="privacyLoading"
Expand Down Expand Up @@ -154,7 +154,6 @@
</template>

<script>
import { getCapabilities } from '@nextcloud/capabilities'
import { getFilePickerBuilder, showError, showSuccess } from '@nextcloud/dialogs'
import { subscribe, unsubscribe } from '@nextcloud/event-bus'
import { generateUrl } from '@nextcloud/router'
Expand All @@ -168,8 +167,7 @@ import NcTextField from '@nextcloud/vue/dist/Components/NcTextField.js'
import MediaDevicesPreview from '../MediaDevicesPreview.vue'

import { PRIVACY } from '../../constants.js'

const supportTypingStatus = getCapabilities()?.spreed?.config?.chat?.['typing-privacy'] !== undefined
import { useSettingsStore } from '../../stores/settingsStore.js'

export default {
name: 'SettingsDialog',
Expand All @@ -184,8 +182,10 @@ export default {
},

setup() {
const settingsStore = useSettingsStore()

return {
supportTypingStatus,
settingsStore,
}
},

Expand Down Expand Up @@ -220,19 +220,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() {
Expand Down Expand Up @@ -280,9 +272,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) {
Expand All @@ -294,8 +285,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'))
Expand Down
80 changes: 0 additions & 80 deletions src/store/settingsStore.js

This file was deleted.

2 changes: 0 additions & 2 deletions src/store/storeConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -55,7 +54,6 @@ export default {
newGroupConversationStore,
participantsStore,
quoteReplyStore,
settingsStore,
sidebarStore,
soundsStore,
talkHashStore,
Expand Down
55 changes: 55 additions & 0 deletions src/stores/__tests__/settingsStore.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { setActivePinia, createPinia } from 'pinia'

import { PRIVACY } from '../../constants.js'
import { useSettingsStore } from '../settingsStore.js'

jest.mock('@nextcloud/initial-state',
() => ({
loadState: jest.fn().mockReturnValue(0),
}))

jest.mock('@nextcloud/capabilities',
() => ({
getCapabilities: jest.fn().mockReturnValue({
spreed: {
config: {
chat: { 'typing-privacy': 1 },
},
},
}),
}))

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)
expect(settingsStore.supportTypingStatus).toBeTruthy()
})

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)
})
})
59 changes: 59 additions & 0 deletions src/stores/settingsStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* @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 { getCapabilities } from '@nextcloud/capabilities'
import { loadState } from '@nextcloud/initial-state'

import { PRIVACY } from '../constants.js'
import { setReadStatusPrivacy, setTypingStatusPrivacy } from '../services/settingsService.js'

export const useSettingsStore = defineStore('settingsStore', {
state: () => ({
readStatusPrivacy: loadState('spreed', 'read_status_privacy', PRIVACY.PRIVATE),
typingStatusPrivacy: loadState('spreed', 'typing_privacy', PRIVACY.PRIVATE),
supportTypingStatus: getCapabilities()?.spreed?.config?.chat?.['typing-privacy'] !== undefined,
}),

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
},
},
})

0 comments on commit 19d3eca

Please sign in to comment.