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 = {

File diff suppressed because it is too large Load Diff

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(),
};
};