Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Polling and make the background instantly available #26

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/routes/+page.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export async function load({ fetch, depends }) {
depends("leaderboard");
let leaderboard = await fetch(
`https://hacknight.navinshrinivas.com/leaderboard_mat`
)
.then((response) => response.json())
.then((leaderboard) =>
leaderboard.sort((a, b) => b.Current_bounty - a.Current_bounty)
)
.catch((e) => {
throw new Error(e.status, "Reddy Anna Is Not Talking");
});

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add leaderboard.sort((a, b) => b.Current_bounty - a.Current_bounty) here. The leaderboard isn't sorted from backend

return {
leaderboard
};
}
160 changes: 67 additions & 93 deletions src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,28 @@
import Card from "./Card.svelte";
import CardRow from "./CardRow.svelte";
import { onMount } from "svelte";
let innerWidth = 0;
import { invalidate } from "$app/navigation";

let leaderboard;
let oldLeaderboard = [];
const fetchLeaderboardData = async () => {
try {
const response = await fetch(
"https://hacknight.navinshrinivas.com/leaderboard_mat"
);
if (!response.ok) {
throw new Error("Reddy Anna Is Not Talking");
} else {
let json_response = await response.json();
export let data;
let innerWidth = 0;

json_response.sort((a, b) => b.Current_bounty - a.Current_bounty);
if (JSON.stringify(json_response) != JSON.stringify(oldLeaderboard)) {
leaderboard = json_response;
oldLeaderboard = leaderboard;
}
}
} catch (err) {
console.log(err);
}
};
let leaderboard = {};

leaderboard = fetchLeaderboardData();
$: if (JSON.stringify(leaderboard) !== JSON.stringify(data.leaderboard)) {
leaderboard = data.leaderboard;
}

onMount(() => {
setInterval(async () => {
await fetchLeaderboardData();
setInterval(() => {
invalidate("leaderboard");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Polling doesn't work on local for me. Have you verified this works?

}, 5000);
});
</script>

<svelte:window bind:innerWidth />

<main>
{#key leaderboard}
{#await leaderboard then}
<Background info={leaderboard} />
{/await}
{/key}
<Background info={leaderboard} />

<div
class="flex flex-col h-[75vh] md:h-[80vh] lg:h-[100vh] justify-between mb-10"
Expand Down Expand Up @@ -78,78 +58,72 @@
</div>
</div>

{#key leaderboard}
{#await leaderboard}
<div class="text-center">loading...</div>
{:then}
<div
class="leaderboard-background rounded-xl bg-[#0F0913] m-4 lg:m-10 p-5 flex flex-col justify-stretch items-center"
>
{#if innerWidth <= 672}
{#each leaderboard as person, i}
<Card
index={i + 1}
username={person.Name}
points={person.Current_bounty}
/>
{/each}
{:else}
{#if innerWidth >= 1440}
<CardRow
index={1}
username={leaderboard[0].Name}
points={leaderboard[0].Current_bounty}
/>
{:else}
<Card
index={1}
username={leaderboard[0].Name}
points={leaderboard[0].Current_bounty}
/>
{/if}
<div
class="leaderboard-background rounded-xl bg-[#0F0913] m-4 lg:m-10 p-5 flex flex-col justify-stretch items-center"
class="grid-wrapper2-3 grid grid-cols-2 items-stretch justify-stretch"
>
{#if innerWidth <= 672}
{#each leaderboard as person, i}
{#if innerWidth >= 1440}
<CardRow
index={2}
username={leaderboard[1].Name}
points={leaderboard[1].Current_bounty}
/>
<CardRow
index={3}
username={leaderboard[2].Name}
points={leaderboard[2].Current_bounty}
/>
{:else}
<Card
index={2}
username={leaderboard[1].Name}
points={leaderboard[1].Current_bounty}
/>
<Card
index={3}
username={leaderboard[2].Name}
points={leaderboard[2].Current_bounty}
/>
{/if}
</div>

<div class="grid-peeps grid justify-stretch items-center w-full">
{#each leaderboard as person, i}
{#if ![0, 1, 2].includes(i)}
<Card
index={i + 1}
username={person.Name}
points={person.Current_bounty}
/>
{/each}
{:else}
{#if innerWidth >= 1440}
<CardRow
index={1}
username={leaderboard[0].Name}
points={leaderboard[0].Current_bounty}
/>
{:else}
<Card
index={1}
username={leaderboard[0].Name}
points={leaderboard[0].Current_bounty}
/>
{/if}
<div
class="grid-wrapper2-3 grid grid-cols-2 items-stretch justify-stretch"
>
{#if innerWidth >= 1440}
<CardRow
index={2}
username={leaderboard[1].Name}
points={leaderboard[1].Current_bounty}
/>
<CardRow
index={3}
username={leaderboard[2].Name}
points={leaderboard[2].Current_bounty}
/>
{:else}
<Card
index={2}
username={leaderboard[1].Name}
points={leaderboard[1].Current_bounty}
/>
<Card
index={3}
username={leaderboard[2].Name}
points={leaderboard[2].Current_bounty}
/>
{/if}
</div>

<div class="grid-peeps grid justify-stretch items-center w-full">
{#each leaderboard as person, i}
{#if ![0, 1, 2].includes(i)}
<Card
index={i + 1}
username={person.Name}
points={person.Current_bounty}
/>
{/if}
{/each}
</div>
{/if}
{/each}
</div>
{/await}
{/key}
{/if}
</div>
</main>

<style>
Expand Down
6 changes: 3 additions & 3 deletions src/routes/Background.svelte
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<script>
export let info = [
{
Name: "Navin Shrinivas",
Current_bounty: 100
Name: "Navin uWu",
Current_bounty: 27
}
];
export let maintainer = false;
Expand Down Expand Up @@ -59,7 +59,7 @@

<main class="unselect">
<div class="text-4xl font-extrabold text-center" disabled>
<span class="chunk" bind:this={infoChunk}
<span bind:this={infoChunk}
>{#each binified as [name, value]}{name}<span class="gradient"
>{value}</span
>{/each}</span
Expand Down
2 changes: 0 additions & 2 deletions src/routes/[slug]/+page.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
let user_details;

export async function load({ params, fetch }) {
let username = params.slug;
try {
Expand Down