summaryrefslogtreecommitdiffstats
path: root/awx/ui/src/screens/ManagementJob/ManagementJob.js
blob: 6678006b68b14261ddb2d33c611080b2c1956dbe (plain)
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
import React, { useState, useEffect, useCallback } from 'react';
import {
  Link,
  Redirect,
  Route,
  Switch,
  useLocation,
  useParams,
  useRouteMatch,
} from 'react-router-dom';

import { t } from '@lingui/macro';
import { CaretLeftIcon } from '@patternfly/react-icons';
import { Card, PageSection } from '@patternfly/react-core';

import { SystemJobTemplatesAPI, OrganizationsAPI } from 'api';
import ContentError from 'components/ContentError';
import ContentLoading from 'components/ContentLoading';
import NotificationList from 'components/NotificationList';
import RoutedTabs from 'components/RoutedTabs';
import { Schedules } from 'components/Schedule';
import { useConfig } from 'contexts/Config';
import useRequest from 'hooks/useRequest';

function ManagementJob({ setBreadcrumb }) {
  const basePath = '/management_jobs';

  const match = useRouteMatch();
  const { id } = useParams();
  const { pathname } = useLocation();
  const { me } = useConfig();

  const [isNotificationAdmin, setIsNotificationAdmin] = useState(false);

  const { isLoading, error, request, result } = useRequest(
    useCallback(
      () =>
        Promise.all([
          SystemJobTemplatesAPI.readDetail(id),
          OrganizationsAPI.read({
            page_size: 1,
            role_level: 'notification_admin_role',
          }),
        ]).then(([{ data: systemJobTemplate }, notificationRoles]) => ({
          systemJobTemplate,
          notificationRoles,
        })),
      [id]
    )
  );

  useEffect(() => {
    request();
  }, [request, pathname]);

  useEffect(() => {
    if (!result) return;
    setIsNotificationAdmin(
      Boolean(result?.notificationRoles?.data?.results?.length)
    );
    setBreadcrumb(result);
  }, [result, setBreadcrumb, setIsNotificationAdmin]);

  useEffect(() => {
    if (!result) return;

    setBreadcrumb(result);
  }, [result, setBreadcrumb]);

  const createSchedule = useCallback(
    (data) =>
      SystemJobTemplatesAPI.createSchedule(result?.systemJobTemplate.id, data),
    [result]
  );
  const loadSchedules = useCallback(
    (params) =>
      SystemJobTemplatesAPI.readSchedules(result?.systemJobTemplate.id, params),
    [result]
  );
  const loadScheduleOptions = useCallback(
    () =>
      SystemJobTemplatesAPI.readScheduleOptions(result?.systemJobTemplate.id),
    [result]
  );

  const shouldShowNotifications =
    result?.systemJobTemplate?.id &&
    (isNotificationAdmin || me?.is_system_auditor);
  const shouldShowSchedules = !!result?.systemJobTemplate?.id;

  const tabsArray = [
    {
      id: 99,
      link: basePath,
      name: (
        <>
          <CaretLeftIcon />
          {t`Back to management jobs`}
        </>
      ),
      isBackButton: true,
    },
  ];

  if (shouldShowSchedules) {
    tabsArray.push({
      id: 0,
      name: t`Schedules`,
      link: `${match.url}/schedules`,
    });
  }

  if (shouldShowNotifications) {
    tabsArray.push({
      id: 1,
      name: t`Notifications`,
      link: `${match.url}/notifications`,
    });
  }

  let Tabs = <RoutedTabs tabsArray={tabsArray} />;
  if (pathname.includes('edit') || pathname.includes('schedules/')) {
    Tabs = null;
  }

  if (error) {
    return (
      <PageSection>
        <Card>
          <ContentError error={error}>
            {error?.response?.status === 404 && (
              <span>
                {t`Management job not found.`}

                <Link to={basePath}>{t`View all management jobs`}</Link>
              </span>
            )}
          </ContentError>
        </Card>
      </PageSection>
    );
  }

  if (isLoading) {
    return (
      <PageSection>
        <Card>
          {Tabs}
          <ContentLoading />
        </Card>
      </PageSection>
    );
  }

  return (
    <PageSection>
      <Card>
        {Tabs}
        <Switch>
          <Redirect
            exact
            from={`${basePath}/:id`}
            to={`${basePath}/:id/schedules`}
          />
          {shouldShowNotifications ? (
            <Route path={`${basePath}/:id/notifications`}>
              <NotificationList
                id={Number(result?.systemJobTemplate?.id)}
                canToggleNotifications={isNotificationAdmin}
                apiModel={SystemJobTemplatesAPI}
              />
            </Route>
          ) : null}
          {shouldShowSchedules ? (
            <Route path={`${basePath}/:id/schedules`}>
              <Schedules
                apiModel={SystemJobTemplatesAPI}
                resource={result.systemJobTemplate}
                createSchedule={createSchedule}
                loadSchedules={loadSchedules}
                loadScheduleOptions={loadScheduleOptions}
                setBreadcrumb={setBreadcrumb}
              />
            </Route>
          ) : null}
        </Switch>
      </Card>
    </PageSection>
  );
}

export default ManagementJob;