diff --git a/src/routes/community/post/[postId]/+page.server.ts b/src/routes/community/post/[postId]/+page.server.ts index 14b7981b..2d3a6af5 100644 --- a/src/routes/community/post/[postId]/+page.server.ts +++ b/src/routes/community/post/[postId]/+page.server.ts @@ -1,7 +1,38 @@ import { error, fail, redirect } from "@sveltejs/kit"; import { validateData } from "$lib/utils"; import { loginUserSchema, registerUserSchema } from "$lib/schemas"; -import { oauthState, oauthVerifier, oauthProvider } from '$lib/store'; +import { userRegion, oauthState, oauthVerifier, oauthProvider } from '$lib/store'; + + +const usRegion = ['cle1','iad1','pdx1','sfo1']; +let fastifyURL = import.meta.env.VITE_EU_FASTIFY_URL; + +userRegion.subscribe(value => { +if (usRegion.includes(value)) { + fastifyURL = import.meta.env.VITE_USEAST_FASTIFY_URL; +} else { + fastifyURL = import.meta.env.VITE_EU_FASTIFY_URL; +} +}); + + +export const load = async ({ params }) => { + + + const getPostId = async () => { + + return params.postId; + }; + + + return { + getPostId: await getPostId(), + }; + + +}; + + export const actions = { diff --git a/src/routes/community/post/[postId]/+page.svelte b/src/routes/community/post/[postId]/+page.svelte index 3270a964..b2e107d3 100644 --- a/src/routes/community/post/[postId]/+page.svelte +++ b/src/routes/community/post/[postId]/+page.svelte @@ -6,10 +6,8 @@ import Downvote from '$lib/components/Downvote.svelte'; import Upvote from '$lib/components/Upvote.svelte'; import VideoPlayer from '$lib/components/VideoPlayer.svelte'; - - import { onMount, onDestroy } from 'svelte'; -import {userRegion, screenWidth, scrollToComment, postIdDeleted, setCache, getCache, tagList, numberOfUnreadNotification, commentAdded, commentUpdated, commentIdDeleted } from '$lib/store'; +import {cachedPosts, userRegion, screenWidth, scrollToComment, postIdDeleted, setCache, getCache, tagList, numberOfUnreadNotification, commentAdded, commentUpdated, commentIdDeleted } from '$lib/store'; import { goto, afterNavigate } from '$app/navigation'; import { base } from '$app/paths' @@ -32,7 +30,7 @@ userRegion.subscribe(value => { }); -let post = data?.getOnePost; +let post = {'id': 'test', 'upvote': 0, 'downvote': 0}; let isScrolled = false; let isLoaded = false; @@ -42,6 +40,22 @@ let upvoteCounter = {}; let downvoteCounter = {}; let userAlreadyVoted; +async function getOnePost() { + + const postData = {'postId': data?.getPostId}; + + const response = await fetch(fastifyURL+'/get-one-post', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify(postData), + }); + + const output = await response.json() + return output?.items + +} const handleUpvote = async (event) => { @@ -258,9 +272,9 @@ const getModerators = async () => { - function isModerator(userId) { - return moderators?.some(moderator => userId === moderator?.user); - } +function isModerator(userId) { + return moderators?.some(moderator => userId === moderator?.user); +} function countAllComments(comments) { // Helper function to recursively count comments @@ -280,7 +294,27 @@ return countComments(comments); let LoginPopup; onMount(async () => { + + post = $cachedPosts?.at(0)?.posts?.find(item => item?.id === data?.getPostId) ?? {}; + + if (Object?.keys(post)?.length !== 0) { + [moderators, comments] = await Promise?.all([ + getModerators(), + getAllComments(), + ]); + + } else { + [post, moderators, comments] = await Promise?.all([ + getOnePost(), + getModerators(), + getAllComments(), + ]); + } + + numberOfComments = countAllComments(comments) || 0; + + upvoteCounter[post.id] = post?.upvote; downvoteCounter[post.id] = post?.downvote; userAlreadyVoted = post?.expand['alreadyVoted(post)']?.some(item => item?.user === data?.user?.id); @@ -300,13 +334,8 @@ onMount(async () => { LoginPopup = (await import('$lib/components/LoginPopup.svelte')).default; } - [moderators, comments] = await Promise.all([ - getModerators(), - getAllComments(), - ]); - numberOfComments = countAllComments(comments) || 0; - console.log(moderators) + if (post?.postType === 'link') { const url = new URL(post.link); @@ -319,18 +348,18 @@ if (post?.postType === 'link') { } -loadTextEditor = true; -isLoaded = true; + loadTextEditor = true; + isLoaded = true; -//comments = data?.allComments?.items.reverse() || [] -//replyComments = data?.allReplyComments?.items.reverse() || [] -// Add a scroll event listener -window.addEventListener('scroll', handleScroll); + //comments = data?.allComments?.items.reverse() || [] + //replyComments = data?.allReplyComments?.items.reverse() || [] + // Add a scroll event listener + window.addEventListener('scroll', handleScroll); -return () => { -// Remove the event listener when the component is unmounted -window.removeEventListener('scroll', handleScroll); -}; + return () => { + // Remove the event listener when the component is unmounted + window.removeEventListener('scroll', handleScroll); + }; }); @@ -351,8 +380,6 @@ function closePost(event) { } - - onDestroy( () => { $commentAdded = ''; $commentIdDeleted = ''; @@ -499,6 +526,7 @@ $: { +{#if isLoaded}
- +{:else} +
+
Loading...
+
+{/if} {#if LoginPopup} diff --git a/src/routes/community/post/[postId]/+page.ts b/src/routes/community/post/[postId]/+page.ts deleted file mode 100644 index 7106608b..00000000 --- a/src/routes/community/post/[postId]/+page.ts +++ /dev/null @@ -1,114 +0,0 @@ -import { userRegion } from '$lib/store'; - - - - -export const load = async ({ params }) => { - - const usRegion = ['cle1','iad1','pdx1','sfo1']; - let fastifyURL = import.meta.env.VITE_EU_FASTIFY_URL; - - userRegion.subscribe(value => { - if (usRegion.includes(value)) { - fastifyURL = import.meta.env.VITE_USEAST_FASTIFY_URL; - } else { - fastifyURL = import.meta.env.VITE_EU_FASTIFY_URL; - } - }); - - - - async function getOnePost() { - - - const postData = {'postId': params.postId}; - - const response = await fetch(fastifyURL+'/get-one-post', { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify(postData), - }); - - const output = await response.json() - - return output?.items - } - - const getPostId = async () => { - - return params.postId; - }; - - - return { - getPostId: await getPostId(), - getOnePost: await getOnePost(), - }; - - -}; - - - - - - - - - -/* -export const actions = { - - - - replycomment: async ({ request, locals }) => { - const body = await request.formData(); - - body.append('user', locals.user.id); - - if (body.get('description') === 'undefined') - { - body.delete('description'); - body.append('description', ''); - console.log('I GOT TRIGGERED') - } - - - const {formData, errors} = await validateData( body, createReplyCommentSchema); - - - if (errors) { - return fail(400, { - data: formData, - errors: errors.fieldErrors - }); - } - - //Each comment gives the user +1 Karma points - await locals.pb.collection("users").update(locals.user.id, { - "karma+": 1, - }) - - try { - let newReplyComment = await locals.pb.collection('replycomments').create(serialize(formData)); - - //User always upvotes their post in the intial state - await locals.pb.collection("replycomments").update(newReplyComment.id, { - "vote+": 1, - }) - - } catch (err) { - console.log('Error: ', err); - throw error(err.status, err.message); - } - - throw redirect(303, '/community/'+body.get('post')); - - - }, - - -}; -*/ \ No newline at end of file