Olá,
Estou tentando implementar no sistema da alurapic algo similar ao que foi acrescentado no método "like", para atualizar o valor do número de comentários toda a vez que o comentário for adicionado. Mas, estou um pouco perdido. Pensei em utilizar o @Output e EventEmitter, e, talvez o BehaviorSubject. Mas, estou na dúvida como implementá-los..
Componente pai: Photo-details html
<div class="bg-white border" *ngIf="(photo$ | async) as photo">
<div class="row ml-1 mr-1">
<div class="col-lg-7 text-center">
<ap-photo [url]="photo.url" [description]="photo.description"></ap-photo>
</div>
<div class="col-lg-4 ml-1 mr-1 p-4">
<p class="text-lg-left break-word">{{ photo.description }}</p>
<small>
<div class="text-left mb-4">
<i
(click)="like(photo)"
class="fa fa-heart-o fa-2x mr-2 pull-left"
>
</i>{{ photo.likes }}
<i
class="fa fa-comment-o fa-2x mr-2 ml-2"
>
</i>{{ photo.comments }}
<i
photoOwnerOnly [ownedPhoto]="photo"
(click)="remove()"
class="fa fa-trash-o fa-2x pull-right"
>
</i>
</div>
<hr>
</small>
<ap-photo-comments
*ngIf="photo.allowComments; else warning"
[photoId]="photoId"
>
</ap-photo-comments>
<ng-template #warning>
<p>User has disabled comments for this photo</p>
</ng-template>
</div>
</div>
</div>
ts
export class PhotoDetailsComponent implements OnInit {
photo$: Observable<Photo>;
photoId: number;
public commentsCount: number;
constructor(
private route: ActivatedRoute,
private photoService: PhotoService,
private router: Router,
private alertService: AlertService,
private userService: UserService
) { }
ngOnInit(): void {
this.photoId = this.route.snapshot.params.photoId;
this.photo$ = this.photoService.findById(this.photoId);
this.photo$
.subscribe(
() => {},
err => {
this.router.navigate(['not-found']);
console.log(err);
}
);
}
// código remove()
like(photo: Photo) {
this.photoService
.like(photo.id)
.subscribe(
liked => {
if(liked){
this.photo$ = this.photoService.findById(photo.id);
}
},
err => {
console.log(err);
this.alertService.warning('Please, login first.', true);
this.router.navigate(['']);
}
);
}
}
Componente filho: Photo-comments html
<div class="mt-4">
<form [formGroup]="commentForm" (submit)="save()">
<div class="input-group">
<textarea
formControlName="comment"
class="form-control"
>
</textarea>
<div class="input-group-append">
<button
class="btn btn-dark pull-left"
[disabled]="commentForm.invalid || commentForm.pending"
>Publish
</button>
</div>
</div>
<ap-vmessage
text="Maximun size allowed is 300"
*ngIf="commentForm.get('comment').errors?.maxlength"
>
</ap-vmessage>
</form>
</div>
ts
save() {
if(this.userService.isLogged()) {
const comment = this.commentForm.get('comment').value as string;
this.comments$ = this.photoService
.addComment(this.photoId, comment)
.pipe(switchMap(() => this.photoService.getComments(this.photoId)))
.pipe(tap(() => {
this.commentForm.reset();
this.alertService.success('Comment has been added!');
}));
} else {
this.alertService.warning('Login before comment', true);
this.router.navigate(['']);
}
};