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 switchWatchList = writable(<boolean>(false));
export const cachedPosts = writable(<Array<any>> []); export const cachedPosts = writable(<Array<any>> {});
export const currentPagePosition = writable(<Number> (0)); export const currentPagePosition = writable(<Number> (0));
export const similarTickerClicked = writable(<boolean>(false)); export const similarTickerClicked = writable(<boolean>(false));

View File

@ -163,7 +163,7 @@ async function getPost() {
let postData; let postData;
if ($cachedPosts?.length === 0) if (Object?.keys($cachedPosts)?.length === 0)
{ {
postData = { postData = {
//userId: data?.user.id, //userId: data?.user.id,
@ -176,8 +176,8 @@ async function getPost() {
{ {
postData = { postData = {
//userId: data?.user.id, //userId: data?.user.id,
startPage: $cachedPosts[0].currentPage, startPage: $cachedPosts?.currentPage,
seenPostId: $cachedPosts[0].seenPostId.length === 0 ? [] : $cachedPosts[0].seenPostId, seenPostId: $cachedPosts?.seenPostId.length === 0 ? [] : $cachedPosts?.seenPostId,
sortingPosts: sortingPosts, sortingPosts: sortingPosts,
}; };
} }
@ -227,9 +227,9 @@ let LoginPopup;
let BottomNavigation; let BottomNavigation;
onMount(async () => { onMount(async () => {
if ($cachedPosts?.length === 0) { if (Object?.keys($cachedPosts)?.length === 0) {
// Only make API requests if cached posts are not available // 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(), getCommunityStats(),
getModerators(), getModerators(),
getPost(), getPost(),
@ -244,7 +244,7 @@ onMount(async () => {
else { else {
// Use cached data if available // Use cached data if available
posts = $cachedPosts?.at(0)?.posts; posts = $cachedPosts?.posts;
communityStats = getCache('', 'getCommunityStats'); communityStats = getCache('', 'getCommunityStats');
moderators = getCache('', 'getModerators'); moderators = getCache('', 'getModerators');
discordData = getCache('','getDiscordWidget'); 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) { async function handleCategoryOfPosts(state) {
loading = true; loading = true;
@ -280,7 +280,7 @@ async function handleCategoryOfPosts(state) {
noPostMore = false; noPostMore = false;
sortingPosts = state; sortingPosts = state;
$cachedPosts = []; $cachedPosts = {};
posts = await getPost(); posts = await getPost();
loading = false; loading = false;
} }
@ -319,7 +319,7 @@ $: {
$: { $: {
if(posts) 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 { error, fail, redirect } from "@sveltejs/kit";
import { validateData } from "$lib/utils"; import { validateData } from "$lib/utils";
import { loginUserSchema, registerUserSchema } from "$lib/schemas"; import { loginUserSchema, registerUserSchema } from "$lib/schemas";
import { userRegion, oauthState, oauthVerifier, oauthProvider } from '$lib/store'; import { 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 = { 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(),
};
};