minor refactoring and moved install logic to new install service

pull/9/head
Chris Veilleux 2018-10-22 21:48:50 -05:00
parent 31f9e04c25
commit 0a6f13a56a
1 changed files with 69 additions and 23 deletions

View File

@ -3,48 +3,94 @@ import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Subject } from "rxjs/internal/Subject";
import { tap } from "rxjs/operators";
export class Skill {
id: number;
credits: Object;
export interface AvailableSkill {
icon: Object;
iconImage: string;
isMycroftMade: boolean;
isSystemSkill: boolean;
marketCategory: string;
name: string;
summary: string;
title: string;
trigger: string;
}
export interface SkillCredits {
name: string;
github_id: string;
}
export interface SkillDetail {
categories: string[];
credits: SkillCredits[];
description: string;
icon: Object;
icon_image: string;
installed: boolean;
title: string;
iconImage: string;
isSystemSkill: boolean;
name: string;
platforms: string[];
repositoryUrl: string;
summary: string;
repository_url: string;
title: string;
triggers: string;
}
const availableSkillsUrl = '/api/skill/available';
const skillUrl = '/api/skill/detail/';
const searchQuery = '?search=';
@Injectable()
export class SkillsService {
private installUrl = '/api/install';
private skillUrl = '/api/skill/';
private skillsUrl = '/api/skills';
private searchQuery = '?search=';
public availableSkills: AvailableSkill[];
public isFiltered = new Subject<boolean>();
constructor(private http: HttpClient) { }
getAllSkills(): Observable<Skill[]> {
return this.http.get<Skill[]>(this.skillsUrl)
getAvailableSkills(): Observable<AvailableSkill[]> {
return this.http.get<AvailableSkill[]>(availableSkillsUrl).pipe(
tap((skills) => {this.availableSkills = skills;})
)
}
getSkillById(id: string): Observable<Skill> {
return this.http.get<Skill>(this.skillUrl + id)
getSkillCategories(): string[] {
let orderedSkillCategories: string[] = [],
skillCategories: string[] = [],
systemCategoryFound: boolean = false;
this.availableSkills.forEach(
(skill) => {
if (!skillCategories.includes(skill.marketCategory)) {
skillCategories.push(skill.marketCategory);}
}
);
skillCategories.sort();
// Make the "System" category display last, if it exists
skillCategories.forEach(
category => {
if (category === 'System') {
systemCategoryFound = true;
} else {
orderedSkillCategories.push(category)
}
}
);
if (systemCategoryFound) {
orderedSkillCategories.push('System')
}
return orderedSkillCategories;
}
searchSkills(searchTerm: string): Observable<Skill[]> {
getSkillById(skillName: string): Observable<SkillDetail> {
return this.http.get<SkillDetail>(skillUrl + skillName)
}
searchSkills(searchTerm: string): Observable<AvailableSkill[]> {
this.isFiltered.next(!!searchTerm);
return this.http.get<Skill[]>(this.skillsUrl + this.searchQuery + searchTerm)
}
installSkill(skill: Skill): Observable<Object> {
return this.http.put<Object>(
this.installUrl,
{skill_url: skill.repository_url}
return this.http.get<AvailableSkill[]>(
availableSkillsUrl + searchQuery + searchTerm
)
}
}