Skip to content

Commit 518c428

Browse files
f-alizadaFarhad Alizada
andauthored
Add dropdown for the case when there are multiple examples (#2208)
* Add dropdown for the case when there are multiple examples --------- Co-authored-by: Farhad Alizada <falizada@microsoft.com>
1 parent 72d11e5 commit 518c428

File tree

4 files changed

+58
-5
lines changed

4 files changed

+58
-5
lines changed

src/components/operations/operation-details/ko/runtime/operation-details.html

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,30 @@ <h4>Request body</h4>
175175
<!-- /ko -->
176176

177177
<!-- ko if: representation.examples -->
178-
<!-- ko foreach: { data: representation.examples, as: 'example' } -->
178+
<!-- ko if: representation.examples.length > 1 -->
179+
<div class="row flex flex-row example-form-control">
180+
<div class="col-12">
181+
<label data-bind="attr : { for: 'select' + representation.contentType + '_' + $index() }, text: 'Examples'"></label>
182+
</div>
183+
<div class="col-4">
184+
<div class="form-group">
185+
<select class="form-control" data-bind="options: representation.examples, value: $component.selectedRepresentatnionsValue()[representation.contentType] ,optionsValue: 'title', optionsText: 'title', attr : {title: 'Representation examples selector', id: 'select-' + representation.contentType + '_' + $index() }"></select>
186+
</div>
187+
</div>
188+
</div>
189+
<!-- ko foreach: { data: representation.examples, as: 'example' } -->
190+
<code-snippet data-bind="visible: $component.selectedRepresentatnionsValue()[representation.contentType]() == example.title"
191+
params="{ title: example.title, content: example.value, language: example.format, description: example.description }">
192+
</code-snippet>
193+
<!-- /ko -->
194+
<!-- /ko -->
195+
196+
<!-- ko if: representation.examples.length == 1 -->
179197
<code-snippet
180-
params="{ title: example.title, content: example.value, language: example.format, description: example.description }">
198+
params="{ title: representation.examples[0].title, content: representation.examples[0].value, language: representation.examples[0].format, description: representation.examples[0].description }">
181199
</code-snippet>
182200
<!-- /ko -->
201+
183202
<!-- /ko -->
184203
</div>
185204
</div>

src/components/operations/operation-details/ko/runtime/operation-details.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ export class OperationDetails {
5252
public readonly protocol: ko.Computed<string>;
5353
public readonly examples: ko.Observable<OperationExamples>;
5454

55+
public readonly selectedRepresentatnionsValue: ko.Observable<object>;
56+
57+
5558
constructor(
5659
private readonly apiService: ApiService,
5760
private readonly oauthService: OAuthService,
@@ -73,6 +76,8 @@ export class OperationDetails {
7376
this.defaultSchemaView = ko.observable("table");
7477
this.useCorsProxy = ko.observable();
7578
this.includeAllHostnames = ko.observable();
79+
this.selectedRepresentatnionsValue = ko.observable<object>();
80+
7681
this.requestUrlSample = ko.computed(() => {
7782

7883
const api = this.api();
@@ -115,6 +120,7 @@ export class OperationDetails {
115120

116121
return api.protocols?.join(", ");
117122
});
123+
118124
this.examples = ko.observable({});
119125

120126
this.apiType = ko.observable();
@@ -231,7 +237,10 @@ export class OperationDetails {
231237

232238
if (operation) {
233239
await this.loadDefinitions(operation);
234-
if (this.showExamples) this.parseExamples(operation);
240+
if (this.showExamples) this.parseResponseExamples(operation);
241+
242+
this.loadRequestExamples(operation);
243+
235244
this.operation(operation);
236245
}
237246
else {
@@ -244,6 +253,19 @@ export class OperationDetails {
244253
this.working(false);
245254
}
246255

256+
public async loadRequestExamples(operation: Operation): Promise<void> {
257+
const representations = operation.request.meaningfulRepresentations();
258+
const requestExamples = {};
259+
if (representations && representations.length) {
260+
for(let i = 0; i < representations.length; i++) {
261+
const value = representations[i].examples?.[0];
262+
if (!value) continue;
263+
requestExamples[representations[i].contentType] = ko.observable(value.title);
264+
}
265+
}
266+
this.selectedRepresentatnionsValue(requestExamples);
267+
}
268+
247269
public async loadDefinitions(operation: Operation): Promise<void> {
248270
const cachedDefinitions = this.definitionsCache.getItem(operation.id);
249271
if (cachedDefinitions) {
@@ -346,11 +368,11 @@ export class OperationDetails {
346368
return result;
347369
}
348370

349-
private parseExamples(operation: Operation): void {
371+
private parseResponseExamples(operation: Operation): void {
350372
const examples = operation.getMeaningfulResponses().reduce((acc, cur) => {
351373
const representations = cur.meaningfulRepresentations();
352374
if (!representations || !representations.length) return acc;
353-
375+
354376
const examplesObj = {}
355377
representations.forEach(representation => {
356378
const value = representation.examples?.[0]?.value;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
.example-form-control{
2+
padding-left: 8px;
3+
4+
label {
5+
font-weight: bold;
6+
}
7+
8+
select {
9+
margin-bottom: 0px;
10+
}
11+
}

src/themes/website/styles/styles.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@
3131
@import "gql.scss";
3232
@import "details.scss";
3333
@import "subscriptions.scss";
34+
@import "operations.scss";

0 commit comments

Comments
 (0)