-
-
Notifications
You must be signed in to change notification settings - Fork 270
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c95da0
commit a7d2f3c
Showing
12 changed files
with
174 additions
and
127 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,10 @@ | ||
.form-group.required { | ||
position: relative; | ||
} | ||
.form-group.required::after { | ||
content: '*'; | ||
position: absolute; | ||
top: 5px; | ||
left: 8px; | ||
color: #f00; | ||
} |
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
75 changes: 27 additions & 48 deletions
75
libs/articles/feature-article-edit/src/lib/article-edit.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,72 +1,51 @@ | ||
import { DynamicFormComponent, Field, formsActions, ListErrorsComponent, ngrxFormsQuery } from '@realworld/core/forms'; | ||
import { ChangeDetectionStrategy, Component, DestroyRef, effect, inject, OnInit, untracked } from '@angular/core'; | ||
import { InputErrorsComponent, ListErrorsComponent } from '@realworld/core/forms'; | ||
import { ChangeDetectionStrategy, Component, effect, inject } from '@angular/core'; | ||
import { OnDestroy } from '@angular/core'; | ||
import { Validators } from '@angular/forms'; | ||
import { Store } from '@ngrx/store'; | ||
import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms'; | ||
import { ArticleStore } from '@realworld/articles/data-access'; | ||
|
||
const structure: Field[] = [ | ||
{ | ||
type: 'INPUT', | ||
name: 'title', | ||
placeholder: 'Article Title', | ||
validator: [Validators.required], | ||
}, | ||
{ | ||
type: 'INPUT', | ||
name: 'description', | ||
placeholder: "What's this article about?", | ||
validator: [Validators.required], | ||
}, | ||
{ | ||
type: 'TEXTAREA', | ||
name: 'body', | ||
placeholder: 'Write your article (in markdown)', | ||
validator: [Validators.required], | ||
}, | ||
{ | ||
type: 'INPUT', | ||
name: 'tagList', | ||
placeholder: 'Enter Tags', | ||
validator: [], | ||
}, | ||
]; | ||
|
||
@Component({ | ||
selector: 'cdt-article-edit', | ||
standalone: true, | ||
templateUrl: './article-edit.component.html', | ||
styleUrls: ['./article-edit.component.css'], | ||
imports: [DynamicFormComponent, ListErrorsComponent], | ||
imports: [ListErrorsComponent, ReactiveFormsModule, InputErrorsComponent], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class ArticleEditComponent implements OnInit, OnDestroy { | ||
private readonly store = inject(Store); | ||
export class ArticleEditComponent implements OnDestroy { | ||
private readonly articleStore = inject(ArticleStore); | ||
private readonly fb = inject(FormBuilder); | ||
|
||
structure$ = this.store.select(ngrxFormsQuery.selectStructure); | ||
data$ = this.store.select(ngrxFormsQuery.selectData); | ||
form = this.fb.nonNullable.group({ | ||
title: ['', [Validators.required]], | ||
description: ['', [Validators.required]], | ||
body: ['', [Validators.required]], | ||
tagList: [''], | ||
}); | ||
|
||
readonly setArticleDataToForm = effect(() => { | ||
const articleLoaded = this.articleStore.getArticleLoaded(); | ||
if (articleLoaded) { | ||
untracked(() => this.store.dispatch(formsActions.setData({ data: this.articleStore.data() }))); | ||
this.form.patchValue({ | ||
title: this.articleStore.data.title(), | ||
description: this.articleStore.data.description(), | ||
body: this.articleStore.data.body(), | ||
tagList: this.articleStore.data.tagList().join(', '), | ||
}); | ||
} | ||
}); | ||
|
||
ngOnInit() { | ||
this.store.dispatch(formsActions.setStructure({ structure })); | ||
} | ||
|
||
updateForm(changes: any) { | ||
this.store.dispatch(formsActions.updateData({ data: changes })); | ||
} | ||
|
||
submit() { | ||
this.articleStore.publishArticle(); | ||
onSubmit() { | ||
if (this.articleStore.data.slug()) { | ||
this.articleStore.editArticle(this.form.getRawValue()); | ||
} else { | ||
this.articleStore.publishArticle({ | ||
article: { ...this.form.getRawValue(), tagList: this.form.controls.tagList.value.split(',') }, | ||
}); | ||
} | ||
} | ||
|
||
ngOnDestroy() { | ||
this.store.dispatch(formsActions.initializeForm()); | ||
this.form.reset(); | ||
} | ||
} |
33 changes: 19 additions & 14 deletions
33
libs/articles/feature-article/src/lib/add-comment/add-comment.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,18 +1,23 @@ | ||
<cdt-list-errors> </cdt-list-errors> | ||
|
||
<div class="card comment-form"> | ||
<cdt-dynamic-form | ||
class="card-block" | ||
(updateForm)="updateForm.emit($event)" | ||
[data$]="data$" | ||
[structure$]="structure$" | ||
[touchedForm$]="touchedForm$" | ||
> | ||
</cdt-dynamic-form> | ||
<div class="card-footer"> | ||
<img [src]="currentUser().image" class="comment-author-img" /> | ||
<button (click)="submitComment.emit(article().slug)" class="btn btn-sm btn-primary" type="submit"> | ||
Post Comment | ||
</button> | ||
</div> | ||
<form [formGroup]="form" (ngSubmit)="submitComment.emit(form.controls.comment.value)"> | ||
<fieldset class="form-group required"> | ||
<textarea | ||
formControlName="comment" | ||
rows="5" | ||
type="text" | ||
placeholder="Write a comment..." | ||
class="form-control form-control-lg" | ||
> | ||
</textarea> | ||
<cdt-input-errors [control]="form.controls.comment" /> | ||
</fieldset> | ||
<div class="card-footer"> | ||
<img [src]="currentUser().image" class="comment-author-img" /> | ||
<button (click)="submitComment.emit(form.controls.comment.value)" class="btn btn-sm btn-primary" type="submit"> | ||
Post Comment | ||
</button> | ||
</div> | ||
</form> | ||
</div> |
18 changes: 10 additions & 8 deletions
18
libs/articles/feature-article/src/lib/add-comment/add-comment.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,21 +1,23 @@ | ||
import { Article, User } from '@realworld/core/api-types'; | ||
import { DynamicFormComponent, Field, ListErrorsComponent } from '@realworld/core/forms'; | ||
import { ChangeDetectionStrategy, Component, Input, input, output } from '@angular/core'; | ||
import { Observable } from 'rxjs'; | ||
import { InputErrorsComponent, ListErrorsComponent } from '@realworld/core/forms'; | ||
import { ChangeDetectionStrategy, Component, inject, input, output } from '@angular/core'; | ||
import { FormBuilder, ReactiveFormsModule } from '@angular/forms'; | ||
|
||
@Component({ | ||
selector: 'cdt-add-comment', | ||
standalone: true, | ||
templateUrl: './add-comment.component.html', | ||
imports: [ListErrorsComponent, DynamicFormComponent], | ||
imports: [ListErrorsComponent, ReactiveFormsModule, InputErrorsComponent], | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class AddCommentComponent { | ||
private readonly fb = inject(FormBuilder); | ||
|
||
article = input.required<Article>(); | ||
currentUser = input.required<User>(); | ||
@Input() data$!: Observable<any>; | ||
@Input() structure$!: Observable<Field[]>; | ||
@Input() touchedForm$!: Observable<boolean>; | ||
submitComment = output<string>(); | ||
updateForm = output<string>(); | ||
|
||
form = this.fb.nonNullable.group({ | ||
comment: [''], | ||
}); | ||
} |
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
Oops, something went wrong.