+
{{ currentConversationName }}
@@ -52,6 +53,7 @@ import moment from '@nextcloud/moment'
import NcRichText from '@nextcloud/vue/dist/Components/NcRichText.js'
+import GuestWelcomeWindow from './GuestWelcomeWindow.vue'
import Lobby from './missingMaterialDesignIcons/Lobby.vue'
import SetGuestUsername from './SetGuestUsername.vue'
@@ -63,6 +65,7 @@ export default {
SetGuestUsername,
NcRichText,
Lobby,
+ GuestWelcomeWindow,
},
computed: {
@@ -107,6 +110,11 @@ export default {
currentUserIsGuest() {
return !this.$store.getters.getUserId()
},
+
+ isGuestWithoutDisplayName() {
+ const userName = this.$store.getters.getDisplayName()
+ return !userName && this.currentUserIsGuest
+ },
},
}
diff --git a/src/components/SetGuestUsername.vue b/src/components/SetGuestUsername.vue
index d4e780d5555..0f02450ce38 100644
--- a/src/components/SetGuestUsername.vue
+++ b/src/components/SetGuestUsername.vue
@@ -54,7 +54,6 @@ import Pencil from 'vue-material-design-icons/Pencil.vue'
import NcButton from '@nextcloud/vue/dist/Components/NcButton.js'
import NcTextField from '@nextcloud/vue/dist/Components/NcTextField.js'
-import { setGuestUserName } from '../services/participantsService.js'
import { useGuestNameStore } from '../stores/guestName.js'
export default {
@@ -104,6 +103,11 @@ export default {
this.delayHandleUserNameFromBrowserStorage = false
}
},
+ // Update the input field text
+ actorDisplayName(newName) {
+ this.guestUserName = newName
+ },
+
},
mounted() {
@@ -123,31 +127,9 @@ export default {
},
methods: {
- async handleChooseUserName() {
- const previousName = this.$store.getters.getDisplayName()
- try {
- this.$store.dispatch('setDisplayName', this.guestUserName)
- this.guestNameStore.addGuestName({
- token: this.token,
- actorId: this.$store.getters.getActorId(),
- actorDisplayName: this.guestUserName,
- }, { noUpdate: false })
- await setGuestUserName(this.token, this.guestUserName)
- if (this.guestUserName !== '') {
- localStorage.setItem('nick', this.guestUserName)
- } else {
- localStorage.removeItem('nick')
- }
- this.isEditingUsername = false
- } catch (exception) {
- this.$store.dispatch('setDisplayName', previousName)
- this.guestNameStore.addGuestName({
- token: this.token,
- actorId: this.$store.getters.getActorId(),
- actorDisplayName: previousName,
- }, { noUpdate: false })
- console.debug(exception)
- }
+ handleChooseUserName() {
+ this.guestNameStore.submitGuestUsername(this.token, this.guestUserName)
+ this.isEditingUsername = false
},
handleEditUsername() {
diff --git a/src/stores/__tests__/guestName.spec.js b/src/stores/__tests__/guestName.spec.js
index 48a7625ab79..e797deec17a 100644
--- a/src/stores/__tests__/guestName.spec.js
+++ b/src/stores/__tests__/guestName.spec.js
@@ -1,7 +1,14 @@
import { createPinia, setActivePinia } from 'pinia'
+import { setGuestUserName } from '../../services/participantsService.js'
+import vuexStore from '../../store/index.js'
+import { generateOCSErrorResponse } from '../../test-helpers.js'
import { useGuestNameStore } from '../guestName.js'
+jest.mock('../../services/participantsService', () => ({
+ setGuestUserName: jest.fn(),
+}))
+
describe('guestNameStore', () => {
let store
@@ -135,4 +142,75 @@ describe('guestNameStore', () => {
expect(global.t).toHaveBeenCalledWith('spreed', 'Guest')
})
+ test('stores the display name when guest submits it', async () => {
+ // Arrange
+ const actor1 = {
+ token: 'token-1',
+ actorId: 'actor-id1',
+ actorDisplayName: t('spreed', 'Guest'),
+ }
+
+ vuexStore.dispatch('setCurrentUser', { uid: 'actor-id1' })
+
+ const newName = 'actor 1'
+
+ // Mock implementation of setGuestUserName
+ setGuestUserName.mockResolvedValue()
+
+ // Act
+ await store.submitGuestUsername(actor1.token, newName)
+
+ // Assert
+ expect(setGuestUserName).toHaveBeenCalledWith(actor1.token, newName)
+ expect(localStorage.setItem).toHaveBeenCalledWith('nick', newName)
+ expect(store.getGuestName('token-1', 'actor-id1')).toBe('actor 1')
+ expect(vuexStore.getters.getDisplayName()).toBe('actor 1')
+ })
+
+ test('removes display name from local storage when user sumbits an empty new name', async () => {
+ // Arrange
+ const actor1 = {
+ token: 'token-1',
+ actorId: 'actor-id1',
+ actorDisplayName: 'actor 1',
+ }
+ const newName = ''
+
+ vuexStore.dispatch('setCurrentUser', { uid: 'actor-id1' })
+
+ // Mock implementation of setGuestUserName
+ setGuestUserName.mockResolvedValue()
+
+ // Act
+ await store.submitGuestUsername(actor1.token, newName)
+
+ // Assert
+ expect(setGuestUserName).toHaveBeenCalledWith(actor1.token, newName)
+ expect(localStorage.removeItem).toHaveBeenCalledWith('nick')
+ })
+
+ test('resets to previous display name if there is an error in setting the new one', async () => {
+ // Arrange
+ const actor1 = {
+ token: 'token-1',
+ actorId: 'actor-id1',
+ actorDisplayName: 'old actor 1',
+ }
+
+ vuexStore.dispatch('setCurrentUser', { uid: 'actor-id1' })
+ store.addGuestName(actor1, { noUpdate: false })
+
+ const newName = 'actor 1'
+
+ // Mock implementation of setGuestUserName
+ const error = generateOCSErrorResponse({ payload: {}, status: 400 })
+ setGuestUserName.mockRejectedValue(error)
+
+ // Act
+ await store.submitGuestUsername(actor1.token, newName)
+
+ // Assert
+ expect(setGuestUserName).toHaveBeenCalledWith(actor1.token, newName)
+ expect(vuexStore.getters.getDisplayName()).toBe(actor1.actorDisplayName)
+ })
})
diff --git a/src/stores/guestName.js b/src/stores/guestName.js
index 8c986136e3c..bb489dca6d9 100644
--- a/src/stores/guestName.js
+++ b/src/stores/guestName.js
@@ -23,6 +23,9 @@
import { defineStore } from 'pinia'
import Vue from 'vue'
+import { setGuestUserName } from '../services/participantsService.js'
+import store from '../store/index.js'
+
export const useGuestNameStore = defineStore('guestName', {
state: () => ({
guestNames: {},
@@ -83,5 +86,42 @@ export const useGuestNameStore = defineStore('guestName', {
Vue.set(this.guestNames[token], actorId, actorDisplayName)
}
},
+
+ /**
+ * Add the submitted guest name to the store
+ *
+ * @param {string} token the token of the conversation
+ * @param {string} name the new guest name
+ */
+ async submitGuestUsername(token, name) {
+ const actorId = store.getters.getActorId()
+ const previousName = this.getGuestName(token, actorId)
+
+ try {
+ store.dispatch('setDisplayName', name)
+ this.addGuestName({
+ token,
+ actorId,
+ actorDisplayName: name,
+ }, { noUpdate: false })
+
+ await setGuestUserName(token, name)
+
+ if (name !== '') {
+ localStorage.setItem('nick', name)
+ } else {
+ localStorage.removeItem('nick')
+ }
+
+ } catch (error) {
+ store.dispatch('setDisplayName', previousName)
+ this.addGuestName({
+ token,
+ actorId,
+ actorDisplayName: previousName,
+ }, { noUpdate: false })
+ console.debug(error)
+ }
+ },
},
})