Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions src/app/article-detail/article-detail.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div>
<h2>{{article.title}}</h2>
<h2>{{article.description}}</h2>
<h2>{{article.body}}</h2>
<h2>{{article.createdAt}}</h2>
<h2>{{article.tagList | json}}</h2>
<button (click)="this.textAreaVisible = true;">Leave a Comment</button>
</div>
<div *ngIf="textAreaVisible">
<input type="text" #comment>
<button (click)="save(comment)">Send</button>
</div>
Empty file.
25 changes: 25 additions & 0 deletions src/app/article-detail/article-detail.component.spec.ts
Original file line number Diff line number Diff line change
@@ -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<ArticleDetailComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ArticleDetailComponent ]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(ArticleDetailComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
22 changes: 22 additions & 0 deletions src/app/article-detail/article-detail.component.ts
Original file line number Diff line number Diff line change
@@ -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();
}
}
30 changes: 26 additions & 4 deletions src/app/articles.service.ts
Original file line number Diff line number Diff line change
@@ -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<any>(url);
}
getTags() {
const url = this.baseUrl + 'tags';
return this.http.get<any>(url);
}

login(){
const url = this.baseUrl + 'users/login';
let body = { user: {email:'sebisportivo@gmail.com', password:'123123123'} };
this.http.post<any>(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);
}



}
8 changes: 2 additions & 6 deletions src/app/articles/articles.component.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@

<div *ngFor="let article of articles">
<h1>{{article.title}} </h1>
<p>{{article.body}}</p>
<div *ngFor="let tag of tags">
<app-tag [tag]="tag"></app-tag>
</div>

<button (click)="loadArticles()">Cargar</button>
{{tags | json}}
<button (click)="loadTags()">Cargar Tags</button>
8 changes: 4 additions & 4 deletions src/app/articles/articles.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}


}
6 changes: 6 additions & 0 deletions src/app/tag/tag.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>{{tag}} </h1>
<button (click)="loadArticles()">Buscar Artículos</button>

<div *ngFor = "let article of articles">
<app-article-detail [article] = "article"></app-article-detail>
</div>
Empty file added src/app/tag/tag.component.scss
Empty file.
25 changes: 25 additions & 0 deletions src/app/tag/tag.component.spec.ts
Original file line number Diff line number Diff line change
@@ -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<TagComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ TagComponent ]
})
.compileComponents();
}));

beforeEach(() => {
fixture = TestBed.createComponent(TagComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should create', () => {
expect(component).toBeTruthy();
});
});
23 changes: 23 additions & 0 deletions src/app/tag/tag.component.ts
Original file line number Diff line number Diff line change
@@ -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);
}

}