This commit is contained in:
MuslemRahimi 2024-06-05 08:15:37 +02:00
parent 05a14b91be
commit 226d21c6e7
5 changed files with 979 additions and 982 deletions

View File

@ -74,7 +74,7 @@ export const oauthProvider = writable(<string> (""));
export const switchWatchList = writable(<boolean>(false));
export const cachedPosts = writable(<Array<any>> []);
export const cachedPosts = writable(<Array<any>> {});
export const currentPagePosition = writable(<Number> (0));
export const similarTickerClicked = writable(<boolean>(false));

View File

@ -163,7 +163,7 @@ async function getPost() {
let postData;
if ($cachedPosts?.length === 0)
if (Object?.keys($cachedPosts)?.length === 0)
{
postData = {
//userId: data?.user.id,
@ -176,8 +176,8 @@ async function getPost() {
{
postData = {
//userId: data?.user.id,
startPage: $cachedPosts[0].currentPage,
seenPostId: $cachedPosts[0].seenPostId.length === 0 ? [] : $cachedPosts[0].seenPostId,
startPage: $cachedPosts?.currentPage,
seenPostId: $cachedPosts?.seenPostId.length === 0 ? [] : $cachedPosts?.seenPostId,
sortingPosts: sortingPosts,
};
}
@ -227,9 +227,9 @@ let LoginPopup;
let BottomNavigation;
onMount(async () => {
if ($cachedPosts?.length === 0) {
if (Object?.keys($cachedPosts)?.length === 0) {
// Only make API requests if cached posts are not available
[communityStats, moderators, posts, discordData] = await Promise.all([
[communityStats, moderators, posts, discordData] = await Promise?.all([
getCommunityStats(),
getModerators(),
getPost(),
@ -244,7 +244,7 @@ onMount(async () => {
else {
// Use cached data if available
posts = $cachedPosts?.at(0)?.posts;
posts = $cachedPosts?.posts;
communityStats = getCache('', 'getCommunityStats');
moderators = getCache('', 'getModerators');
discordData = getCache('','getDiscordWidget');
@ -269,7 +269,7 @@ onDestroy(async () => {
let sortingPosts = $cachedPosts?.at(0)?.sortingPosts?.length > 0 ? $cachedPosts?.at(0)?.sortingPosts : 'hot';
let sortingPosts = $cachedPosts?.sortingPosts?.length > 0 ? $cachedPosts?.sortingPosts : 'hot';
async function handleCategoryOfPosts(state) {
loading = true;
@ -280,7 +280,7 @@ async function handleCategoryOfPosts(state) {
noPostMore = false;
sortingPosts = state;
$cachedPosts = [];
$cachedPosts = {};
posts = await getPost();
loading = false;
}
@ -319,7 +319,7 @@ $: {
$: {
if(posts)
{
$cachedPosts = [{"sortingPosts": sortingPosts,'currentPage': currentPage, 'seenPostId': seenPostId, 'posts': posts}];
$cachedPosts = {"sortingPosts": sortingPosts,'currentPage': currentPage, 'seenPostId': seenPostId, 'posts': posts};
}
}

View File

@ -1,38 +1,7 @@
import { error, fail, redirect } from "@sveltejs/kit";
import { validateData } from "$lib/utils";
import { loginUserSchema, registerUserSchema } from "$lib/schemas";
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(),
};
};
import { oauthState, oauthVerifier, oauthProvider } from '$lib/store';
export const actions = {

View File

@ -6,8 +6,10 @@ 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 {cachedPosts, userRegion, screenWidth, scrollToComment, postIdDeleted, setCache, getCache, tagList, numberOfUnreadNotification, commentAdded, commentUpdated, commentIdDeleted } from '$lib/store';
import {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'
@ -30,7 +32,7 @@ userRegion.subscribe(value => {
});
let post = {'id': 'test', 'upvote': 0, 'downvote': 0};
let post = data?.getOnePost;
let isScrolled = false;
let isLoaded = false;
@ -40,22 +42,6 @@ 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) => {
@ -295,26 +281,6 @@ 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);
@ -334,8 +300,12 @@ onMount(async () => {
LoginPopup = (await import('$lib/components/LoginPopup.svelte')).default;
}
[moderators, comments] = await Promise.all([
getModerators(),
getAllComments(),
]);
numberOfComments = countAllComments(comments) || 0;
if (post?.postType === 'link') {
const url = new URL(post.link);
@ -380,6 +350,8 @@ function closePost(event) {
}
onDestroy( () => {
$commentAdded = '';
$commentIdDeleted = '';
@ -526,7 +498,6 @@ $: {
<!--<section on:click={closePost} class="bg-[#0F0F0F] cursor-zoom-out min-h-screen">-->
<!--in:pageTransitionIn={{ duration: 250, screenWidth: $screenWidth }}-->
{#if isLoaded}
<div class="overflow-hidden flex flex-row item-start w-full lg:mt-5 relative max-w-6xl m-auto lg:px-5 sm:pb-40">
<!--Start Voting-->
<div style="top: 4rem;" class="hidden lg:flex flex-col items-center relative h-fit sticky z-20 lg:mr-3 ">
@ -925,11 +896,7 @@ $: {
</div>
</div>
{:else}
<div class="flex justify-center items-center m-auto h-full w-full max-w-6xl">
<div class="loader">Loading...</div>
</div>
{/if}
<!--Start Login Modal-->
{#if LoginPopup}

View File

@ -0,0 +1,61 @@
import { userRegion, cachedPosts } from '$lib/store';
import { get } from 'svelte/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() {
// Get the current value of cachedPosts
const cachedValue = get(cachedPosts);
// Try to find the post in the cached value
const output = cachedValue?.posts?.find(item => item?.id === params.postId) ?? {};
// If the post is found in the cache, return it
if (Object.keys(output).length !== 0) {
return output;
}
// If the post is not found in the cache, fetch it from the endpoint
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 result = await response.json();
// Assuming the result contains an 'items' array
return result.items;
}
const getPostId = async () => {
return params.postId;
};
return {
getPostId: await getPostId(),
getOnePost: await getOnePost(),
};
};