From 2bf5ffb3c7054a49aac6405d98d9c2b48c7c9cc6 Mon Sep 17 00:00:00 2001 From: Olli Raitio Date: Tue, 5 Nov 2024 15:24:16 +0200 Subject: [PATCH] implement MHL.allowExcelGeneration [#567] --- .../generate-spreadsheet.component.ts | 16 +++++++--------- .../app/+project-form/project-form.component.ts | 8 +++++--- projects/laji/src/app/shared/model/Form.ts | 1 + .../app/shared/service/project-form.service.ts | 15 +++++++++++++-- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/projects/laji/src/app/+project-form/generate-spreadsheet/generate-spreadsheet.component.ts b/projects/laji/src/app/+project-form/generate-spreadsheet/generate-spreadsheet.component.ts index 0225d7b9be..88e654eda6 100644 --- a/projects/laji/src/app/+project-form/generate-spreadsheet/generate-spreadsheet.component.ts +++ b/projects/laji/src/app/+project-form/generate-spreadsheet/generate-spreadsheet.component.ts @@ -1,6 +1,5 @@ import { ChangeDetectionStrategy, Component } from '@angular/core'; import { ActivatedRoute } from '@angular/router'; -import { Observable } from 'rxjs'; import { ProjectFormService } from '../../shared/service/project-form.service'; import { map } from 'rxjs/operators'; @@ -11,16 +10,15 @@ import { map } from 'rxjs/operators'; changeDetection: ChangeDetectionStrategy.OnPush }) export class GenerateSpreadsheetComponent { - readonly excelForm$: Observable; + + readonly excelForm$ = this.projectFormService.getProjectFormFromRoute$(this.route).pipe( + map(form => this.projectFormService.getExcelFormOptions(form)), + map(form => Array.isArray(form) ? form : [form]), + map(forms => forms.filter(f => f.allowGenerate).map(f => f.formID)) + ); constructor( private route: ActivatedRoute, public projectFormService: ProjectFormService -) { - this.excelForm$ = projectFormService.getProjectFormFromRoute$(this.route).pipe( - map(form => projectFormService.getExcelFormIDs(form)), - map(form => Array.isArray(form) ? form : [form]) - ); - } - + ) {} } diff --git a/projects/laji/src/app/+project-form/project-form.component.ts b/projects/laji/src/app/+project-form/project-form.component.ts index 8bda34bb6e..1f4d9f53ce 100644 --- a/projects/laji/src/app/+project-form/project-form.component.ts +++ b/projects/laji/src/app/+project-form/project-form.component.ts @@ -231,8 +231,10 @@ export class ProjectFormComponent implements OnInit, OnDestroy { } private getNavLinks(projectForm: ProjectForm, rights: Rights, queryParams: Params): NavLink[] { - const allowExcel = this.projectFormService.getExcelFormIDs(projectForm).length; - const {form, subForms} = projectForm; + const excelFormOptions = this.projectFormService.getExcelFormOptions(projectForm); + const allowExcel = excelFormOptions.length; + const allowExcelGeneration = excelFormOptions.some(options => options.allowGenerate); + const { form, subForms } = projectForm; return [ { link: ['about'], @@ -255,7 +257,7 @@ export class ProjectFormComponent implements OnInit, OnDestroy { label: 'excel.import', lajiFormOption: 'options.allowExcel' }, - rights.edit && allowExcel && { + rights.edit && allowExcelGeneration && { link: ['generate'], label: 'excel.generate', lajiFormOption: 'options.allowExcel' diff --git a/projects/laji/src/app/shared/model/Form.ts b/projects/laji/src/app/shared/model/Form.ts index 397b95ec2b..083093d59f 100644 --- a/projects/laji/src/app/shared/model/Form.ts +++ b/projects/laji/src/app/shared/model/Form.ts @@ -64,6 +64,7 @@ export namespace Form { prepopulateWithTaxonSets?: string[]; emptyOnNoCount?: boolean; allowExcel?: boolean; + allowExcelGeneration?: boolean; excludeFromGlobalExcel?: boolean; allowTemplate?: boolean; forms?: string[]; diff --git a/projects/laji/src/app/shared/service/project-form.service.ts b/projects/laji/src/app/shared/service/project-form.service.ts index 7438111897..5de13144d4 100644 --- a/projects/laji/src/app/shared/service/project-form.service.ts +++ b/projects/laji/src/app/shared/service/project-form.service.ts @@ -31,6 +31,11 @@ export interface NamedPlacesRouteData extends NamedPlacesQueryModel { namedPlace?: NamedPlace; } +export interface ExcelFormOptions { + formID: string; + allowGenerate: boolean; +} + @Injectable({providedIn: 'root'}) export class ProjectFormService { constructor( @@ -111,9 +116,15 @@ export class ProjectFormService { return this.getProjectRootRoute$(route).pipe(map(_route => _route.snapshot.params['projectID'])); } + getExcelFormOptions(projectForm: ProjectForm): ExcelFormOptions[] { + const getExcelOptions = (form: Form.SchemaForm | Form.List) => form.options?.allowExcel + ? { formID: form.id, allowGenerate: form.options.allowExcelGeneration !== false } + : undefined; + return [getExcelOptions(projectForm.form), ...projectForm.subForms.map(getExcelOptions)].filter(f => f); + } + getExcelFormIDs(projectForm: ProjectForm): string[] { - const allowsExcel = (form: Form.SchemaForm | Form.List) => form.options?.allowExcel && form.id; - return [allowsExcel(projectForm.form), ...projectForm.subForms.map(allowsExcel)].filter(f => f) as string[]; + return this.getExcelFormOptions(projectForm).map(f => f.formID); } getSubmissionsPageTitle(form: Form.SchemaForm, isAdmin: boolean) {