Skip to content

Commit 5ede5e8

Browse files
committed
UIFC-437-use-generic-hooks undo more for specific requests
1 parent c29dd45 commit 5ede5e8

2 files changed

Lines changed: 30 additions & 32 deletions

File tree

src/components/MetadataSources/SourceManagement/SourceManagementView.js

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,54 @@
11
import PropTypes from 'prop-types';
2-
import {
3-
get,
4-
isEmpty,
5-
} from 'lodash';
2+
import { get } from 'lodash';
63
import { withRouter, Link } from 'react-router-dom';
74
import { FormattedMessage } from 'react-intl';
5+
import { useQuery } from 'react-query';
86

9-
import { useOkapiKyQuery } from '@folio/stripes-leipzig-components';
107
import {
118
Button,
129
Col,
1310
KeyValue,
1411
NoValue,
1512
Row,
1613
} from '@folio/stripes/components';
14+
import { useOkapiKy } from '@folio/stripes/core';
1715

18-
import {
19-
API_ORGANIZATIONS,
20-
QK_ORGANIZATIONS,
21-
} from '../../../util/constants';
16+
import { API_ORGANIZATION_BY_ID } from '../../../util/constants';
2217
import urls from '../../DisplayUtils/urls';
2318
import SelectAllCollections from './SelectAllCollections';
2419

2520
const SourceManagementView = ({
2621
metadataSource,
27-
organizationId,
2822
stripes,
2923
}) => {
30-
let orgValue;
3124
const sourceId = get(metadataSource, 'id', '-');
32-
const sourcesOrganization = get(metadataSource, 'organization', <NoValue />);
25+
const organization = get(metadataSource, 'organization', <NoValue />);
3326

34-
const { data: organization, isLoading: isLoadingOrganization, isError } = useOkapiKyQuery({
35-
queryKey: [QK_ORGANIZATIONS, organizationId],
36-
id: organizationId,
37-
api: API_ORGANIZATIONS,
38-
});
27+
const useOrganization = () => {
28+
const ky = useOkapiKy();
3929

40-
if (!isEmpty(organizationId) && !isLoadingOrganization) {
41-
if (isError) {
42-
if (sourcesOrganization.name) {
43-
orgValue = sourcesOrganization.name;
44-
} else {
45-
orgValue = <NoValue />;
46-
}
47-
} else {
48-
orgValue = (
49-
<Link to={{ pathname: `${urls.organizationView(organization.id)}` }}>
50-
{sourcesOrganization.name}
51-
</Link>
52-
);
53-
}
30+
const { isError } = useQuery(
31+
[organization?.id],
32+
() => ky.get(API_ORGANIZATION_BY_ID(organization?.id)).json(),
33+
// The query will not execute until the id exists
34+
{ enabled: Boolean(organization?.id) }
35+
);
36+
return ({ isError });
37+
};
38+
39+
const { isError } = useOrganization();
40+
41+
let orgValue;
42+
if (!organization?.name) {
43+
orgValue = <NoValue />;
44+
} else if (isError) {
45+
orgValue = organization.name;
46+
} else {
47+
orgValue = (
48+
<Link to={{ pathname: `${urls.organizationView(organization.id)}` }}>
49+
{organization.name}
50+
</Link>
51+
);
5452
}
5553

5654
return (
@@ -107,7 +105,6 @@ const SourceManagementView = ({
107105

108106
SourceManagementView.propTypes = {
109107
metadataSource: PropTypes.object,
110-
organizationId: PropTypes.string,
111108
stripes: PropTypes.object,
112109
};
113110

src/util/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export const API_COLLECTIONS_SELECT_ALL_BY_SOURCE_ID = (sourceId) => `finc-selec
44
export const API_EZB_CREDENTIALS = 'finc-select/ezb-credentials';
55
export const API_FILTERS = 'finc-select/filters';
66
export const API_ORGANIZATIONS = 'organizations-storage/organizations';
7+
export const API_ORGANIZATION_BY_ID = (organizationId) => `organizations-storage/organizations/${organizationId}`;
78
export const API_SOURCES = 'finc-select/metadata-sources';
89
export const API_TINY_SOURCES = 'finc-config/tiny-metadata-sources';
910

0 commit comments

Comments
 (0)