Skip to content

Commit

Permalink
Added support for tags as a refrence in url for apis and operations (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ygrik authored Mar 16, 2021
1 parent 4340ac6 commit d4198ea
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 10 deletions.
8 changes: 2 additions & 6 deletions src/components/apis/list-of-apis/ko/runtime/api-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { TagGroup } from "../../../../../models/tagGroup";
import { SearchQuery } from "../../../../../contracts/searchQuery";
import { RouteHelper } from "../../../../../routing/routeHelper";
import { Tag } from "../../../../../models/tag";
import { Utils } from "../../../../../utils";


@RuntimeComponent({
Expand Down Expand Up @@ -61,18 +60,15 @@ export class ApiList {
@OnMounted()
public async initialize(): Promise<void> {
this.groupByTag(this.defaultGroupByTagToEnabled());
this.tags.subscribe(this.resetSearch);

await this.resetSearch();

this.pattern
.extend({ rateLimit: { timeout: Constants.defaultInputDelayMs, method: "notifyWhenChangesStop" } })
.subscribe(this.resetSearch);

this.tags
.subscribe(this.resetSearch);

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ export class OperationList {
this.selectedOperationName(operationName);

this.groupByTag(this.defaultGroupByTagToEnabled());
this.tags.subscribe(this.resetSearch);

this.showUrlPath(this.defaultShowUrlPath());

if (this.selectedApiName()) {
Expand All @@ -103,9 +105,6 @@ export class OperationList {
.extend({ rateLimit: { timeout: Constants.defaultInputDelayMs, method: "notifyWhenChangesStop" } })
.subscribe(this.resetSearch);

this.tags
.subscribe(this.resetSearch);

this.groupByTag
.subscribe(this.loadOperations);

Expand Down
37 changes: 36 additions & 1 deletion src/components/tag-input/tag-input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import tagListTemplate from "./tag-list.html";
import { Component, Event, Param, OnMounted } from "@paperbits/common/ko/decorators";
import { Tag } from "../../models/tag";
import { TagService } from "../../services/tagService";
import { RouteHelper } from "../../routing/routeHelper";
import { TagContract } from "../../contracts/tag";
import { Utils } from "../../utils";

@Component({
selector: "tag-input",
Expand All @@ -19,7 +22,10 @@ export class TagInput {
public readonly canAddTags: ko.Computed<boolean>;
public readonly availableTags: ko.Computed<Tag[]>;

constructor(private readonly tagService: TagService) {
constructor(
private readonly tagService: TagService,
private readonly routeHelper: RouteHelper
) {
this.tags = ko.observableArray();
this.scope = ko.observable();
this.pattern = ko.observable();
Expand All @@ -43,6 +49,8 @@ export class TagInput {
return;
}

this.initTagsFilter();

await this.resetSearch();

this.pattern
Expand All @@ -53,6 +61,31 @@ export class TagInput {
.subscribe(this.resetSearch);
}

private initTagsFilter() {
const tagsValue = this.routeHelper.getTags();

if (tagsValue) {
const tags = tagsValue.split("|");
const tagItems = [];
if (tags && tags.length > 0) {
for (let i = 0; i < tags.length; i++) {
const tag = tags[i];
const tagContract: TagContract = Utils.armifyContract("tags", {
id: `/tags/${tag}`,
name: tag
});
tagItems.push(new Tag(tagContract));

}
this.selection(tagItems);
if (this.onChange) {
this.onChange(this.selection());
this.onDismiss.notifySubscribers();
}
}
}
}

public async loadPageOfTags(): Promise<void> {
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));
Expand All @@ -66,6 +99,7 @@ export class TagInput {

public addTag(tag: Tag): void {
this.selection.push(tag);
this.routeHelper.setHashParameter("tags", this.selection().map(t => t.name).join("|"));

if (this.onChange) {
this.onChange(this.selection());
Expand All @@ -75,6 +109,7 @@ export class TagInput {

public removeTag(tag: Tag): void {
this.selection.remove(tag);
this.routeHelper.setHashParameter("tags", this.selection().map(t => t.name).join("|"));

if (this.onChange) {
this.onChange(this.selection());
Expand Down
20 changes: 20 additions & 0 deletions src/routing/routeHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,33 @@ export class RouteHelper {
return params.get(name);
}

public setHashParameter(name: string, value: string): void {
const route = this.router.getCurrentRoute();
const params = new URLSearchParams(route.hash ? `?${route.hash}` : "");

if(value) {
params.set(name, value);
} else {
params.delete(name);
}

this.router.navigateTo(`#${params.toString()}`);
}

/**
* Returns ARM resource name of the API specified in hash parameter of the current route, e.g. "httpbin".
*/
public getApiName(): string {
return this.getHashParameter("api");
}

/**
* Returns ARM resource name of the API specified in hash parameter of the current route, e.g. "httpbin".
*/
public getTags(): string {
return this.getHashParameter("tags");
}

/**
* Returns ARM resource name of the operation specified in hash parameter of the current route, e.g. "status".
*/
Expand Down

0 comments on commit d4198ea

Please sign in to comment.