Skip to content

Commit d777bca

Browse files
committed
Polish out some last few bugs/rough UX from the custom API settings
1 parent 39061dc commit d777bca

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

src/components/settings/api-settings-card.tsx

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { ApiMetadata } from '../../model/api/api-interfaces';
2929
const UploadSpecButton = styled(SettingsButton).attrs(() => ({
3030
type: 'submit'
3131
}))`
32-
grid-column: 1 / span 2;
32+
grid-column: 1 / span 3;
3333
`;
3434

3535
const BaseUrlInput = styled(TextInput)`
@@ -82,7 +82,7 @@ export class ApiSettingsCard extends React.Component<
8282
private uploadSpecButtonRef = React.createRef<HTMLButtonElement>();
8383

8484
@observable
85-
private specUploadInProgress = false;
85+
private specProcessingInProgress = false;
8686

8787
@observable
8888
private enteredBaseUrl = "";
@@ -127,12 +127,12 @@ export class ApiSettingsCard extends React.Component<
127127
{ !this.selectedSpec
128128
? <UploadSpecButton
129129
type='submit' // Ensures we can show validation messages here
130-
onClick={this.specUploadInProgress ? undefined : this.uploadSpec}
130+
onClick={this.specProcessingInProgress ? undefined : this.uploadSpec}
131131
ref={this.uploadSpecButtonRef}
132132
>
133-
{ this.specUploadInProgress
133+
{ this.specProcessingInProgress
134134
? <Icon icon={['fas', 'spinner']} spin />
135-
: "Load an OpenAPI spec"
135+
: "Load an OpenAPI or Swagger spec"
136136
}
137137
</UploadSpecButton>
138138
: <>
@@ -148,20 +148,19 @@ export class ApiSettingsCard extends React.Component<
148148
<Icon icon={['fas', 'undo']} />
149149
</UndoButton>
150150
</Spec>
151+
<AddButton
152+
disabled={!this.selectedSpec || !this.enteredBaseUrl || !!this.baseUrlError}
153+
onClick={this.saveApi}
154+
>
155+
<Icon icon={['fas', 'save']} />
156+
</AddButton>
151157
</>
152158
}
153-
154-
<AddButton
155-
disabled={!this.selectedSpec || !this.enteredBaseUrl || !!this.baseUrlError}
156-
onClick={this.saveApi}
157-
>
158-
<Icon icon={['fas', 'plus']} />
159-
</AddButton>
160159
</ApiRows>
161160

162161
<SettingsExplanation>
163-
APIs added here will provide documentation and validation for all requests within their
164-
base URL.
162+
APIs added here will provide documentation and validation for all matching
163+
requests within their base URL.
165164
</SettingsExplanation>
166165
<SettingsExplanation>
167166
HTTP Toolkit also includes built-in specifications for 2600+ popular public APIs.
@@ -170,18 +169,21 @@ export class ApiSettingsCard extends React.Component<
170169
}
171170

172171
uploadSpec = flow(function * (this: ApiSettingsCard) {
173-
this.specUploadInProgress = true;
174172
updateValidationMessage(this.uploadSpecButtonRef.current!);
175173

176174
try {
177175
const file: string = yield uploadFile('text', ['.json', '.yaml']);
178176
if (!file) return;
179177

178+
this.specProcessingInProgress = true;
180179
let content: any = yield attempt(() =>
181180
JSON.parse(file)
182181
).catch(() =>
183182
yaml.parse(file)
184-
);
183+
).catch((e) => {
184+
console.warn('OpenAPI spec parsing error:', e);
185+
throw new Error('File could not be parsed as either YAML or JSON')
186+
});
185187

186188
let openApiSpec: OpenAPIObject;
187189

@@ -208,7 +210,9 @@ export class ApiSettingsCard extends React.Component<
208210
}
209211

210212
// Build the API just to test that we *can* (we'll rebuild with the base URL later)
211-
yield buildApiMetadataAsync(openApiSpec);
213+
yield buildApiMetadataAsync(openApiSpec, [
214+
'api.build.example' // Need a default base here in case the spec has no servers
215+
]);
212216

213217
this.selectedSpec = openApiSpec;
214218

@@ -229,7 +233,7 @@ export class ApiSettingsCard extends React.Component<
229233
console.log(e);
230234
updateValidationMessage(this.uploadSpecButtonRef.current!, asError(e).message);
231235
} finally {
232-
this.specUploadInProgress = false;
236+
this.specProcessingInProgress = false;
233237
}
234238
}).bind(this);
235239

0 commit comments

Comments
 (0)