Skip to content

Commit f22546e

Browse files
ERM-2533: Refactor interfaces code to react-query (#1174)
* feat: Interfaces (!WIP!) Initial steps to moving interfaces over to react-query, currently not functional -- DO NOT MERGE AS IS refs ERM-2533 * feat: Interfaces Wired up interfaces to new hooks, avoiding stripes-connect ERM-2533 * test: Tests Fixed tests ERM-2533
1 parent 4be434b commit f22546e

File tree

6 files changed

+14
-376
lines changed

6 files changed

+14
-376
lines changed

src/components/AgreementSections/Organizations/Organizations.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@ export default class Organizations extends React.Component {
3535
}),
3636
),
3737
}).isRequired,
38-
handlers: PropTypes.shape({
39-
onFetchCredentials: PropTypes.func,
40-
}),
4138
id: PropTypes.string,
4239
};
4340

@@ -52,7 +49,6 @@ export default class Organizations extends React.Component {
5249
<ViewOrganizationCard
5350
key={`${org.orgsUuid}`}
5451
data-test-organizations-org
55-
fetchCredentials={this.props.handlers.onFetchCredentials}
5652
headerStart={
5753
<span>
5854
<AppIcon app="organizations" size="small">
@@ -66,6 +62,7 @@ export default class Organizations extends React.Component {
6662
}
6763
interfaces={interfaces}
6864
note={note}
65+
org={org}
6966
roles={roles}
7067
/>
7168
);

src/components/AgreementSections/Organizations/Organizations.test.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,6 @@ const agreement = {
162162
]
163163
};
164164

165-
const handlers = {
166-
onFetchCredentials: () => { }
167-
};
168-
169165
let renderComponent;
170166

