From 8693c0675733c94aa49a39eeff8d0a273cf38b85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benedek=20L=C3=A1szl=C3=B3?= Date: Mon, 29 Apr 2024 19:24:58 +0200 Subject: [PATCH] delete posts --- src/app/services/auth/auth.service.ts | 2 +- src/app/services/data/post.service.ts | 34 +++++++++---- src/app/services/data/user.service.ts | 2 +- src/app/shared/views/post/post.component.html | 7 ++- src/app/shared/views/post/post.component.scss | 50 ++++++++++++++----- src/app/shared/views/post/post.component.ts | 33 ++++++++++-- src/model/Post.ts | 1 + 7 files changed, 100 insertions(+), 29 deletions(-) diff --git a/src/app/services/auth/auth.service.ts b/src/app/services/auth/auth.service.ts index 3fc7caf..4574368 100644 --- a/src/app/services/auth/auth.service.ts +++ b/src/app/services/auth/auth.service.ts @@ -18,7 +18,7 @@ export class AuthService { } } - getToken() { + getUser() { return this.auth.user; } diff --git a/src/app/services/data/post.service.ts b/src/app/services/data/post.service.ts index c07d435..9cffea8 100644 --- a/src/app/services/data/post.service.ts +++ b/src/app/services/data/post.service.ts @@ -10,19 +10,32 @@ import { user } from '@angular/fire/auth'; providedIn: 'root' }) export class PostService { - constructor(private auth: AuthService, private afs: AngularFirestore) { } getFeed(length: Number, start: Number): Promise> { return new Promise>((resolve, reject) => { + let posts = Array(); + if (this.auth.checkAllowed()) { - this.afs.collection('post').valueChanges().subscribe(data => { + this.afs.collection('post').valueChanges({idField: 'id'}).subscribe(data => { data.forEach(i => { this.afs.doc((i as any).user).valueChanges().subscribe(user => { - i.user = user as User; + posts.push({ + user: { + username: (user as User).username, + email: (user as User).email, + picture: (user as User).picture, + followed: (user as User).followed + }, + content: i.content, + image: i.image, + likes: i.likes, + liked: i.liked, + id: i.id + }) }); }); - resolve(data); + resolve(posts); }); } else { reject() @@ -31,13 +44,12 @@ export class PostService { } getPosts(username: String, lenght: Number, start: Number) { - console.log(username) return new Promise>((resolve, reject) => { if (this.auth.checkAllowed()) { if (username == undefined) { - this.auth.getToken().subscribe(cred => { + this.auth.getUser().subscribe(cred => { this.afs.collection('user', ref => ref.where('id', '==', cred?.uid)).valueChanges().subscribe(user => { - this.afs.collection('post', ref => ref.where('user', '==', this.afs.doc('/user/' + user[0].username).ref)).valueChanges().subscribe(data => { + this.afs.collection('post', ref => ref.where('user', '==', this.afs.doc('/user/' + user[0].username).ref)).valueChanges({idField:'id'}).subscribe(data => { data.forEach(i => { i.user = user[0] as User; }) @@ -47,7 +59,7 @@ export class PostService { }); } else { this.afs.collection('user').doc(username as string).valueChanges().subscribe(user => { - this.afs.collection('post', ref => ref.where('user', '==', this.afs.doc('/user/' + username).ref)).valueChanges().subscribe(data => { + this.afs.collection('post', ref => ref.where('user', '==', this.afs.doc('/user/' + username).ref)).valueChanges({idField:'id'}).subscribe(data => { data.forEach(i => { i.user = user as User; }) @@ -66,7 +78,7 @@ export class PostService { return new Promise((resolve, reject) => { console.log('new post') if (this.auth.checkAllowed()) { - this.auth.getToken().subscribe(data => { + this.auth.getUser().subscribe(data => { this.afs.collection('user', ref => ref.where('id', '==', data?.uid)).valueChanges().subscribe(user => { this.afs.collection('post').add({ user: this.afs.doc('/user/' + user[0].username).ref, @@ -82,4 +94,8 @@ export class PostService { } }); } + + removePost(id: String) { + return this.afs.doc('/post/'+id).delete(); + } } diff --git a/src/app/services/data/user.service.ts b/src/app/services/data/user.service.ts index 695a45f..f3ca9a8 100644 --- a/src/app/services/data/user.service.ts +++ b/src/app/services/data/user.service.ts @@ -16,7 +16,7 @@ export class UserService { if (username != undefined) { this.afs.collection('user', ref => ref.where('username', '==', username)).valueChanges().subscribe(data => { resolve(data[0] as User) }); } else { - this.auth.getToken().subscribe(cred => { + this.auth.getUser().subscribe(cred => { this.afs.collection('user', ref => ref.where('id', '==', cred?.uid)).valueChanges().subscribe(user => { resolve(user[0]) }) }); } diff --git a/src/app/shared/views/post/post.component.html b/src/app/shared/views/post/post.component.html index 9af22d8..4c7faa9 100644 --- a/src/app/shared/views/post/post.component.html +++ b/src/app/shared/views/post/post.component.html @@ -1,6 +1,9 @@ -
+
{{post?.content}}
- {{post?.likes}} +
+ {{post?.likes}} + +
\ No newline at end of file diff --git a/src/app/shared/views/post/post.component.scss b/src/app/shared/views/post/post.component.scss index 10e0c17..692ac26 100644 --- a/src/app/shared/views/post/post.component.scss +++ b/src/app/shared/views/post/post.component.scss @@ -14,20 +14,46 @@ border-radius: 4px; } - .likes { - display: block; - margin: 10px auto; - width: 10px; - aspect-ratio: 1; - } + .icons { + display: flex; + margin: 10px; - .likes::before { - content: "❤️"; - filter: saturate(0); - } + span { + flex: 1; + text-align: center; + display: inline-block; + height: fit-content; + } - .likes.liked::before { - filter: saturate(1) !important; + .likes::before { + content: "❤️"; + filter: saturate(0); + } + + .likes.liked::before { + filter: saturate(1) !important; + } + + .owned::before { + content: "❌"; + } } } +@keyframes delete { + 0% { + transform: scaleY(1); + opacity: 100%; + } + + 100% { + transform: scaleY(0); + display: none; + opacity: 0%; + } +} + +.deleted { + transform-origin: top center; + animation: delete 1s forwards; +} \ No newline at end of file diff --git a/src/app/shared/views/post/post.component.ts b/src/app/shared/views/post/post.component.ts index 7165b18..5d0e787 100644 --- a/src/app/shared/views/post/post.component.ts +++ b/src/app/shared/views/post/post.component.ts @@ -1,16 +1,41 @@ -import { Component, Input } from '@angular/core'; - +import { Component, Input, OnInit } from '@angular/core'; +import { CommonModule } from '@angular/common'; import { SmallUserComponent } from '../small-user/small-user.component'; import { Post } from '../../../../model/Post'; import { NgIf } from '@angular/common'; +import { AuthService } from '../../../services/auth/auth.service'; +import { PostService } from '../../../services/data/post.service'; @Component({ selector: 'app-post', standalone: true, - imports: [SmallUserComponent, NgIf], + imports: [SmallUserComponent, NgIf, CommonModule], templateUrl: './post.component.html', styleUrl: './post.component.scss' }) -export class PostComponent { +export class PostComponent implements OnInit { + @Input() post: Post | undefined; + + isOwned: boolean = false; + deleted: boolean = false; + + constructor(private auth: AuthService, private postSv: PostService) { } + + ngOnInit(): void { + this.auth.getUser().subscribe(user => { + this.isOwned = user?.email == this.post?.user.email; + }); + } + + remove() { + this.postSv.removePost(this.post?.id as String) + .then(() => { + this.deleted = true; + console.log(this.deleted); + }) + .catch(() => { + alert("failed to delete post"); + }) + } } diff --git a/src/model/Post.ts b/src/model/Post.ts index ed1f8c3..3179d3c 100644 --- a/src/model/Post.ts +++ b/src/model/Post.ts @@ -6,4 +6,5 @@ export interface Post { image: String; likes: Number; liked: Boolean; + id: String; } \ No newline at end of file