|
1 | 1 | import PropTypes from 'prop-types'; |
2 | | -import { |
3 | | - get, |
4 | | - isEmpty, |
5 | | -} from 'lodash'; |
| 2 | +import { get } from 'lodash'; |
6 | 3 | import { withRouter, Link } from 'react-router-dom'; |
7 | 4 | import { FormattedMessage } from 'react-intl'; |
| 5 | +import { useQuery } from 'react-query'; |
8 | 6 |
|
9 | | -import { useOkapiKyQuery } from '@folio/stripes-leipzig-components'; |
10 | 7 | import { |
11 | 8 | Button, |
12 | 9 | Col, |
13 | 10 | KeyValue, |
14 | 11 | NoValue, |
15 | 12 | Row, |
16 | 13 | } from '@folio/stripes/components'; |
| 14 | +import { useOkapiKy } from '@folio/stripes/core'; |
17 | 15 |
|
18 | | -import { |
19 | | - API_ORGANIZATIONS, |
20 | | - QK_ORGANIZATIONS, |
21 | | -} from '../../../util/constants'; |
| 16 | +import { API_ORGANIZATION_BY_ID } from '../../../util/constants'; |
22 | 17 | import urls from '../../DisplayUtils/urls'; |
23 | 18 | import SelectAllCollections from './SelectAllCollections'; |
24 | 19 |
|
25 | 20 | const SourceManagementView = ({ |
26 | 21 | metadataSource, |
27 | | - organizationId, |
28 | 22 | stripes, |
29 | 23 | }) => { |
30 | | - let orgValue; |
31 | 24 | const sourceId = get(metadataSource, 'id', '-'); |
32 | | - const sourcesOrganization = get(metadataSource, 'organization', <NoValue />); |
| 25 | + const organization = get(metadataSource, 'organization', <NoValue />); |
33 | 26 |
|
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(); |
39 | 29 |
|
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 | + ); |
54 | 52 | } |
55 | 53 |
|
56 | 54 | return ( |
@@ -107,7 +105,6 @@ const SourceManagementView = ({ |
107 | 105 |
|
108 | 106 | SourceManagementView.propTypes = { |
109 | 107 | metadataSource: PropTypes.object, |
110 | | - organizationId: PropTypes.string, |
111 | 108 | stripes: PropTypes.object, |
112 | 109 | }; |
113 | 110 |
|
|
0 commit comments