foreman_snapshot_management 4.3.0 → 4.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (24) hide show
  1. checksums.yaml +4 -4
  2. data/lib/foreman_snapshot_management/version.rb +1 -1
  3. data/webpack/components/SnapshotManagement/SnapshotManagementReducer.js +0 -7
  4. data/webpack/components/SnapshotManagement/SnapshotManagementSelectors.js +0 -1
  5. data/webpack/components/SnapshotManagement/__tests__/SnapshotManagement.test.js +35 -0
  6. data/webpack/components/SnapshotManagement/__tests__/__snapshots__/SnapshotManagementReducer.test.js.snap +0 -3
  7. data/webpack/components/SnapshotManagement/components/SnapshotForm/SnapshotForm.js +33 -67
  8. data/webpack/components/SnapshotManagement/components/SnapshotForm/SnapshotFormField.js +62 -0
  9. data/webpack/components/SnapshotManagement/components/SnapshotForm/SnapshotFormHelper.js +64 -0
  10. data/webpack/components/SnapshotManagement/components/SnapshotForm/__tests__/SnapshotForm.test.js +72 -24
  11. data/webpack/components/SnapshotManagement/components/SnapshotFormModal/SnapshotFormModal.js +1 -0
  12. data/webpack/components/SnapshotManagement/components/SnapshotFormModal/__tests__/SnapshotFormModal.test.js +36 -9
  13. data/webpack/components/SnapshotManagement/components/SnapshotList/SnapshotList.js +185 -229
  14. data/webpack/components/SnapshotManagement/components/SnapshotList/SnapshotListHelper.js +55 -22
  15. data/webpack/components/SnapshotManagement/components/SnapshotList/__tests__/SnapshotList.test.js +73 -54
  16. data/webpack/components/SnapshotManagement/components/SnapshotList/snapshotList.scss +6 -0
  17. data/webpack/components/SnapshotManagement/index.js +0 -1
  18. data/webpack/components/SnapshotManagementCard/__tests__/SnapshotManagementCard.test.js +28 -17
  19. data/webpack/test_setup.js +0 -5
  20. metadata +6 -7
  21. data/webpack/components/SnapshotManagement/components/SnapshotForm/__tests__/__snapshots__/SnapshotForm.test.js.snap +0 -571
  22. data/webpack/components/SnapshotManagement/components/SnapshotFormModal/__tests__/__snapshots__/SnapshotFormModal.test.js.snap +0 -57
  23. data/webpack/components/SnapshotManagement/components/SnapshotList/__tests__/__snapshots__/SnapshotList.test.js.snap +0 -1081
  24. data/webpack/components/SnapshotManagementCard/__tests__/__snapshots__/SnapshotManagementCard.test.js.snap +0 -122
@@ -1,267 +1,223 @@
1
- import React, { Component } from 'react';
1
+ import React, { useState, useEffect } from 'react';
2
2
  import PropTypes from 'prop-types';
3
3
  import {
4
4
  Alert,
5
- // Button,
6
- Table as PfTable,
7
- FormControl,
8
- inlineEditFormatterFactory,
9
- } from 'patternfly-react';
5
+ EmptyState,
6
+ EmptyStateBody,
7
+ TextArea,
8
+ TextInput,
9
+ } from '@patternfly/react-core';
10
+ import { Table, Thead, Tbody, Tr, Th, Td } from '@patternfly/react-table';
10
11
  import { cloneDeep, findIndex } from 'lodash';
11
12
 
12
13
  import { translate as __ } from 'foremanReact/common/I18n';
13
- import {
14
- Table,
15
- TableBody,
16
- column,
17
- headerFormatterWithProps,
18
- cellFormatterWithProps,
19
- } from 'foremanReact/components/common/table';
20
14
  import ShortDateTime from 'foremanReact/components/common/dates/ShortDateTime';
21
- import AlertBody from 'foremanReact/components/common/Alert/AlertBody';
22
15
  import Loading from 'foremanReact/components/Loading/Loading';
23
16
  import 'foremanReact/redux/API';
24
17
 
25
18
  import { renderListEntryButtons } from './SnapshotListHelper';
26
19
  import './snapshotList.scss';
27
20
 
