Skip to content

Commit 857dec2

Browse files
committed
Populate dashboard directly from user's progress repo
1 parent 33f5a6c commit 857dec2

File tree

3 files changed

+25
-44
lines changed

3 files changed

+25
-44
lines changed

src/Dashboard.tsx

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { Link, useParams } from "react-router";
66
import { Exercise, useGetExercisesQuery } from "./api/queries/get_exercises";
77
import { useGetUserQuery } from "./api/queries/get_user";
88
import { useGetUserProgressQuery } from "./api/queries/get_user_progress";
9-
import { useGetUserUpdateTimeQuery } from "./api/queries/get_user_update_time";
109
import Spinner from "./components/Spinner";
1110

1211
type UserProblemSetStatus = string;
@@ -24,7 +23,7 @@ function Dashboard() {
2423
isLoading: isUserProgressLoading,
2524
isRefetchError: isUserProgressRefetching,
2625
refetch: refetchUserProgress,
27-
} = useGetUserProgressQuery(user?.id);
26+
} = useGetUserProgressQuery(user?.login);
2827

2928
const parsedUserProgress = useMemo(() => {
3029
if (isUserProgressLoading || userProgress == null) {
@@ -74,32 +73,6 @@ function Dashboard() {
7473
await refetchUserProgress()
7574
}, [refetchUserProgress])
7675

77-
const {
78-
data: latestActionRun
79-
} = useGetUserUpdateTimeQuery(user?.id)
80-
81-
const formatTimeWithTimezone = useCallback((dateObj: Date) => {
82-
const formatter = new Intl.DateTimeFormat('en-GB', {
83-
day: '2-digit',
84-
month: 'short',
85-
year: 'numeric',
86-
hour: '2-digit',
87-
minute: '2-digit',
88-
hour12: false,
89-
timeZone: Intl.DateTimeFormat().resolvedOptions().timeZone
90-
});
91-
92-
return formatter.format(dateObj)
93-
}, [])
94-
95-
const expectedNextUpdateTime = useMemo(() => {
96-
if (latestActionRun == null) {
97-
return null
98-
}
99-
const expectedTime = new Date(latestActionRun.getTime() + 30 * 60 * 1000)
100-
return expectedTime
101-
}, [latestActionRun])
102-
10376
return (
10477
<div className="lg:w-[40%] my-16 mx-auto md:w-[60%] w-[80%]">
10578
<h3 className="text-2xl font-bold mb-4">Git Mastery Progress Dashboard</h3>
@@ -116,12 +89,6 @@ function Dashboard() {
11689
</div>
11790
<p className="text-gray-700 font-semibold">Find your progress for the various Git Mastery exercises.</p>
11891
<p className="text-gray-700">To view all exercises, visit the <a className="text-blue-800 underline" href="https://git-mastery.github.io/exercises">exercises directory</a>.</p>
119-
{latestActionRun != null && expectedNextUpdateTime != null && (
120-
<p className="mt-2 italic">Last updated on {formatTimeWithTimezone(latestActionRun)}.
121-
<br />Next update around {formatTimeWithTimezone(expectedNextUpdateTime)}
122-
<br />If there is a discrepancy, open a ticket with the Git-Mastery team <a className="text-blue-800 underline" href="https://github.com/git-mastery/git-mastery">here</a>
123-
</p>
124-
)}
12592
</div>
12693
<div>
12794
{(isUserLoading || isUserProgressLoading || isUserProgressRefetching || isProblemSetsLoading) ? (
@@ -134,7 +101,19 @@ function Dashboard() {
134101
<p className="mb-4 text-red-700">User <strong>{username}</strong> does not exist</p>
135102
<Link to="/" className="hover:cursor-pointer border-1 border-red-700 bg-red-700 text-white rounded-sm px-4 py-2 font-semibold">← Return to search</Link>
136103
</div>
137-
) : (
104+
) : userProgress == null ? (
105+
<div className="text-center">
106+
<p className="mb-4 text-red-700">
107+
No progress repository found for <strong>{username}</strong>.
108+
</p>
109+
<Link
110+
to="/"
111+
className="hover:cursor-pointer border-1 border-red-700 bg-red-700 text-white rounded-sm px-4 py-2 font-semibold"
112+
>
113+
← Return to search
114+
</Link>
115+
</div>
116+
) : (
138117
exerciseGroups.size === 0 ? (
139118
<div className="text-center">
140119
<p className="mb-4">You have not completed any exercises yet</p>

src/api/queries/get_user.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import axios from "axios"
22
import { useQuery } from "react-query"
33

44
export interface User {
5-
id: number;
5+
login: string;
66
}
77

88
export const getUser = async (username: string) => {
99
try {
10-
const user = await axios.get<User>(`https://api.github.com/users/${username}`)
10+
const encodedUsername = encodeURIComponent(username);
11+
const user = await axios.get<User>(`https://api.github.com/users/${encodedUsername}`)
1112
return user.data
1213
} catch {
1314
return null
@@ -18,7 +19,7 @@ export const useGetUserQuery = (username: string | undefined) => {
1819
return useQuery<User | null>({
1920
queryKey: ["get-user", username],
2021
queryFn: () => getUser(username!),
21-
enabled: username != null,
22+
enabled: !!username,
2223
})
2324
}
2425

src/api/queries/get_user_progress.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,21 @@ export interface UserProgress {
66
status: string;
77
}
88

9-
export const getUserProgress = async (userId: number) => {
9+
export const getUserProgress = async (username: string) => {
1010
try {
11-
const result = await axios.get<UserProgress[]>(`https://raw.githubusercontent.com/git-mastery/progress/refs/heads/tracker/students/${userId}.json`);
11+
const encodedUsername = encodeURIComponent(username);
12+
const result = await axios.get<UserProgress[]>(`https://raw.githubusercontent.com/${encodedUsername}/${encodedUsername}-gitmastery-progress/refs/heads/main/progress.json`);
1213
return result.data;
1314
} catch {
1415
return null;
1516
}
1617
}
1718

18-
export const useGetUserProgressQuery = (userId: number | undefined) => {
19+
export const useGetUserProgressQuery = (username: string | undefined) => {
1920
return useQuery<UserProgress[] | null>({
20-
queryKey: ["get-user-progress", userId],
21-
queryFn: () => getUserProgress(userId!),
22-
enabled: userId != null,
21+
queryKey: ["get-user-progress", username],
22+
queryFn: () => getUserProgress(username!),
23+
enabled: !!username,
2324
});
2425
}
2526

0 commit comments

Comments
 (0)