Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement MHL.allowExcelGeneration #669

Merged
merged 4 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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