|
@@ -2,7 +2,18 @@ import { Component, OnInit } from '@angular/core';
|
|
|
import { ActivatedRoute } from '@angular/router';
|
|
|
import { CloudQuery } from '../../../lib/ncloud';
|
|
|
import { Location } from '@angular/common';
|
|
|
+import { CommentModalComponent } from '../../comment-modal/comment-modal.component';
|
|
|
+import { ModalController } from '@ionic/angular';
|
|
|
+import { CommentsListModalComponent } from 'src/app/tab1/post-detail/comments-list-modal/comments-list-modal.component';
|
|
|
|
|
|
+interface Comment {
|
|
|
+ username: string;
|
|
|
+ avatar: string;
|
|
|
+ content: string;
|
|
|
+ user: {
|
|
|
+ objectId: string;
|
|
|
+ };
|
|
|
+}
|
|
|
@Component({
|
|
|
selector: 'app-post-detail',
|
|
|
templateUrl: './post-detail.component.html',
|
|
@@ -14,13 +25,69 @@ export class PostDetailComponent implements OnInit {
|
|
|
liked: boolean = false;
|
|
|
collected: boolean = false;
|
|
|
followed: boolean = false;
|
|
|
-
|
|
|
- constructor(private route: ActivatedRoute, private location: Location) {}
|
|
|
+ comments: { username: string; avatar: string; content: string; }[] = [];
|
|
|
+ showComments: boolean = false;
|
|
|
+ constructor(private route: ActivatedRoute, private location: Location,private modalController: ModalController) {}
|
|
|
|
|
|
ngOnInit() {
|
|
|
this.loadPostDetail();
|
|
|
+ this.loadComments();
|
|
|
+ }
|
|
|
+ async loadComments() {
|
|
|
+ if (this.post) {
|
|
|
+ this.comments = await this.fetchComments(this.post.objectId);
|
|
|
+ }
|
|
|
}
|
|
|
+ toggleComments() {
|
|
|
+ this.showComments = !this.showComments;
|
|
|
+ }
|
|
|
+ async fetchComments(postId: string): Promise<{ username: string; avatar: string; content: string; }[]> {
|
|
|
+ const commentQuery = new CloudQuery('HnbComment');
|
|
|
+ const results: Comment[] = await commentQuery.find1({ post: { __type: 'Pointer', className: 'HnbPost', objectId: postId } });
|
|
|
+
|
|
|
+
|
|
|
+ const commentsData = await Promise.all(results.map(async (comment: Comment) => {
|
|
|
+ const userQuery = new CloudQuery('HnbUser');
|
|
|
+ const user = await userQuery.get(comment.user.objectId);
|
|
|
+ console.log('Fetched user:', user);
|
|
|
+ return {
|
|
|
+ username: user.username,
|
|
|
+ avatar: user.avatarUrl,
|
|
|
+ content: comment.content
|
|
|
+ };
|
|
|
+ }));
|
|
|
|
|
|
+ return commentsData;
|
|
|
+ }
|
|
|
+ async openCommentsListModal(): Promise<void> {
|
|
|
+ const modal = await this.modalController.create({
|
|
|
+ component: CommentsListModalComponent,
|
|
|
+ cssClass: 'comments-list-modal',
|
|
|
+ componentProps: {
|
|
|
+ postId: this.post.objectId,
|
|
|
+ },
|
|
|
+ });
|
|
|
+
|
|
|
+ await modal.present();
|
|
|
+ }
|
|
|
+
|
|
|
+ async openCommentDialog(): Promise<void> {
|
|
|
+ const modal = await this.modalController.create({
|
|
|
+ component: CommentModalComponent,
|
|
|
+ cssClass: 'comment-modal',
|
|
|
+ backdropDismiss: true,
|
|
|
+ breakpoints: [0.5, 0.7],
|
|
|
+ initialBreakpoint: 0.5
|
|
|
+ });
|
|
|
+
|
|
|
+ await modal.present();
|
|
|
+
|
|
|
+ const { data, role } = await modal.onWillDismiss();
|
|
|
+
|
|
|
+ if (role === 'confirm') {
|
|
|
+ await this.submitComment(data);
|
|
|
+ }
|
|
|
+ }
|
|
|
async loadPostDetail() {
|
|
|
const objectId = this.route.snapshot.paramMap.get('id');
|
|
|
if (objectId) {
|
|
@@ -120,58 +187,63 @@ export class PostDetailComponent implements OnInit {
|
|
|
|
|
|
async toggleCollect() {
|
|
|
try {
|
|
|
- const username = await this.getLatestUserName();
|
|
|
+ const username = await this.getLatestUserName();
|
|
|
|
|
|
- if (!username) {
|
|
|
- console.error('Failed to get latest username.');
|
|
|
- return;
|
|
|
- }
|
|
|
+ if (!username) {
|
|
|
+ console.error('Failed to get latest username.');
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- const collectQuery = new CloudQuery('HnbColloct');
|
|
|
+ const collectQuery = new CloudQuery('HnbColloct');
|
|
|
|
|
|
- if (this.collected) {
|
|
|
-
|
|
|
- const collectEntry = await collectQuery.findOne({
|
|
|
- username: username,
|
|
|
- post: { __type: 'Pointer', className: 'HnbPost', objectId: this.post.objectId }
|
|
|
- });
|
|
|
+ if (this.collected) {
|
|
|
+
|
|
|
+ const collectEntry = await collectQuery.findOne({
|
|
|
+ username: username,
|
|
|
+ title: { __type: 'Pointer', className: 'HnbPost', objectId: this.post.objectId }
|
|
|
+ });
|
|
|
|
|
|
- if (collectEntry) {
|
|
|
- await collectQuery.delete(collectEntry.objectId);
|
|
|
- this.post.collectCount -= 1;
|
|
|
- }
|
|
|
- } else {
|
|
|
-
|
|
|
- const newCollectEntry = {
|
|
|
- username: username,
|
|
|
- title: { __type: 'Pointer', className: 'HnbPost', objectId: this.post.objectId }
|
|
|
- };
|
|
|
- await collectQuery.add(newCollectEntry);
|
|
|
- this.post.collectCount += 1;
|
|
|
- }
|
|
|
+ if (collectEntry) {
|
|
|
+ await collectQuery.delete(collectEntry.objectId);
|
|
|
+ this.post.collectCount -= 1;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+
|
|
|
+ const newCollectEntry = {
|
|
|
+ username: username,
|
|
|
+ title: { __type: 'Pointer', className: 'HnbPost', objectId: this.post.objectId }
|
|
|
+ };
|
|
|
+ await collectQuery.add(newCollectEntry);
|
|
|
+ this.post.collectCount += 1;
|
|
|
+ }
|
|
|
|
|
|
- console.log('Updating post with collectCount:', this.post.collectCount);
|
|
|
- await new CloudQuery('HnbPost').update(this.post.objectId, { collectCount: this.post.collectCount });
|
|
|
- this.collected = !this.collected;
|
|
|
+ console.log('Updating post with collectCount:', this.post.collectCount);
|
|
|
+ await new CloudQuery('HnbPost').update(this.post.objectId, { collectCount: this.post.collectCount });
|
|
|
+ this.collected = !this.collected;
|
|
|
} catch (error) {
|
|
|
- console.error('Error updating collect status:', error);
|
|
|
+ console.error('Error updating collect status:', error);
|
|
|
}
|
|
|
- }
|
|
|
+}
|
|
|
|
|
|
- async loadAuthorDetails(author: any) {
|
|
|
- if (author && author.objectId) {
|
|
|
- try {
|
|
|
- const userQuery = new CloudQuery('HnbUser');
|
|
|
- const authorData = await userQuery.get(author.objectId);
|
|
|
- this.authorName = authorData.username;
|
|
|
- } catch (error) {
|
|
|
- console.error('Error loading author details:', error);
|
|
|
- this.authorName = '未知作者';
|
|
|
- }
|
|
|
- } else {
|
|
|
- this.authorName = '未知作者';
|
|
|
+async loadAuthorDetails(author: any) {
|
|
|
+ if (author && author.objectId) {
|
|
|
+ try {
|
|
|
+ const userQuery = new CloudQuery('HnbUser');
|
|
|
+ const authorData = await userQuery.get(author.objectId);
|
|
|
+ this.authorName = authorData.username;
|
|
|
+
|
|
|
+
|
|
|
+ this.post.author.avatar = authorData.avatar || 'path/to/default/avatar.png';
|
|
|
+ } catch (error) {
|
|
|
+ console.error('Error loading author details:', error);
|
|
|
+ this.authorName = '未知作者';
|
|
|
+ this.post.author.avatar = 'path/to/default/avatar.png';
|
|
|
}
|
|
|
+ } else {
|
|
|
+ this.authorName = '未知作者';
|
|
|
+ this.post.author.avatar = 'path/to/default/avatar.png';
|
|
|
}
|
|
|
+}
|
|
|
|
|
|
async getLatestUserName(): Promise<string | null> {
|
|
|
const url = `http://dev.fmode.cn:1337/parse/classes/HnbUser2?order=-createdAt&limit=1`;
|
|
@@ -195,6 +267,31 @@ export class PostDetailComponent implements OnInit {
|
|
|
|
|
|
return result.results.length > 0 ? result.results[0].username : null;
|
|
|
}
|
|
|
+ async submitComment(content: string) {
|
|
|
+ if (!content || content.trim() === '') {
|
|
|
+ console.error('Comment content is empty.');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const username = await this.getLatestUserName();
|
|
|
+
|
|
|
+ if (!username) {
|
|
|
+ console.error('Failed to get latest username.');
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const commentEntry = {
|
|
|
+ username: username,
|
|
|
+ content: content,
|
|
|
+ post: { __type: 'Pointer', className: 'HnbPost', objectId: this.post.objectId }
|
|
|
+ };
|
|
|
+
|
|
|
+ const commentQuery = new CloudQuery('HnbComment');
|
|
|
+ await commentQuery.add(commentEntry);
|
|
|
+ console.log('Comment submitted:', commentEntry);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
|
|
|
goBack() {
|
|
|
this.location.back();
|