diff --git a/app/Http/Controllers/Forms/FormController.php b/app/Http/Controllers/Forms/FormController.php index f541de5..6c99b7b 100644 --- a/app/Http/Controllers/Forms/FormController.php +++ b/app/Http/Controllers/Forms/FormController.php @@ -34,7 +34,7 @@ class FormController extends Controller $this->authorize('viewAny', Form::class); $workspaceIsPro = $workspace->is_pro; - $forms = $workspace->forms()->with(['creator','views','submissions'])->get()->map(function (Form $form) use ($workspace, $workspaceIsPro){ + $forms = $workspace->forms()->with(['creator','views','submissions'])->paginate(10)->through(function (Form $form) use ($workspace, $workspaceIsPro){ // Add attributes for faster loading $form->extra = (object) [ 'loadedWorkspace' => $workspace, diff --git a/resources/js/pages/forms/show.vue b/resources/js/pages/forms/show.vue index e9c5dd3..7e250ea 100644 --- a/resources/js/pages/forms/show.vue +++ b/resources/js/pages/forms/show.vue @@ -306,7 +306,7 @@ import FormSubmissions from '../../components/open/forms/components/FormSubmissi const loadForms = function () { store.commit('open/forms/startLoading') store.dispatch('open/workspaces/loadIfEmpty').then(() => { - store.dispatch('open/forms/load', store.state['open/workspaces'].currentId) + store.dispatch('open/forms/loadIfEmpty', store.state['open/workspaces'].currentId) }) } diff --git a/resources/js/pages/home.vue b/resources/js/pages/home.vue index 570d223..76e88f8 100644 --- a/resources/js/pages/home.vue +++ b/resources/js/pages/home.vue @@ -10,13 +10,11 @@ Create a new form -
- -
-

+ +

You don't have any form yet.

-
+
@@ -66,6 +64,9 @@ .

+
+ +
@@ -83,7 +84,7 @@ import OpenFormFooter from '../components/pages/OpenFormFooter' const loadForms = function () { store.commit('open/forms/startLoading') store.dispatch('open/workspaces/loadIfEmpty').then(() => { - store.dispatch('open/forms/load', store.state['open/workspaces'].currentId) + store.dispatch('open/forms/loadIfEmpty', store.state['open/workspaces'].currentId) }) } diff --git a/resources/js/store/modules/open/forms.js b/resources/js/store/modules/open/forms.js index f8af0c1..ae9b237 100644 --- a/resources/js/store/modules/open/forms.js +++ b/resources/js/store/modules/open/forms.js @@ -2,6 +2,7 @@ import axios from 'axios' export const formsEndpoint = '/api/open/workspaces/{workspaceId}/forms' export const namespaced = true +export let currentPage = 1 // state export const state = { @@ -36,6 +37,9 @@ export const mutations = { set (state, items) { state.content = items }, + append (state, items) { + state.content = state.content.concat(items) + }, addOrUpdate (state, item) { state.content = state.content.filter((val) => val.id !== item.id) state.content.push(item) @@ -56,12 +60,26 @@ export const actions = { resetState (context) { context.commit('set', []) context.commit('stopLoading') + currentPage = 1 }, load (context, workspaceId) { context.commit('startLoading') - return axios.get(formsEndpoint.replace('{workspaceId}', workspaceId)).then((response) => { - context.commit('set', response.data) - context.commit('stopLoading') + return axios.get(formsEndpoint.replace('{workspaceId}', workspaceId)+'?page='+currentPage).then((response) => { + context.commit((currentPage == 1) ? 'set' : 'append', response.data.data) + if (currentPage < response.data.meta.last_page) { + currentPage += 1 + context.dispatch('load', workspaceId) + } else { + context.commit('stopLoading') + currentPage = 1 + } }) + }, + loadIfEmpty (context, workspaceId) { + if (context.state.content.length === 0) { + return context.dispatch('load', workspaceId) + } + context.commit('stopLoading') + return Promise.resolve() } } diff --git a/tests/Feature/Forms/FormTest.php b/tests/Feature/Forms/FormTest.php index be124eb..d0c08d0 100644 --- a/tests/Feature/Forms/FormTest.php +++ b/tests/Feature/Forms/FormTest.php @@ -28,12 +28,10 @@ it('can fetch forms', function () { $this->getJson(route('open.workspaces.forms.index', $workspace->id)) ->assertSuccessful() - ->assertJsonCount(1) - ->assertJson(function (AssertableJson $json) use ($form) { - return $json->where('0.id', $form->id) - ->whereType('0.title', 'string') - ->whereType('0.properties', 'array'); - }); + ->assertJsonCount(3) + ->assertSuccessful() + ->assertJsonPath('data.0.id', $form->id) + ->assertJsonPath('data.0.title', $form->title); }); it('can update a form', function () { @@ -152,4 +150,4 @@ it('can create form with dark mode', function () { ->where('dark_mode', 'dark') ->etc(); }); -}); \ No newline at end of file +});