Skip to content

Beautiful Open-Source Form Builder

fix loading show form page (#361)

Favour Olayinka2191f4621456
diff --git a/app/Http/Controllers/Forms/FormController.php b/app/Http/Controllers/Forms/FormController.php
index 52fec5dc6..b21c2170b 100644
--- a/app/Http/Controllers/Forms/FormController.php
+++ b/app/Http/Controllers/Forms/FormController.php
@@ -55,6 +55,26 @@ class FormController extends Controller
return FormResource::collection($forms);
}
+ public function show($slug)
+ {
+ $form = Form::whereSlug($slug)->firstOrFail();
+ $this->authorize('view', $form);
+
+ // Add attributes for faster loading
+ $workspace = $form->workspace;
+ $form->extra = (object)[
+ 'loadedWorkspace' => $workspace,
+ 'workspaceIsPro' => $workspace->is_pro,
+ 'userIsOwner' => true,
+ 'cleanings' => $this->formCleaner
+ ->processForm(request(), $form)
+ ->simulateCleaning($workspace)
+ ->getPerformedCleanings(),
+ ];
+
+ return new FormResource($form);
+ }
+
/**
* Return all user forms, used for zapier
*
diff --git a/client/pages/forms/[slug]/show.vue b/client/pages/forms/[slug]/show.vue
index 9d9fb2bbc..5d150c7df 100644
--- a/client/pages/forms/[slug]/show.vue
+++ b/client/pages/forms/[slug]/show.vue
@@ -151,6 +151,7 @@ useOpnSeoMeta({
title: 'Home'
})
+const route = useRoute()
const authStore = useAuthStore()
const formsStore = useFormsStore()
const workingFormStore = useWorkingFormStore()
@@ -195,7 +196,7 @@ onMounted(() => {
if (form.value) {
workingFormStore.set(form.value)
} else {
- formsStore.loadAll(useWorkspacesStore().currentId)
+ formsStore.loadForm(route.params.slug)
}
})
diff --git a/client/stores/forms.js b/client/stores/forms.js
index ceb33e7d9..799b9dde9 100644
--- a/client/stores/forms.js
+++ b/client/stores/forms.js
@@ -2,6 +2,7 @@ import {defineStore} from 'pinia'
import {useContentStore} from "~/composables/stores/useContentStore.js";
export const formsEndpoint = '/open/workspaces/{workspaceId}/forms'
+export const singleFormEndpoint = '/open/forms/{slug}'
export const useFormsStore = defineStore('forms', () => {
@@ -33,6 +34,16 @@ export const useFormsStore = defineStore('forms', () => {
throw error
})
}
+ const loadForm = (slug) => {
+ contentStore.startLoading()
+ return opnFetch(singleFormEndpoint.replace('{slug}', slug))
+ .then(response => {
+ contentStore.save(response)
+ })
+ .finally(() => {
+ contentStore.stopLoading()
+ })
+ }
const load = (workspaceId, slug) => {
contentStore.startLoading()
@@ -76,6 +87,7 @@ export const useFormsStore = defineStore('forms', () => {
publicLoad,
publicFetch,
loadAll,
+ loadForm,
load,
}
})
diff --git a/routes/api.php b/routes/api.php
index 33885bb6c..d96855192 100644
--- a/routes/api.php
+++ b/routes/api.php
@@ -54,6 +54,7 @@ Route::group(['middleware' => 'auth:api'], function () {
Route::prefix('open')->name('open.')->group(function () {
Route::get('/forms', [FormController::class, 'indexAll'])->name('forms.index-all');
+ Route::get('/forms/{slug}', [FormController::class, 'show'])->name('forms.show');
Route::prefix('workspaces')->name('workspaces.')->group(function () {
Route::get('/', [WorkspaceController::class, 'index'])->name('index');
diff --git a/tests/Feature/Forms/FormTest.php b/tests/Feature/Forms/FormTest.php
index 2d8a7974c..42c698c88 100644
--- a/tests/Feature/Forms/FormTest.php
+++ b/tests/Feature/Forms/FormTest.php
@@ -34,6 +34,19 @@ it('can fetch forms', function () {
->assertJsonPath('data.0.title', $form->title);
});
+it('can fetch a form', function () {
+ $user = $this->actingAsProUser();
+ $workspace = $this->createUserWorkspace($user);
+ $form = $this->createForm($user, $workspace);
+
+ $response = $this->getJson(route('open.forms.show', $form->slug))
+ ->assertSuccessful()
+ ->assertJson([
+ 'id' => $form->id,
+ 'title' => $form->title
+ ]);
+});
+
it('can update a form', function () {
$user = $this->actingAsUser();
$workspace = $this->createUserWorkspace($user);