Skip to content

Commit 171ba65

Browse files
committed
fix: comments in home page
1 parent ebfd79b commit 171ba65

File tree

5 files changed

+61
-62
lines changed

5 files changed

+61
-62
lines changed

platforms/pictique/src/lib/fragments/Post/Post.svelte

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
></Avatar>
113113
<h2>{username}</h2>
114114
</div>
115-
<ActionMenu {options}/>
115+
<ActionMenu {options} />
116116
</div>
117117
{/if}
118118
{#if imgUris.length > 0}
@@ -128,22 +128,22 @@
128128
<div
129129
bind:this={galleryRef}
130130
onscroll={handleScroll}
131-
class="hide-scrollbar rounded-4xl flex aspect-[4/5] snap-x snap-mandatory flex-nowrap gap-2 overflow-hidden overflow-x-scroll md:aspect-[16/9]"
131+
class="hide-scrollbar flex aspect-[4/5] snap-x snap-mandatory flex-nowrap gap-2 overflow-hidden overflow-x-scroll rounded-4xl md:aspect-[16/9]"
132132
>
133133
{#each imgUris as img}
134134
<div class="aspect-[4/5] h-full w-full snap-center md:aspect-[16/9]">
135135
<img
136136
src={img}
137137
alt={text}
138-
class="rounded-4xl h-full w-full object-cover"
138+
class="h-full w-full rounded-4xl object-cover"
139139
onerror={handleImageError}
140140
/>
141141
</div>
142142
{/each}
143143
</div>
144144
{#if imgUris.length > 1}
145145
<div
146-
class="absolute bottom-4 start-[50%] mt-2 flex translate-x-[-50%] items-center justify-center gap-1"
146+
class="absolute start-[50%] bottom-4 mt-2 flex translate-x-[-50%] items-center justify-center gap-1"
147147
>
148148
{#if imgUris.length > 1}
149149
<div class="mt-2 flex items-center justify-center gap-1">

platforms/pictique/src/lib/fragments/SettingsNavigationButton/SettingsNavigationButton.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
<img
3939
width="24px"
4040
height="24px"
41-
class="flex aspect-square size-10 items-center justify-center rounded-full md:size-12 object-cover"
41+
class="flex aspect-square size-10 items-center justify-center rounded-full object-cover md:size-12"
4242
src={profileSrc}
4343
alt=""
4444
/>

platforms/pictique/src/lib/fragments/SideBar/SideBar.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@
160160
width="24px"
161161
height="24px"
162162
class="aspect-square rounded-full object-cover"
163-
src={profileSrc || "https://picsum.photos/200/200"}
163+
src={profileSrc || 'https://picsum.photos/200/200'}
164164
alt="profile"
165165
/>
166166
</span>

platforms/pictique/src/routes/(protected)/+layout.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@
161161
<MessageInput
162162
class="sticky start-0 bottom-4 mt-4 w-full px-2"
163163
variant="comment"
164-
src={profile?.avatarUrl}
164+
src={profile?.avatarUrl || 'https://picsum.photos/200/200'}
165165
bind:value={commentValue}
166166
{handleSend}
167167
bind:input={commentInput}

platforms/pictique/src/routes/(protected)/home/+page.svelte

Lines changed: 54 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@
33
import { onMount } from 'svelte';
44
import type { CupertinoPane } from 'cupertino-pane';
55
import { Comment, MessageInput } from '$lib/fragments';
6-
import type { CommentType, userProfile } from '$lib/types';
6+
import type { userProfile } from '$lib/types';
77
import { showComments } from '$lib/store/store.svelte';
88
import { posts, isLoading, error, fetchFeed, toggleLike } from '$lib/stores/posts';
9-
import { activePostId } from '$lib/stores/comments';
9+
import { activePostId, comments, createComment } from '$lib/stores/comments';
1010
import { apiClient, getAuthId } from '$lib/utils';
1111
1212
let listElement: HTMLElement;
1313
let drawer: CupertinoPane | undefined = $state();
1414
let commentValue: string = $state('');
1515
let commentInput: HTMLInputElement | undefined = $state();
16-
let _comments = $state<CommentType[]>([]);
1716
let activeReplyToId: string | null = $state(null);
1817
let followError = $state<string | null>(null);
1918
let ownerId: string | null = $state(null);
2019
let profile = $state<userProfile | null>(null);
2120
let loading = $state(true);
21+
let isCommentsLoading = $state(false);
22+
let commentsError = $state<string | null>(null);
2223
2324
const onScroll = () => {
2425
if (listElement.scrollTop + listElement.clientHeight >= listElement.scrollHeight) {
@@ -27,38 +28,16 @@
2728
};
2829
2930
const handleSend = async () => {
30-
const newComment = {
31-
userImgSrc: 'https://www.gravatar.com/avatar/2c7d99fe281ecd3bcd65ab915bac6dd5?s=250',
32-
name: 'You',
33-
commentId: Date.now().toString(),
34-
comment: commentValue,
35-
isUpVoted: false,
36-
isDownVoted: false,
37-
upVotes: 0,
38-
time: 'Just now',
39-
replies: []
40-
};
31+
console.log($activePostId, commentValue);
32+
if (!$activePostId || !commentValue.trim()) return;
4133
42-
if (activeReplyToId) {
43-
// Find the parent comment by id and push reply
44-
const addReplyToComment = (commentsArray: CommentType[]) => {
45-
for (const c of commentsArray) {
46-
if (c.commentId === activeReplyToId) {
47-
c.replies.push(newComment);
48-
return true;
49-
} else if (c.replies.length) {
50-
if (addReplyToComment(c.replies)) return true;
51-
}
52-
}
53-
return false;
54-
};
55-
addReplyToComment(_comments);
56-
} else {
57-
// If no activeReplyToId, add as a new parent comment
58-
_comments = [newComment, ..._comments];
34+
try {
35+
await createComment($activePostId, commentValue);
36+
commentValue = '';
37+
activeReplyToId = null;
38+
} catch (err) {
39+
console.error('Failed to create comment:', err);
5940
}
60-
commentValue = '';
61-
activeReplyToId = null;
6241
};
6342
6443
async function handleFollow(profileId: string) {
@@ -140,26 +119,46 @@
140119
</div>
141120

142121
<Drawer bind:drawer>
143-
<ul class="pb-4">
144-
<h3 class="text-black-600 mb-6 text-center">{_comments.length} Comments</h3>
145-
{#each _comments as comment}
146-
<li class="mb-4">
147-
<Comment
148-
{comment}
149-
handleReply={() => {
150-
activeReplyToId = comment.commentId;
151-
commentInput?.focus();
152-
}}
153-
/>
154-
</li>
155-
{/each}
156-
<MessageInput
157-
class="fixed start-0 bottom-4 mt-4 w-full px-5"
158-
variant="comment"
159-
src={profile?.avatarUrl || 'https://picsum.photos/200/200'}
160-
bind:value={commentValue}
161-
{handleSend}
162-
bind:input={commentInput}
163-
/>
164-
</ul>
122+
{#if showComments.value}
123+
<ul class="pb-4">
124+
<h3 class="text-black-600 mb-6 text-center">{$comments.length} Comments</h3>
125+
{#if isCommentsLoading}
126+
<li class="text-center text-gray-500">Loading comments...</li>
127+
{:else if commentsError}
128+
<li class="text-center text-red-500">{commentsError}</li>
129+
{:else}
130+
{#each $comments as comment}
131+
<li class="mb-4">
132+
<Comment
133+
comment={{
134+
userImgSrc:
135+
comment.author.avatarUrl ||
136+
'https://picsum.photos/200/200',
137+
name: comment.author.name || comment.author.handle,
138+
commentId: comment.id,
139+
comment: comment.text,
140+
isUpVoted: false,
141+
isDownVoted: false,
142+
upVotes: 0,
143+
time: new Date(comment.createdAt).toLocaleDateString(),
144+
replies: []
145+
}}
146+
handleReply={() => {
147+
activeReplyToId = comment.id;
148+
commentInput?.focus();
149+
}}
150+
/>
151+
</li>
152+
{/each}
153+
{/if}
154+
<MessageInput
155+
class="sticky start-0 bottom-4 mt-4 w-full px-2"
156+
variant="comment"
157+
src={profile?.avatarUrl || 'https://picsum.photos/200/200'}
158+
bind:value={commentValue}
159+
{handleSend}
160+
bind:input={commentInput}
161+
/>
162+
</ul>
163+
{/if}
165164
</Drawer>

0 commit comments

Comments
 (0)