diff --git a/src/routes/+page.js b/src/routes/+page.js new file mode 100644 index 0000000..0f3b836 --- /dev/null +++ b/src/routes/+page.js @@ -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"); + }); + + return { + leaderboard + }; +} diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index ca8543a..81db40a 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -3,36 +3,20 @@ 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"); }, 5000); }); @@ -40,11 +24,7 @@
- {#key leaderboard} - {#await leaderboard then} - - {/await} - {/key} +
- {#key leaderboard} - {#await leaderboard} -
loading...
- {:then} +
+ {#if innerWidth <= 672} + {#each leaderboard as person, i} + + {/each} + {:else} + {#if innerWidth >= 1440} + + {:else} + + {/if}
- {#if innerWidth <= 672} - {#each leaderboard as person, i} + {#if innerWidth >= 1440} + + + {:else} + + + {/if} +
+ +
+ {#each leaderboard as person, i} + {#if ![0, 1, 2].includes(i)} - {/each} - {:else} - {#if innerWidth >= 1440} - - {:else} - {/if} -
- {#if innerWidth >= 1440} - - - {:else} - - - {/if} -
- -
- {#each leaderboard as person, i} - {#if ![0, 1, 2].includes(i)} - - {/if} - {/each} -
- {/if} + {/each}
- {/await} - {/key} + {/if} +