-
Notifications
You must be signed in to change notification settings - Fork 6
/
project-base.ts
144 lines (127 loc) · 5.13 KB
/
project-base.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import { ChangeDetectorRef, Directive, OnDestroy, OnInit } from '@angular/core';
import { Title } from '@angular/platform-browser';
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';
import { ReadProject, ReadUser } from '@dasch-swiss/dsp-js';
import { ProjectService } from '@dasch-swiss/vre/shared/app-helper-services';
import {
IKeyValuePairs,
LoadProjectAction,
LoadProjectOntologiesAction,
OntologiesSelectors,
ProjectsSelectors,
UserSelectors,
} from '@dasch-swiss/vre/shared/app-state';
import { Actions, Select, Store, ofActionSuccessful } from '@ngxs/store';
import { Observable, Subject, combineLatest } from 'rxjs';
import { filter, map, take, takeUntil, takeWhile } from 'rxjs/operators';
@Directive()
export class ProjectBase implements OnInit, OnDestroy {
destroyed: Subject<void> = new Subject<void>();
projectUuid: string;
project: ReadProject; // TODO use project$ instead
// permissions of logged-in user
isAdmin$: Observable<boolean> = combineLatest([
this._store.select(UserSelectors.user).pipe(filter(user => user !== null)) as Observable<ReadUser>,
this._store.select(UserSelectors.userProjectAdminGroups),
this._route.params,
this._route.parent.params,
]).pipe(
takeUntil(this.destroyed),
map(([user, userProjectGroups, params, parentParams]) => {
const projectIri = this._projectService.uuidToIri(params.uuid ? params.uuid : parentParams.uuid);
return ProjectService.IsProjectAdminOrSysAdmin(user, userProjectGroups, projectIri);
})
);
get projectIri() {
return this._projectService.uuidToIri(this.projectUuid);
}
@Select(UserSelectors.user) user$: Observable<ReadUser>;
@Select(UserSelectors.userProjectAdminGroups) userProjectAdminGroups$: Observable<string[]>;
@Select(ProjectsSelectors.isCurrentProjectAdminOrSysAdmin) isCurrentProjectAdminOrSysAdmin: Observable<boolean>;
@Select(ProjectsSelectors.isCurrentProjectMember) isProjectMember$: Observable<boolean>;
@Select(ProjectsSelectors.currentProject) project$: Observable<ReadProject>;
@Select(ProjectsSelectors.isProjectsLoading) isProjectsLoading$: Observable<boolean>;
@Select(ProjectsSelectors.projectMembers) projectMembers$: Observable<IKeyValuePairs<ReadUser>>;
constructor(
protected _store: Store,
protected _route: ActivatedRoute,
protected _projectService: ProjectService,
protected _titleService: Title,
protected _router: Router,
protected _cd: ChangeDetectorRef,
protected _actions$: Actions
) {
// get the uuid of the current project
this.projectUuid = this._route.snapshot.params.uuid
? this._route.snapshot.params.uuid
: this._route.parent.snapshot.params.uuid;
}
ngOnInit(): void {
this.loadProject();
}
ngOnDestroy(): void {
// this._store.dispatch([new ClearCurrentProjectAction(), new ClearProjectOntologiesAction(this.projectUuid)]);
this.destroyed.next();
this.destroyed.complete();
}
protected getCurrentProject(projects: ReadProject[]): ReadProject {
if (!projects) {
return null;
}
return projects.find(x => x.id.split('/').pop() === this.projectUuid);
}
private loadProject(): void {
this.isProjectsLoading$
.pipe(
takeWhile(isLoading => isLoading === false && this.projectUuid !== undefined),
take(1)
)
.subscribe({
next: () => {
const projectMembers = this._store.selectSnapshot(ProjectsSelectors.projectMembers)[this.projectIri];
this.project = this._store.selectSnapshot(ProjectsSelectors.currentProject);
if (!this.project || this.project.id !== this.projectIri || !projectMembers) {
// get current project data, project members and project groups
// and set the project state here
this._store.dispatch(new LoadProjectAction(this.projectUuid, true));
} else if (!this.isOntologiesAvailable()) {
this._store.dispatch(new LoadProjectOntologiesAction(this.projectUuid));
}
},
complete: () => {
this._actions$
.pipe(ofActionSuccessful(LoadProjectAction))
.pipe(take(1))
.subscribe(() => this.setProjectData());
},
});
}
private setProjectData(): void {
this.project = this._store.selectSnapshot(ProjectsSelectors.currentProject);
if (!this.project) {
return;
}
this._titleService.setTitle(this.project.shortname);
this._store.dispatch(new LoadProjectOntologiesAction(this.project.id));
}
protected static navigationEndFilter(event: Observable<any>) {
return event.pipe(
filter(e => e instanceof NavigationEnd),
filter(e => !(e as NavigationEnd).url.startsWith('api'))
);
}
private isOntologiesAvailable(): boolean {
const currentProjectOntologies = this._store.selectSnapshot(OntologiesSelectors.currentProjectOntologies);
let result = true;
this.project.ontologies.forEach(ontoIri => {
if (
!currentProjectOntologies ||
currentProjectOntologies.length === 0 ||
!currentProjectOntologies.find(o => o.id === ontoIri)
) {
result = false;
}
});
return result;
}
}