Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dropdown for the case when there are multiple examples #2208

Merged
merged 4 commits into from
Jun 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,30 @@ <h4>Request body</h4>
<!-- /ko -->

<!-- ko if: representation.examples -->
<!-- ko foreach: { data: representation.examples, as: 'example' } -->
<!-- ko if: representation.examples.length > 1 -->
<div class="row flex flex-row example-form-control">
<div class="col-12">
<label data-bind="attr : { for: 'select' + representation.contentType + '_' + $index() }, text: 'Examples'"></label>
</div>
<div class="col-4">
<div class="form-group">
<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>
</div>
</div>
</div>
<!-- ko foreach: { data: representation.examples, as: 'example' } -->
f-alizada marked this conversation as resolved.
Show resolved Hide resolved
<code-snippet data-bind="visible: $component.selectedRepresentatnionsValue()[representation.contentType]() == example.title"
params="{ title: example.title, content: example.value, language: example.format, description: example.description }">
</code-snippet>
<!-- /ko -->
<!-- /ko -->

<!-- ko if: representation.examples.length == 1 -->
f-alizada marked this conversation as resolved.
Show resolved Hide resolved
<code-snippet
params="{ title: example.title, content: example.value, language: example.format, description: example.description }">
params="{ title: representation.examples[0].title, content: representation.examples[0].value, language: representation.examples[0].format, description: representation.examples[0].description }">
</code-snippet>
<!-- /ko -->

<!-- /ko -->
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ export class OperationDetails {
public readonly protocol: ko.Computed<string>;
public readonly examples: ko.Observable<OperationExamples>;

public readonly selectedRepresentatnionsValue: ko.Observable<object>;


constructor(
private readonly apiService: ApiService,
private readonly oauthService: OAuthService,
Expand All @@ -73,6 +76,8 @@ export class OperationDetails {
this.defaultSchemaView = ko.observable("table");
this.useCorsProxy = ko.observable();
this.includeAllHostnames = ko.observable();
this.selectedRepresentatnionsValue = ko.observable<object>();

this.requestUrlSample = ko.computed(() => {

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

return api.protocols?.join(", ");
});

this.examples = ko.observable({});

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

if (operation) {
await this.loadDefinitions(operation);
if (this.showExamples) this.parseExamples(operation);
if (this.showExamples) this.parseResponseExamples(operation);

this.loadRequestExamples(operation);

this.operation(operation);
}
else {
Expand All @@ -244,6 +253,19 @@ export class OperationDetails {
this.working(false);
}

public async loadRequestExamples(operation: Operation): Promise<void> {
const representations = operation.request.meaningfulRepresentations();
const requestExamples = {};
if (representations && representations.length) {
f-alizada marked this conversation as resolved.
Show resolved Hide resolved
for(let i = 0; i < representations.length; i++) {
const value = representations[i].examples?.[0];
if (!value) continue;
requestExamples[representations[i].contentType] = ko.observable(value.title);
}
}
this.selectedRepresentatnionsValue(requestExamples);
}

public async loadDefinitions(operation: Operation): Promise<void> {
const cachedDefinitions = this.definitionsCache.getItem(operation.id);
if (cachedDefinitions) {
Expand Down Expand Up @@ -346,11 +368,11 @@ export class OperationDetails {
return result;
}

private parseExamples(operation: Operation): void {
private parseResponseExamples(operation: Operation): void {
const examples = operation.getMeaningfulResponses().reduce((acc, cur) => {
const representations = cur.meaningfulRepresentations();
if (!representations || !representations.length) return acc;

f-alizada marked this conversation as resolved.
Show resolved Hide resolved
const examplesObj = {}
representations.forEach(representation => {
const value = representation.examples?.[0]?.value;
Expand Down
11 changes: 11 additions & 0 deletions src/themes/website/styles/operations.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.example-form-control{
padding-left: 8px;

label {
font-weight: bold;
}

select {
margin-bottom: 0px;
}
}
1 change: 1 addition & 0 deletions src/themes/website/styles/styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@
@import "gql.scss";
@import "details.scss";
@import "subscriptions.scss";
@import "operations.scss";