-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Getting articles data from dev.to api
Adding articles state to component store although it was not needed but added as I wanted to learn about the component store
- Loading branch information
1 parent
9fcf4f6
commit 564c152
Showing
20 changed files
with
180 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export interface Organization { | ||
name: string; | ||
username: string; | ||
slug: string; | ||
profile_image: string; | ||
profile_image_90: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export interface User { | ||
name: string; | ||
username: string; | ||
twitter_username: string; | ||
github_username: string; | ||
website_url: string; | ||
profile_image: string; | ||
profile_image_90: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Organization } from "./Organisation"; | ||
import { User } from "./User"; | ||
|
||
export interface Article { | ||
type_of: string; | ||
id: number; | ||
title: string; | ||
description: string; | ||
cover_image: string; | ||
readable_publish_date: string; | ||
social_image: string; | ||
tag_list: string[]; | ||
tags: string; | ||
slug: string; | ||
path: string; | ||
url: string; | ||
canonical_url: string; | ||
comments_count: number; | ||
positive_reactions_count: number; | ||
public_reactions_count: number; | ||
collection_id: null; | ||
created_at: Date; | ||
edited_at: Date; | ||
crossposted_at: null; | ||
published_at: Date; | ||
last_comment_at: Date; | ||
published_timestamp: Date; | ||
reading_time_minutes: number; | ||
user: User; | ||
organization: Organization; | ||
} |
2 changes: 1 addition & 1 deletion
2
src/app/pages/home/articles/article-card/article-card.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<p>article-card works!</p> | ||
<pre>{{article|json}}</pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
:host{ | ||
display: block; | ||
width:500px; | ||
overflow-x: auto; | ||
} |
5 changes: 3 additions & 2 deletions
5
src/app/pages/home/articles/article-card/article-card.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 4 additions & 2 deletions
6
src/app/pages/home/articles/article-container/article-container.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
<app-article-header></app-article-header> | ||
<app-featured-article></app-featured-article> | ||
<app-article-card></app-article-card> | ||
<app-featured-article [featured]="featuredArticle$|push"></app-featured-article> | ||
<ng-container *ngFor="let article of articles$|push"> | ||
<app-article-card [article]="article"></app-article-card> | ||
</ng-container> |
15 changes: 7 additions & 8 deletions
15
src/app/pages/home/articles/article-container/article-container.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
import { Component, OnInit } from '@angular/core'; | ||
import { Component } from '@angular/core'; | ||
import { ArticleStore } from '../services/article.store'; | ||
|
||
@Component({ | ||
selector: 'app-article-container', | ||
templateUrl: './article-container.component.html', | ||
styleUrls: ['./article-container.component.scss'] | ||
styleUrls: ['./article-container.component.scss'], | ||
}) | ||
export class ArticleContainerComponent implements OnInit { | ||
|
||
constructor() { } | ||
|
||
ngOnInit(): void { | ||
} | ||
export class ArticleContainerComponent { | ||
articles$ = this.articleStore.articles$; | ||
featuredArticle$ = this.articleStore.featuredArticle$; | ||
|
||
constructor(private articleStore: ArticleStore) {} | ||
} |
2 changes: 1 addition & 1 deletion
2
src/app/pages/home/articles/featured-article/featured-article.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<p>featured-article works!</p> | ||
<pre>{{featured|json}}</pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
:host{ | ||
display: block; | ||
width:500px; | ||
overflow-x: auto; | ||
} |
15 changes: 6 additions & 9 deletions
15
src/app/pages/home/articles/featured-article/featured-article.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,12 @@ | ||
import { Component, OnInit } from '@angular/core'; | ||
import { Component, Input, OnInit } from '@angular/core'; | ||
import { Article } from 'src/app/models/articles'; | ||
|
||
@Component({ | ||
selector: 'app-featured-article', | ||
templateUrl: './featured-article.component.html', | ||
styleUrls: ['./featured-article.component.scss'] | ||
styleUrls: ['./featured-article.component.scss'], | ||
}) | ||
export class FeaturedArticleComponent implements OnInit { | ||
|
||
constructor() { } | ||
|
||
ngOnInit(): void { | ||
} | ||
|
||
export class FeaturedArticleComponent { | ||
@Input() featured!: Article; | ||
constructor() {} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/app/pages/home/articles/services/article-api.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { ArticleApiService } from './article-api.service'; | ||
|
||
describe('ArticleApiService', () => { | ||
let service: ArticleApiService; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(ArticleApiService); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
16 changes: 16 additions & 0 deletions
16
src/app/pages/home/articles/services/article-api.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { HttpClient } from '@angular/common/http'; | ||
import { Injectable } from '@angular/core'; | ||
import { Observable } from 'rxjs'; | ||
import { Article } from 'src/app/models/articles'; | ||
import { environment } from 'src/environments/environment'; | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class ArticleApiService { | ||
constructor(private http: HttpClient) {} | ||
|
||
getArticles(): Observable<Article[]> { | ||
return this.http.get<Article[]>(`${environment.baseApi}articles`); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/app/pages/home/articles/services/article.service.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { ArticleStore } from './article.store'; | ||
|
||
describe('ArticleStore', () => { | ||
let service: ArticleStore; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({}); | ||
service = TestBed.inject(ArticleStore); | ||
}); | ||
|
||
it('should be created', () => { | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { HttpErrorResponse } from '@angular/common/http'; | ||
import { Injectable } from '@angular/core'; | ||
import { ComponentStore, tapResponse } from '@ngrx/component-store'; | ||
import { Article } from '../../../../models/articles'; | ||
import { ArticleApiService } from './article-api.service'; | ||
|
||
interface ArticlesState { | ||
articles: Article[]; | ||
featured?: Article; | ||
} | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class ArticleStore extends ComponentStore<ArticlesState> { | ||
readonly articles$ = this.select((state) => state.articles); | ||
readonly featuredArticle$ = this.select((state) => state.featured); | ||
readonly setArticles = this.updater( | ||
(state: ArticlesState, articles: Article[]) => ({ | ||
...state, | ||
articles: articles.splice(1), | ||
featured: articles[0], | ||
}) | ||
); | ||
readonly getArticles = this.effect(() => | ||
this.articleApiS.getArticles().pipe( | ||
tapResponse( | ||
(articles) => this.setArticles(articles), | ||
(error) => this.logError(error) | ||
) | ||
) | ||
); | ||
constructor(private articleApiS: ArticleApiService) { | ||
super({ articles: [] }); | ||
} | ||
|
||
logError(error: unknown) { | ||
console.error(error); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export const environment = { | ||
production: true | ||
production: true, | ||
baseApi: 'https://dev.to/api/', | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters