From 2136d2e9b84d4aebc87612805f48c46b37d411b2 Mon Sep 17 00:00:00 2001 From: laurensvanderbilt Date: Fri, 8 Nov 2024 16:19:37 +0100 Subject: [PATCH] Added update call when switching between pages --- .../form-view-model.component.html | 2 ++ .../form-view-model.component.ts | 35 +++++++++++++++++-- .../src/lib/services/view-model.service.ts | 30 ++++++++++------ 3 files changed, 54 insertions(+), 13 deletions(-) diff --git a/projects/valtimo/form-view-model/src/lib/components/form-view-model/form-view-model.component.html b/projects/valtimo/form-view-model/src/lib/components/form-view-model/form-view-model.component.html index 300349976..91b6d5744 100644 --- a/projects/valtimo/form-view-model/src/lib/components/form-view-model/form-view-model.component.html +++ b/projects/valtimo/form-view-model/src/lib/components/form-view-model/form-view-model.component.html @@ -46,5 +46,7 @@ (submit)="onSubmit($event)" (change)="onChange($event)" (focusout)="onBlur($event)" + (nextPage)="onNextPage($event)" + (prevPage)="onPreviousPage($event)" > diff --git a/projects/valtimo/form-view-model/src/lib/components/form-view-model/form-view-model.component.ts b/projects/valtimo/form-view-model/src/lib/components/form-view-model/form-view-model.component.ts index d8333ce7e..85c00092a 100644 --- a/projects/valtimo/form-view-model/src/lib/components/form-view-model/form-view-model.component.ts +++ b/projects/valtimo/form-view-model/src/lib/components/form-view-model/form-view-model.component.ts @@ -79,6 +79,9 @@ export class FormViewModelComponent implements OnInit { public errors: string[] = []; + private preventNextPage = false; + private preventPreviousPage = false; + public readonly submission$ = new BehaviorSubject({}); public readonly form$ = new BehaviorSubject(undefined); public readonly formName$ = new BehaviorSubject(undefined); @@ -237,6 +240,28 @@ export class FormViewModelComponent implements OnInit { } } + public onNextPage(event: any): void { + this.preventNextPage = true; + this.formio.formio.setPage(this.formio.formio.page - 1); + this.handleChanges(); + } + + public onPreviousPage(event: any): void { + this.preventPreviousPage = true; + this.formio.formio.setPage(this.formio.formio.page + 1); + this.handleChanges(); + } + + private handlePageChange(): void { + if (this.preventNextPage) { + this.preventNextPage = false; + this.formio.formio.setPage(this.formio.formio.page + 1); + } else if (this.preventPreviousPage) { + this.preventPreviousPage = false; + this.formio.formio.setPage(this.formio.formio.page - 1); + } + } + public loadInitialViewModel(): void { combineLatest([this.formName$, this.taskInstanceId$]) .pipe( @@ -265,12 +290,13 @@ export class FormViewModelComponent implements OnInit { return combineLatest([this.formName$, this.taskInstanceId$, this.change$]).pipe( take(1), switchMap(([formName, taskInstanceId, change]) => - this.viewModelService.updateViewModel(formName, taskInstanceId, change.data).pipe( + this.viewModelService.updateViewModel(formName, taskInstanceId, change.data, this.formio.formio.page).pipe( tap({ next: viewModel => { const submission = this.submission$.value; submission.data = viewModel; this.submission$.next(submission); + this.handlePageChange(); this.loading$.next(false); this.errors = []; }, @@ -318,11 +344,14 @@ export class FormViewModelComponent implements OnInit { take(1), switchMap(([formName, processDefinitionKey, change]) => this.viewModelService - .updateViewModelForStartForm(formName, processDefinitionKey, change.data) + .updateViewModelForStartForm(formName, processDefinitionKey, change.data, this.formio.formio.page) .pipe( tap({ next: viewModel => { - this.submission$.next({data: viewModel}); + const submission = this.submission$.value; + submission.data = viewModel; + this.submission$.next(submission); + this.handlePageChange(); this.loading$.next(false); this.errors = []; }, diff --git a/projects/valtimo/form-view-model/src/lib/services/view-model.service.ts b/projects/valtimo/form-view-model/src/lib/services/view-model.service.ts index 4b945332b..5a2795140 100644 --- a/projects/valtimo/form-view-model/src/lib/services/view-model.service.ts +++ b/projects/valtimo/form-view-model/src/lib/services/view-model.service.ts @@ -15,7 +15,7 @@ */ import {Injectable} from '@angular/core'; -import {HttpClient, HttpHeaders, HttpParams, HttpResponse} from '@angular/common/http'; +import {HttpClient, HttpHeaders} from '@angular/common/http'; import {Observable} from 'rxjs'; import {BaseApiService, ConfigService} from '@valtimo/config'; import {InterceptorSkip} from '@valtimo/security'; @@ -42,13 +42,18 @@ export class ViewModelService extends BaseApiService { public updateViewModel( formName: string, taskInstanceId: string, - viewModel: object + viewModel: object, + page: number ): Observable { - return this.httpClient.post(this.getApiUrl(`/v1/form/view-model/user-task`), viewModel, { - params: { + const params = { formName, taskInstanceId, - }, + } + if (!isNaN(page)) { + params['page'] = page; + } + return this.httpClient.post(this.getApiUrl(`/v1/form/view-model/user-task`), viewModel, { + params: params, headers: new HttpHeaders().set(InterceptorSkip, '400'), }); } @@ -83,13 +88,18 @@ export class ViewModelService extends BaseApiService { public updateViewModelForStartForm( formName: string, processDefinitionKey: string, - viewModel: object + viewModel: object, + page: number ): Observable { + const params = { + formName, + processDefinitionKey, + } + if (!isNaN(page)) { + params['page'] = page; + } return this.httpClient.post(this.getApiUrl(`/v1/form/view-model/start-form`), viewModel, { - params: { - formName, - processDefinitionKey, - }, + params: params, headers: new HttpHeaders().set(InterceptorSkip, '400'), }); }