Skip to content

Commit 091fe65

Browse files
authoredApr 14, 2023
Select collection screen - multi select, repository field, repo filter (#3581)
* add repo col and filter * add multi select in select collection screen * add source repo name to add alert * fix alert plural translation Issue: AAH-2274
1 parent 6d869ee commit 091fe65

File tree

4 files changed

+89
-23
lines changed

4 files changed

+89
-23
lines changed
 

‎CHANGES/2274.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improved select collection screen: added repository field, multi select and repository filter

‎src/actions/ansible-repository-collection-version-add.tsx

+54-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { t } from '@lingui/macro';
2-
import { Button, Modal, Radio } from '@patternfly/react-core';
1+
import { plural, t } from '@lingui/macro';
2+
import { Button, Checkbox, Modal } from '@patternfly/react-core';
33
import React, { useState } from 'react';
44
import {
55
AnsibleRepositoryAPI,
@@ -8,29 +8,44 @@ import {
88
} from 'src/api';
99
import { AlertList, AlertType, DetailList, closeAlert } from 'src/components';
1010
import { canEditAnsibleRepository } from 'src/permissions';
11-
import { handleHttpError, parsePulpIDFromURL, taskAlert } from 'src/utilities';
11+
import {
12+
RepositoriesUtils,
13+
handleHttpError,
14+
parsePulpIDFromURL,
15+
taskAlert,
16+
} from 'src/utilities';
1217
import { Action } from './action';
1318

1419
const add = (
1520
{ repositoryHref, repositoryName },
16-
{ namespace, name, version, pulp_href: collectionVersionHref },
21+
collections,
1722
{ addAlert, setState, query },
1823
) => {
1924
const pulpId = parsePulpIDFromURL(repositoryHref);
20-
return AnsibleRepositoryAPI.addContent(pulpId, collectionVersionHref)
25+
const collectionVersionHrefs = collections.map(
26+
(c) => c.collection_version.pulp_href,
27+
);
28+
return AnsibleRepositoryAPI.addContent(pulpId, collectionVersionHrefs)
2129
.then(({ data }) => {
22-
addAlert(
23-
taskAlert(
24-
data.task,
25-
t`Started adding ${namespace}.${name} v${version} to repository "${repositoryName}".`,
26-
),
30+
collections.map(
31+
({ collection_version: { name, namespace, version }, repository }) => {
32+
addAlert(
33+
taskAlert(
34+
data.task,
35+
t`Started adding ${namespace}.${name} v${version} from "${repository.name}" to repository "${repositoryName}".`,
36+
),
37+
);
38+
setState((ms) => ({ ...ms, addCollectionVersionModal: null }));
39+
query({});
40+
},
2741
);
28-
setState((ms) => ({ ...ms, addCollectionVersionModal: null }));
29-
query({});
3042
})
3143
.catch(
3244
handleHttpError(
33-
t`Failed to add ${namespace}.${name} v${version} to repository "${repositoryName}".`,
45+
plural(collections.length, {
46+
one: `Failed to add collection to repository "${repositoryName}".`,
47+
other: `Failed to add collections to repository "${repositoryName}".`,
48+
}),
3449
() => setState((ms) => ({ ...ms, addCollectionVersionModal: null })),
3550
addAlert,
3651
),
@@ -45,7 +60,7 @@ const AddCollectionVersionModal = ({
4560
closeAction: () => void;
4661
}) => {
4762
const [alerts, setAlerts] = useState([]);
48-
const [selected, setSelected] = useState(null);
63+
const [selected, setSelected] = useState<CollectionVersionSearch[]>([]);
4964

5065
const addAlert = (alert: AlertType) => {
5166
setAlerts([...alerts, alert]);
@@ -75,22 +90,31 @@ const AddCollectionVersionModal = ({
7590
const renderTableRow = (item: CollectionVersionSearch, index: number) => {
7691
const {
7792
collection_version: { name, namespace, version, description },
93+
repository,
7894
} = item;
7995

8096
return (
81-
<tr onClick={() => setSelected(item)} key={index}>
97+
<tr
98+
onClick={() =>
99+
setSelected(
100+
RepositoriesUtils.pushToOrFilterOutCollections(item, selected),
101+
)
102+
}
103+
key={index}
104+
>
82105
<td>
83-
<Radio
106+
<Checkbox
84107
aria-label={`${namespace}.${name} v${version}`}
85108
id={`collection-${index}`}
86-
isChecked={selected === item}
109+
isChecked={selected.includes(item)}
87110
name={`collection-${index}`}
88111
/>
89112
</td>
90113
<td>
91114
{namespace}.{name} v{version}
92115
</td>
93116
<td>{description}</td>
117+
<td>{repository.name}</td>
94118
</tr>
95119
);
96120
};
@@ -138,6 +162,10 @@ const AddCollectionVersionModal = ({
138162
id: 'namespace',
139163
title: t`Namespace`,
140164
},
165+
{
166+
id: 'repository_name',
167+
title: t`Repository`,
168+
},
141169
]}
142170
noDataDescription={t`Collection versions will appear once a collection is uploaded.`}
143171
noDataTitle={t`No collection versions yet`}
@@ -159,6 +187,11 @@ const AddCollectionVersionModal = ({
159187
type: 'none',
160188
id: 'col2',
161189
},
190+
{
191+
title: t`Repository`,
192+
type: 'none',
193+
id: 'col3',
194+
},
162195
]}
163196
title={t`Collection versions`}
164197
/>
@@ -178,13 +211,13 @@ export const ansibleRepositoryCollectionVersionAddAction = Action({
178211
modal: ({ addAlert, state, setState, query }) =>
179212
state.addCollectionVersionModal ? (
180213
<AddCollectionVersionModal
181-
addAction={(collection) =>
182-
add(state.addCollectionVersionModal, collection.collection_version, {
214+
addAction={(collections: CollectionVersionSearch[]) => {
215+
add(state.addCollectionVersionModal, collections, {
183216
addAlert,
184217
setState,
185218
query,
186-
})
187-
}
219+
});
220+
}}
188221
closeAction={() =>
189222
setState((ms) => ({ ...ms, addCollectionVersionModal: null }))
190223
}

‎src/api/ansible-repository.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ class API extends PulpAPI {
2222
});
2323
}
2424

25-
addContent(id, collection_version_href) {
25+
addContent(id, collection_version_hrefs) {
2626
return this.http.post(this.apiPath + id + '/modify/', {
27-
add_content_units: [collection_version_href],
27+
add_content_units: collection_version_hrefs,
2828
});
2929
}
3030

‎src/utilities/repositories.ts

+32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { t } from '@lingui/macro';
22
import { Repositories } from 'src/api/repositories';
3+
import { CollectionVersionSearch } from 'src/api/response-types/collection';
34
import { Repository } from 'src/api/response-types/repositories';
45
import { waitForTaskUrl } from 'src/utilities';
56
import { parsePulpIDFromURL } from 'src/utilities/parse-pulp-id';
@@ -82,4 +83,35 @@ export class RepositoriesUtils {
8283
true,
8384
);
8485
}
86+
87+
public static pushToOrFilterOutCollections(
88+
selectedCollection: CollectionVersionSearch,
89+
collections: CollectionVersionSearch[],
90+
): CollectionVersionSearch[] {
91+
// check if collection is already selected
92+
const selectedItem = collections.find(
93+
({ collection_version: { name, namespace, version }, repository }) =>
94+
name === selectedCollection.collection_version.name &&
95+
namespace === selectedCollection.collection_version.namespace &&
96+
version === selectedCollection.collection_version.version &&
97+
repository.name === selectedCollection.repository.name,
98+
);
99+
100+
// if collection is not selected, add it to selected items
101+
if (!selectedItem) {
102+
return [...collections, selectedCollection];
103+
}
104+
105+
// unselect collection
106+
return collections.filter(
107+
({ collection_version, repository }) =>
108+
collection_version.name !==
109+
selectedCollection.collection_version.name ||
110+
collection_version.namespace !==
111+
selectedCollection.collection_version.namespace ||
112+
collection_version.version !==
113+
selectedCollection.collection_version.version ||
114+
repository.name !== selectedCollection.repository.name,
115+
);
116+
}
85117
}

0 commit comments

Comments
 (0)
Please sign in to comment.