Skip to content

Commit

Permalink
Merge pull request #669 from luomus/allow-excel-generation-567
Browse files Browse the repository at this point in the history
Implement MHL.allowExcelGeneration
  • Loading branch information
Blodir authored Nov 25, 2024
2 parents 20a4899 + 1552bb0 commit 1706127
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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';
import { map, switchMap } from 'rxjs/operators';
import { FormPermissionService } from '../../shared/service/form-permission.service';

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

readonly excelForm$ = this.projectFormService.getProjectFormFromRoute$(this.route).pipe(
switchMap(projectForm => this.formPermissionService.getRights(projectForm.form).pipe(
map(rights => ({ projectForm, rights })
))),
map(({ projectForm, rights }) => this.projectFormService.getExcelFormOptions(projectForm, rights.admin)),
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])
);
}

public projectFormService: ProjectFormService,
private formPermissionService: FormPermissionService
) {}
}
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, rights.admin);
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
17 changes: 14 additions & 3 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']));
}

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[];
getExcelFormOptions(projectForm: ProjectForm, isAdmin: boolean): ExcelFormOptions[] {
const getExcelOptions = (form: Form.SchemaForm | Form.List) => form.options?.allowExcel
? { formID: form.id, allowGenerate: isAdmin || form.options.allowExcelGeneration !== false }
: undefined;
return [getExcelOptions(projectForm.form), ...projectForm.subForms.map(getExcelOptions)].filter(f => f);
}

getExcelFormIDs(projectForm: ProjectForm, isAdmin: boolean): string[] {
return this.getExcelFormOptions(projectForm, isAdmin).map(f => f.formID);
}

getSubmissionsPageTitle(form: Form.SchemaForm, isAdmin: boolean) {
Expand Down

0 comments on commit 1706127

Please sign in to comment.