1
1
import './documentNode.scss' ;
2
2
3
- import React , { FunctionComponent , useMemo , useState , useEffect , useContext } from 'react' ;
3
+ import axios from 'axios' ;
4
+ import React , { FunctionComponent , useContext , useEffect , useMemo , useState } from 'react' ;
4
5
import { Link , useHistory } from 'react-router-dom' ;
6
+ import { Button } from 'semantic-ui-react' ;
5
7
6
- import { DOCUMENT_TYPE_NAMES , TYPE_IS_PART_OF , TYPE_CONTAINS , TYPE_LINKED_TO , TYPE_RELATED } from '../../const' ;
8
+ import {
9
+ DOCUMENT_TYPE_NAMES ,
10
+ TYPE_CONTAINS ,
11
+ TYPE_IS_PART_OF ,
12
+ TYPE_LINKED_TO ,
13
+ TYPE_RELATED ,
14
+ } from '../../const' ;
15
+ import { useEnvironment } from '../../hooks' ;
16
+ import { applyFilters } from '../../hooks/applyFilters' ;
7
17
import { Document } from '../../types' ;
8
18
import { getDocumentDisplayName , groupLinksByType } from '../../utils' ;
9
- import { useEnvironment } from '../../hooks' ;
10
- import axios from 'axios' ;
11
- import { LoadingAndErrorIndicator } from '../LoadingAndErrorIndicator' ;
12
19
import { getApiEndpoint , getInternalUrl } from '../../utils/document' ;
13
- import { Button } from 'semantic-ui-react' ;
14
20
import { FilterButton } from '../FilterButton/FilterButton' ;
15
- import { applyFilters } from '../../hooks/applyFilters ' ;
21
+ import { LoadingAndErrorIndicator } from '../LoadingAndErrorIndicator ' ;
16
22
17
23
export interface DocumentNode {
18
- node : Document ,
19
- linkType : string ,
20
- hasLinktypeRelatedParent ?: Boolean ,
24
+ node : Document ;
25
+ linkType : string ;
26
+ hasLinktypeRelatedParent ?: Boolean ;
21
27
}
22
28
23
- const linkTypesToNest = [ TYPE_IS_PART_OF , TYPE_RELATED ]
24
- const linkTypesExcludedInNesting = [ TYPE_CONTAINS ]
25
- const linkTypesExcludedWhenNestingRelatedTo = [ TYPE_RELATED , TYPE_IS_PART_OF , TYPE_CONTAINS ]
29
+ const linkTypesToNest = [ TYPE_IS_PART_OF , TYPE_RELATED ] ;
30
+ const linkTypesExcludedInNesting = [ TYPE_CONTAINS ] ;
31
+ const linkTypesExcludedWhenNestingRelatedTo = [ TYPE_RELATED , TYPE_IS_PART_OF , TYPE_CONTAINS ] ;
26
32
27
- export const DocumentNode : FunctionComponent < DocumentNode > = ( { node, linkType, hasLinktypeRelatedParent } ) => {
33
+ export const DocumentNode : FunctionComponent < DocumentNode > = ( {
34
+ node,
35
+ linkType,
36
+ hasLinktypeRelatedParent,
37
+ } ) => {
28
38
const [ expanded , setExpanded ] = useState < boolean > ( false ) ;
29
- const isStandard = node . doctype in [ " Tool" , " Code" , " Standard" ] ;
39
+ const isStandard = node . doctype in [ ' Tool' , ' Code' , ' Standard' ] ;
30
40
const { apiUrl } = useEnvironment ( ) ;
31
41
const [ nestedNode , setNestedNode ] = useState < Document > ( ) ;
32
42
const [ loading , setLoading ] = useState < boolean > ( false ) ;
@@ -42,7 +52,8 @@ export const DocumentNode: FunctionComponent<DocumentNode> = ({ node, linkType,
42
52
useEffect ( ( ) => {
43
53
if ( ! isStandard && linkTypesToNest . includes ( linkType ) ) {
44
54
setLoading ( true ) ;
45
- axios . get ( getApiEndpoint ( node , apiUrl ) )
55
+ axios
56
+ . get ( getApiEndpoint ( node , apiUrl ) )
46
57
. then ( function ( response ) {
47
58
setLoading ( false ) ;
48
59
setNestedNode ( response . data . data ) ;
@@ -56,90 +67,98 @@ export const DocumentNode: FunctionComponent<DocumentNode> = ({ node, linkType,
56
67
}
57
68
} , [ id ] ) ;
58
69
59
-
60
-
61
70
const fetchedNodeHasLinks = ( ) => {
62
71
return usedNode . links && usedNode . links . length > 0 ;
63
- }
72
+ } ;
64
73
65
74
const hasActiveLinks = ( ) => {
66
75
return getTopicsToDisplayOrderdByLinkType ( ) . length > 0 ;
67
- }
76
+ } ;
68
77
69
78
const isNestedInRelated = ( ) : Boolean => {
70
- return hasLinktypeRelatedParent || ( linkType === TYPE_RELATED ) ;
71
- }
79
+ return hasLinktypeRelatedParent || linkType === TYPE_RELATED ;
80
+ } ;
72
81
73
82
const getTopicsToDisplayOrderdByLinkType = ( ) => {
74
83
return Object . entries ( linksByType )
75
84
. filter ( ( [ type , _ ] ) => ! linkTypesExcludedInNesting . includes ( type ) )
76
- . filter ( ( [ type , _ ] ) => isNestedInRelated ( ) ? ! linkTypesExcludedWhenNestingRelatedTo . includes ( type ) : true )
77
- }
85
+ . filter ( ( [ type , _ ] ) =>
86
+ isNestedInRelated ( ) ? ! linkTypesExcludedWhenNestingRelatedTo . includes ( type ) : true
87
+ ) ;
88
+ } ;
78
89
79
90
const Hyperlink = ( hyperlink ) => {
80
91
if ( ! hyperlink . hyperlink ) {
81
92
return < > </ > ;
82
93
}
83
94
84
- return < >
85
- < span >
86
- Reference:
87
- </ span >
88
- < a href = { hyperlink . hyperlink } target = "_blank" > { hyperlink . hyperlink } </ a >
89
- </ >
90
- }
95
+ return (
96
+ < >
97
+ < span > Reference:</ span >
98
+ < a href = { hyperlink . hyperlink } target = "_blank" >
99
+ { ' ' }
100
+ { hyperlink . hyperlink }
101
+ </ a >
102
+ </ >
103
+ ) ;
104
+ } ;
91
105
const SimpleView = ( ) => {
92
- return < >
93
- < div className = { `title external-link document-node f2` } >
94
- < Link to = { getInternalUrl ( usedNode ) } >
95
- < i aria-hidden = "true" className = "circle icon" > </ i >
96
- { getDocumentDisplayName ( usedNode ) }
97
- </ Link >
98
- </ div >
99
- < div className = { `content` } > </ div >
100
-
101
- </ >
102
- }
106
+ return (
107
+ < >
108
+ < div className = { `title external-link document-node f2` } >
109
+ < Link to = { getInternalUrl ( usedNode ) } >
110
+ < i aria-hidden = "true" className = "circle icon" > </ i >
111
+ { getDocumentDisplayName ( usedNode ) }
112
+ </ Link >
113
+ </ div >
114
+ < div className = { `content` } > </ div >
115
+ </ >
116
+ ) ;
117
+ } ;
103
118
104
119
const NestedView = ( ) => {
105
- return < >
106
-
107
- < LoadingAndErrorIndicator loading = { loading } error = { error } />
108
- < div className = { `title${ active } document-node` } onClick = { ( ) => setExpanded ( ! expanded ) } >
109
- < i aria-hidden = "true" className = "dropdown icon" > </ i >
110
- < Link to = { getInternalUrl ( usedNode ) } >
111
- { getDocumentDisplayName ( usedNode ) }
112
- </ Link >
113
- </ div >
114
- < div className = { `content ${ active } document-node` } >
115
- < Hyperlink hyperlink = { usedNode . hyperlink } />
116
- { expanded
117
- && getTopicsToDisplayOrderdByLinkType ( ) . map ( ( [ type , links ] ) => {
118
- return (
119
- < div className = "document-node__link-type-container" key = { type } >
120
- < div >
121
- < span > { usedNode . doctype } : { usedNode . name } - { usedNode . section } </ span >
122
- < b > { DOCUMENT_TYPE_NAMES [ type ] } </ b > :
123
- </ div >
124
- < div >
125
- < div className = "accordion ui fluid styled f0" >
126
- { links . map ( ( link , i ) =>
120
+ return (
121
+ < >
122
+ < LoadingAndErrorIndicator loading = { loading } error = { error } />
123
+ < div className = { `title${ active } document-node` } onClick = { ( ) => setExpanded ( ! expanded ) } >
124
+ < i aria-hidden = "true" className = "dropdown icon" > </ i >
125
+ < Link to = { getInternalUrl ( usedNode ) } > { getDocumentDisplayName ( usedNode ) } </ Link >
126
+ </ div >
127
+ < div className = { `content ${ active } document-node` } >
128
+ < Hyperlink hyperlink = { usedNode . hyperlink } / >
129
+ { expanded &&
130
+ getTopicsToDisplayOrderdByLinkType ( ) . map ( ( [ type , links ] ) => {
131
+ return (
132
+ < div className = "document-node__link-type-container" key = { type } >
133
+ < div >
134
+ < span >
135
+ { usedNode . doctype } : { usedNode . name } - { usedNode . section } { ' ' }
136
+ </ span >
137
+ < b > { DOCUMENT_TYPE_NAMES [ type ] } </ b > :
138
+ </ div >
139
+ < div >
140
+ < div className = "accordion ui fluid styled f0" >
141
+ { links . map ( ( link , i ) => (
127
142
< div key = { Math . random ( ) } >
128
- < DocumentNode node = { link . document } linkType = { type } hasLinktypeRelatedParent = { isNestedInRelated ( ) } key = { Math . random ( ) } />
129
- < FilterButton document = { link . document } />
130
- </ div >
131
- )
132
- }
143
+ < DocumentNode
144
+ node = { link . document }
145
+ linkType = { type }
146
+ hasLinktypeRelatedParent = { isNestedInRelated ( ) }
147
+ key = { Math . random ( ) }
148
+ />
149
+ < FilterButton document = { link . document } />
150
+ </ div >
151
+ ) ) }
152
+ </ div >
133
153
</ div >
134
154
</ div >
135
- </ div >
136
- ) ;
137
- } ) }
155
+ ) ;
156
+ } ) }
138
157
{ /* <FilterButton/> */ }
139
- </ div >
140
- </ >
141
-
142
- }
158
+ </ div >
159
+ </ >
160
+ ) ;
161
+ } ;
143
162
144
163
return hasActiveLinks ( ) ? < NestedView /> : < SimpleView /> ;
145
164
} ;
0 commit comments