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 ReadOnlyEditor from "components/editorComponents/ReadOnlyEditor"; import { getActionResponses } from "selectors/entitiesSelector"; import { Colors } from "constants/Colors"; import _ from "lodash"; import FormActionButton from "./form/FormActionButton"; import { RequestView } from "./RequestView"; import { useLocalStorage } from "utils/hooks/localstorage"; import { CHECK_REQUEST_BODY, DONT_SHOW_THIS_AGAIN, SHOW_REQUEST, } from "constants/messages"; const ResponseWrapper = styled.div` position: relative; flex: 1; height: 100%; `; 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.primaryOld : 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 => ( // // // // // ))} // //
KeyValue
{k}{props.data[k].join(", ")}
//
// ); // }; type Props = ReduxStateProps & RouteComponentProps; const EMPTY_RESPONSE: ActionResponse = { statusCode: "", duration: "", body: {}, headers: {}, request: { headers: {}, body: {}, httpMethod: "", url: "", }, size: "", }; const FailedMessageContainer = styled.div` width: 100%; background: #29cca3; height: 77px; position: absolute; z-index: 10; bottom: 0; padding-top: 10px; padding-bottom: 7px; padding-left: 15px; font-family: ${props => props.theme.fonts.text}; font-style: normal; font-weight: 500; font-size: 14px; line-height: 16px; p { margin-bottom: 5px; color: white; } // display: flex; // justify-content: center; // align-items: center; `; const TabbedViewWrapper = styled.div` height: calc(100% - 30px); `; const StyledFormActionButton = styled(FormActionButton)` &&& { padding: 10px 12px 9px 9px; margin-right: 9px; border: 0; } `; 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 [requestDebugVisible, setRequestDebugVisible] = useLocalStorage( "requestDebugVisible", "true", ); const [selectedIndex, setSelectedIndex] = useState(0); const tabs = [ { key: "body", title: "Response Body", panelComponent: ( <> {hasFailed && !isRunning && requestDebugVisible === "true" && (

{CHECK_REQUEST_BODY}

{ setRequestDebugVisible(false); }} /> { setSelectedIndex(1); }} />
)} ), }, { key: "request", title: "Request", 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));