-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(DiscourseCategoryActivityComponent): Convert to standalone (#…
- Loading branch information
1 parent
23d1260
commit 487d8b7
Showing
5 changed files
with
49 additions
and
59 deletions.
There are no files selected for viewing
34 changes: 19 additions & 15 deletions
34
...pp/modules/library/discourse-category-activity/discourse-category-activity.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,16 +1,20 @@ | ||
<ng-container *ngIf="isValidCategoryURL"> | ||
<mat-divider></mat-divider> | ||
<p fxLayoutAlign="start center"> | ||
<mat-icon>forum</mat-icon> | ||
<a href="{{ categoryURL }}" target="_blank"> | ||
<strong i18n>Discuss ({{ postCount }}{{ hasMoreTopics ? '+' : '' }})</strong> | ||
</a> | ||
</p> | ||
<ul class="topics" *ngIf="topics.length"> | ||
<li *ngFor="let topic of topics.slice(0, 3)"> | ||
<a href="{{ discourseBaseUrl }}/t/{{ topic.slug }}/{{ topic.id }}" target="_blank"> | ||
{{ topic.title }} | ||
</a> | ||
</li> | ||
@if (validCategoryURL) { | ||
<mat-divider /> | ||
<p fxLayoutAlign="start center"> | ||
<mat-icon>forum</mat-icon> | ||
<a href="{{ categoryURL }}" target="_blank"> | ||
<strong i18n>Discuss ({{ postCount }}{{ hasMoreTopics ? '+' : '' }})</strong> | ||
</a> | ||
</p> | ||
@if (topics.length > 0) { | ||
<ul class="topics"> | ||
@for (topic of topics.slice(0,3); track topic.id) { | ||
<li> | ||
<a href="{{ discourseBaseUrl }}/t/{{ topic.slug }}/{{ topic.id }}" target="_blank"> | ||
{{ topic.title }} | ||
</a> | ||
</li> | ||
} | ||
</ul> | ||
</ng-container> | ||
} | ||
} |
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
40 changes: 18 additions & 22 deletions
40
src/app/modules/library/discourse-category-activity/discourse-category-activity.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,44 +1,40 @@ | ||
import { CommonModule } from '@angular/common'; | ||
import { HttpClient } from '@angular/common/http'; | ||
import { Component, Input } from '@angular/core'; | ||
import { FlexLayoutModule } from '@angular/flex-layout'; | ||
import { MatDividerModule } from '@angular/material/divider'; | ||
import { MatIconModule } from '@angular/material/icon'; | ||
|
||
@Component({ | ||
imports: [CommonModule, FlexLayoutModule, MatDividerModule, MatIconModule], | ||
selector: 'discourse-category-activity', | ||
templateUrl: 'discourse-category-activity.component.html', | ||
styleUrls: ['discourse-category-activity.component.scss'] | ||
standalone: true, | ||
styleUrl: 'discourse-category-activity.component.scss', | ||
templateUrl: 'discourse-category-activity.component.html' | ||
}) | ||
export class DiscourseCategoryActivityComponent { | ||
@Input() | ||
categoryURL: string = ''; | ||
|
||
discourseBaseUrl: string = ''; | ||
hasMoreTopics: boolean = false; | ||
isValidCategoryURL: boolean = false; | ||
postCount: number = 0; | ||
topics: any[] = []; | ||
@Input() categoryURL: string = ''; | ||
protected discourseBaseUrl: string = ''; | ||
protected hasMoreTopics: boolean; | ||
protected postCount: number = 0; | ||
protected topics: any[] = []; | ||
protected validCategoryURL: boolean; | ||
|
||
constructor(private http: HttpClient) {} | ||
|
||
ngOnInit() { | ||
ngOnInit(): void { | ||
this.retrieveCategory(); | ||
} | ||
|
||
retrieveCategory(): void { | ||
private retrieveCategory(): void { | ||
this.http.get(`${this.categoryURL}.json?order=latest`).subscribe(({ topic_list }: any) => { | ||
if (topic_list.topics) { | ||
this.isValidCategoryURL = true; | ||
this.validCategoryURL = true; | ||
this.topics = topic_list.topics; | ||
this.postCount = this.countPosts(); | ||
this.postCount = this.topics.reduce((sum, topic) => sum + topic.posts_count, 0); | ||
this.discourseBaseUrl = this.categoryURL.match(/(.+)\/c\/.+/)[1]; | ||
this.hasMoreTopics = topic_list.more_topics_url ? true : false; | ||
} | ||
}); | ||
} | ||
|
||
countPosts(): number { | ||
let postCount = 0; | ||
this.topics.forEach((topic) => { | ||
postCount += topic.posts_count; | ||
}); | ||
return postCount; | ||
} | ||
} |
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