-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathis-project.guard.ts
More file actions
77 lines (61 loc) · 2.4 KB
/
Copy pathis-project.guard.ts
File metadata and controls
77 lines (61 loc) · 2.4 KB
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
import { Store } from '@ngxs/store';
import { map, switchMap } from 'rxjs/operators';
import { inject } from '@angular/core';
import { CanMatchFn, Route, Router, UrlSegment } from '@angular/router';
import { UserSelectors } from '@core/store/user';
import { CurrentResourceType } from '@shared/enums';
import { CurrentResourceSelectors, GetResource } from '@shared/stores';
export const isProjectGuard: CanMatchFn = (route: Route, segments: UrlSegment[]) => {
const store = inject(Store);
const router = inject(Router);
const id = segments[0]?.path;
if (!id) {
return false;
}
const currentResource = store.selectSnapshot(CurrentResourceSelectors.getCurrentResource);
const currentUser = store.selectSnapshot(UserSelectors.getCurrentUser);
if (currentResource && !id.startsWith(currentResource.id)) {
if (currentResource.type === CurrentResourceType.Projects && currentResource.parentId) {
router.navigate(['/', currentResource.parentId, 'files', id]);
return true;
}
if (currentResource.type === CurrentResourceType.Preprints && currentResource.parentId) {
router.navigate(['/preprints', currentResource.parentId, id]);
return true;
}
if (currentResource.type === CurrentResourceType.Users) {
if (currentUser && currentUser.id === currentResource.id) {
router.navigate(['/profile']);
} else {
router.navigate(['/user', id]);
}
return false;
}
return currentResource.type === CurrentResourceType.Projects;
}
return store.dispatch(new GetResource(id)).pipe(
switchMap(() => store.select(CurrentResourceSelectors.getCurrentResource)),
map((resource) => {
if (!resource || !id.startsWith(resource.id)) {
return false;
}
if (resource.type === CurrentResourceType.Projects && resource.parentId) {
router.navigate(['/', resource.parentId, 'files', id]);
return true;
}
if (resource.type === CurrentResourceType.Preprints && resource.parentId) {
router.navigate(['/preprints', resource.parentId, id]);
return true;
}
if (resource.type === CurrentResourceType.Users) {
if (currentUser && currentUser.id === resource.id) {
router.navigate(['/profile']);
} else {
router.navigate(['/user', id]);
}
return false;
}
return resource.type === CurrentResourceType.Projects;
})
);
};