171167
describe('Organizations', () => {
@@ -174,7 +170,6 @@ describe('Organizations', () => {
174170
renderComponent = renderWithIntl(
175171
<Organizations
176172
agreement={agreement}
177-
handlers={handlers}
178173
id="organizations"
179174
/>,
180175
translationsProperties
@@ -196,7 +191,6 @@ describe('Organizations', () => {
196191
renderComponent = renderWithIntl(
197192
<Organizations
198193
agreement={{}}
199-
handlers={handlers}
200194
id="organizations"
201195
/>,
202196
translationsProperties

src/routes/AgreementLineCreateRoute/AgreementLineCreateRoute.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,6 @@ AgreementLineCreateRoute.propTypes = {
125125
agreementId: PropTypes.string.isRequired,
126126
}).isRequired
127127
}).isRequired,
128-
resources: PropTypes.shape({
129-
basket: PropTypes.arrayOf(PropTypes.object),
130-
}).isRequired,
131-
stripes: PropTypes.shape({
132-
hasInterface: PropTypes.func.isRequired,
133-
hasPerm: PropTypes.func.isRequired,
134-
}).isRequired,
135128
};
136129

137130
export default stripesConnect(AgreementLineCreateRoute);

src/routes/AgreementViewRoute/AgreementViewRoute.js

Lines changed: 13 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,27 @@ import PropTypes from 'prop-types';
44
import { useMutation, useQuery, useQueryClient } from 'react-query';
55
import { FormattedMessage, useIntl } from 'react-intl';
66

7-
import { get, flatten, uniqBy } from 'lodash';
7+
import { flatten } from 'lodash';
88

9-
import { CalloutContext, stripesConnect, useOkapiKy } from '@folio/stripes/core';
10-
import { useAgreement, useInfiniteFetch, useUsers } from '@folio/stripes-erm-components';
9+
import { CalloutContext, useOkapiKy } from '@folio/stripes/core';
10+
import { useInfiniteFetch, useInterfaces, useUsers } from '@folio/stripes-erm-components';
1111

1212
import { generateKiwtQueryParams } from '@k-int/stripes-kint-components';
1313

1414
import View from '../../components/views/Agreement';
1515
import { parseMclPageSize, urls } from '../../components/utilities';
16-
import { errorTypes, resultCount } from '../../constants';
16+
import { errorTypes } from '../../constants';
1717

1818
import { joinRelatedAgreements } from '../utilities/processRelatedAgreements';
1919

2020
import { useAgreementsHelperApp, useAgreementsSettings, useChunkedOrderLines } from '../../hooks';
2121
import { AGREEMENT_ENDPOINT, AGREEMENT_ERESOURCES_ENDPOINT, AGREEMENT_LINES_ENDPOINT } from '../../constants/endpoints';
2222

23-
const { RECORDS_PER_REQUEST_MEDIUM } = resultCount;
24-
25-
const credentialsArray = [];
2623
const AgreementViewRoute = ({
2724
handlers = {},
2825
history,
2926
location,
3027
match: { params: { id: agreementId } },
31-
mutator,
32-
resources,
3328
}) => {
3429
const queryClient = useQueryClient();
3530

@@ -52,6 +47,11 @@ const AgreementViewRoute = ({
5247

5348
const { agreement, isAgreementLoading } = useAgreement({ agreementId });
5449

50+
51+
const interfaces = useInterfaces({
52+
interfaceIds: flatten((agreement?.orgs ?? []).map(o => o?.org?.orgsUuid_object?.interfaces ?? []))
53+
}) ?? [];
54+
5555
const { mutateAsync: deleteAgreement } = useMutation(
5656
[agreementPath, 'ui-agreements', 'AgreementViewRoute', 'deleteAgreement'],
5757
() => ky.delete(agreementPath).then(() => queryClient.invalidateQueries(['ERM', 'Agreements']))
@@ -200,32 +200,18 @@ const AgreementViewRoute = ({
200200
}
201201
);
202202

203-
const getRecord = (id, resourceType) => {
204-
return get(resources, `${resourceType}.records`, [])
205-
.find(i => i.id === id);
206-
};
207-
208203
const getCompositeAgreement = () => {
209204
const contacts = agreement.contacts.map(c => ({
210205
...c,
211206
user: users?.find(user => user?.id === c.user) || c.user,
212207
}));
213208

214-
const interfacesCredentials = uniqBy(get(resources, 'interfacesCredentials.records', []), 'id');
215-
216-
if (interfacesCredentials[0]) {
217-
const index = credentialsArray.findIndex(object => object.id === interfacesCredentials[0].id);
218-
if (index === -1) {
219-
credentialsArray.push(interfacesCredentials[0]);
220-
}
221-
}
222-
223209
const orgs = agreement.orgs.map(o => ({
224210
...o,
225-
interfaces: get(o, 'org.orgsUuid_object.interfaces', [])
211+
interfaces: (o?.org?.orgsUuid_object?.interfaces ?? [])
226212
.map(id => ({
227-
...getRecord(id, 'interfaces') || {},
228-
credentials: credentialsArray.find(cred => cred.interfaceId === id)
213+
...(interfaces?.find(int => int?.id === id) ?? {}),
214+
/* Credentials are now handled by ViewOrganizationCard directly */
229215
})),
230216
}));
231217

@@ -281,10 +267,6 @@ const AgreementViewRoute = ({
281267
history.push(`${urls.agreementEdit(agreementId)}${location.search}`);
282268
};
283269

284-
const handleFetchCredentials = (id) => {
285-
mutator.interfaceRecord.replace({ id });
286-
};
287-
288270
const handleViewAgreementLine = (lineId) => {
289271
history.push(`${urls.agreementLineView(agreementId, lineId)}${location.search}`);
290272
};
@@ -319,7 +301,6 @@ const AgreementViewRoute = ({
319301
onExportAgreement: exportAgreement,
320302
onExportEResourcesAsJSON: exportEresourcesAsJson,
321303
onExportEResourcesAsKBART: exportEresourcesAsKBART,
322-
onFetchCredentials: handleFetchCredentials,
323304
onFilterEResources: handleFilterEResources,
324305
onNeedMoreEResources: (_askAmount, index) => fetchNextEresourcePage({ pageParam: index }),
325306
onNeedMoreLines: (_askAmount, index) => fetchNextLinesPage({ pageParam: index }),
@@ -331,36 +312,6 @@ const AgreementViewRoute = ({
331312
);
332313
};
333314

334-
AgreementViewRoute.manifest = Object.freeze({
335-
interfaces: { // We can and shouold migrate these to react-query at some point as a separate task
336-
type: 'okapi',
337-
path: 'organizations-storage/interfaces',
338-
perRequest: RECORDS_PER_REQUEST_MEDIUM,
339-
params: (_q, _p, _r, _l, props) => {
340-
const orgs = get(props.resources, 'agreement.records[0].orgs', []);
341-
const interfaces = flatten(orgs.map(o => get(o, 'org.orgsUuid_object.interfaces', [])));
342-
const query = [
343-
...new Set(interfaces.map(i => `id==${i}`))
344-
].join(' or ');
345-
346-
return query ? { query } : {};
347-
},
348-
fetch: props => !!props.stripes.hasInterface('organizations-storage.interfaces', '2.0'),
349-
permissionsRequired: 'organizations-storage.interfaces.collection.get',
350-
records: 'interfaces',
351-
},
352-
interfacesCredentials: {
353-
clientGeneratePk: false,
354-
throwErrors: false,
355-
path: 'organizations-storage/interfaces/%{interfaceRecord.id}/credentials',
356-
type: 'okapi',
357-
pk: 'FAKE_PK', // it's done to fool stripes-connect not to add cred id to the path's end.
358-
permissionsRequired: 'organizations-storage.interfaces.credentials.item.get',
359-
fetch: props => !!props.stripes.hasInterface('organizations-storage.interfaces', '1.0 2.0'),
360-
},
361-
interfaceRecord: {},
362-
});
363-
364315
AgreementViewRoute.propTypes = {
365316
handlers: PropTypes.object,
366317
history: PropTypes.shape({
@@ -374,19 +325,6 @@ AgreementViewRoute.propTypes = {
374325
id: PropTypes.string.isRequired,
375326
}).isRequired
376327
}).isRequired,
377-
mutator: PropTypes.shape({
378-
interfaceRecord: PropTypes.shape({
379-
replace: PropTypes.func,
380-
}),
381-
}),
382-
resources: PropTypes.shape({
383-
interfaces: PropTypes.object,
384-
query: PropTypes.object,
385-
}).isRequired,
386-
stripes: PropTypes.shape({
387-
hasInterface: PropTypes.func.isRequired,
388-
hasPerm: PropTypes.func.isRequired,
389-
}).isRequired,
390328
};
391329

392-
export default stripesConnect(AgreementViewRoute);
330+
export default AgreementViewRoute;

src/routes/AgreementViewRoute/AgreementViewRoute.test.js

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,8 @@ import { Button } from '@folio/stripes/components';
1010
import { Button as ButtonInteractor } from '@folio/stripes-testing';
1111
import {
1212
agreement,
13-
agreementLines,
14-
agreementEresources,
15-
eresourcesFilterPath,
16-
interfaces,
17-
orderLines,
18-
query,
19-
settings,
20-
users,
21-
tagsEnabled,
2213
location,
23-
handlers,
2414
match,
25-
intl
2615
} from './testResources';
2716
import translationsProperties from '../../../test/helpers';
2817
import AgreementViewRoute from './AgreementViewRoute';
@@ -39,10 +28,6 @@ const FilterEResourceButton = (props) => {
3928
return <Button onClick={props.handlers.onFilterEResources}>FilterEResourceButton</Button>;
4029
};
4130

42-
const FetchCredentialsButton = (props) => {
43-
return <Button onClick={props.handlers.onFetchCredentials}>FetchCredentialsButton</Button>;
44-
};
45-
4631
const EditButton = (props) => {
4732
return <Button onClick={props.handlers.onEdit}>EditButton</Button>;
4833
};
@@ -89,12 +74,6 @@ FilterEResourceButton.propTypes = {
8974
}),
9075
};
9176

92-
FetchCredentialsButton.propTypes = {
93-
handlers: PropTypes.shape({
94-
onFetchCredentials: PropTypes.func,
95-
}),
96-
};
97-
9877
EditButton.propTypes = {
9978
handlers: PropTypes.shape({
10079
onEdit: PropTypes.func,
@@ -138,7 +117,6 @@ CloneButton.propTypes = {
138117
};
139118

140119
const historyPushMock = jest.fn();
141-
const mutatorFetchReplaceMock = jest.fn();
142120

143121
jest.mock('@folio/stripes-erm-components', () => ({
144122
...jest.requireActual('@folio/stripes-erm-components'),
@@ -152,7 +130,6 @@ jest.mock('../../components/views/Agreement', () => {
152130
<AgreementLineButton {...props} />
153131
<NeedMoreLinesButton {...props} />
154132
<FilterEResourceButton {...props} />
155-
<FetchCredentialsButton {...props} />
156133
<EditButton {...props} />
157134
<NeedMoreEResourcesButton {...props} />
158135
<ToggleTagsButton {...props} />
@@ -165,30 +142,11 @@ jest.mock('../../components/views/Agreement', () => {
165142
});
166143

167144
const data = {
168-
handlers,
169145
history:{
170146
push: historyPushMock,
171147
},
172-
intl,
173148
location,
174149
match,
175-
mutator:{
176-
interfaceRecord:{
177-
replace: mutatorFetchReplaceMock,
178-
},
179-
},
180-
resources: {
181-
agreement,
182-
agreementLines,
183-
agreementEresources,
184-
eresourcesFilterPath,
185-
interfaces,
186-
orderLines,
187-
query,
188-
settings,
189-
users,
190-
},
191-
tagsEnabled
192150
};
193151

194152
useQuery.mockImplementation(() => ({ data: agreement, isLoading: false }));
@@ -230,11 +188,6 @@ describe('AgreementViewRoute', () => {
230188
expect(historyPushMock).toHaveBeenCalled();
231189
});
232190

233-
test('triggers the FetchCredentialsButton callback', async () => {
234-
await ButtonInteractor('FetchCredentialsButton').click();
235-
expect(mutatorFetchReplaceMock).toHaveBeenCalled();
236-
});
237-
238191
test('triggers the CloseButton callback', async () => {
239192
await ButtonInteractor('CloseButton').click();
240193
expect(historyPushMock).toHaveBeenCalled();

0 commit comments

Comments
 (0)