mirror of
https://github.com/ansible/awx.git
synced 2026-05-07 01:17:37 -02:30
Remove duplicate notification admin request
This commit is contained in:
@@ -12,7 +12,7 @@ import { t } from '@lingui/macro';
|
|||||||
import { withI18n } from '@lingui/react';
|
import { withI18n } from '@lingui/react';
|
||||||
import styled from 'styled-components';
|
import styled from 'styled-components';
|
||||||
|
|
||||||
import { ConfigAPI, MeAPI, OrganizationsAPI, RootAPI } from '../../api';
|
import { ConfigAPI, MeAPI, RootAPI } from '../../api';
|
||||||
import { ConfigProvider } from '../../contexts/Config';
|
import { ConfigProvider } from '../../contexts/Config';
|
||||||
import { SESSION_TIMEOUT_KEY } from '../../constants';
|
import { SESSION_TIMEOUT_KEY } from '../../constants';
|
||||||
import { isAuthenticated } from '../../util/auth';
|
import { isAuthenticated } from '../../util/auth';
|
||||||
@@ -148,22 +148,8 @@ function AppContainer({ i18n, navRouteConfig = [], children }) {
|
|||||||
results: [me],
|
results: [me],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
] = await Promise.all([ConfigAPI.read(), MeAPI.read()]);
|
||||||
data: { results: notificationAdminResults },
|
setConfig({ ...data, me });
|
||||||
},
|
|
||||||
] = await Promise.all([
|
|
||||||
ConfigAPI.read(),
|
|
||||||
MeAPI.read(),
|
|
||||||
OrganizationsAPI.read({
|
|
||||||
page_size: 1,
|
|
||||||
role_level: 'notification_admin_role',
|
|
||||||
}),
|
|
||||||
]);
|
|
||||||
setConfig({
|
|
||||||
...data,
|
|
||||||
me,
|
|
||||||
isNotificationAdmin: Boolean(notificationAdminResults?.length),
|
|
||||||
});
|
|
||||||
setIsReady(true);
|
setIsReady(true);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.response.status === 401) {
|
if (err.response.status === 401) {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import React, { useEffect, useCallback } from 'react';
|
import React, { useState, useEffect, useCallback } from 'react';
|
||||||
import {
|
import {
|
||||||
Link,
|
Link,
|
||||||
Redirect,
|
Redirect,
|
||||||
@@ -13,7 +13,7 @@ import { t } from '@lingui/macro';
|
|||||||
import { CaretLeftIcon } from '@patternfly/react-icons';
|
import { CaretLeftIcon } from '@patternfly/react-icons';
|
||||||
import { Card, PageSection } from '@patternfly/react-core';
|
import { Card, PageSection } from '@patternfly/react-core';
|
||||||
|
|
||||||
import { SystemJobTemplatesAPI } from '../../api';
|
import { SystemJobTemplatesAPI, OrganizationsAPI } from '../../api';
|
||||||
import ContentError from '../../components/ContentError';
|
import ContentError from '../../components/ContentError';
|
||||||
import ContentLoading from '../../components/ContentLoading';
|
import ContentLoading from '../../components/ContentLoading';
|
||||||
import NotificationList from '../../components/NotificationList';
|
import NotificationList from '../../components/NotificationList';
|
||||||
@@ -28,18 +28,39 @@ function ManagementJob({ i18n, setBreadcrumb }) {
|
|||||||
const match = useRouteMatch();
|
const match = useRouteMatch();
|
||||||
const { id } = useParams();
|
const { id } = useParams();
|
||||||
const { pathname } = useLocation();
|
const { pathname } = useLocation();
|
||||||
const { me, isNotificationAdmin } = useConfig();
|
const { me } = useConfig();
|
||||||
|
|
||||||
const canReadNotifications = isNotificationAdmin || me?.is_system_auditor;
|
const [isNotificationAdmin, setIsNotificationAdmin] = useState(false);
|
||||||
|
|
||||||
const { isLoading, error, request, result } = useRequest(
|
const { isLoading, error, request, result } = useRequest(
|
||||||
useCallback(async () => SystemJobTemplatesAPI.readDetail(id), [id])
|
useCallback(
|
||||||
|
() =>
|
||||||
|
Promise.all([
|
||||||
|
SystemJobTemplatesAPI.readDetail(id),
|
||||||
|
OrganizationsAPI.read({
|
||||||
|
page_size: 1,
|
||||||
|
role_level: 'notification_admin_role',
|
||||||
|
}),
|
||||||
|
]).then(([systemJobTemplate, notificationRoles]) => ({
|
||||||
|
systemJobTemplate,
|
||||||
|
notificationRoles,
|
||||||
|
})),
|
||||||
|
[id]
|
||||||
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
request();
|
request();
|
||||||
}, [request, pathname]);
|
}, [request, pathname]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!result) return;
|
||||||
|
setIsNotificationAdmin(
|
||||||
|
Boolean(result?.notificationRoles?.data?.results?.length)
|
||||||
|
);
|
||||||
|
setBreadcrumb(result);
|
||||||
|
}, [result, setBreadcrumb, setIsNotificationAdmin]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!result) return;
|
if (!result) return;
|
||||||
|
|
||||||
@@ -47,18 +68,26 @@ function ManagementJob({ i18n, setBreadcrumb }) {
|
|||||||
}, [result, setBreadcrumb]);
|
}, [result, setBreadcrumb]);
|
||||||
|
|
||||||
const createSchedule = useCallback(
|
const createSchedule = useCallback(
|
||||||
data => SystemJobTemplatesAPI.createSchedule(result.id, data),
|
data =>
|
||||||
|
SystemJobTemplatesAPI.createSchedule(result?.systemJobTemplate.id, data),
|
||||||
[result]
|
[result]
|
||||||
);
|
);
|
||||||
const loadSchedules = useCallback(
|
const loadSchedules = useCallback(
|
||||||
params => SystemJobTemplatesAPI.readSchedules(result.id, params),
|
params =>
|
||||||
|
SystemJobTemplatesAPI.readSchedules(result?.systemJobTemplate.id, params),
|
||||||
[result]
|
[result]
|
||||||
);
|
);
|
||||||
const loadScheduleOptions = useCallback(
|
const loadScheduleOptions = useCallback(
|
||||||
() => SystemJobTemplatesAPI.readScheduleOptions(result.id),
|
() =>
|
||||||
|
SystemJobTemplatesAPI.readScheduleOptions(result?.systemJobTemplate.id),
|
||||||
[result]
|
[result]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const shouldShowNotifications =
|
||||||
|
result?.systemJobTemplate?.id &&
|
||||||
|
(isNotificationAdmin || me?.is_system_auditor);
|
||||||
|
const shouldShowSchedules = !!result?.systemJobTemplate?.id;
|
||||||
|
|
||||||
const tabsArray = [
|
const tabsArray = [
|
||||||
{
|
{
|
||||||
id: 99,
|
id: 99,
|
||||||
@@ -70,14 +99,17 @@ function ManagementJob({ i18n, setBreadcrumb }) {
|
|||||||
</>
|
</>
|
||||||
),
|
),
|
||||||
},
|
},
|
||||||
{
|
];
|
||||||
|
|
||||||
|
if (shouldShowSchedules) {
|
||||||
|
tabsArray.push({
|
||||||
id: 0,
|
id: 0,
|
||||||
name: i18n._(t`Schedules`),
|
name: i18n._(t`Schedules`),
|
||||||
link: `${match.url}/schedules`,
|
link: `${match.url}/schedules`,
|
||||||
},
|
});
|
||||||
];
|
}
|
||||||
|
|
||||||
if (canReadNotifications) {
|
if (shouldShowNotifications) {
|
||||||
tabsArray.push({
|
tabsArray.push({
|
||||||
id: 1,
|
id: 1,
|
||||||
name: i18n._(t`Notifications`),
|
name: i18n._(t`Notifications`),
|
||||||
@@ -129,27 +161,29 @@ function ManagementJob({ i18n, setBreadcrumb }) {
|
|||||||
from={`${basePath}/:id`}
|
from={`${basePath}/:id`}
|
||||||
to={`${basePath}/:id/schedules`}
|
to={`${basePath}/:id/schedules`}
|
||||||
/>
|
/>
|
||||||
{canReadNotifications ? (
|
{shouldShowNotifications ? (
|
||||||
<Route path={`${basePath}/:id/notifications`}>
|
<Route path={`${basePath}/:id/notifications`}>
|
||||||
<NotificationList
|
<NotificationList
|
||||||
id={Number(result?.id)}
|
id={Number(result?.systemJobTemplate?.id)}
|
||||||
canToggleNotifications={isNotificationAdmin}
|
canToggleNotifications={isNotificationAdmin}
|
||||||
apiModel={SystemJobTemplatesAPI}
|
apiModel={SystemJobTemplatesAPI}
|
||||||
/>
|
/>
|
||||||
</Route>
|
</Route>
|
||||||
) : null}
|
) : null}
|
||||||
<Route path={`${basePath}/:id/schedules`}>
|
{shouldShowSchedules ? (
|
||||||
<Schedules
|
<Route path={`${basePath}/:id/schedules`}>
|
||||||
apiModel={SystemJobTemplatesAPI}
|
<Schedules
|
||||||
resource={result}
|
apiModel={SystemJobTemplatesAPI}
|
||||||
createSchedule={createSchedule}
|
resource={result.systemJobTemplate}
|
||||||
loadSchedules={loadSchedules}
|
createSchedule={createSchedule}
|
||||||
loadScheduleOptions={loadScheduleOptions}
|
loadSchedules={loadSchedules}
|
||||||
setBreadcrumb={setBreadcrumb}
|
loadScheduleOptions={loadScheduleOptions}
|
||||||
launchConfig={{}}
|
setBreadcrumb={setBreadcrumb}
|
||||||
surveyConfig={{}}
|
launchConfig={{}}
|
||||||
/>
|
surveyConfig={{}}
|
||||||
</Route>
|
/>
|
||||||
|
</Route>
|
||||||
|
) : null}
|
||||||
</Switch>
|
</Switch>
|
||||||
</Card>
|
</Card>
|
||||||
</PageSection>
|
</PageSection>
|
||||||
|
|||||||
Reference in New Issue
Block a user