Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 37 additions & 10 deletions src/views/SpacesView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ SPDX-FileCopyrightText: 2026 LibreCode Coop and contributors
SPDX-License-Identifier: AGPL-3.0-or-later
-->
<script setup>
import { computed, inject, nextTick, onMounted, onBeforeUnmount, ref } from 'vue'
import { computed, inject, nextTick, onMounted, onBeforeUnmount, ref, watch } from 'vue'
import { t } from '@nextcloud/l10n'
import NcButton from '@nextcloud/vue/components/NcButton'
import NcDialog from '@nextcloud/vue/components/NcDialog'
Expand Down Expand Up @@ -86,6 +86,9 @@ function openSpace(event, space) {
navigate('dashboard')
}
let requestSeq = 0
let viewMounted = true
let creationDialogRevision = 0
watch(creating, () => { ++creationDialogRevision }, { flush: 'sync' })
async function refreshArchived() {
const token = ++requestSeq
loading.value = true
Expand All @@ -99,7 +102,7 @@ async function refreshArchived() {
}
}
onMounted(() => { refreshArchived(); loadCreationPolicy() })
onBeforeUnmount(() => { ++requestSeq })
onBeforeUnmount(() => { ++requestSeq; viewMounted = false })
function startEdit(space) {
problem.value = ''
editing.value = space
Expand Down Expand Up @@ -159,29 +162,53 @@ async function restore(space) {
}
async function create() {
if (!newName.value.trim() || loading.value) return
const originId = Number(w.activeId.value)
const submittedKind = newKind.value
let dialogRevision = creationDialogRevision
const ownsView = () => viewMounted && creationDialogRevision === dialogRevision
&& Number(w.activeId.value) === originId && w.activeTab.value === 'spaces'
loading.value = true
problem.value = ''
try {
const created = await w.call('POST', '/api/spaces', {
name: newName.value.trim(), kind: newKind.value, currency: 'BRL',
color: newKind.value === 'organization' ? '#64748B' : newColor.value,
name: newName.value.trim(), kind: submittedKind, currency: 'BRL',
color: submittedKind === 'organization' ? '#64748B' : newColor.value,
})
creating.value = false
newName.value = ''
newColor.value = '#64748B'
const ownsCompletion = ownsView() && creating.value
if (ownsCompletion) {
creating.value = false
dialogRevision = creationDialogRevision
newName.value = ''
newColor.value = '#64748B'
}

// The server may have committed the workspace after the user left or
// dismissed this dialog. Refresh the authorized list, but do not let
// that obsolete UI action take over another screen or workspace.
await w.refreshSpaces()
w.activeId.value = Number(created.id)
const createdId = Number(created.id)
// On the first run, refreshing the list can select the new workspace
// and remount this view. That is part of this still-owned creation.
const initialSelection = originId === 0
&& Number(w.activeId.value) === createdId
&& w.activeTab.value === 'spaces'
&& creationDialogRevision === dialogRevision
if (!ownsCompletion || (!ownsView() && !initialSelection)
|| !w.spaces.value.some(s => Number(s.id) === createdId)) return

w.activeId.value = createdId
// Wait until activeId watchers have reset navigation before opening onboarding.
await nextTick()
if (Number(w.activeId.value) !== createdId || w.activeTab.value !== 'spaces') return
notice.value = t('librefinance', 'Independent workspace created.')
if (newKind.value === 'organization') {
if (submittedKind === 'organization') {
onboarding.value = true
navigate('companies')
} else {
navigate('dashboard')
}
} catch (error) {
problem.value = financeError(error)
if (ownsView()) problem.value = financeError(error)
} finally {
loading.value = false
}
Expand Down
111 changes: 111 additions & 0 deletions tests/e2e/saas-workspace-create-navigation.spec.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// SPDX-FileCopyrightText: 2026 LibreCode Coop and contributors
// SPDX-License-Identifier: AGPL-3.0-or-later

import { test, expect } from '@playwright/test'
import { login, makeSpace, goToView } from './helpers.mjs'

test('a delayed workspace creation respects subsequent browser navigation', async ({ page }) => {
await login(page)
const originalName = await makeSpace(page, 'personal')
await goToView(page, 'Visão geral')
const original = new URL(page.url())
await goToView(page, 'Gestões financeiras')
await page.getByRole('button', { name: 'Nova gestão', exact: true }).click()
const dialog = page.getByRole('dialog', { name: 'Criar gestão financeira' })
const newName = 'Synthetic late creation ' + Date.now().toString(36)
await dialog.getByRole('textbox', { name: 'Nome da gestão' }).fill(newName)
await page.locator('#lf-space-kind-new').click()
await page.getByRole('option').nth(2).click()

let deliver
let release
const committed = new Promise(resolve => { deliver = resolve })
const delayed = new Promise(resolve => { release = resolve })
await page.route('**/apps/librefinance/api/spaces', async route => {
if (route.request().method() !== 'POST') return route.continue()
// Use the real server write; only delay delivery of its response.
const response = await route.fetch()
deliver(response.status())
await delayed
await route.fulfill({ response })
})
try {
await dialog.getByRole('button', { name: 'Criar gestão independente', exact: true }).click()
expect(await committed).toBe(201)
await page.goBack()
await expect(dialog).toBeHidden()
await expect(page).toHaveURL(original.href)
const refreshed = page.waitForResponse(response =>
response.request().method() === 'GET' && response.url().endsWith('/api/spaces'))
release()
expect((await refreshed).status()).toBe(200)
// Observe the new authorized listing, not just the completion of HTTP.
await page.locator('#lf-space-select').click()
await expect(page.getByRole('option').filter({ hasText: newName })).toBeVisible()
await page.keyboard.press('Escape')
await expect(page.locator('.lf-context-identity')).toContainText(originalName)
await expect(page).toHaveURL(original.href)
await expect(page.getByRole('dialog', { name: 'Cadastrar empresa' })).toHaveCount(0)
if (process.env.LF_WORKSPACE_CREATE_REVIEW_CAPTURE === '1') {
await page.locator('.lf-context-identity').screenshot({ path: 'test-results/workspace-create-navigation-history.png' })
}
} finally {
release()
}
})

test('a delayed workspace creation respects a cancelled and reopened dialog', async ({ page }) => {
await login(page)
const originalName = await makeSpace(page, 'personal')
await goToView(page, 'Visão geral')
await goToView(page, 'Gestões financeiras')
const original = new URL(page.url())
await page.getByRole('button', { name: 'Nova gestão', exact: true }).click()
const dialog = page.getByRole('dialog', { name: 'Criar gestão financeira' })
const newName = 'Synthetic late creation ' + Date.now().toString(36)
await dialog.getByRole('textbox', { name: 'Nome da gestão' }).fill(newName)
await page.locator('#lf-space-kind-new').click()
await page.getByRole('option').nth(2).click()

let deliver
let release
const committed = new Promise(resolve => { deliver = resolve })
const delayed = new Promise(resolve => { release = resolve })
await page.route('**/apps/librefinance/api/spaces', async route => {
if (route.request().method() !== 'POST') return route.continue()
// Use the real server write; only delay delivery of its response.
const response = await route.fetch()
deliver(response.status())
await delayed
await route.fulfill({ response })
})
try {
await dialog.getByRole('button', { name: 'Criar gestão independente', exact: true }).click()
expect(await committed).toBe(201)
await dialog.getByRole('button', { name: 'Cancelar', exact: true }).click()
await expect(dialog).toBeHidden()
await page.getByRole('button', { name: 'Nova gestão', exact: true }).click()
const nextDraft = dialog.getByRole('textbox', { name: 'Nome da gestão' })
await nextDraft.fill('Synthetic next draft')
await expect(page).toHaveURL(original.href)
const refreshed = page.waitForResponse(response =>
response.request().method() === 'GET' && response.url().endsWith('/api/spaces'))
release()
expect((await refreshed).status()).toBe(200)
// The response belongs to the previous dialog, not to this draft.
await expect(dialog).toBeVisible()
await expect(nextDraft).toHaveValue('Synthetic next draft')
await expect(dialog.getByRole('button', { name: 'Criar gestão independente', exact: true })).toBeEnabled()
await dialog.getByRole('button', { name: 'Cancelar', exact: true }).click()
await expect(page.getByRole('list', { name: 'Áreas de minha propriedade' })
.getByText(newName, { exact: true })).toBeVisible()
await expect(page.locator('.lf-context-identity')).toContainText(originalName)
await expect(page).toHaveURL(original.href)
await expect(page.getByRole('dialog', { name: 'Cadastrar empresa' })).toHaveCount(0)
if (process.env.LF_WORKSPACE_CREATE_REVIEW_CAPTURE === '1') {
await page.locator('.lf-context-identity').screenshot({ path: 'test-results/workspace-create-navigation-cancel.png' })
}
} finally {
release()
}
})