11import { Link , MessageBar , MessageBarType } from '@fluentui/react' ;
22import { useState } from 'react' ;
3- import { IAuthenticateResult } from '../../../../types/authentication' ;
4- import { Mode } from '../../../../types/enums' ;
5- import { IGraphResponse } from '../../../../types/query-response' ;
3+ import { useDispatch } from 'react-redux' ;
64
5+ import { AppDispatch , useAppSelector } from '../../../../store' ;
6+ import { Mode } from '../../../../types/enums' ;
77import { IQuery } from '../../../../types/query-runner' ;
8+ import { getContentType } from '../../../services/actions/query-action-creator-util' ;
89import { runQuery } from '../../../services/actions/query-action-creators' ;
910import { setSampleQuery } from '../../../services/actions/query-input-action-creators' ;
1011import { MOZILLA_CORS_DOCUMENTATION_LINK } from '../../../services/graph-constants' ;
@@ -15,31 +16,32 @@ interface ODataLink {
1516 name : string ;
1617}
1718
18- export function ResponseMessages (
19- graphResponse : IGraphResponse ,
20- sampleQuery : IQuery ,
21- authToken : IAuthenticateResult ,
22- graphExplorerMode : Mode ,
23- dispatch : Function ) {
24-
25- function getOdataLinkFromResponseBody ( responseBody : any ) : ODataLink | null {
26- const odataLinks = [ 'nextLink' , 'deltaLink' ] ;
27- let data = null ;
28- if ( responseBody ) {
29- odataLinks . forEach ( link => {
30- if ( responseBody [ `@odata.${ link } ` ] ) {
31- data = {
32- link : responseBody [ `@odata.${ link } ` ] ,
33- name : link
34- } ;
35- }
36- } ) ;
37- }
38- return data ;
19+ function getOdataLinkFromResponseBody ( responseBody : any ) : ODataLink | null {
20+ const odataLinks = [ 'nextLink' , 'deltaLink' ] ;
21+ let data = null ;
22+ if ( responseBody ) {
23+ odataLinks . forEach ( link => {
24+ if ( responseBody [ `@odata.${ link } ` ] ) {
25+ data = {
26+ link : responseBody [ `@odata.${ link } ` ] ,
27+ name : link
28+ } ;
29+ }
30+ } ) ;
3931 }
32+ return data ;
33+ }
34+
35+ export const ResponseMessages = ( ) => {
36+ const dispatch : AppDispatch = useDispatch ( ) ;
37+ const messageBars = [ ] ;
38+
39+ const { graphResponse : { body, headers } , sampleQuery, authToken, graphExplorerMode
40+ } = useAppSelector ( ( state ) => state ) ;
4041 const [ displayMessage , setDisplayMessage ] = useState ( true ) ;
42+
4143 const tokenPresent = ! ! authToken . token ;
42- const { body } = graphResponse ;
44+ const contentType = getContentType ( headers ) ;
4345 const odataLink = getOdataLinkFromResponseBody ( body ) ;
4446
4547 const setQuery = ( ) => {
@@ -51,7 +53,7 @@ export function ResponseMessages(
5153
5254 // Display link to step to next result
5355 if ( odataLink ) {
54- return (
56+ messageBars . push (
5557 < MessageBar messageBarType = { MessageBarType . info } >
5658 { translateMessage ( 'This response contains an @odata property.' ) } : @odata.{ odataLink . name }
5759 < Link onClick = { ( ) => setQuery ( ) } underline >
@@ -63,7 +65,7 @@ export function ResponseMessages(
6365
6466 // Display link to download file response
6567 if ( body ?. contentDownloadUrl ) {
66- return (
68+ messageBars . push (
6769 < div >
6870 < MessageBar messageBarType = { MessageBarType . warning } >
6971 { translateMessage ( 'This response contains unviewable content' ) }
@@ -77,7 +79,7 @@ export function ResponseMessages(
7779
7880 // Show CORS compliance message
7981 if ( body ?. throwsCorsError ) {
80- return (
82+ messageBars . push (
8183 < div >
8284 < MessageBar messageBarType = { MessageBarType . warning } >
8385 { translateMessage ( 'Response content not available due to CORS policy' ) }
@@ -90,7 +92,7 @@ export function ResponseMessages(
9092 }
9193
9294 if ( body && ! tokenPresent && displayMessage && graphExplorerMode === Mode . Complete ) {
93- return (
95+ messageBars . push (
9496 < div >
9597 < MessageBar
9698 messageBarType = { MessageBarType . warning }
@@ -104,4 +106,20 @@ export function ResponseMessages(
104106 </ div >
105107 ) ;
106108 }
107- }
109+
110+ if ( contentType === 'application/json' && typeof body === 'string' ) {
111+ messageBars . push (
112+ < div >
113+ < MessageBar
114+ messageBarType = { MessageBarType . info }
115+ onDismiss = { ( ) => setDisplayMessage ( false ) }
116+ dismissButtonAriaLabel = { translateMessage ( 'Close' ) }
117+ >
118+ { translateMessage ( 'Malformed JSON body' ) }
119+ </ MessageBar >
120+ </ div >
121+ ) ;
122+ }
123+
124+ return messageBars ;
125+ }
0 commit comments