From 7720869f29d662c788fc1a7a4dd6f24d5a53bca1 Mon Sep 17 00:00:00 2001 From: Alex Kaminskii Date: Tue, 13 Dec 2022 15:43:41 +0100 Subject: [PATCH] fix(frontend):fetchTabs review fixes --- .../components/FetchTabs/FetchTabs.tsx | 99 +++++++++++-------- 1 file changed, 57 insertions(+), 42 deletions(-) diff --git a/frontend/app/components/shared/FetchDetailsModal/components/FetchTabs/FetchTabs.tsx b/frontend/app/components/shared/FetchDetailsModal/components/FetchTabs/FetchTabs.tsx index 83b6f3767..e4f5d8756 100644 --- a/frontend/app/components/shared/FetchDetailsModal/components/FetchTabs/FetchTabs.tsx +++ b/frontend/app/components/shared/FetchDetailsModal/components/FetchTabs/FetchTabs.tsx @@ -1,4 +1,5 @@ import React, { useEffect, useState } from 'react'; +import logger from 'App/logger' import Headers from '../Headers'; import { JSONTree, Tabs, NoContent } from 'UI'; import AnimatedSVG, { ICONS } from 'Shared/AnimatedSVG/AnimatedSVG'; @@ -13,6 +14,36 @@ function isValidJSON(o: any): o is Object { return typeof o === "object" && o != null } +function parseRequestResponse( + r: string, + setHeaders: (hs: Record) => void, + setJSONBody: (body: Object) => void, + setStringBody: (body: string) => void, +) { + try { + let json = JSON.parse(r) + const hs = json.headers + const bd = json.body as string + if (typeof hs === "object") { + setHeaders(hs); + } + if (typeof bd !== 'string') { + throw new Error(`body is not a string`) + } + try { + let jBody = JSON.parse(bd) + if (typeof jBody === "object" && jBody != null) { + setJSONBody(jBody) + } else { + setStringBody(bd) + } + } catch { + setStringBody(bd) + } + } catch(e) { logger.error("Error decoding payload json:", e, r)} +} + + interface Props { resource: any; } @@ -20,41 +51,27 @@ function FetchTabs(props: Props) { const { resource } = props; const [activeTab, setActiveTab] = useState(HEADERS); const onTabClick = (tab: string) => setActiveTab(tab); - const [jsonRequest, setJsonRequest] = useState(null); - const [jsonResponse, setJsonResponse] = useState(null); + const [jsonRequest, setJsonRequest] = useState(null); + const [jsonResponse, setJsonResponse] = useState(null); + const [stringRequest, setStringRequest] = useState(''); + const [stringResponse, setStringResponse ] = useState(''); const [requestHeaders, setRequestHeaders] = useState | null>(null); const [responseHeaders, setResponseHeaders] = useState | null>(null); useEffect(() => { const { request, response } = resource; - - try { - let jRequest = JSON.parse(request) - if (typeof jRequest.headers === "object") { - setRequestHeaders(jRequest.headers); - } - try { - let jBody = JSON.parse(jRequest.body) - jBody = isValidJSON(jBody) ? jBody : jRequest.body - setJsonRequest(jBody) - } catch { - setJsonRequest(jRequest.body) - } - } catch {} - - try { - let jResponse = JSON.parse(response) - if (typeof jResponse.headers === "object") { - setResponseHeaders(jResponse.headers); - } - try { - let jBody = JSON.parse(jResponse.body) - jBody = isValidJSON(jBody) ? jBody : jResponse.body - setJsonResponse(jBody) - } catch { - setJsonResponse(jResponse.body) - } - } catch {} + parseRequestResponse( + request, + setRequestHeaders, + setJsonRequest, + setStringRequest, + ) + parseRequestResponse( + response, + setResponseHeaders, + setJsonResponse, + setStringResponse, + ) }, [resource]); const renderActiveTab = () => { @@ -70,16 +87,15 @@ function FetchTabs(props: Props) { } size="small" - show={!jsonRequest} + show={!jsonRequest && !stringRequest} // animatedIcon="no-results" >
- { !isValidJSON(jsonRequest) ? ( -
{jsonRequest}
- ) : ( - - )} + { jsonRequest + ? + :
{stringRequest}
+ }
@@ -95,16 +111,15 @@ function FetchTabs(props: Props) {
} size="small" - show={!jsonResponse} + show={!jsonResponse && !stringResponse} // animatedIcon="no-results" >
- { !isValidJSON(jsonResponse) ? ( -
{jsonResponse}
- ) : ( - - )} + { jsonResponse + ? + :
{stringResponse}
+ }