1+ import React , { Component } from "react"
2+ import PropTypes from "prop-types"
3+
4+ export default class ConvertModal extends Component {
5+ constructor ( ) {
6+ super ( )
7+ this . state = {
8+ error : null ,
9+ status : "new"
10+ }
11+ }
12+ convertDefinition = async ( ) => {
13+ this . setState ( { status : "converting" } )
14+
15+ try {
16+ const conversionResult = await this . performConversion ( )
17+ this . setState ( {
18+ status : "success" ,
19+ } )
20+ this . props . updateEditorContent ( conversionResult )
21+ } catch ( e ) {
22+ this . setState ( {
23+ error : e ,
24+ status : "errored" ,
25+ } )
26+ }
27+ }
28+
29+ performConversion = async ( ) => {
30+ const res = await fetch ( "//converter.swagger.io/api/convert" , {
31+ method : "POST" ,
32+ headers : {
33+ "Content-Type" : "application/yaml" ,
34+ "Accept" : "application/yaml" ,
35+ } ,
36+ body : this . props . editorContent
37+ } )
38+
39+ const body = await res . text ( )
40+
41+ if ( ! res . ok ) {
42+ throw new Error ( body )
43+ }
44+
45+ return body
46+ }
47+
48+ render ( ) {
49+ const { onClose, getComponent, converterUrl } = this . props
50+
51+ if ( this . state . status === "new" ) {
52+ return < ConvertModalStepNew
53+ onClose = { onClose }
54+ onContinue = { ( ) => this . convertDefinition ( ) }
55+ getComponent = { getComponent }
56+ converterUrl = { converterUrl }
57+ />
58+ }
59+
60+ if ( this . state . status === "converting" ) {
61+ return < ConvertModalStepConverting
62+ getComponent = { getComponent }
63+ />
64+ }
65+
66+ if ( this . state . status === "success" ) {
67+ return < ConvertModalStepSuccess
68+ onClose = { onClose }
69+ getComponent = { getComponent }
70+ />
71+ }
72+
73+ if ( this . state . status === "errored" ) {
74+ return < ConvertModalStepErrored
75+ onClose = { onClose }
76+ error = { this . state . error }
77+ getComponent = { getComponent }
78+ />
79+ }
80+
81+
82+ return null
83+ }
84+ }
85+
86+ ConvertModal . propTypes = {
87+ editorContent : PropTypes . string . isRequired ,
88+ converterUrl : PropTypes . string . isRequired ,
89+ getComponent : PropTypes . func . isRequired ,
90+ updateEditorContent : PropTypes . func . isRequired ,
91+ onClose : PropTypes . func . isRequired ,
92+ }
93+
94+ const ConvertModalStepNew = ( { getComponent, onClose, onContinue, converterUrl } ) => {
95+ const Modal = getComponent ( "TopbarModal" )
96+
97+ return < Modal className = "modal" styleName = "modal-dialog-sm" onCloseClick = { onClose } >
98+ < div className = "container modal-message" >
99+ < h2 > Convert to OpenAPI 3</ h2 >
100+ < p >
101+ This feature uses the Swagger Converter API to convert your Swagger 2.0
102+ definition to OpenAPI 3.
103+ </ p >
104+ < p >
105+ Swagger Editor's contents will be sent to < b > < code > { converterUrl } </ code > </ b > and overwritten
106+ by the conversion result.
107+ </ p >
108+ </ div >
109+ < div className = "right" >
110+ < button className = "btn cancel" onClick = { onClose } > Cancel</ button >
111+ < button className = "btn" onClick = { onContinue } > Convert</ button >
112+ </ div >
113+ </ Modal >
114+ }
115+
116+ ConvertModalStepNew . propTypes = {
117+ getComponent : PropTypes . func . isRequired ,
118+ onClose : PropTypes . func . isRequired ,
119+ onContinue : PropTypes . func . isRequired ,
120+ converterUrl : PropTypes . string . isRequired ,
121+ }
122+
123+ const ConvertModalStepConverting = ( { getComponent } ) => {
124+ const Modal = getComponent ( "TopbarModal" )
125+
126+ return < Modal className = "modal" styleName = "modal-dialog-sm" hideCloseButton = { true } >
127+ < div className = "container modal-message" >
128+ < h2 > Converting...</ h2 >
129+ < p >
130+ Please wait.
131+ </ p >
132+ </ div >
133+ </ Modal >
134+ }
135+
136+ ConvertModalStepConverting . propTypes = {
137+ getComponent : PropTypes . func . isRequired ,
138+ }
139+
140+ const ConvertModalStepSuccess = ( { getComponent, onClose } ) => {
141+ const Modal = getComponent ( "TopbarModal" )
142+
143+ return < Modal className = "modal" styleName = "modal-dialog-sm" onCloseClick = { onClose } >
144+ < div className = "container modal-message" >
145+ < h2 > Conversion complete</ h2 >
146+ < p >
147+ Your definition was successfully converted to OpenAPI 3!
148+ </ p >
149+ </ div >
150+ < div className = "right" >
151+ < button className = "btn" onClick = { onClose } > Close</ button >
152+ </ div >
153+ </ Modal >
154+ }
155+
156+
157+ ConvertModalStepSuccess . propTypes = {
158+ getComponent : PropTypes . func . isRequired ,
159+ onClose : PropTypes . func . isRequired ,
160+ }
161+
162+ const ConvertModalStepErrored = ( { getComponent, onClose, error } ) => {
163+ const Modal = getComponent ( "TopbarModal" )
164+
165+ return < Modal className = "modal" styleName = "modal-dialog-sm" onCloseClick = { onClose } >
166+ < div className = "container modal-message" >
167+ < h2 > Conversion failed</ h2 >
168+ < p >
169+ The converter service was unable to convert your definition.
170+ </ p >
171+ < p >
172+ Here's what the service told us:
173+ </ p >
174+ < code >
175+ { error . toString ( ) }
176+ </ code >
177+ </ div >
178+ < div className = "right" >
179+ < button className = "btn" onClick = { onClose } > Close</ button >
180+ </ div >
181+ </ Modal >
182+ }
183+
184+ ConvertModalStepErrored . propTypes = {
185+ getComponent : PropTypes . func . isRequired ,
186+ onClose : PropTypes . func . isRequired ,
187+ error : PropTypes . any . isRequired ,
188+ }
0 commit comments