Skip to content

Commit 1408027

Browse files
committed
Refactor ListGWEnvironments component to eliminate code duplication
1 parent b159be2 commit 1408027

File tree

2 files changed

+123
-230
lines changed

2 files changed

+123
-230
lines changed

portals/admin/src/main/webapp/source/src/app/components/GatewayEnvironments/ListGWEnviornments.jsx

Lines changed: 121 additions & 228 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ function apiCall() {
6969
*/
7070
export default function ListGWEnviornments() {
7171
const intl = useIntl();
72-
let columProps;
7372
const { settings } = useAppContext();
7473
// Dialog state for Live Gateways
7574
const [liveGatewaysOpen, setLiveGatewaysOpen] = useState(false);
@@ -89,19 +88,80 @@ export default function ListGWEnviornments() {
8988
};
9089

9190
const isGatewayTypeAvailable = settings.gatewayTypes.length >= 2;
92-
if (isGatewayTypeAvailable) {
93-
columProps = [
94-
{ name: 'name', options: { display: false } },
95-
{
96-
name: 'displayName',
97-
label: intl.formatMessage({
98-
id: 'AdminPages.Gateways.table.header.displayName',
99-
defaultMessage: 'Name',
100-
}),
101-
options: {
102-
sort: true,
103-
},
91+
92+
// Helper function to render virtual hosts
93+
const renderVhosts = (vhosts) => {
94+
return (
95+
vhosts.map((vhost) => (
96+
<div key={`${vhost.host}:${vhost.httpsPort}`}>
97+
{
98+
'https://' + vhost.host + (vhost.httpsPort === 443 ? '' : ':' + vhost.httpsPort)
99+
+ (vhost.httpContext ? '/' + vhost.httpContext.replace(/^\//g, '') : '')
100+
}
101+
</div>
102+
))
103+
);
104+
};
105+
106+
// Helper function to render permissions
107+
const renderPermissions = (permissions) => {
108+
return (
109+
<div>
110+
<Permission
111+
type={permissions.permissionType}
112+
roles={permissions.roles}
113+
/>
114+
</div>
115+
);
116+
};
117+
118+
// Helper function to render gateway instances
119+
const renderGatewayInstances = (value, tableMeta) => {
120+
if (typeof tableMeta.rowData === 'object') {
121+
const envId = tableMeta.rowData[isGatewayTypeAvailable ? 8 : 7]; // Adjust index based on gatewayType column presence
122+
const envName = tableMeta.rowData[1]; // 'displayName'
123+
const gatewayType = isGatewayTypeAvailable ? tableMeta.rowData[2] : 'Regular'; // Default to 'Regular' if no gatewayType column
124+
const isDisabled = gatewayType !== 'Regular';
125+
126+
const button = (
127+
<IconButton
128+
onClick={() => handleOpenLiveGateways(envId, envName)}
129+
disabled={isDisabled}
130+
>
131+
<FormatListBulletedIcon aria-label='gateway-instances-list-icon' />
132+
</IconButton>
133+
);
134+
135+
return isDisabled ? (
136+
<StyledTooltip
137+
title={intl.formatMessage({
138+
id: 'AdminPages.Gateways.table.gatewayInstances.tooltip.notSupported',
139+
defaultMessage: 'Not supported for this gateway type',
140+
})}
141+
>
142+
<span>{button}</span>
143+
</StyledTooltip>
144+
) : button;
145+
} else {
146+
return <div />;
147+
}
148+
};
149+
150+
// Build column configuration
151+
const columProps = [
152+
{ name: 'name', options: { display: false } },
153+
{
154+
name: 'displayName',
155+
label: intl.formatMessage({
156+
id: 'AdminPages.Gateways.table.header.displayName',
157+
defaultMessage: 'Name',
158+
}),
159+
options: {
160+
sort: true,
104161
},
162+
},
163+
// Conditionally include gatewayType column
164+
...(isGatewayTypeAvailable ? [
105165
{
106166
name: 'gatewayType',
107167
label: intl.formatMessage({
@@ -112,232 +172,65 @@ export default function ListGWEnviornments() {
112172
sort: false,
113173
},
114174
},
115-
{
116-
name: 'type',
117-
label: intl.formatMessage({
118-
id: 'AdminPages.Gateways.table.header.type',
119-
defaultMessage: 'Type',
120-
}),
121-
options: {
122-
sort: false,
123-
},
175+
] : []),
176+
{
177+
name: 'type',
178+
label: intl.formatMessage({
179+
id: 'AdminPages.Gateways.table.header.type',
180+
defaultMessage: 'Type',
181+
}),
182+
options: {
183+
sort: false,
124184
},
125-
{
126-
name: 'description',
127-
label: intl.formatMessage({
128-
id: 'AdminPages.Gateways.table.header.description',
129-
defaultMessage: 'Description',
130-
}),
131-
options: {
132-
sort: false,
133-
},
185+
},
186+
{
187+
name: 'description',
188+
label: intl.formatMessage({
189+
id: 'AdminPages.Gateways.table.header.description',
190+
defaultMessage: 'Description',
191+
}),
192+
options: {
193+
sort: false,
134194
},
135-
{
136-
name: 'vhosts',
137-
label: intl.formatMessage({
138-
id: 'AdminPages.Gateways.table.header.vhosts',
139-
defaultMessage: 'Virtual Host(s)',
140-
}),
141-
options: {
142-
sort: false,
143-
customBodyRender: (vhosts) => {
144-
return (
145-
vhosts.map((vhost) => (
146-
<div>
147-
{
148-
'https://' + vhost.host + (vhost.httpsPort === 443 ? '' : ':' + vhost.httpsPort)
149-
+ (vhost.httpContext ? '/' + vhost.httpContext.replace(/^\//g, '') : '')
150-
}
151-
</div>
152-
))
153-
);
154-
},
155-
},
195+
},
196+
{
197+
name: 'vhosts',
198+
label: intl.formatMessage({
199+
id: 'AdminPages.Gateways.table.header.vhosts',
200+
defaultMessage: 'Virtual Host(s)',
201+
}),
202+
options: {
203+
sort: false,
204+
customBodyRender: renderVhosts,
156205
},
157-
{
158-
name: 'permissions',
159-
label: intl.formatMessage({
160-
id: 'AdminPages.Gateways.table.header.permission',
161-
defaultMessage: 'Visibility',
162-
}),
163-
options: {
164-
sort: false,
165-
customBodyRender: (permissions) => {
166-
return (
167-
<div>
168-
<Permission
169-
type={permissions.permissionType}
170-
roles={permissions.roles}
171-
/>
172-
</div>
173-
);
174-
},
175-
},
206+
},
207+
{
208+
name: 'permissions',
209+
label: intl.formatMessage({
210+
id: 'AdminPages.Gateways.table.header.permission',
211+
defaultMessage: 'Visibility',
212+
}),
213+
options: {
214+
sort: false,
215+
customBodyRender: renderPermissions,
176216
},
177-
...(settings.isGatewayNotificationEnabled ? [
178-
{
179-
name: 'gatewayInstances',
180-
label: intl.formatMessage({
181-
id: 'AdminPages.Gateways.table.header.gatewayInstances',
182-
defaultMessage: 'Gateway Instances',
183-
}),
184-
options: {
185-
sort: false,
186-
customBodyRender: (value, tableMeta) => {
187-
if (typeof tableMeta.rowData === 'object') {
188-
const envId = tableMeta.rowData[8]; // 'id' is the last column
189-
const envName = tableMeta.rowData[1]; // 'displayName'
190-
const gatewayType = tableMeta.rowData[2]; // 'gatewayType'
191-
const isDisabled = gatewayType !== 'Regular';
192-
193-
const button = (
194-
<IconButton
195-
onClick={() => handleOpenLiveGateways(envId, envName)}
196-
disabled={isDisabled}
197-
>
198-
<FormatListBulletedIcon aria-label='gateway-instances-list-icon' />
199-
</IconButton>
200-
);
201-
202-
return isDisabled ? (
203-
<StyledTooltip
204-
title={intl.formatMessage({
205-
id: 'AdminPages.Gateways.table.gatewayInstances.tooltip.notSupported',
206-
defaultMessage: 'Not supported for this gateway type',
207-
})}
208-
>
209-
<span>{button}</span>
210-
</StyledTooltip>
211-
) : button;
212-
} else {
213-
return <div />;
214-
}
215-
},
216-
},
217-
},
218-
] : []),
219-
{ name: 'id', options: { display: false } },
220-
];
221-
} else {
222-
columProps = [
223-
{ name: 'name', options: { display: false } },
217+
},
218+
// Conditionally include gateway instances column
219+
...(settings.isGatewayNotificationEnabled ? [
224220
{
225-
name: 'displayName',
221+
name: 'gatewayInstances',
226222
label: intl.formatMessage({
227-
id: 'AdminPages.Gateways.table.header.displayName',
228-
defaultMessage: 'Name',
229-
}),
230-
options: {
231-
sort: true,
232-
},
233-
},
234-
{
235-
name: 'type',
236-
label: intl.formatMessage({
237-
id: 'AdminPages.Gateways.table.header.type',
238-
defaultMessage: 'Type',
223+
id: 'AdminPages.Gateways.table.header.gatewayInstances',
224+
defaultMessage: 'Gateway Instances',
239225
}),
240226
options: {
241227
sort: false,
228+
customBodyRender: renderGatewayInstances,
242229
},
243230
},
244-
{
245-
name: 'description',
246-
label: intl.formatMessage({
247-
id: 'AdminPages.Gateways.table.header.description',
248-
defaultMessage: 'Description',
249-
}),
250-
options: {
251-
sort: false,
252-
},
253-
},
254-
{
255-
name: 'vhosts',
256-
label: intl.formatMessage({
257-
id: 'AdminPages.Gateways.table.header.vhosts',
258-
defaultMessage: 'Virtual Host(s)',
259-
}),
260-
options: {
261-
sort: false,
262-
customBodyRender: (vhosts) => {
263-
return (
264-
vhosts.map((vhost) => (
265-
<div>
266-
{
267-
'https://' + vhost.host + (vhost.httpsPort === 443 ? '' : ':' + vhost.httpsPort)
268-
+ (vhost.httpContext ? '/' + vhost.httpContext.replace(/^\//g, '') : '')
269-
}
270-
</div>
271-
))
272-
);
273-
},
274-
},
275-
},
276-
{
277-
name: 'permissions',
278-
label: intl.formatMessage({
279-
id: 'AdminPages.Gateways.table.header.permission',
280-
defaultMessage: 'Visibility',
281-
}),
282-
options: {
283-
sort: false,
284-
customBodyRender: (permissions) => {
285-
return (
286-
<div>
287-
<Permission
288-
type={permissions.permissionType}
289-
roles={permissions.roles}
290-
/>
291-
</div>
292-
);
293-
},
294-
},
295-
},
296-
...(settings.isGatewayNotificationEnabled ? [
297-
{
298-
name: 'gatewayInstances',
299-
label: intl.formatMessage({
300-
id: 'AdminPages.Gateways.table.header.gatewayInstances',
301-
defaultMessage: 'Gateway Instances',
302-
}),
303-
options: {
304-
sort: false,
305-
customBodyRender: (value, tableMeta) => {
306-
if (typeof tableMeta.rowData === 'object') {
307-
const envId = tableMeta.rowData[8]; // 'id' is the last column
308-
const envName = tableMeta.rowData[1]; // 'displayName'
309-
const gatewayType = tableMeta.rowData[2]; // 'gatewayType'
310-
const isDisabled = gatewayType !== 'Regular';
311-
312-
const button = (
313-
<IconButton
314-
onClick={() => handleOpenLiveGateways(envId, envName)}
315-
disabled={isDisabled}
316-
>
317-
<FormatListBulletedIcon aria-label='gateway-instances-list-icon' />
318-
</IconButton>
319-
);
320-
321-
return isDisabled ? (
322-
<StyledTooltip
323-
title={intl.formatMessage({
324-
id: 'AdminPages.Gateways.table.gatewayInstances.tooltip.notSupported',
325-
defaultMessage: 'Not supported for this gateway type',
326-
})}
327-
>
328-
<span>{button}</span>
329-
</StyledTooltip>
330-
) : button;
331-
} else {
332-
return <div />;
333-
}
334-
},
335-
},
336-
},
337-
] : []),
338-
{ name: 'id', options: { display: false } },
339-
];
340-
}
231+
] : []),
232+
{ name: 'id', options: { display: false } },
233+
];
341234
const addButtonProps = {
342235
triggerButtonText: intl.formatMessage({
343236
id: 'AdminPages.Gateways.List.addButtonProps.triggerButtonText',

portals/publisher/src/main/webapp/source/src/app/components/Apis/Details/Environments/Environments.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3399,7 +3399,7 @@ export default function Environments() {
33993399
</IconButton>
34003400
</Tooltip>
34013401
</TableCell>
3402-
{settings.isGatewayNotificationEnabled === true && (
3402+
{settings.isGatewayNotificationEnabled && (
34033403
<TableCell align='justify'>
34043404
<FormattedMessage
34053405
id='Apis.Details.Environments.Environments.gateway.deployment.status'
@@ -3544,7 +3544,7 @@ export default function Environments() {
35443544
EnvDeployments={allEnvDeployments[row.name]}
35453545
/>
35463546
</TableCell>
3547-
{settings.isGatewayNotificationEnabled === true && (
3547+
{settings.isGatewayNotificationEnabled && (
35483548
<TableCell align='justify'>
35493549
{envDeploymentStatusComponent(row, allEnvDeployments)}
35503550
</TableCell>

0 commit comments

Comments
 (0)