Skip to content

Commit b4380ce

Browse files
committed
ISPN-12091 Create a task
1 parent 5353a1e commit b4380ce

File tree

6 files changed

+150
-7
lines changed

6 files changed

+150
-7
lines changed

src/app/CacheManagers/TasksTableDisplay.tsx

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useEffect, useState } from 'react';
2-
import { Table, TableBody, TableHeader, TableVariant } from '@patternfly/react-table';
32
import {
43
Badge,
4+
Button,
55
Bullseye,
66
EmptyState,
77
EmptyStateBody,
@@ -30,6 +30,10 @@ import {
3030
import { useTranslation } from 'react-i18next';
3131
import { useFetchTask } from '@app/services/tasksHook';
3232
import { ExecuteTasks } from '@app/Tasks/ExecuteTasks';
33+
import { ConsoleServices } from '@services/ConsoleServices';
34+
import { ConsoleACL } from '@services/securityService';
35+
import { useConnectedUser } from '@app/services/userManagementHook';
36+
import { CreateTask } from '@app/Tasks/CreateTask';
3337

3438
const TasksTableDisplay = (props: { setTasksCount: (number) => void; isVisible: boolean }) => {
3539
const { tasks, loading, error, reload } = useFetchTask();
@@ -40,11 +44,13 @@ const TasksTableDisplay = (props: { setTasksCount: (number) => void; isVisible:
4044
});
4145
const [rows, setRows] = useState<(string | any)[]>([]);
4246
const [taskToExecute, setTaskToExecute] = useState<Task>();
47+
const [isCreateTask, setIsCreateTask] = useState(false);
4348
const { t } = useTranslation();
4449
const brandname = t('brandname.brandname');
50+
const { connectedUser } = useConnectedUser();
4551

4652
const columnNames = {
47-
name: t('cache-managers.tasks.task-name'),
53+
name: t('cache-managers.tasks.name'),
4854
type: t('cache-managers.tasks.task-type'),
4955
context: t('cache-managers.tasks.context-name'),
5056
operation: t('cache-managers.tasks.operation-name'),
@@ -135,14 +141,28 @@ const TasksTableDisplay = (props: { setTasksCount: (number) => void; isVisible:
135141
);
136142
};
137143

144+
const buildCreateTaskButton = () => {
145+
if (!ConsoleServices.security().hasConsoleACL(ConsoleACL.CREATE, connectedUser)) {
146+
return <ToolbarItem />;
147+
}
148+
return (
149+
<React.Fragment>
150+
<ToolbarItem>
151+
<Button onClick={() => setIsCreateTask(!isCreateTask)}>Add task</Button>
152+
</ToolbarItem>
153+
</React.Fragment>
154+
);
155+
};
156+
138157
if (!props.isVisible) {
139158
return <span />;
140159
}
141160

142161
return (
143162
<React.Fragment>
144-
<Toolbar id="counters-table-toolbar">
163+
<Toolbar id="task-table-toolbar">
145164
<ToolbarContent>
165+
{buildCreateTaskButton()}
146166
<ToolbarItem variant={ToolbarItemVariant.pagination}>
147167
<Pagination
148168
itemCount={filteredTasks.length}
@@ -213,6 +233,14 @@ const TasksTableDisplay = (props: { setTasksCount: (number) => void; isVisible:
213233
reload();
214234
}}
215235
/>
236+
<CreateTask
237+
isModalOpen={isCreateTask}
238+
submitModal={() => {
239+
reload();
240+
setIsCreateTask(false);
241+
}}
242+
closeModal={() => setIsCreateTask(false)}
243+
/>
216244
</React.Fragment>
217245
);
218246
};

src/app/Tasks/CreateTask.tsx

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import React, { useState } from 'react';
2+
import { Button, ButtonVariant, Form, FormGroup, Modal, ModalVariant, TextInput } from '@patternfly/react-core';
3+
import { useTranslation } from 'react-i18next';
4+
import { CodeEditor } from '@patternfly/react-code-editor';
5+
import formUtils, { IField } from '@services/formUtils';
6+
import { useCreateTask } from '@app/services/tasksHook';
7+
import { PopoverHelp } from '@app/Common/PopoverHelp';
8+
9+
const CreateTask = (props: { isModalOpen: boolean; submitModal: () => void; closeModal: () => void }) => {
10+
const { t } = useTranslation();
11+
12+
const nameInitialState: IField = {
13+
value: '',
14+
isValid: false,
15+
validated: 'default'
16+
};
17+
18+
const [name, setName] = useState<IField>(nameInitialState);
19+
const [script, setScript] = useState<string>('');
20+
const { onCreateTask } = useCreateTask(name.value, script);
21+
22+
const handleSubmit = () => {
23+
const isValid = formUtils.validateRequiredField(
24+
name.value.trim(),
25+
t('cache-managers.counters.modal-counter-name'),
26+
setName
27+
);
28+
29+
if (isValid) {
30+
props.submitModal();
31+
onCreateTask();
32+
}
33+
};
34+
35+
return (
36+
<Modal
37+
id={'create-task-modal'}
38+
variant={ModalVariant.medium}
39+
isOpen={props.isModalOpen}
40+
title={t('cache-managers.tasks.create-task')}
41+
onClose={props.closeModal}
42+
aria-label={t('cache-managers.tasks.create-task')}
43+
disableFocusTrap={true}
44+
actions={[
45+
<Button key={'Confirm'} aria-label={'Confirm'} variant={ButtonVariant.primary} onClick={handleSubmit}>
46+
{t('cache-managers.tasks.confirm')}
47+
</Button>,
48+
<Button key={'Cancel'} aria-label={'Cancel'} variant={ButtonVariant.link} onClick={props.closeModal}>
49+
{t('cache-managers.tasks.cancel')}
50+
</Button>
51+
]}
52+
>
53+
<Form
54+
onSubmit={(e) => {
55+
e.preventDefault();
56+
}}
57+
>
58+
<FormGroup
59+
validated={name.validated}
60+
isRequired
61+
helperTextInvalid={name.invalidText}
62+
isInline
63+
label={t('cache-managers.tasks.task-name')}
64+
>
65+
<TextInput
66+
type="text"
67+
validated={name.validated}
68+
value={name.value}
69+
onChange={(value) => formUtils.validateRequiredField(value, t('cache-managers.tasks.task-name'), setName)}
70+
/>
71+
</FormGroup>
72+
<FormGroup
73+
label={t('cache-managers.tasks.script')}
74+
labelIcon={
75+
<PopoverHelp
76+
name="script"
77+
label={t('cache-managers.tasks.script')}
78+
content={t('cache-managers.tasks.script-tooltip')}
79+
/>
80+
}
81+
>
82+
<CodeEditor isLineNumbersVisible code={script} onChange={setScript} height="200px" />
83+
</FormGroup>
84+
</Form>
85+
</Modal>
86+
);
87+
};
88+
89+
export { CreateTask };

src/app/Tasks/ExecuteTasks.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ const ExecuteTasks = (props: { task; isModalOpen: boolean; closeModal: () => voi
3232

3333
return (
3434
<Modal
35-
height={'1000px'}
3635
id={'execute-task-modal'}
3736
variant={ModalVariant.medium}
3837
isOpen={props.isModalOpen}

src/app/assets/languages/en.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
},
138138
"tasks": {
139139
"tasks-table-label": "Tasks",
140-
"task-name": "Name",
140+
"name": "Name",
141141
"task-type": "Type",
142142
"context-name": "Context name",
143143
"operation-name": "Operation name",
@@ -148,7 +148,12 @@
148148
"no-tasks-body": "Create tasks with the CLI or a remote client.",
149149
"execute": "Execute",
150150
"execution": "Execution",
151-
"cancel": "Cancel"
151+
"cancel": "Cancel",
152+
"create-task": "Create task",
153+
"task-name": "Task name",
154+
"script": "Script",
155+
"script-tooltip": "Script tooltip",
156+
"confirm": "Confirm"
152157
}
153158
},
154159
"caches": {

src/app/services/tasksHook.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,17 @@ export function useExecuteTask(name: string, params) {
4848
onExecute
4949
};
5050
}
51+
export function useCreateTask(taskName: string, script) {
52+
const { addAlert } = useApiAlert();
53+
54+
const onCreateTask = () => {
55+
ConsoleServices.tasks()
56+
.createTask(taskName, script)
57+
.then((actionResponse) => {
58+
addAlert(actionResponse);
59+
});
60+
};
61+
return {
62+
onCreateTask
63+
};
64+
}

src/services/tasksService.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,19 @@ export class TasksService {
3939

4040
public executeTask(name, params): Promise<ActionResponse> {
4141
const parameterURL = this.createExecuteTaskURL(params);
42-
console.log('parameterURL', parameterURL);
4342
return this.utils.post({
4443
url: this.endpoint + '/' + name + '?action=exec' + parameterURL,
4544
successMessage: `The script has been successfully executed`,
4645
errorMessage: `Unexpected error executing.`
4746
});
4847
}
48+
49+
public createTask(name: string, script): Promise<ActionResponse> {
50+
return this.utils.post({
51+
url: this.endpoint + '/' + name,
52+
successMessage: `Task ${name} has been created`,
53+
errorMessage: `Unexpected error creating task ${name}`,
54+
body: script
55+
});
56+
}
4957
}

0 commit comments

Comments
 (0)