feat: add button to check position of user

This commit is contained in:
thomas
2024-04-02 22:41:42 +02:00
parent 1327aaf8fe
commit 5413b9e337
6 changed files with 84 additions and 13 deletions

View File

@@ -1,5 +1,5 @@
<script> <script>
import { token } from './github-store'; import { token, username } from './github-store';
let error = false; let error = false;
let loading = true; let loading = true;
@@ -12,6 +12,7 @@
if (token) { if (token) {
fetchStats(); fetchStats();
isStar(); isStar();
getUser();
} }
}); });
@@ -25,7 +26,6 @@
}); });
if (response.ok) { if (response.ok) {
isStarByUser = !isStarByUser; isStarByUser = !isStarByUser;
console.log('Starred', isStarByUser);
} }
} catch (e) { } catch (e) {
console.error(e); console.error(e);
@@ -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() { async function fetchStats() {
try { try {

View File

@@ -3,6 +3,8 @@ import { derived, writable } from 'svelte/store';
export const token = writable<string | null>(null); export const token = writable<string | null>(null);
export const isConnected = writable(false); export const isConnected = writable(false);
export const username = writable(null);
export const isLoading = writable(true); export const isLoading = writable(true);
export const error = writable(false); export const error = writable(false);
export const data = writable<any[]>([]); export const data = writable<any[]>([]);

View File

@@ -1,11 +1,12 @@
<script> <script>
import UserBox from './UserBox.svelte'; import UserBox from './UserBox.svelte';
import Spinner from './Spinner.svelte'; import Spinner from './Spinner.svelte';
import { isConnected, token } from '../github/github-store'; import { isConnected, token, username } from '../github/github-store';
let users = []; let users = [];
let loading = true; let loading = true;
let error = null; let error = null;
let isUsernamePresent = false;
token.subscribe(token => { token.subscribe(token => {
if (token) { if (token) {
@@ -54,11 +55,13 @@
page++; page++;
} }
isUsernamePresent = Object.keys(prCounts).some((value) => value === $username);
users = Object.entries(prCounts).map(([login, pr]) => ({ users = Object.entries(prCounts).map(([login, pr]) => ({
login, login,
avatar: pr.avatar, avatar: pr.avatar,
count: pr.count, 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); })).filter((r) => r.login !== 'allcontributors[bot]').sort((a, b) => b.count - a.count);
} catch (e) { } catch (e) {
@@ -70,10 +73,14 @@
} }
</script> </script>
{#if !$isConnected} {#if !$isConnected}
<div class="important-block not-connected">Log in to Github to see the list</div> <div class="important-block not-connected">Log in to Github to see the list</div>
{:else} {:else}
{#if isUsernamePresent}
<div class="link-username">
<a href={`#${$username}`}>Check my position</a>
</div>
{/if}
{#if loading} {#if loading}
<Spinner /> <Spinner />
{:else if error} {:else if error}
@@ -95,12 +102,19 @@
margin-top: 1rem; margin-top: 1rem;
} }
.link-username {
margin-top: 2rem;
width: 100%;
display: flex;
justify-content: flex-end;
}
.box { .box {
display: flex; display: flex;
flex-wrap: wrap; flex-wrap: wrap;
justify-items: center; justify-items: center;
gap: 1.5rem; gap: 1.5rem;
margin-top: 2rem; margin-top: 0.5rem;
} }
.challenge-number { .challenge-number {

View File

@@ -1,12 +1,13 @@
<script> <script>
import UserBox from './UserBox.svelte'; import UserBox from './UserBox.svelte';
import Spinner from './Spinner.svelte'; import Spinner from './Spinner.svelte';
import { isConnected, token } from '../github/github-store'; import { isConnected, token, username } from '../github/github-store';
let users = []; let users = [];
let loading = true; let loading = true;
let error = null; let error = null;
let isUsernamePresent = false;
token.subscribe(token => { token.subscribe(token => {
if (token) { if (token) {
@@ -28,6 +29,8 @@
} }
}); });
isUsernamePresent = Object.keys(prCounts).some((value) => value === $username);
return Object.entries(prCounts).map(([login, pr]) => ({ return Object.entries(prCounts).map(([login, pr]) => ({
login, login,
avatar: pr.avatar, avatar: pr.avatar,
@@ -58,6 +61,11 @@
{#if !$isConnected} {#if !$isConnected}
<div class="important-block not-connected">Log in to Github to see the list</div> <div class="important-block not-connected">Log in to Github to see the list</div>
{:else} {:else}
{#if isUsernamePresent}
<div class="link-username">
<a href={`#${$username}`}>Check my position</a>
</div>
{/if}
{#if loading} {#if loading}
<Spinner /> <Spinner />
{:else if error} {:else if error}
@@ -78,11 +86,18 @@
margin-top: 1rem; margin-top: 1rem;
} }
.link-username {
margin-top: 2rem;
width: 100%;
display: flex;
justify-content: flex-end;
}
.box { .box {
display: flex; display: flex;
flex-wrap: wrap; flex-wrap: wrap;
justify-items: center; justify-items: center;
gap: 1rem; gap: 1rem;
margin-top: 2rem; margin-top: 0.5rem;
} }
</style> </style>

View File

@@ -1,11 +1,12 @@
<script> <script>
import UserBox from './UserBox.svelte'; import UserBox from './UserBox.svelte';
import Spinner from './Spinner.svelte'; import Spinner from './Spinner.svelte';
import { isConnected, token } from '../github/github-store'; import { isConnected, token, username } from '../github/github-store';
let users = []; let users = [];
let loading = true; let loading = true;
let error = null; let error = null;
let isUsernamePresent = false;
token.subscribe(token => { token.subscribe(token => {
if (token) { if (token) {
@@ -55,6 +56,8 @@
} }
isUsernamePresent = Object.keys(prCounts).some((value) => value === $username);
users = Object.entries(prCounts).map(([login, pr]) => ({ users = Object.entries(prCounts).map(([login, pr]) => ({
login, login,
avatar: pr.avatar, avatar: pr.avatar,
@@ -74,6 +77,11 @@
{#if !$isConnected} {#if !$isConnected}
<div class="important-block not-connected">Log in to Github to see the list</div> <div class="important-block not-connected">Log in to Github to see the list</div>
{:else} {:else}
{#if isUsernamePresent}
<div class="link-username">
<a href={`#${$username}`}>Check my position</a>
</div>
{/if}
{#if loading} {#if loading}
<Spinner /> <Spinner />
{:else if error} {:else if error}
@@ -94,11 +102,18 @@
margin-top: 1rem; margin-top: 1rem;
} }
.link-username {
margin-top: 2rem;
width: 100%;
display: flex;
justify-content: flex-end;
}
.box { .box {
display: flex; display: flex;
flex-wrap: wrap; flex-wrap: wrap;
justify-items: center; justify-items: center;
gap: 1rem; gap: 1rem;
margin-top: 2rem; margin-top: 0.5rem;
} }
</style> </style>

View File

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