summaryrefslogtreecommitdiff
path: root/src/core/components/execute.jsx
blob: e6b3e1b0bed1c4cd219b0207d770ec7097d484b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import React, { Component } from "react"
import PropTypes from "prop-types"

export default class Execute extends Component {

  static propTypes = {
    specSelectors: PropTypes.object.isRequired,
    specActions: PropTypes.object.isRequired,
    operation: PropTypes.object.isRequired,
    path: PropTypes.string.isRequired,
    method: PropTypes.string.isRequired,
    oas3Selectors: PropTypes.object.isRequired,
    oas3Actions: PropTypes.object.isRequired,
    onExecute: PropTypes.func,
    disabled: PropTypes.bool
  }

  handleValidateParameters = () => {
    let { specSelectors, specActions, path, method } = this.props
    specActions.validateParams([path, method])
    return specSelectors.validateBeforeExecute([path, method])
  }

  handleValidateRequestBody = () => {
    let { path, method, specSelectors, oas3Selectors, oas3Actions } = this.props
    let validationErrors = {
      missingBodyValue: false,
      missingRequiredKeys: []
    }
    // context: reset errors, then (re)validate
    oas3Actions.clearRequestBodyValidateError({ path, method })
    let oas3RequiredRequestBodyContentType = specSelectors.getOAS3RequiredRequestBodyContentType([path, method])
    let oas3RequestBodyValue = oas3Selectors.requestBodyValue(path, method)
    let oas3ValidateBeforeExecuteSuccess = oas3Selectors.validateBeforeExecute([path, method])
    let oas3RequestContentType = oas3Selectors.requestContentType(path, method)

    if (!oas3ValidateBeforeExecuteSuccess) {
      validationErrors.missingBodyValue = true
      oas3Actions.setRequestBodyValidateError({ path, method, validationErrors })
      return false
    }
    if (!oas3RequiredRequestBodyContentType) {
      return true
    }
    let missingRequiredKeys = oas3Selectors.validateShallowRequired({
      oas3RequiredRequestBodyContentType,
      oas3RequestContentType,
      oas3RequestBodyValue
    })
    if (!missingRequiredKeys || missingRequiredKeys.length < 1) {
      return true
    }
    missingRequiredKeys.forEach((missingKey) => {
      validationErrors.missingRequiredKeys.push(missingKey)
    })
    oas3Actions.setRequestBodyValidateError({ path, method, validationErrors })
    return false
  }

  handleValidationResultPass = () => {
    let { specActions, operation, path, method } = this.props
    if (this.props.onExecute) {
      // loading spinner
      this.props.onExecute()
    }
    specActions.execute({ operation, path, method })
  }

  handleValidationResultFail = () => {
    let { specActions, path, method } = this.props
    // deferred by 40ms, to give element class change time to settle.
    specActions.clearValidateParams([path, method])
    setTimeout(() => {
      specActions.validateParams([path, method])
    }, 40)
  }

  handleValidationResult = (isPass) => {
    if (isPass) {
      this.handleValidationResultPass()
    } else {
      this.handleValidationResultFail()
    }
  }

  onClick = () => {
    let paramsResult = this.handleValidateParameters()
    let requestBodyResult = this.handleValidateRequestBody()
    let isPass = paramsResult && requestBodyResult
    this.handleValidationResult(isPass)
  }

  onChangeProducesWrapper = ( val ) => this.props.specActions.changeProducesValue([this.props.path, this.props.method], val)

  render(){
    const { disabled } = this.props
    return (
        <button className="btn execute opblock-control__btn" onClick={ this.onClick } disabled={disabled}>
          Execute
        </button>
    )
  }
}