28
- class SnapshotList extends Component {
29
- constructor(props) {
30
- super(props);
31
- this.state = {
32
- columns: this.defineColumns(),
33
- editMode: false,
34
- rows: cloneDeep(props.snapshots),
35
- };
36
- }
37
-
38
- defineColumns() {
39
- const {
40
- canDelete,
41
- canRevert,
42
- canUpdate,
43
- host,
44
- deleteAction,
45
- updateAction,
46
- rollbackAction,
47
- } = this.props;
48
-
49
- const inlineEditController = {
50
- isEditing: ({ rowData }) => rowData.backup !== undefined,
51
- onActivate: ({ rowData }) => {
52
- const rows = cloneDeep(this.state.rows);
53
- const index = findIndex(rows, { id: rowData.id });
54
-
55
- rows[index].backup = cloneDeep(rows[index]);
56
-
57
- this.setState({ rows, editMode: true });
58
- },
59
- onConfirm: ({ rowData }) => {
60
- const rows = cloneDeep(this.state.rows);
61
- const index = findIndex(rows, { id: rowData.id });
62
-
63
- delete rows[index].backup;
64
-
65
- this.setState({ rows, editMode: false });
66
- updateAction(host, rowData);
67
- },
68
- onCancel: ({ rowData }) => {
69
- const rows = cloneDeep(this.state.rows);
70
- const index = findIndex(rows, { id: rowData.id });
71
-
72
- rows[index] = cloneDeep(rows[index].backup);
73
- delete rows[index].backup;
21
+ const SnapshotList = ({
22
+ host,
23
+ loadSnapshots,
24
+ deleteAction,
25
+ updateAction,
26
+ rollbackAction,
27
+ isLoading,
28
+ isWorking,
29
+ hasError,
30
+ error,
31
+ snapshots,
32
+ canDelete,
33
+ canRevert,
34
+ canUpdate,
35
+ capabilities,
36
+ }) => {
37
+ const [editMode, setEditMode] = useState(false);
38
+ const [rows, setRows] = useState(() => cloneDeep(snapshots));
74
39
 
75
- this.setState({ rows, editMode: false });
76
- },
77
- onChange: (value, { rowData, property }) => {
78
- const rows = cloneDeep(this.state.rows);
79
- const index = findIndex(rows, { id: rowData.id });
40
+ useEffect(() => {
41
+ loadSnapshots(host.id);
42
+ // eslint-disable-next-line react-hooks/exhaustive-deps
43
+ }, []);
80
44
 
81
- rows[index][property] = value;
45
+ useEffect(() => {
46
+ setRows(cloneDeep(snapshots));
47
+ }, [snapshots]);
82
48
 
83
- this.setState({ rows });
84
- },
85
- };
86
- this.inlineEditController = inlineEditController;
87
-
88
- const renderButtons = renderListEntryButtons(
89
- canDelete,
90
- canRevert,
91
- canUpdate,
92
- host,
93
- rollbackAction,
94
- deleteAction,
95
- inlineEditController
96
- );
97
- const inlineEditButtonCellFormatter = inlineEditFormatterFactory({
98
- isEditing: additionalData => this.state.editMode,
99
- renderValue: renderButtons(false),
100
- renderEdit: renderButtons(true),
49
+ const updateRow = (rowData, change) =>
50
+ setRows(currentRows => {
51
+ const newRows = cloneDeep(currentRows);
52
+ const index = findIndex(newRows, { id: rowData.id });
53
+ return change(newRows, index);
101
54
  });
102
55
 
103
- const inlineEditFormatter = inlineEditFormatterFactory({
104
- isEditing: additionalData => {
105
- if (
106
- additionalData.property === 'name' &&
107
- !this.props.capabilities.editableSnapshotName
108
- )
109
- return false;
110
- return inlineEditController.isEditing(additionalData);
111
- },
112
- renderValue: (value, additionalData) => {
113
- let date = '';
114
- if (
115
- additionalData.property === 'name' &&
116
- additionalData.rowData.formatted_created_at
117
- )
118
- date = (
119
- <span className="snapshot-date">
120
- <ShortDateTime
121
- date={new Date(additionalData.rowData.formatted_created_at)}
122
- defaultValue={__('N/A')}
123
- showRelativeTimeTooltip
124
- />
125
- </span>
126
- );
127
- return (
128
- <span className="static description">
129
- {value}
130
- <br />
131
- {date}
132
- </span>
133
- );
134
- },
135
- renderEdit: (value, additionalData) => {
136
- let type = 'input';
137
- if (additionalData.property === 'description') type = 'textarea';
138
- return (
139
- <FormControl
140
- type="text"
141
- defaultValue={value}
142
- onBlur={e =>
143
- inlineEditController.onChange(e.target.value, additionalData)
144
- }
145
- componentClass={type}
146
- />
147
- );
148
- },
149
- });
150
- const editCellFormatters = [cellFormatterWithProps];
151
- if (canUpdate) editCellFormatters.unshift(inlineEditFormatter);
56
+ const inlineEditController = {
57
+ isEditing: ({ rowData }) => rowData.backup !== undefined,
58
+ onActivate: ({ rowData }) => {
59
+ updateRow(rowData, (newRows, index) => {
60
+ newRows[index].backup = cloneDeep(newRows[index]);
61
+ return newRows;
62
+ });
63
+ setEditMode(true);
64
+ },
65
+ onConfirm: ({ rowData }) => {
66
+ const index = findIndex(rows, { id: rowData.id });
67
+ const updated = cloneDeep(rows[index]);
68
+ delete updated.backup;
152
69
 
153
- const columns = [
154
- column(
155
- 'name',
156
- __('Snapshot'),
157
- [headerFormatterWithProps],
158
- editCellFormatters
159
- ),
160
- column(
161
- 'description',
162
- __('Description'),
163
- [headerFormatterWithProps],
164
- editCellFormatters
165
- ),
166
- ];
167
- if (canDelete || canUpdate || canRevert)
168
- columns.push(
169
- column(
170
- '',
171
- __('Action'),
172
- [headerFormatterWithProps],
173
- [inlineEditButtonCellFormatter],
174
- { className: 'action-buttons' }
175
- )
176
- );
177
- return columns;
178
- }
70
+ updateRow(rowData, (newRows, i) => {
71
+ delete newRows[i].backup;
72
+ return newRows;
73
+ });
74
+ setEditMode(false);
75
+ updateAction(host, updated);
76
+ },
77
+ onCancel: ({ rowData }) => {
78
+ updateRow(rowData, (newRows, index) => {
79
+ newRows[index] = cloneDeep(newRows[index].backup);
80
+ delete newRows[index].backup;
81
+ return newRows;
82
+ });
83
+ setEditMode(false);
84
+ },
85
+ onChange: (value, { rowData, property }) => {
86
+ updateRow(rowData, (newRows, index) => {
87
+ newRows[index][property] = value;
88
+ return newRows;
89
+ });
90
+ },
91
+ };
179
92
 
180
- componentDidMount() {
181
- this.props.loadSnapshots(this.props.host.id);
182
- }
93
+ const getBodyMessage = () => {
94
+ if (isLoading || isWorking) return <Loading textSize="sm" />;
183
95
 
184
- // FIXME: Remove soon, as this is deprecated!
185
- componentWillReceiveProps(newProps) {
186
- if (newProps.snapshots !== this.props.snapshots) {
187
- this.setState({ rows: newProps.snapshots });
188
- }
189
- if (newProps.needsReload) {
190
- newProps.loadSnapshots(newProps.host.id);
191
- }
192
- }
193
-
194
- getBodyMessage() {
195
- if (this.props.isLoading || this.props.isWorking)
196
- return <Loading textSize="sm" />;
197
-
198
- if (this.props.hasError)
96
+ if (hasError)
199
97
  return (
200
98
  <Alert
201
99
  variant="danger"
202
100
  isInline
203
101
  title={__('Failed to load snapshot list')}
102
+ ouiaId="snapshot-list-error"
204
103
  >
205
- <AlertBody title={__('Failed to load snapshot list')}>
206
- {/* IMHO the line-break should be done by AlertBody :-( */}
207
- <br />
208
- {this.props.error.message}
209
- </AlertBody>
104
+ {error?.message}
210
105
  </Alert>
211
106
  );
212
107
 
213
108
  return undefined;
214
- }
109
+ };
215
110
 
216
- render() {
217
- const { columns } = this.state;
218
- const bodyMessage = this.getBodyMessage();
111
+ const renderCell = (rowData, property, isEditingRow) => {
112
+ const value = rowData[property];
113
+ const editable = property !== 'name' || capabilities.editableSnapshotName;
114
+
115
+ if (isEditingRow && editable) {
116
+ const Input = property === 'description' ? TextArea : TextInput;
117
+ return (
118
+ <Input
119
+ aria-label={property}
120
+ value={value || ''}
121
+ onChange={(_event, newValue) =>
122
+ inlineEditController.onChange(newValue, {
123
+ rowData,
124
+ property,
125
+ })
126
+ }
127
+ {...(property === 'description'
128
+ ? {}
129
+ : { ouiaId: `snapshot-${property}-input` })}
130
+ />
131
+ );
132
+ }
219
133
 
220
134
  return (
221
- <div>
222
- {/*
223
- <Button
224
- disabled={this.props.isLoading}
225
- onClick={() => this.props.loadSnapshots(this.props.host.id)}
226
- >
227
- ReloadData
228
- </Button>
229
- */}
230
- <Table
231
- caption="Snapshot List"
232
- columns={columns}
233
- rows={this.state.rows}
234
- bodyMessage={bodyMessage}
235
- inlineEdit
236
- components={{
237
- body: {
238
- row: PfTable.InlineEditRow,
239
- cell: cellProps => cellProps.children,
240
- },
241
- }}
242
- >
243
- <PfTable.Header key="header" />
244
- <TableBody
245
- key="body"
246
- columns={columns}
247
- rows={this.state.rows}
248
- message={bodyMessage}
249
- rowKey="id"
250
- onRow={(rowData, { rowIndex }) => ({
251
- role: 'row',
252
- isEditing: () => this.inlineEditController.isEditing({ rowData }),
253
- onCancel: () =>
254
- this.inlineEditController.onCancel({ rowData, rowIndex }),
255
- onConfirm: () =>
256
- this.inlineEditController.onConfirm({ rowData, rowIndex }),
257
- last: rowIndex === this.state.rows.length - 1,
258
- })}
259
- />
260
- </Table>
261
- </div>
135
+ <span className="static description">
136
+ {value}
137
+ <br />
138
+ {property === 'name' && rowData.formatted_created_at && (
139
+ <span className="snapshot-date">
140
+ <ShortDateTime
141
+ date={new Date(rowData.formatted_created_at)}
142
+ defaultValue={__('N/A')}
143
+ showRelativeTimeTooltip
144
+ />
145
+ </span>
146
+ )}
147
+ </span>
262
148
  );
263
- }
264
- }
149
+ };
150
+
151
+ const bodyMessage = getBodyMessage();
152
+ const hasActions = canDelete || canUpdate || canRevert;
153
+ const renderButtons = renderListEntryButtons(
154
+ canDelete,
155
+ canRevert,
156
+ canUpdate,
157
+ host,
158
+ rollbackAction,
159
+ deleteAction,
160
+ inlineEditController
161
+ );
162
+ const columnCount = hasActions ? 3 : 2;
163
+
164
+ return (
165
+ <div>
166
+ <Table
167
+ aria-label={__('Snapshot List')}
168
+ variant="compact"
169
+ ouiaId="snapshot-list-table"
170
+ >
171
+ <Thead>
172
+ <Tr ouiaId="snapshot-list-row-head">
173
+ <Th>{__('Snapshot')}</Th>
174
+ <Th>{__('Description')}</Th>
175
+ {hasActions && <Th>{__('Action')}</Th>}
176
+ </Tr>
177
+ </Thead>
178
+ <Tbody>
179
+ {bodyMessage && (
180
+ <Tr ouiaId="snapshot-list-rows">
181
+ <Td colSpan={columnCount}>{bodyMessage}</Td>
182
+ </Tr>
183
+ )}
184
+ {!bodyMessage && rows.length === 0 && (
185
+ <Tr ouiaId="snapshot-list-row-empty">
186
+ <Td colSpan={columnCount}>
187
+ <EmptyState>
188
+ <EmptyStateBody>{__('No snapshots found')}</EmptyStateBody>
189
+ </EmptyState>
190
+ </Td>
191
+ </Tr>
192
+ )}
193
+ {!bodyMessage &&
194
+ rows.map(rowData => {
195
+ const isEditingRow = inlineEditController.isEditing({ rowData });
196
+ return (
197
+ <Tr key={rowData.id} ouiaId={`snapshot-row-${rowData.id}`}>
198
+ <Td dataLabel={__('Snapshot')}>
199
+ {renderCell(rowData, 'name', isEditingRow)}
200
+ </Td>
201
+ <Td dataLabel={__('Description')}>
202
+ {renderCell(rowData, 'description', isEditingRow)}
203
+ </Td>
204
+ {hasActions && (
205
+ <Td
206
+ dataLabel={__('Action')}
207
+ className="action-buttons"
208
+ isActionCell
209
+ >
210
+ {renderButtons(rowData, isEditingRow, editMode)}
211
+ </Td>
212
+ )}
213
+ </Tr>
214
+ );
215
+ })}
216
+ </Tbody>
217
+ </Table>
218
+ </div>
219
+ );
220
+ };
265
221
 
266
222
  SnapshotList.propTypes = {
267
223
  /*
@@ -1,5 +1,12 @@
1
1
  import React from 'react';
2
- import { Button, Icon } from 'patternfly-react';
2
+ import { Button } from '@patternfly/react-core';
3
+ import {
4
+ PencilAltIcon,
5
+ HistoryIcon,
6
+ TrashIcon,
7
+ CheckIcon,
8
+ TimesIcon,
9
+ } from '@patternfly/react-icons';
3
10
 
4
11
  import { sprintf, translate as __ } from 'foremanReact/common/I18n';
5
12
 
@@ -13,58 +20,84 @@ export const renderListEntryButtons = (
13
20
  snapshotRollbackAction,
14
21
  snapshotDeleteAction,
15
22
  inlineEditController
16
- ) => disabled => (value, additionalData) => {
23
+ ) => (rowData, isEditingRow, disabled) => {
24
+ if (isEditingRow) {
25
+ return (
26
+ <div className="snapshot-action-buttons">
27
+ <Button
28
+ key="confirm-button"
29
+ variant="plain"
30
+ aria-label={__('Confirm')}
31
+ onClick={() => inlineEditController.onConfirm({ rowData })}
32
+ ouiaId={`snapshot-confirm-button-${rowData.id}`}
33
+ >
34
+ <CheckIcon />
35
+ </Button>
36
+ <Button
37
+ key="cancel-button"
38
+ variant="plain"
39
+ aria-label={__('Cancel')}
40
+ onClick={() => inlineEditController.onCancel({ rowData })}
41
+ ouiaId={`snapshot-cancel-edit-button-${rowData.id}`}
42
+ >
43
+ <TimesIcon />
44
+ </Button>
45
+ </div>
46
+ );
47
+ }
48
+
17
49
  const buttons = [];
18
50
 
19
- // Edit Button
20
51
  if (canUpdate) {
21
52
  buttons.push(
22
53
  <Button
23
54
  key="edit-button"
24
- bsStyle="default"
25
- disabled={disabled}
26
- onClick={() => inlineEditController.onActivate(additionalData)}
55
+ variant="plain"
56
+ isDisabled={disabled}
57
+ aria-label={__('edit entry')}
58
+ onClick={() => inlineEditController.onActivate({ rowData })}
59
+ ouiaId={`snapshot-edit-button-${rowData.id}`}
27
60
  >
28
- <Icon type="pf" name="edit" title={__('edit entry')} />
61
+ <PencilAltIcon />
29
62
  </Button>
30
63
  );
31
64
  }
32
65
 
33
- // Rollback button
34
66
  if (canRevert) {
35
67
  buttons.push(
36
68
  <Button
37
69
  key="rollback-button"
38
- bsStyle="default"
39
- disabled={disabled}
70
+ variant="plain"
71
+ isDisabled={disabled}
72
+ aria-label={__('Rollback')}
40
73
  onClick={() =>
41
- window.confirm(
42
- sprintf(__('Rollback to "%s"?'), additionalData.rowData.name)
43
- ) && snapshotRollbackAction(host, additionalData.rowData)
74
+ window.confirm(sprintf(__('Rollback to "%s"?'), rowData.name)) &&
75
+ snapshotRollbackAction(host, rowData)
44
76
  }
77
+ ouiaId={`snapshot-rollback-button-${rowData.id}`}
45
78
  >
46
- <Icon type="pf" name="history" title={__('Rollback')} />
79
+ <HistoryIcon />
47
80
  </Button>
48
81
  );
49
82
  }
50
83
 
51
- // Delete Button
52
84
  if (canDelete) {
53
85
  buttons.push(
54
86
  <Button
55
87
  key="delete-button"
56
- bsStyle="default"
57
- disabled={disabled}
88
+ variant="plain"
89
+ isDisabled={disabled}
90
+ aria-label={__('Delete')}
58
91
  onClick={() =>
59
- window.confirm(
60
- sprintf(__('Delete Snapshot "%s"?'), additionalData.rowData.name)
61
- ) && snapshotDeleteAction(host, additionalData.rowData)
92
+ window.confirm(sprintf(__('Delete Snapshot "%s"?'), rowData.name)) &&
93
+ snapshotDeleteAction(host, rowData)
62
94
  }
95
+ ouiaId={`snapshot-delete-button-${rowData.id}`}
63
96
  >
64
- <Icon type="pf" name="delete" title={__('Delete')} />
97
+ <TrashIcon />
65
98
  </Button>
66
99
  );
67
100
  }
68
101
 
69
- return <td className="action-buttons">{buttons}</td>;
102
+ return <div className="snapshot-action-buttons">{buttons}</div>;
70
103
  };