diff --git a/src/app/app.module.ts b/src/app/app.module.ts index e4710df..5cf4089 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -5,11 +5,15 @@ import { AppRoutingModule } from './app-routing.module'; import { AppComponent } from './app.component'; import { ArticlesComponent } from './articles/articles.component'; import { HttpClientModule } from '@angular/common/http'; +import { TagComponent } from './tag/tag.component'; +import { ArticleDetailComponent } from './article-detail/article-detail.component'; @NgModule({ declarations: [ AppComponent, - ArticlesComponent + ArticlesComponent, + TagComponent, + ArticleDetailComponent ], imports: [ BrowserModule, diff --git a/src/app/article-detail/article-detail.component.html b/src/app/article-detail/article-detail.component.html new file mode 100644 index 0000000..4777340 --- /dev/null +++ b/src/app/article-detail/article-detail.component.html @@ -0,0 +1,12 @@ +
+

{{article.title}}

+

{{article.description}}

+

{{article.body}}

+

{{article.createdAt}}

+

{{article.tagList | json}}

+ +
+
+ + +
diff --git a/src/app/article-detail/article-detail.component.scss b/src/app/article-detail/article-detail.component.scss new file mode 100644 index 0000000..e69de29 diff --git a/src/app/article-detail/article-detail.component.spec.ts b/src/app/article-detail/article-detail.component.spec.ts new file mode 100644 index 0000000..424aaed --- /dev/null +++ b/src/app/article-detail/article-detail.component.spec.ts @@ -0,0 +1,25 @@ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; + +import { ArticleDetailComponent } from './article-detail.component'; + +describe('ArticleDetailComponent', () => { + let component: ArticleDetailComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ ArticleDetailComponent ] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(ArticleDetailComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/article-detail/article-detail.component.ts b/src/app/article-detail/article-detail.component.ts new file mode 100644 index 0000000..eebe304 --- /dev/null +++ b/src/app/article-detail/article-detail.component.ts @@ -0,0 +1,22 @@ +import { Component, OnInit, Input } from '@angular/core'; +import { ArticlesService } from '../articles.service'; + +@Component({ + selector: 'app-article-detail', + templateUrl: './article-detail.component.html', + styleUrls: ['./article-detail.component.scss'] +}) +export class ArticleDetailComponent implements OnInit { + + @Input() article:any; + textAreaVisible:boolean; + comment: string = ''; + constructor(private service:ArticlesService) { } + + ngOnInit(): void { + + } + save(comment){ + this.service.postComment(comment.value,this.article).subscribe(); + } +} diff --git a/src/app/articles.service.ts b/src/app/articles.service.ts index 060f616..62ae5d8 100644 --- a/src/app/articles.service.ts +++ b/src/app/articles.service.ts @@ -1,20 +1,42 @@ import { Injectable } from '@angular/core'; -import { HttpClient } from '@angular/common/http'; +import { HttpClient, HttpHeaders } from '@angular/common/http'; @Injectable({ providedIn: 'root' }) export class ArticlesService { - readonly baseUrl = 'https://conduit.productionready.io/api/'; + user: any; constructor(private http: HttpClient) { } - getArticles() { - const url = this.baseUrl + 'articles'; + getArticles(selectedTag) { + console.log(selectedTag) + const url = this.baseUrl + 'articles?tag='+selectedTag+ '&limit=5'; return this.http.get(url); } getTags() { const url = this.baseUrl + 'tags'; return this.http.get(url); } + + login(){ + const url = this.baseUrl + 'users/login'; + let body = { user: {email:'sebisportivo@gmail.com', password:'123123123'} }; + this.http.post(url,body).subscribe(response => this.user = response.user); + } + + postComment(commentParameter, article){ + + const url = this.baseUrl + 'articles/' + article.slug + '/comments'; + let postBody = {comment: {body:commentParameter}}; + const httpOptions = { + headers: new HttpHeaders({ + 'Authorization': 'Token ' + this.user.token + }) + }; + return this.http.post(url, postBody, httpOptions); + } + + + } diff --git a/src/app/articles/articles.component.html b/src/app/articles/articles.component.html index 404cd99..2fa39b9 100644 --- a/src/app/articles/articles.component.html +++ b/src/app/articles/articles.component.html @@ -1,9 +1,5 @@ -
-

{{article.title}}

-

{{article.body}}

+
+
- - -{{tags | json}} diff --git a/src/app/articles/articles.component.ts b/src/app/articles/articles.component.ts index 434bf47..a8499eb 100644 --- a/src/app/articles/articles.component.ts +++ b/src/app/articles/articles.component.ts @@ -13,12 +13,12 @@ export class ArticlesComponent implements OnInit { constructor(private service: ArticlesService) { } ngOnInit(): void { - - } - loadArticles() { - this.service.getArticles().subscribe(response => this.articles = response.articles); + this.service.login(); } + loadTags() { this.service.getTags().subscribe(response => this.tags = response.tags); } + + } diff --git a/src/app/tag/tag.component.html b/src/app/tag/tag.component.html new file mode 100644 index 0000000..18ce09d --- /dev/null +++ b/src/app/tag/tag.component.html @@ -0,0 +1,6 @@ +

{{tag}}

+ + +
+ +
diff --git a/src/app/tag/tag.component.scss b/src/app/tag/tag.component.scss new file mode 100644 index 0000000..e69de29 diff --git a/src/app/tag/tag.component.spec.ts b/src/app/tag/tag.component.spec.ts new file mode 100644 index 0000000..b70f018 --- /dev/null +++ b/src/app/tag/tag.component.spec.ts @@ -0,0 +1,25 @@ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; + +import { TagComponent } from './tag.component'; + +describe('TagComponent', () => { + let component: TagComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ TagComponent ] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(TagComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/tag/tag.component.ts b/src/app/tag/tag.component.ts new file mode 100644 index 0000000..7581059 --- /dev/null +++ b/src/app/tag/tag.component.ts @@ -0,0 +1,23 @@ +import { Component, OnInit, Input } from '@angular/core'; +import { ArticlesService } from '../articles.service'; + +@Component({ + selector: 'app-tag', + templateUrl: './tag.component.html', + styleUrls: ['./tag.component.scss'] +}) +export class TagComponent implements OnInit { + + articles:any; + @Input() tag:any; + + constructor(private service:ArticlesService) { } + + ngOnInit(): void { + } + + loadArticles() { + this.service.getArticles(this.tag).subscribe(response => this.articles = response.articles); + } + +}