Skip to content

Commit

Permalink
feat: add button to check position of user
Browse files Browse the repository at this point in the history
  • Loading branch information
tomalaforge committed Apr 2, 2024
1 parent 1327aaf commit 5413b9e
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 13 deletions.
23 changes: 21 additions & 2 deletions docs/src/components/github/GitHubStats.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script>
import { token } from './github-store';
import { token, username } from './github-store';
let error = false;
let loading = true;
Expand All @@ -12,6 +12,7 @@
if (token) {
fetchStats();
isStar();
getUser();
}
});
Expand All @@ -25,7 +26,6 @@
});
if (response.ok) {
isStarByUser = !isStarByUser;
console.log('Starred', isStarByUser);
}
} catch (e) {
console.error(e);
Expand All @@ -50,6 +50,25 @@
}
}
async function getUser() {
try {
const response = await fetch(`https://api.github.com/user`, {
method: 'GET',
headers: {
Authorization: `token ${$token}`
}
});
if (response.ok) {
const { login } = await response.json();
username.set(login);
}
} catch (e) {
console.error(e);
} finally {
loadingStar = false;
}
}
async function fetchStats() {
try {
Expand Down
2 changes: 2 additions & 0 deletions docs/src/components/github/github-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { derived, writable } from 'svelte/store';
export const token = writable<string | null>(null);
export const isConnected = writable(false);

export const username = writable(null);

export const isLoading = writable(true);
export const error = writable(false);
export const data = writable<any[]>([]);
Expand Down
22 changes: 18 additions & 4 deletions docs/src/components/leaderboard/LeaderboardAnswer.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<script>
import UserBox from './UserBox.svelte';
import Spinner from './Spinner.svelte';
import { isConnected, token } from '../github/github-store';
import { isConnected, token, username } from '../github/github-store';
let users = [];
let loading = true;
let error = null;
let isUsernamePresent = false;
token.subscribe(token => {
if (token) {
Expand Down Expand Up @@ -54,11 +55,13 @@
page++;
}
isUsernamePresent = Object.keys(prCounts).some((value) => value === $username);
users = Object.entries(prCounts).map(([login, pr]) => ({
login,
avatar: pr.avatar,
count: pr.count,
challengeNumber: pr.challengeNumber.sort((a, b) => a - b)
challengeNumber: pr.challengeNumber.sort((a, b) => a - b),
})).filter((r) => r.login !== 'allcontributors[bot]').sort((a, b) => b.count - a.count);
} catch (e) {
Expand All @@ -70,10 +73,14 @@
}
</script>
{#if !$isConnected}
<div class="important-block not-connected">Log in to Github to see the list</div>
{:else}
{#if isUsernamePresent}
<div class="link-username">
<a href={`#${$username}`}>Check my position</a>
</div>
{/if}
{#if loading}
<Spinner />
{:else if error}
Expand All @@ -95,12 +102,19 @@
margin-top: 1rem;
}
.link-username {
margin-top: 2rem;
width: 100%;
display: flex;
justify-content: flex-end;
}
.box {
display: flex;
flex-wrap: wrap;
justify-items: center;
gap: 1.5rem;
margin-top: 2rem;
margin-top: 0.5rem;
}
.challenge-number {
Expand Down
21 changes: 18 additions & 3 deletions docs/src/components/leaderboard/LeaderboardChallenge.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<script>
import UserBox from './UserBox.svelte';
import Spinner from './Spinner.svelte';
import { isConnected, token } from '../github/github-store';
import { isConnected, token, username } from '../github/github-store';
let users = [];
let loading = true;
let error = null;
let isUsernamePresent = false;
token.subscribe(token => {
if (token) {
Expand All @@ -28,6 +29,8 @@
}
});
isUsernamePresent = Object.keys(prCounts).some((value) => value === $username);
return Object.entries(prCounts).map(([login, pr]) => ({
login,
avatar: pr.avatar,
Expand Down Expand Up @@ -58,6 +61,11 @@
{#if !$isConnected}
<div class="important-block not-connected">Log in to Github to see the list</div>
{:else}
{#if isUsernamePresent}
<div class="link-username">
<a href={`#${$username}`}>Check my position</a>
</div>
{/if}
{#if loading}
<Spinner />
{:else if error}
Expand All @@ -77,12 +85,19 @@
.not-connected {
margin-top: 1rem;
}
.link-username {
margin-top: 2rem;
width: 100%;
display: flex;
justify-content: flex-end;
}
.box {
display: flex;
flex-wrap: wrap;
justify-items: center;
gap: 1rem;
margin-top: 2rem;
margin-top: 0.5rem;
}
</style>
19 changes: 17 additions & 2 deletions docs/src/components/leaderboard/LeaderboardCommit.svelte
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<script>
import UserBox from './UserBox.svelte';
import Spinner from './Spinner.svelte';
import { isConnected, token } from '../github/github-store';
import { isConnected, token, username } from '../github/github-store';
let users = [];
let loading = true;
let error = null;
let isUsernamePresent = false;
token.subscribe(token => {
if (token) {
Expand Down Expand Up @@ -55,6 +56,8 @@
}
isUsernamePresent = Object.keys(prCounts).some((value) => value === $username);
users = Object.entries(prCounts).map(([login, pr]) => ({
login,
avatar: pr.avatar,
Expand All @@ -74,6 +77,11 @@
{#if !$isConnected}
<div class="important-block not-connected">Log in to Github to see the list</div>
{:else}
{#if isUsernamePresent}
<div class="link-username">
<a href={`#${$username}`}>Check my position</a>
</div>
{/if}
{#if loading}
<Spinner />
{:else if error}
Expand All @@ -94,11 +102,18 @@
margin-top: 1rem;
}
.link-username {
margin-top: 2rem;
width: 100%;
display: flex;
justify-content: flex-end;
}
.box {
display: flex;
flex-wrap: wrap;
justify-items: center;
gap: 1rem;
margin-top: 2rem;
margin-top: 0.5rem;
}
</style>
10 changes: 8 additions & 2 deletions docs/src/components/leaderboard/UserBox.svelte
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<script>
export let avatar;
import { username } from '../github/github-store';
export let avatar;
export let login;
export let index;
</script>

<div class="user-box">
<div class={`user-box ${login === $username ? 'own-box' : ''}`} id={login === $username ? $username: index}>
<div class="user-info">
<img src={avatar} alt="" width="40" height="40" class="avatar" />
<div class="name-box">
Expand Down Expand Up @@ -38,6 +40,10 @@
width: 100%;
}
.own-box {
border: 1px solid red;
}
.user-info {
display: flex;
padding: 1rem 0 1rem 1rem;
Expand Down

0 comments on commit 5413b9e

Please sign in to comment.