Skip to content

refactor: Update api endpoints #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/hooks/useFetchThemes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,26 @@ const useFetchThemes = (
const fetchData = async () => {
setLoading(true);
try {
let apiThemes;
let apiThemes = null;
if (url.startsWith("http")) {
let finalUrl = `${url}?pageSize=${pageSize}&pageNum=${pageNum}`;
if (searchQuery) {
finalUrl += `&searchQuery=${encodeURIComponent(searchQuery)}`;
}

const response = await galleryApiFetch(finalUrl);
apiThemes = await response.json();
const result = await response.json();
apiThemes = result.data;
} else {
apiThemes = Placeholders.themes;
}

const themes = await fetchThemesFromGitHub(apiThemes);
setThemes(themes);
if (apiThemes) {
Copy link
Contributor

@NeonNature NeonNature Sep 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be missing something here but wouldn't apiThemes always be present/not null because it's either the API response or the placeholders?

could also initialize with let apiThemes = Placeholder.themes instead of null if that's the case too. or is there a case the response could be null?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right that the response can actually be null - if success is false then the data field will be absent 😝

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gotcha. Looks good to me, if so.

const themes = await fetchThemesFromGitHub(apiThemes);
setThemes(themes);
} else {
setError(Error("Failed to fetch theme."));
}
} catch (err: unknown) {
setError(err as Error);
} finally {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useFetchUserData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const useLoginUser = (url: string, provider: string, key: string) => {
try {
const response = await galleryApiFetch(`${url}?provider=${provider}&key=${key}`);
const result = await response.json();
setData(result);
setData(result.data);
} catch (err: unknown) {
setError(err as Error);
} finally {
Expand Down
6 changes: 3 additions & 3 deletions src/interfaces/UserData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ export interface UserData {
name: string;
email: string;
handle: string;
avatar_url: string;
avatarUrl: string;
status: string;
location: string;
profile_url: string;
profileUrl: string;
provider: string;
provider_user_id: string;
providerUserId: string;
}
10 changes: 6 additions & 4 deletions src/pages/UserProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ const UserProfilePage: React.FC = () => {
const refreshUserData = async () => {
try {
const response = await galleryApiFetch(Endpoints.fetchUserProfile);
const result = await response.json();
setUserData(result);
if (response.ok) {
const result = await response.json();
setUserData(result.data);
}
} catch {
// no update if error
}
Expand All @@ -37,7 +39,7 @@ const UserProfilePage: React.FC = () => {
<div className="flex items-center">
<div className="rounded-full w-32 h-32 bg-gray-700 overflow-hidden mr-6">
<img
src={userData?.avatar_url || botAvatar}
src={userData?.avatarUrl || botAvatar}
alt={userData?.name || 'Bot Avatar'}
className="w-full h-full object-cover"
/>
Expand All @@ -53,7 +55,7 @@ const UserProfilePage: React.FC = () => {
<div className="mt-6">
<div className="flex items-center mb-4">
<Github className="mr-2" />
<Link to={userData?.profile_url as string} className="text-sm">
<Link to={userData?.profileUrl as string} className="text-sm">
{userData?.handle}
</Link>
</div>
Expand Down
Loading