|
| 1 | +import React, { PureComponent } from "react" |
| 2 | +import PropTypes from "prop-types" |
| 3 | +import { fromJS } from "immutable" |
| 4 | +import { getSampleSchema } from "core/utils" |
| 5 | + |
| 6 | +const NOOP = Function.prototype |
| 7 | + |
| 8 | +export default class RequestBodyEditor extends PureComponent { |
| 9 | + |
| 10 | + static propTypes = { |
| 11 | + requestBody: PropTypes.object.isRequired, |
| 12 | + mediaType: PropTypes.string.isRequired, |
| 13 | + onChange: PropTypes.func, |
| 14 | + getComponent: PropTypes.func.isRequired, |
| 15 | + isExecute: PropTypes.bool, |
| 16 | + specSelectors: PropTypes.object.isRequired, |
| 17 | + }; |
| 18 | + |
| 19 | + static defaultProps = { |
| 20 | + mediaType: "application/json", |
| 21 | + requestBody: fromJS({}), |
| 22 | + onChange: NOOP, |
| 23 | + }; |
| 24 | + |
| 25 | + constructor(props, context) { |
| 26 | + super(props, context) |
| 27 | + |
| 28 | + this.state = { |
| 29 | + isEditBox: false, |
| 30 | + value: "" |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + componentDidMount() { |
| 35 | + this.setValueToSample.call(this) |
| 36 | + } |
| 37 | + |
| 38 | + componentWillReceiveProps(nextProps) { |
| 39 | + if(this.props.mediaType !== nextProps.mediaType) { |
| 40 | + // media type was changed |
| 41 | + this.setValueToSample(nextProps.mediaType) |
| 42 | + } |
| 43 | + |
| 44 | + if(!this.props.isExecute && nextProps.isExecute) { |
| 45 | + // we just entered execute mode, |
| 46 | + // so enable editing for convenience |
| 47 | + this.setState({ isEditBox: true }) |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + setValueToSample = (explicitMediaType) => { |
| 52 | + this.onChange(this.sample(explicitMediaType)) |
| 53 | + } |
| 54 | + |
| 55 | + sample = (explicitMediaType) => { |
| 56 | + let { requestBody, mediaType } = this.props |
| 57 | + let schema = requestBody.getIn(["content", explicitMediaType || mediaType, "schema"]).toJS() |
| 58 | + |
| 59 | + return getSampleSchema(schema, explicitMediaType || mediaType, { |
| 60 | + includeWriteOnly: true |
| 61 | + }) |
| 62 | + } |
| 63 | + |
| 64 | + onChange = (value) => { |
| 65 | + this.setState({value}) |
| 66 | + this.props.onChange(value) |
| 67 | + } |
| 68 | + |
| 69 | + handleOnChange = e => { |
| 70 | + const { mediaType } = this.props |
| 71 | + const isJson = /json/i.test(mediaType) |
| 72 | + const inputValue = isJson ? e.target.value.trim() : e.target.value |
| 73 | + |
| 74 | + this.onChange(inputValue) |
| 75 | + } |
| 76 | + |
| 77 | + toggleIsEditBox = () => this.setState( state => ({isEditBox: !state.isEditBox})) |
| 78 | + |
| 79 | + render() { |
| 80 | + let { |
| 81 | + isExecute, |
| 82 | + getComponent, |
| 83 | + } = this.props |
| 84 | + |
| 85 | + const Button = getComponent("Button") |
| 86 | + const TextArea = getComponent("TextArea") |
| 87 | + const HighlightCode = getComponent("highlightCode") |
| 88 | + |
| 89 | + let { value, isEditBox } = this.state |
| 90 | + |
| 91 | + return ( |
| 92 | + <div className="body-param"> |
| 93 | + { |
| 94 | + isEditBox && isExecute |
| 95 | + ? <TextArea className={"body-param__text"} value={value} onChange={ this.handleOnChange }/> |
| 96 | + : (value && <HighlightCode className="body-param__example" |
| 97 | + value={ value }/>) |
| 98 | + } |
| 99 | + <div className="body-param-options"> |
| 100 | + { |
| 101 | + !isExecute ? null |
| 102 | + : <div className="body-param-edit"> |
| 103 | + <Button className={isEditBox ? "btn cancel body-param__example-edit" : "btn edit body-param__example-edit"} |
| 104 | + onClick={this.toggleIsEditBox}>{ isEditBox ? "Cancel" : "Edit"} |
| 105 | + </Button> |
| 106 | + </div> |
| 107 | + } |
| 108 | + </div> |
| 109 | + |
| 110 | + </div> |
| 111 | + ) |
| 112 | + |
| 113 | + } |
| 114 | +} |
0 commit comments