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
18 changes: 2 additions & 16 deletions lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use OCA\Forms\Db\Form;
use OCA\Forms\Db\FormMapper;
use OCA\Forms\Db\ShareMapper;
use OCA\Forms\Db\SubmissionMapper;
use OCA\Forms\Service\ConfigService;
use OCA\Forms\Service\FormsService;
use OCP\Accounts\IAccountManager;
Expand Down Expand Up @@ -47,7 +46,6 @@ public function __construct(
IRequest $request,
private readonly FormMapper $formMapper,
private readonly ShareMapper $shareMapper,
private readonly SubmissionMapper $submissionMapper,
private readonly ConfigService $configService,
private readonly FormsService $formsService,
private readonly IAccountManager $accountManager,
Expand All @@ -67,7 +65,7 @@ public function __construct(
#[NoAdminRequired()]
#[NoCSRFRequired()]
#[FrontpageRoute(verb: 'GET', url: '/')]
public function index(?string $hash = null, ?int $submissionId = null): TemplateResponse {
public function index(?string $hash = null): TemplateResponse {
// Ensure the Comments client is available and load comments resources
$this->commentsManager->load();
Util::addScript($this->appName, 'forms-main', 'comments');
Expand All @@ -87,15 +85,6 @@ public function index(?string $hash = null, ?int $submissionId = null): Template
}
}

if (isset($submissionId)) {
try {
$submission = $this->submissionMapper->findById($submissionId);
$this->initialState->provideInitialState('submissionId', $submission->id);
} catch (DoesNotExistException) {
// Ignore exception and just don't set the initialState value
}
}

return new TemplateResponse($this->appName, self::TEMPLATE_MAIN, [
'id-app-content' => '#app-content-vue',
'id-app-navigation' => '#app-navigation-vue',
Expand Down Expand Up @@ -129,10 +118,7 @@ public function resultsViews(string $hash): TemplateResponse {
#[NoCSRFRequired()]
#[FrontpageRoute(verb: 'GET', url: '/{hash}/submit/{submissionId}', requirements: ['hash' => '[a-zA-Z0-9]{16,}', 'submissionId' => '\d+'])]
public function submitViewWithSubmission(string $hash, int $submissionId): TemplateResponse {
$form = $this->formMapper->findByHash($hash);
$canUserEditSubmission = $this->formsService->canDeleteResults($form);

return $canUserEditSubmission ? $this->index($hash, $submissionId) : $this->index($hash);
return $this->index($hash);
}

/**
Expand Down
46 changes: 43 additions & 3 deletions src/views/Submit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -562,9 +562,7 @@ export default defineComponent({
const routeSubmissionId = Array.isArray(rawSubmissionId)
? rawSubmissionId[0]
: rawSubmissionId
const id =
routeSubmissionId || loadState(formsAppName, 'submissionId', null)
return id ? parseInt(String(id), 10) : null
return routeSubmissionId ? parseInt(String(routeSubmissionId), 10) : null
})

/**
Expand Down Expand Up @@ -636,6 +634,35 @@ export default defineComponent({
)
}

/**
* Replace the stored draft with all current answers (e.g. after loading a submission)
*/
function saveAllAnswersToLocalStorage(): void {
if (!props.isLoggedIn) {
return
}
const state: StoredAnswersMap = {}
for (const question of validQuestions.value) {
const value = answers.value[question.id]
if (
value === undefined
|| (Array.isArray(value)
? value.length === 0
: Object.keys(value).length === 0)
) {
continue
}
state[`${question.id}`] = {
value,
type: answerTypes[question.type].component.name ?? '',
}
}
localStorage.setItem(
`nextcloud_forms_${props.publicView ? props.shareHash : props.hash}`,
JSON.stringify(state),
)
}

/**
* Deletes a non-existing field from local storage
*/
Expand Down Expand Up @@ -743,6 +770,7 @@ export default defineComponent({
}

answers.value = loaded
saveAllAnswersToLocalStorage()
} catch (error) {
logger.error('Error while loading response', { error })
showError(
Expand Down Expand Up @@ -1014,6 +1042,18 @@ export default defineComponent({
},
)

watch(submissionId, async (): Promise<void> => {
if (props.publicView || !props.isLoggedIn) {
return
}
resetData()
if (submissionId.value) {
await fetchSubmission()
} else {
initFromLocalStorage()
}
})

// Navigation guards only work inside the routed Forms app, not the public submit entry.
if (!props.publicView) {
onBeforeRouteUpdate(async () => {
Expand Down
4 changes: 0 additions & 4 deletions tests/Unit/Controller/PageControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use OCA\Forms\Controller\PageController;
use OCA\Forms\Db\FormMapper;
use OCA\Forms\Db\ShareMapper;
use OCA\Forms\Db\SubmissionMapper;
use OCA\Forms\Service\ConfigService;
use OCA\Forms\Service\FormsService;
use OCP\Accounts\IAccountManager;
Expand All @@ -33,7 +32,6 @@ class PageControllerTest extends TestCase {
private IRequest|MockObject $request;
private FormMapper|MockObject $formMapper;
private ShareMapper|MockObject $shareMapper;
private SubmissionMapper|MockObject $submissionMapper;
private ConfigService|MockObject $configService;
private FormsService|MockObject $formsService;
private IAccountManager|MockObject $accountManager;
Expand All @@ -50,7 +48,6 @@ public function setUp(): void {
$this->request = $this->createMock(IRequest::class);
$this->formMapper = $this->createMock(FormMapper::class);
$this->shareMapper = $this->createMock(ShareMapper::class);
$this->submissionMapper = $this->createMock(SubmissionMapper::class);
$this->configService = $this->createMock(ConfigService::class);
$this->formsService = $this->createMock(FormsService::class);
$this->accountManager = $this->createMock(IAccountManager::class);
Expand All @@ -66,7 +63,6 @@ public function setUp(): void {
$this->request,
$this->formMapper,
$this->shareMapper,
$this->submissionMapper,
$this->configService,
$this->formsService,
$this->accountManager,
Expand Down
Loading