import React, { useState } from "react"; import { connect } from "react-redux"; import { withRouter, RouteComponentProps } from "react-router"; import FormRow from "./FormRow"; import { BaseText } from "components/designSystems/blueprint/TextComponent"; import { BaseTabbedView } from "components/designSystems/appsmith/TabbedView"; import styled from "styled-components"; import { AppState } from "reducers"; import { ActionResponse } from "api/ActionAPI"; import { formatBytes } from "utils/helpers"; import { APIEditorRouteParams } from "constants/routes"; import LoadingOverlayScreen from "components/editorComponents/LoadingOverlayScreen"; import CodeEditor from "components/editorComponents/CodeEditor"; import { getActionResponses } from "selectors/entitiesSelector"; import { Colors } from "constants/Colors"; import _ from "lodash"; import FormActionButton from "./form/FormActionButton"; const ResponseWrapper = styled.div` position: relative; flex: 1; height: 100%; overflow-y: scroll; `; const ResponseMetaInfo = styled.div` display: flex; ${BaseText} { color: #768896; margin: 0 5px; } `; const StatusCodeText = styled(BaseText)<{ code: string }>` color: ${props => props.code.match(/2\d\d/) ? props.theme.colors.primary : Colors.RED}; `; const TableWrapper = styled.div` &&& { table { table-layout: fixed; width: 100%; td { font-size: 12px; width: 50%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } } } `; interface ReduxStateProps { responses: Record; isRunning: Record; } const ResponseHeadersView = (props: { data: Record }) => { if (!props.data) return
; return ( {Object.keys(props.data).map(k => ( ))}
Key Value
{k} {props.data[k].join(", ")}
); }; type Props = ReduxStateProps & RouteComponentProps; const EMPTY_RESPONSE = { statusCode: "", duration: "", body: {}, headers: {}, requestBody: null, requestHeaders: {}, size: "", }; const FailedMessageContainer = styled.div` width: calc(100% - 29px); position: absolute; left: 29px; z-index: 10; bottom: 48%; display: flex; justify-content: center; align-items: center; `; const TabbedViewWrapper = styled.div` height: calc(100% - 30px); `; const ApiResponseView = (props: Props) => { const { match: { params: { apiId }, }, responses, } = props; let response: ActionResponse = EMPTY_RESPONSE; let isRunning = false; let hasFailed = false; if (apiId && apiId in responses) { response = responses[apiId] || EMPTY_RESPONSE; isRunning = props.isRunning[apiId]; hasFailed = response.statusCode ? response.statusCode[0] !== "2" : false; } const [selectedIndex, setSelectedIndex] = useState(0); const tabs = [ { key: "body", title: "Response Body", panelComponent: ( <> {hasFailed && !isRunning && ( { setSelectedIndex(3); }} /> )} ), }, { key: "headers", title: "Response Headers", panelComponent: , }, { key: "requestHeaders", title: "Request Headers", panelComponent: , }, { key: "requestBody", title: "Request Body", panelComponent: ( ), }, ]; return ( {isRunning && ( Sending Request )} {response.statusCode && ( Status: {response.statusCode} )} {response.duration && ( Time: {response.duration} ms )} {response.size && ( Size: {formatBytes(parseInt(response.size))} )} ); }; const mapStateToProps = (state: AppState): ReduxStateProps => { return { responses: getActionResponses(state), isRunning: state.ui.apiPane.isRunning, }; }; export default connect(mapStateToProps)(withRouter(ApiResponseView));