Skip to content

Commit

Permalink
implement MHL.allowExcelGeneration [#567]
Browse files Browse the repository at this point in the history
  • Loading branch information
olzraiti committed Nov 5, 2024
1 parent 46b8b17 commit 2bf5ffb
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -11,16 +10,15 @@ import { map } from 'rxjs/operators';
changeDetection: ChangeDetectionStrategy.OnPush
})
export class GenerateSpreadsheetComponent {
readonly excelForm$: Observable<string[]>;

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])
);
}

) {}
}
8 changes: 5 additions & 3 deletions projects/laji/src/app/+project-form/project-form.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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'
Expand Down
1 change: 1 addition & 0 deletions projects/laji/src/app/shared/model/Form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export namespace Form {
prepopulateWithTaxonSets?: string[];
emptyOnNoCount?: boolean;
allowExcel?: boolean;
allowExcelGeneration?: boolean;
excludeFromGlobalExcel?: boolean;
allowTemplate?: boolean;
forms?: string[];
Expand Down
15 changes: 13 additions & 2 deletions projects/laji/src/app/shared/service/project-form.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 2bf5ffb

Please sign in to comment.