1
- import React from 'react' ;
1
+ import React , { useState } from 'react' ;
2
2
3
3
const DiscoveryInterface = ( ) => {
4
+ const [ accessToken , setAccessToken ] = useState ( '' ) ;
5
+ const [ companies , setCompanies ] = useState ( [ ] ) ;
6
+ const [ loading , setLoading ] = useState ( false ) ;
7
+ const [ error , setError ] = useState ( '' ) ;
8
+ const [ success , setSuccess ] = useState ( '' ) ;
9
+
10
+ const handleDiscoveryCall = async ( ) => {
11
+ if ( ! accessToken . trim ( ) ) {
12
+ setError ( 'Please enter your access token' ) ;
13
+ return ;
14
+ }
15
+
16
+ setLoading ( true ) ;
17
+ setError ( '' ) ;
18
+ setSuccess ( '' ) ;
19
+ setCompanies ( [ ] ) ;
20
+
21
+ try {
22
+ const response = await fetch ( 'https://analytics.discovery.adobe.net/discovery/1.0/discovery/me' , {
23
+ method : 'GET' ,
24
+ headers : {
25
+ 'accept' : 'application/json' ,
26
+ 'x-user-auth' : accessToken
27
+ }
28
+ } ) ;
29
+
30
+ if ( ! response . ok ) {
31
+ throw new Error ( `HTTP error! status: ${ response . status } ` ) ;
32
+ }
33
+
34
+ const data = await response . json ( ) ;
35
+
36
+ // Extract companies from the response
37
+ const allCompanies = [ ] ;
38
+ if ( data . imsOrgs && Array . isArray ( data . imsOrgs ) ) {
39
+ data . imsOrgs . forEach ( org => {
40
+ if ( org . companies && Array . isArray ( org . companies ) ) {
41
+ org . companies . forEach ( company => {
42
+ allCompanies . push ( {
43
+ ...company ,
44
+ imsOrgId : org . imsOrgId
45
+ } ) ;
46
+ } ) ;
47
+ }
48
+ } ) ;
49
+ }
50
+
51
+ setCompanies ( allCompanies ) ;
52
+ setSuccess ( `Successfully retrieved ${ allCompanies . length } companies` ) ;
53
+ } catch ( err ) {
54
+ setError ( `Error: ${ err . message } ` ) ;
55
+ } finally {
56
+ setLoading ( false ) ;
57
+ }
58
+ } ;
59
+
60
+ const copyToClipboard = ( text ) => {
61
+ navigator . clipboard . writeText ( text ) . then ( ( ) => {
62
+ setSuccess ( `Copied "${ text } " to clipboard` ) ;
63
+ setTimeout ( ( ) => setSuccess ( '' ) , 3000 ) ;
64
+ } ) . catch ( ( ) => {
65
+ setError ( 'Failed to copy to clipboard' ) ;
66
+ } ) ;
67
+ } ;
68
+
4
69
return (
5
- < div className = "card_developer_console" style = { { marginBottom : '32px' } } >
70
+ < div className = "card_developer_console" style = { {
71
+ marginBottom : '32px' ,
72
+ marginLeft : '16px' ,
73
+ marginRight : '16px' ,
74
+ padding : '24px'
75
+ } } >
6
76
< div style = { { display : "flex" , gap : "32px" , flexDirection : "column" } } >
7
77
< div style = { { display : "flex" , gap : "16px" , flexDirection : "column" } } >
8
- < h3 className = "spectrum-Heading spectrum-Heading--sizeS side-header" >
9
- Company Discovery
10
- </ h3 >
11
78
< p className = "spectrum-Body spectrum-Body--sizeM" >
12
- After copying your access token from above, paste it here to get your available companies and copy the globalCompanyId values .
79
+ After copying your access token from Credentials above, paste it here to get your available companies and you can copy the global company id you want to use in requests below .
13
80
</ p >
14
81
</ div >
15
82
@@ -19,6 +86,8 @@ const DiscoveryInterface = () => {
19
86
</ label >
20
87
< input
21
88
type = "text"
89
+ value = { accessToken }
90
+ onChange = { ( e ) => setAccessToken ( e . target . value ) }
22
91
placeholder = "Paste your access token here"
23
92
style = { {
24
93
width : '100%' ,
@@ -31,34 +100,87 @@ const DiscoveryInterface = () => {
31
100
/>
32
101
33
102
< button
103
+ onClick = { handleDiscoveryCall }
104
+ disabled = { loading || ! accessToken . trim ( ) }
34
105
style = { {
35
- backgroundColor : '#0265DC' ,
106
+ backgroundColor : loading || ! accessToken . trim ( ) ? '#ccc' : '#0265DC' ,
36
107
color : 'white' ,
37
108
border : 'none' ,
38
109
padding : '8px 16px' ,
39
110
borderRadius : '4px' ,
40
- cursor : 'pointer' ,
111
+ cursor : loading || ! accessToken . trim ( ) ? 'not-allowed' : 'pointer' ,
41
112
fontSize : '14px' ,
42
113
width : 'fit-content'
43
114
} }
44
115
>
45
- Get Companies
116
+ { loading ? 'Loading...' : ' Get Companies' }
46
117
</ button >
47
118
</ div >
48
119
49
- < div style = { {
50
- padding : '12px' ,
51
- backgroundColor : '#f8f9fa' ,
52
- border : '1px solid #e0e0e0' ,
53
- borderRadius : '4px' ,
54
- fontSize : '14px' ,
55
- color : '#666'
56
- } } >
57
- 💡 < strong > Coming Soon:</ strong > This feature will allow you to discover your available companies and copy globalCompanyId values for use in API calls.
58
- </ div >
120
+ { error && (
121
+ < div style = { {
122
+ padding : '12px' ,
123
+ backgroundColor : '#ffebee' ,
124
+ border : '1px solid #f44336' ,
125
+ borderRadius : '4px' ,
126
+ color : '#c62828' ,
127
+ fontSize : '14px'
128
+ } } >
129
+ { error }
130
+ </ div >
131
+ ) }
132
+
133
+ { success && (
134
+ < div style = { {
135
+ padding : '12px' ,
136
+ backgroundColor : '#e8f5e8' ,
137
+ border : '1px solid #4caf50' ,
138
+ borderRadius : '4px' ,
139
+ color : '#2e7d32' ,
140
+ fontSize : '14px'
141
+ } } >
142
+ { success }
143
+ </ div >
144
+ ) }
145
+
146
+ { companies . length > 0 && (
147
+ < div style = { { display : "flex" , gap : "16px" , flexDirection : "column" } } >
148
+ < h4 className = "spectrum-Heading spectrum-Heading--sizeS side-header" >
149
+ Available Global Company IDs
150
+ </ h4 >
151
+ < p className = "spectrum-Body spectrum-Body--sizeM" >
152
+ Click on a globalCompanyId to copy it to your clipboard:
153
+ </ p >
154
+
155
+ < div style = { { display : 'flex' , flexWrap : 'wrap' , gap : '8px' } } >
156
+ { companies . map ( ( company , index ) => (
157
+ < button
158
+ key = { index }
159
+ onClick = { ( ) => copyToClipboard ( company . globalCompanyId ) }
160
+ style = { {
161
+ backgroundColor : '#0265DC' ,
162
+ color : 'white' ,
163
+ border : 'none' ,
164
+ padding : '8px 12px' ,
165
+ borderRadius : '4px' ,
166
+ cursor : 'pointer' ,
167
+ fontSize : '14px' ,
168
+ fontFamily : 'monospace'
169
+ } }
170
+ >
171
+ { company . globalCompanyId }
172
+ </ button >
173
+ ) ) }
174
+ </ div >
175
+
176
+ < p className = "spectrum-Body spectrum-Body--sizeM" style = { { fontStyle : 'italic' , color : '#666' } } >
177
+ 💡 Tip: Copy the globalCompanyId and paste it into the globalCompanyId parameter in the API calls below.
178
+ </ p >
179
+ </ div >
180
+ ) }
59
181
</ div >
60
182
</ div >
61
183
) ;
62
184
} ;
63
185
64
- export default DiscoveryInterface ;
186
+ export default DiscoveryInterface ;
0 commit comments