Skip to content

Commit

Permalink
refact: refact fireblog-page and post card with edit and delete imple…
Browse files Browse the repository at this point in the history
…mentation
  • Loading branch information
Sye0w committed Oct 4, 2024
1 parent b0fef5e commit a7f6977
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
18 changes: 15 additions & 3 deletions src/app/components/post-card/post-card.component.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { Component, Input } from '@angular/core';
import { Component, EventEmitter, Input, Output } from '@angular/core';
import { LikesComponent } from "../likes/likes/likes.component";
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { MatButtonModule } from '@angular/material/button';
import { MatFormFieldModule } from '@angular/material/form-field';
import { MatInputModule } from '@angular/material/input';
import { MatIconModule } from '@angular/material/icon';
import { MatExpansionModule } from '@angular/material/expansion';
import { IBlog, IUser } from '../../services/blog.interface';
import { FireblogFacadeService } from '../../services/fireblog/fireblog-facade.service';
// import { FireblogFacadeService } from '../../services/fireblog-facade.service';
import { CommentsComponent } from "../comments/comments.component";
import { PrependAtPipe } from '../../pipe/prepend-at.pipe';

@Component({
selector: 'app-post-card',
Expand All @@ -20,13 +22,17 @@ import { FireblogFacadeService } from '../../services/fireblog/fireblog-facade.s
MatButtonModule,
MatFormFieldModule,
MatInputModule,
MatIconModule
MatIconModule,
MatExpansionModule,
CommentsComponent,
PrependAtPipe
],
templateUrl: './post-card.component.html',
styleUrl: './post-card.component.scss'
})
export class PostCardComponent {
@Input() blogPost!: IBlog;
@Output() commentToggled = new EventEmitter<string>();

randomAvatarUrl: string = '';
currentUser: IUser | null = null;
Expand Down Expand Up @@ -94,4 +100,10 @@ export class PostCardComponent {
.catch(error => console.error('Error deleting post:', error));
}
}

toggleComments() {
if (this.blogPost.id) {
this.commentToggled.emit(this.blogPost.id);
}
}
}
11 changes: 9 additions & 2 deletions src/app/services/fireblog/fireblog-facade.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Injectable } from '@angular/core';
import { collectionData, docData } from '@angular/fire/firestore';
import { collection, doc, addDoc, updateDoc, deleteDoc } from 'firebase/firestore';
import { collection, doc, addDoc, updateDoc, deleteDoc, arrayUnion } from 'firebase/firestore';
import { Firestore } from '@angular/fire/firestore';
import { BehaviorSubject, Observable } from 'rxjs';
import { IBlog, IUser } from '../blog.interface';
import { IBlog, IComment, IUser } from '../blog.interface';
import { Auth, User, onAuthStateChanged } from '@angular/fire/auth';

@Injectable({
Expand Down Expand Up @@ -78,4 +78,11 @@ export class FireblogFacadeService {
const blogPostDoc = doc(this.firestore, `fireblogPosts/${id}`);
await deleteDoc(blogPostDoc);
}

async addCommentToBlogPost(postId: string, comment: IComment): Promise<void> {
const blogPostDoc = doc(this.firestore, `fireblogPosts/${postId}`);
await updateDoc(blogPostDoc, {
comments: arrayUnion(comment)
});
}
}

0 comments on commit a7f6977

Please sign in to comment.