-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathindex.tsx
219 lines (211 loc) · 6.3 KB
/
index.tsx
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import { DashboardOutlined, IdcardOutlined } from '@ant-design/icons';
import {
ENABLE_HEALTHCARE_SERVICES,
ENABLE_FHIR_GROUP,
ENABLE_PATIENTS_MODULE,
ENABLE_FHIR_CARE_TEAM,
ENABLE_QUEST,
ENABLE_TEAMS_ASSIGNMENT_MODULE,
ENABLE_FHIR_COMMODITY,
} from '../configs/env';
import {
URL_USER,
URL_LOCATION_UNIT,
URL_TEAMS,
URL_TEAM_ASSIGNMENT,
URL_USER_GROUPS,
URL_USER_ROLES,
URL_FHIR_CARE_TEAM,
} from '../constants';
import { URL_ALL_LOCATIONS } from '@opensrp/fhir-location-management';
import { QUEST_VIEW_URL } from '@opensrp/fhir-views';
import type { TFunction } from '@opensrp/i18n';
import { LIST_HEALTHCARE_URL } from '@opensrp/fhir-healthcare-service';
import { LIST_COMMODITY_URL, LIST_GROUP_URL } from '@opensrp/fhir-group-management';
import { LIST_PATIENTS_URL } from '@opensrp/fhir-client';
import {
COMPOSITE_ENABLE_LOCATIONS_MANAGEMENT,
COMPOSITE_ENABLE_TEAM_MANAGEMENT,
COMPOSITE_ENABLE_USER_MANAGEMENT,
} from '../configs/settings';
import React from 'react';
import { UserRole } from '@opensrp/rbac/dist/types/roleDefinition';
/** Interface for menu items */
export interface Route {
key: string;
enabled?: boolean;
url?: string;
title: string;
isHomePageLink?: boolean;
otherProps?: {
icon?: string | JSX.Element;
};
permissions?: string[];
children?: Route[];
}
export interface GetRoutes {
(roles: string[], t: TFunction, userRole: UserRole): Route[];
}
/** Gets Routes For Application
*
* @param roles User's roles
* @returns {Route[]} returns generated routes
*/
export function getRoutes(roles: string[], t: TFunction, userRole: UserRole): Route[] {
const routes: Route[] = [
{
otherProps: { icon: <DashboardOutlined /> },
title: t('Administration'),
key: 'admin',
enabled: true,
permissions: [],
children: [
{
title: t('User Management'),
key: 'user-management',
isHomePageLink: true,
url: URL_USER,
permissions: ['iam_user.read'],
enabled: COMPOSITE_ENABLE_USER_MANAGEMENT,
children: [
{ title: t('Users'), key: 'users', url: URL_USER, permissions: ['iam_user.read'] },
{
title: t('User Groups'),
key: 'user-groups',
url: URL_USER_GROUPS,
permissions: ['iam_group.read'],
},
{
title: t('User Roles'),
key: 'user-roles',
url: URL_USER_ROLES,
permissions: ['iam_role.read'],
},
],
},
{
title: t('Location Management'),
key: 'location-management',
isHomePageLink: true,
url: URL_LOCATION_UNIT,
permissions: ['Location.read'],
enabled: COMPOSITE_ENABLE_LOCATIONS_MANAGEMENT,
children: [
{
title: t('Hierarchy'),
url: URL_LOCATION_UNIT,
key: 'location-unit',
permissions: ['Location.read'],
},
{
title: t('All Locations'),
url: URL_ALL_LOCATIONS,
key: 'all-locations',
permissions: ['Location.read'],
},
],
},
{
title: t('Care Teams Management'),
key: 'fhir-care-team',
isHomePageLink: true,
permissions: ['CareTeam.read'],
enabled: ENABLE_FHIR_CARE_TEAM,
url: URL_FHIR_CARE_TEAM,
},
{
title: t('Team Management'),
key: 'team-management',
isHomePageLink: true,
permissions: ['Organization.read'],
url: URL_TEAMS,
enabled: COMPOSITE_ENABLE_TEAM_MANAGEMENT,
children: [
{ title: t('Teams'), url: URL_TEAMS, key: 'TEAMS', permissions: ['Organization.read'] },
{
permissions: ['OrganizationAffiliation.read'],
title: t('Team Assignment'),
url: URL_TEAM_ASSIGNMENT,
key: 'team-assignment',
enabled: ENABLE_TEAMS_ASSIGNMENT_MODULE,
},
],
},
{
title: t('Group Management'),
key: 'fhir-group',
url: LIST_GROUP_URL,
isHomePageLink: true,
permissions: ['Group.read'],
enabled: ENABLE_FHIR_GROUP,
},
{
title: t('Commodity Management'),
key: 'fhir-commodity',
isHomePageLink: true,
url: LIST_COMMODITY_URL,
permissions: ['Group.read'],
enabled: ENABLE_FHIR_COMMODITY,
},
{
title: t('Questionnaire Management'),
key: 'fhir-quest',
permissions: ['Questionnaire.read'],
enabled: ENABLE_QUEST,
url: QUEST_VIEW_URL,
isHomePageLink: true,
},
{
title: t('Healthcare Services'),
key: 'healthcare',
isHomePageLink: true,
url: LIST_HEALTHCARE_URL,
permissions: ['HealthcareService.read'],
enabled: ENABLE_HEALTHCARE_SERVICES,
},
],
},
{
otherProps: { icon: <IdcardOutlined /> },
title: t('Patients'),
key: 'fhir-patients',
permissions: ['Patient.read'],
enabled: ENABLE_PATIENTS_MODULE,
url: LIST_PATIENTS_URL,
isHomePageLink: true,
},
];
return filterFalsyRoutes(routes, userRole);
}
/** Removes the disabled Routes from
*
* @param routes all routes
* @returns {Route[]} returns only enabled routes
*/
export function filterFalsyRoutes(routes: Route[], userRole: UserRole): Route[] {
return routes
.filter(
(e) =>
(!e.hasOwnProperty('enabled') || (e.hasOwnProperty('enabled') && e.enabled === true)) &&
userRole.hasPermissions(e.permissions ?? [])
)
.map((e) => {
return e.children ? { ...e, children: filterFalsyRoutes(e.children, userRole) } : e;
});
}
export const getRoutesForHomepage: GetRoutes = (roles, t, userRole) => {
const routes = getRoutes(roles, t, userRole);
const homePageRoutes: Route[] = [];
function extractHomePAgeLink(routes: Route[]) {
for (const route of routes) {
if (route.isHomePageLink) {
homePageRoutes.push(route);
}
if (route.children) {
extractHomePAgeLink(route.children);
}
}
}
extractHomePAgeLink(routes);
return homePageRoutes;
};