Skip to content

Commit

Permalink
added comment textbox and tree rendering component
Browse files Browse the repository at this point in the history
  • Loading branch information
t-shah02 committed May 13, 2024
1 parent 724a51b commit 8530700
Show file tree
Hide file tree
Showing 19 changed files with 4,241 additions and 1,664 deletions.
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
"@sveltejs/kit": "^1.20.4",
"@types/bcryptjs": "^2.4.4",
"@types/jsonwebtoken": "^9.0.5",
"@types/markdown-it": "^14.1.1",
"@types/markdown-it-emoji": "^3.0.1",
"@types/node": "^20.10.5",
"@types/sanitize-html": "^2.11.0",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"@typescript-eslint/parser": "^6.0.0",
"autoprefixer": "^10.4.14",
Expand Down Expand Up @@ -57,10 +60,15 @@
"dependencies": {
"@aws-sdk/client-s3": "^3.421.0",
"@prisma/client": "^5.9.1",
"@sveltejs/svelte-virtual-list": "^3.0.1",
"@zerodevx/svelte-toast": "^0.9.5",
"bcryptjs": "^2.4.3",
"jsonwebtoken": "^9.0.2",
"sharp": "^0.32.6"
"markdown-it": "^14.1.0",
"markdown-it-emoji": "^3.0.0",
"sanitize-html": "^2.13.0",
"sharp": "^0.32.6",
"svelte-tiny-virtual-list": "^2.1.0"
},
"lint-staged": {
"*.{js,jsx,ts,tsx}": [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
Warnings:
- You are about to drop the column `searchable` on the `Artist` table. All the data in the column will be lost.
- You are about to drop the column `searchable` on the `Post` table. All the data in the column will be lost.
- You are about to drop the column `searchable` on the `Tag` table. All the data in the column will be lost.
- You are about to drop the column `searchable` on the `User` table. All the data in the column will be lost.
*/
-- DropIndex
DROP INDEX "artist_searchable_idx";

-- DropIndex
DROP INDEX "post_searchable_idx";

-- DropIndex
DROP INDEX "tag_searchable_idx";

-- DropIndex
DROP INDEX "user_searchable_idx";

-- AlterTable
ALTER TABLE "Artist" DROP COLUMN "searchable";

-- AlterTable
ALTER TABLE "Comment" ALTER COLUMN "content" SET DATA TYPE VARCHAR(1500);

-- AlterTable
ALTER TABLE "Post" DROP COLUMN "searchable";

-- AlterTable
ALTER TABLE "Tag" DROP COLUMN "searchable";

-- AlterTable
ALTER TABLE "User" DROP COLUMN "searchable";
2 changes: 1 addition & 1 deletion prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ model Post {
model Comment {
id String @id @default(uuid())
createdAt DateTime @default(now())
content String @db.VarChar(350)
content String @db.VarChar(1500)
replies Comment[] @relation(name: "replies")
post Post @relation(fields: [postId], references: [id], onDelete: Cascade)
postId String
Expand Down
54 changes: 54 additions & 0 deletions src/lib/client/api/comments.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { convertDataStructureToIncludeDatetimes } from '$lib/shared/helpers/dates';
import { buildUrl } from '$lib/shared/helpers/urls';
import type { IComment, ICommentCreateBody } from '$lib/shared/types/comments';
import { toast } from '@zerodevx/svelte-toast';
import { FAILURE_TOAST_OPTIONS } from '../constants/toasts';
import { commentTreeStore } from '../stores/comments';

export const getComments = async (
postId: string,
Expand All @@ -13,3 +18,52 @@ export const getComments = async (
const finalUrl = buildUrl(`/api/comments/find/post/${postId}`, params);
return await fetch(finalUrl);
};

export const createPostCommentsPaginator = (postId: string, parentCommentId: string | null) => {
let pageNumber = 0;
let noMoreComments = false;

return async () => {
if (noMoreComments) {
return { pageNumber, noMoreComments };
}

const response = await getComments(postId, parentCommentId, pageNumber);

if (response.ok) {
const comments: IComment[] = convertDataStructureToIncludeDatetimes<IComment>(
(await response.json()) as IComment[],
['createdAt']
);

commentTreeStore.update((currentCommentTree) => {
comments.forEach((comment) => {
currentCommentTree.addComment(comment);
});

return currentCommentTree;
});

if (comments.length === 0) {
toast.push('There are no more comments left to load', FAILURE_TOAST_OPTIONS);
noMoreComments = true;
} else {
pageNumber++;
}
} else {
toast.push(
'There was an error that occured while loading the comments',
FAILURE_TOAST_OPTIONS
);
}

return { pageNumber, noMoreComments };
};
};

export const createComment = async (body: ICommentCreateBody) => {
return await fetch('/api/comments/create', {
method: 'POST',
body: JSON.stringify(body)
});
};
Loading

0 comments on commit 8530700

Please sign in to comment.