Skip to content

Commit

Permalink
Fixed issues with filtering by tags. (#812)
Browse files Browse the repository at this point in the history
  • Loading branch information
azaslonov authored Aug 4, 2020
1 parent ce521cf commit 8321bfc
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class ApiListDropdown {
public readonly selectedApiName: ko.Observable<string>;
public readonly working: ko.Observable<boolean>;
public readonly pattern: ko.Observable<string>;
public readonly tags: ko.Observable<string[]>;
public readonly tags: ko.Observable<Tag[]>;
public readonly page: ko.Observable<number>;
public readonly hasPager: ko.Computed<boolean>;
public readonly hasPrevPage: ko.Observable<boolean>;
Expand Down Expand Up @@ -163,7 +163,7 @@ export class ApiListDropdown {
}

public async onTagsChange(tags: Tag[]): Promise<void> {
this.tags(tags.map(tag => Utils.getResourceName("tags", tag.id)));
this.tags(tags);
}

@OnDestroyed()
Expand Down
4 changes: 2 additions & 2 deletions src/components/apis/list-of-apis/ko/runtime/api-list-tiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class ApiListTiles {
public readonly selectedApiName: ko.Observable<string>;
public readonly working: ko.Observable<boolean>;
public readonly pattern: ko.Observable<string>;
public readonly tags: ko.Observable<string[]>;
public readonly tags: ko.Observable<Tag[]>;
public readonly page: ko.Observable<number>;
public readonly hasPager: ko.Computed<boolean>;
public readonly hasPrevPage: ko.Observable<boolean>;
Expand Down Expand Up @@ -146,6 +146,6 @@ export class ApiListTiles {
}

public async onTagsChange(tags: Tag[]): Promise<void> {
this.tags(tags.map(tag => Utils.getResourceName("tags", tag.id)));
this.tags(tags);
}
}
4 changes: 2 additions & 2 deletions src/components/apis/list-of-apis/ko/runtime/api-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class ApiList {
public readonly apiGroups: ko.ObservableArray<TagGroup<Api>>;
public readonly working: ko.Observable<boolean>;
public readonly pattern: ko.Observable<string>;
public readonly tags: ko.Observable<string[]>;
public readonly tags: ko.Observable<Tag[]>;
public readonly page: ko.Observable<number>;
public readonly hasPager: ko.Computed<boolean>;
public readonly hasPrevPage: ko.Observable<boolean>;
Expand Down Expand Up @@ -140,6 +140,6 @@ export class ApiList {
}

public async onTagsChange(tags: Tag[]): Promise<void> {
this.tags(tags.map(tag => Utils.getResourceName("tags", tag.id)));
this.tags(tags);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class OperationList {
public readonly groupByTag: ko.Observable<boolean>;
public readonly operationGroups: ko.ObservableArray<TagGroup<Operation>>;
public readonly pattern: ko.Observable<string>;
public readonly tags: ko.Observable<string[]>;
public readonly tags: ko.Observable<Tag[]>;
public readonly pageNumber: ko.Observable<number>;
public readonly hasPrevPage: ko.Observable<boolean>;
public readonly hasNextPage: ko.Observable<boolean>;
Expand Down Expand Up @@ -235,7 +235,7 @@ export class OperationList {
}

public async onTagsChange(tags: Tag[]): Promise<void> {
this.tags(tags.map(tag => Utils.getResourceName("tags", tag.id)));
this.tags(tags);
}

@OnDestroyed()
Expand Down
8 changes: 6 additions & 2 deletions src/components/tag-input/tag-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class TagInput {

constructor(private readonly tagService: TagService) {
this.tags = ko.observableArray();
this.scope = ko.observable();
this.pattern = ko.observable();
this.selection = ko.observableArray([]);
this.availableTags = ko.computed<Tag[]>(() => this.tags().filter(tag => !this.selection().map(x => x.id).includes(tag.id)));
Expand All @@ -29,7 +30,7 @@ export class TagInput {
}

@Param()
public scope: string;
public scope: ko.Observable<string>;

@Event()
public onChange: (tags: Tag[]) => void;
Expand All @@ -47,10 +48,13 @@ export class TagInput {
this.pattern
.extend({ rateLimit: { timeout: Constants.defaultInputDelayMs, method: "notifyWhenChangesStop" } })
.subscribe(this.resetSearch);

this.scope
.subscribe(this.resetSearch);
}

public async loadPageOfTags(): Promise<void> {
const pageOfTags = await this.tagService.getTags(this.scope, this.pattern());
const pageOfTags = await this.tagService.getTags(this.scope(), this.pattern());
const tags = pageOfTags.value.filter(tag => !this.selection().map(x => x.id).includes(tag.id));

this.tags(tags);
Expand Down
4 changes: 3 additions & 1 deletion src/contracts/searchQuery.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Tag } from "./../models/tag";

export interface SearchQuery {
pattern?: string;
tags?: string[];
tags?: Tag[];
skip?: number;
take?: number;
grouping?: string;
Expand Down
51 changes: 8 additions & 43 deletions src/services/apiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class ApiService {
if (searchQuery) {
if (searchQuery.tags) {
searchQuery.tags.forEach((tag, index) => {
query = Utils.addQueryParameter(query, `tags[${index}]=${tag}`);
query = Utils.addQueryParameter(query, `tags[${index}]=${tag.name}`);
});
}

Expand Down Expand Up @@ -98,7 +98,7 @@ export class ApiService {

if (searchQuery) {
if (searchQuery.tags && searchQuery.tags.length > 0) {
const tagFilterEntries = searchQuery.tags.map((tag) => `tag/name eq '${tag}'`);
const tagFilterEntries = searchQuery.tags.map((tag) => `tag/id eq '${Utils.getResourceName("tags", tag.id)}'`);
odataFilterEntries.push(`(${tagFilterEntries.join(" or ")})`);
}

Expand Down Expand Up @@ -157,7 +157,7 @@ export class ApiService {

if (searchRequest) {
if (searchRequest.tags && searchRequest.tags.length > 0) {
const tagFilterEntries = searchRequest.tags.map((tag) => `tag/name eq '${tag}'`);
const tagFilterEntries = searchRequest.tags.map((tag) => `tag/id eq '${Utils.getResourceName("tags", tag.id)}'`);
odataFilterEntries.push(`(${tagFilterEntries.join(" or ")})`);
}

Expand Down Expand Up @@ -338,7 +338,7 @@ export class ApiService {

if (searchQuery) {
searchQuery.tags.forEach((tag, index) => {
query = Utils.addQueryParameter(query, `tags[${index}]=${tag}`);
query = Utils.addQueryParameter(query, `tags[${index}]=${tag.name}`);
});

if (searchQuery.pattern) {
Expand All @@ -363,32 +363,12 @@ export class ApiService {
return page;
}

private lastApiSchemas = {};

public async getApiSchema(schemaId: string): Promise<Schema> {
const apiId = schemaId.split("/schemas").shift();
let cachedApi = this.lastApiSchemas[apiId];
const contract = await this.mapiClient.get<SchemaContract>(`${schemaId}`);
const model = new Schema(contract);

if (!cachedApi) {
// clean cache if apiId changed
if (Object.keys(this.lastApiSchemas).length > 0) {
this.lastApiSchemas = {};
}
this.lastApiSchemas[apiId] = {};
cachedApi = this.lastApiSchemas[apiId];
}

const cached = cachedApi && cachedApi[schemaId];
if (cached) {
return cached;
}

const schema = await this.mapiClient.get<SchemaContract>(`${schemaId}`);
const loaded = new Schema(schema);

cachedApi[schemaId] = loaded;

return loaded;
return model;
}

public async getSchemas(api: Api): Promise<Page<Schema>> {
Expand Down Expand Up @@ -419,24 +399,11 @@ export class ApiService {
return SchemaType.openapi;
}

private lastApiProducts = {};

public async getAllApiProducts(apiId: string): Promise<Page<Product>> {
if (!apiId) {
throw new Error(`Parameter "apiId" not specified.`);
}

const cachedApi = this.lastApiProducts[apiId];

if (!cachedApi) {
// clean cache if apiId changed
if (Object.keys(this.lastApiProducts).length > 0) {
this.lastApiProducts = {};
}
} else {
return cachedApi;
}


const result = [];
const pageOfProducts = await this.mapiClient.get<Page<ProductContract>>(`${apiId}/products`);

Expand All @@ -447,9 +414,7 @@ export class ApiService {
const page = new Page<Product>();
page.value = result;
page.count = pageOfProducts.count;
// page.nextPage = pageOfProductContracts.nextPage;

this.lastApiProducts[apiId] = page;
return page;
}

Expand Down

0 comments on commit 8321bfc

Please sign in to comment.