CLASSROOMIO-T-214: Scroll to bottom properly

This commit is contained in:
Rotimi Best
2023-10-08 16:51:01 +01:00
parent bf292068f4
commit bb029dced6
@@ -43,13 +43,23 @@
return; return;
} }
comments = [
...comments,
{
comment: comment,
name: 'You',
avatar: $profile.avatar_url,
commentAt: new Date().getTime().toLocaleString()
}
];
comment = '';
scrollToBottom();
await supabase.from('lesson_comment').insert({ await supabase.from('lesson_comment').insert({
lesson_id: $lesson.id, lesson_id: $lesson.id,
groupmember_id: groupmember.id, groupmember_id: groupmember.id,
comment comment
}); });
comment = '';
} }
function handleKeyDown(e: KeyboardEvent) { function handleKeyDown(e: KeyboardEvent) {
@@ -58,9 +68,22 @@
} }
} }
function scrollToBottom() {
setTimeout(() => {
bodyRef.scrollTo({
top: bodyRef.scrollHeight
});
}, 100);
}
async function handleInsert(payload: RealtimePostgresChangesPayload<LessonCommentInsertPayload>) { async function handleInsert(payload: RealtimePostgresChangesPayload<LessonCommentInsertPayload>) {
const insertedComment = payload.new as LessonCommentInsertPayload; const insertedComment = payload.new as LessonCommentInsertPayload;
if (groupmember && groupmember.id === insertedComment.groupmember_id) {
console.log('is my comment');
return;
}
const { const {
data, data,
error error
@@ -87,17 +110,14 @@
...comments, ...comments,
{ {
comment: insertedComment.comment, comment: insertedComment.comment,
name: data.id === insertedComment.groupmember_id ? 'You' : data.profile.fullname || '', name: data.profile.fullname,
avatar: data.profile.avatar_url, avatar: data.profile.avatar_url,
commentAt: insertedComment.created_at commentAt: insertedComment.created_at
} }
]; ];
$lesson.totalComments = comments.length; $lesson.totalComments = comments.length;
setTimeout(() => {
bodyRef.scrollTo({ scrollToBottom();
top: bodyRef.scrollHeight
});
}, 100);
} }
async function fetchComments(people: GroupPerson[], lessonId?: string | null) { async function fetchComments(people: GroupPerson[], lessonId?: string | null) {
@@ -139,6 +159,7 @@
: lessonComment.groupmember.profile.fullname : lessonComment.groupmember.profile.fullname
}; };
}); });
scrollToBottom();
} }
onMount(async () => { onMount(async () => {