Skip to content

Commit 11a33b3

Browse files
Merge pull request #5 from Sanjai-Shaarugesh/features
added new features for users 🪶
2 parents 0daf7d7 + 30c56af commit 11a33b3

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
lines changed

src/routes/users/+layout.server.ts

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ export const prerender = false;
66
export const load:LayoutServerLoad = async (event) => {
77
const session = await event.locals.auth();
88

9-
const RandomUsers = Math.floor(Math.random() * 1000000);
9+
const RandomUsers = Math.floor(Math.random() * 100000);
1010

1111
if (!session?.user) {
1212
throw redirect(303, '/login');
1313
}
1414
const getRandomUsers = async () => {
15-
const res = await fetch(`https://api.github.com/users?since=${RandomUsers}&per_page=50`, {
15+
const res = await fetch(`https://api.github.com/users?since=${RandomUsers}&per_page=100`, {
1616
headers: {
1717
Accept: 'application/vnd.github+json',
1818
//@ts-ignore
@@ -21,9 +21,60 @@ export const load:LayoutServerLoad = async (event) => {
2121
}
2222
});
2323

24-
return await res.json();
24+
const users = await res.json();
25+
26+
const userDetailsPromise = users.map(async (user:any)=>{
27+
const userDetailsRes = await fetch(user.url,{
28+
headers: {
29+
Accept: 'application/vnd.github+json',
30+
//@ts-ignore
31+
Authorization: `Bearer ${session?.access_token}`,
32+
'X-Github-Api-Version': '2022-11-28'
33+
}
34+
});
35+
const userDetails = await userDetailsRes.json();
36+
37+
const reposRes = await fetch(userDetails.repos_url,{
38+
headers:{
39+
Accept: 'application/vnd.github+json',
40+
//@ts-ignore
41+
Authorization: `Bearer ${session?.access_token}`,
42+
'X-Github-Api-Version': '2022-11-28'
43+
}
44+
});
45+
46+
const repos = await reposRes.json();
47+
48+
const totalStars = repos.reduce((sum: number, repo: any) => sum + repo.stargazers_count, 0);
49+
50+
return {
51+
...userDetails,
52+
totalStars // Adding total stars count to user details
53+
};
54+
});
55+
56+
const userDetails = await Promise.all(userDetailsPromise);
57+
58+
59+
60+
//@ts-ignore
61+
return userDetails.sort((a,b)=>{
62+
if(b.followers === a.followers){
63+
return b.totalStars - a.totalStars;
64+
65+
}
66+
return b.followers - a.followers;
67+
})
68+
69+
2570
};
71+
72+
2673
return {
27-
RandomUsers: await getRandomUsers()
28-
};
29-
};
74+
RandomUsers : await getRandomUsers()
75+
}
76+
77+
78+
79+
};
80+

src/routes/users/+page.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
88
let isMouseEntered = false;
99
export let data;
10+
//console.log(data);
1011
let vCard = false;
1112
</script>
1213

src/routes/users/[slug]/+layout.server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const load: LayoutServerLoad = async ({ fetch, params, locals }) => {
2020
const res = await fetch(`https://api.github.com/users/${username}`, {
2121
headers: {
2222
Accept: 'application/vnd.github+json',
23+
//@ts-ignore
2324
Authorization: `Bearer ${session.access_token}`, // Fixed typo in "Authorization"
2425
'X-Github-Api-Version': '2022-11-28'
2526
}

0 commit comments

Comments
 (0)