diff --git a/src/app/articles.service.ts b/src/app/articles.service.ts index 060f616..d819ae1 100644 --- a/src/app/articles.service.ts +++ b/src/app/articles.service.ts @@ -1,14 +1,15 @@ import { Injectable } from '@angular/core'; -import { HttpClient } from '@angular/common/http'; +import { HttpClient, HttpHeaders } from '@angular/common/http'; @Injectable({ - providedIn: 'root' + providedIn: 'root', }) export class ArticlesService { - readonly baseUrl = 'https://conduit.productionready.io/api/'; + hardcodedMail = 'utnfrro@utn.frro'; + hardcodedPass = 'utnutnutn'; - constructor(private http: HttpClient) { } + constructor(private http: HttpClient) {} getArticles() { const url = this.baseUrl + 'articles'; return this.http.get(url); @@ -17,4 +18,44 @@ export class ArticlesService { const url = this.baseUrl + 'tags'; return this.http.get(url); } + getArticlesByTag(tag: string) { + const url = this.baseUrl + `articles?tag=${tag}`; + return this.http.get(url); + } + + postComment(text, slug) { + const loginUrl = this.baseUrl + 'users/login'; + this.http + .post(loginUrl, { + user: { + email: this.hardcodedMail, + password: this.hardcodedPass, + }, + }) + .subscribe((response) => { + const token = response['user']['token']; + const url = this.baseUrl + `/articles/${slug}/comments`; + this.http + .post( + url, + { + comment: { body: text }, + }, + { + headers: new HttpHeaders({ + 'Content-Type': 'application/json', + Authorization: 'Token ' + token, + }), + } + ) + .subscribe((res) => + alert( + 'Thanks ' + + res['comment']['author']['username'] + + ', your comment was saved' + ) + ), + (err) => console.log(err); + }); + } } diff --git a/src/app/articles/articles.component.html b/src/app/articles/articles.component.html index 404cd99..9fd8646 100644 --- a/src/app/articles/articles.component.html +++ b/src/app/articles/articles.component.html @@ -1,9 +1,31 @@ - -
-

{{article.title}}

-

{{article.body}}

+
+

Filter articles by your favorite TAG

- -{{tags | json}} - +
+ +
+
+
+

{{ article.title }}

+

{{ article.description }}

+

{{ article.body }}

+

Article created at {{ article.createdAt }}

+
+ +
+ +
+ +
diff --git a/src/app/articles/articles.component.scss b/src/app/articles/articles.component.scss index e69de29..5289da4 100644 --- a/src/app/articles/articles.component.scss +++ b/src/app/articles/articles.component.scss @@ -0,0 +1,36 @@ +.article { + border: 2px solid lightgray; + background-color: #d4d4d4; + padding: 2em; + margin: 1em; +} + +.tag{ + border: none; + border-radius: 3px; + background-color: steelblue; + color: white; + padding: 5px; + margin: 2px; + float: left; + font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif; +} + +.ownTag{ + border: none; + border-radius: 3px; + background-color: rgb(57, 185, 157); + padding: 5px; + margin: 2px; + float: right; + font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif; +} + +.title{ + margin-left: 10px; + font-family: Cambria, Cochin, Georgia, Times, 'Times New Roman', serif; +} + +textarea { + resize: none; +} \ No newline at end of file diff --git a/src/app/articles/articles.component.ts b/src/app/articles/articles.component.ts index 434bf47..8439cb8 100644 --- a/src/app/articles/articles.component.ts +++ b/src/app/articles/articles.component.ts @@ -4,21 +4,31 @@ import { ArticlesService } from '../articles.service'; @Component({ selector: 'app-articles', templateUrl: './articles.component.html', - styleUrls: ['./articles.component.scss'] + styleUrls: ['./articles.component.scss'], }) export class ArticlesComponent implements OnInit { - - articles:any = [] + articles: any = []; tags: any = []; - constructor(private service: ArticlesService) { } - - ngOnInit(): void { - + constructor(private service: ArticlesService) { + this.loadTags(); } + + ngOnInit(): void {} loadArticles() { - this.service.getArticles().subscribe(response => this.articles = response.articles); + this.service + .getArticles() + .subscribe((response) => (this.articles = response.articles)); } loadTags() { - this.service.getTags().subscribe(response => this.tags = response.tags); + this.service.getTags().subscribe((response) => (this.tags = response.tags)); + } + showArticlesForTag(tag) { + this.service + .getArticlesByTag(tag) + .subscribe((response) => (this.articles = response.articles.slice(0, 5))); + } + commentArticle({text, slug}) { + this.service + .postComment(text, slug); } }