@@ -4,32 +4,27 @@ import PropTypes from 'prop-types';
44import { useMutation , useQuery , useQueryClient } from 'react-query' ;
55import { 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
1212import { generateKiwtQueryParams } from '@k-int/stripes-kint-components' ;
1313
1414import View from '../../components/views/Agreement' ;
1515import { parseMclPageSize , urls } from '../../components/utilities' ;
16- import { errorTypes , resultCount } from '../../constants' ;
16+ import { errorTypes } from '../../constants' ;
1717
1818import { joinRelatedAgreements } from '../utilities/processRelatedAgreements' ;
1919
2020import { useAgreementsHelperApp , useAgreementsSettings , useChunkedOrderLines } from '../../hooks' ;
2121import { AGREEMENT_ENDPOINT , AGREEMENT_ERESOURCES_ENDPOINT , AGREEMENT_LINES_ENDPOINT } from '../../constants/endpoints' ;
2222
23- const { RECORDS_PER_REQUEST_MEDIUM } = resultCount ;
24-
25- const credentialsArray = [ ] ;
2623const 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-
364315AgreementViewRoute . 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 ;
0 commit comments