getting user profile from api

This commit is contained in:
Zamitto 2024-06-11 23:05:05 -03:00
parent a974141360
commit b8895afc0a
8 changed files with 57 additions and 14 deletions

View file

@ -1,17 +1,15 @@
import axios from "axios";
import { registerEvent } from "../register-event"; import { registerEvent } from "../register-event";
import { userProfileSchema } from "../helpers/validators"; import { userProfileSchema } from "../helpers/validators";
import { logger } from "@main/services"; import { logger } from "@main/services";
import { HydraApi } from "@main/services/hydra-api";
const getUserProfile = async ( const getUserProfile = async (
_event: Electron.IpcMainInvokeEvent, _event: Electron.IpcMainInvokeEvent,
username: string username: string
) => { ) => {
return axios return HydraApi.get(`/profile/${username}`)
.get(`${process.env.API_URL}/profile/${username}`)
.then((response) => { .then((response) => {
const profile = userProfileSchema.parse(response.data); const profile = userProfileSchema.parse(response.data);
console.log(profile);
return profile; return profile;
}) })
.catch((err) => { .catch((err) => {
@ -20,4 +18,4 @@ const getUserProfile = async (
}); });
}; };
registerEvent("getUserProfiel", getUserProfile); registerEvent("getUserProfile", getUserProfile);

View file

@ -39,6 +39,7 @@ export function Header({ onSearch, onClear, search }: HeaderProps) {
const title = useMemo(() => { const title = useMemo(() => {
if (location.pathname.startsWith("/game")) return headerTitle; if (location.pathname.startsWith("/game")) return headerTitle;
if (location.pathname.startsWith("/profile")) return headerTitle;
if (location.pathname.startsWith("/search")) return t("search_results"); if (location.pathname.startsWith("/search")) return t("search_results");
return t(pathTitle[location.pathname]); return t(pathTitle[location.pathname]);

View file

@ -143,6 +143,10 @@ export function Sidebar() {
} }
}; };
const handleClickProfile = () => {
navigate("/profile/brownie");
};
return ( return (
<> <>
<aside <aside
@ -154,7 +158,11 @@ export function Sidebar() {
maxWidth: sidebarWidth, maxWidth: sidebarWidth,
}} }}
> >
<button type="button" className={styles.profileButton}> <button
type="button"
className={styles.profileButton}
onClick={handleClickProfile}
>
<div className={styles.profileAvatar}> <div className={styles.profileAvatar}>
<PersonIcon /> <PersonIcon />

View file

@ -13,6 +13,7 @@ import type {
StartGameDownloadPayload, StartGameDownloadPayload,
RealDebridUser, RealDebridUser,
DownloadSource, DownloadSource,
UserProfile,
} from "@types"; } from "@types";
import type { DiskSpace } from "check-disk-space"; import type { DiskSpace } from "check-disk-space";

View file

@ -27,6 +27,7 @@ import {
import { store } from "./store"; import { store } from "./store";
import * as resources from "@locales"; import * as resources from "@locales";
import { Profile } from "./pages/profile/profile";
i18n i18n
.use(LanguageDetector) .use(LanguageDetector)
@ -54,6 +55,7 @@ ReactDOM.createRoot(document.getElementById("root")!).render(
<Route path="/game/:shop/:objectID" Component={GameDetails} /> <Route path="/game/:shop/:objectID" Component={GameDetails} />
<Route path="/search" Component={SearchResults} /> <Route path="/search" Component={SearchResults} />
<Route path="/settings" Component={Settings} /> <Route path="/settings" Component={Settings} />
<Route path="profile/:username" Component={Profile} />
</Route> </Route>
</Routes> </Routes>
</HashRouter> </HashRouter>

View file

@ -0,0 +1,16 @@
import { UserProfile } from "@types";
import { useTranslation } from "react-i18next";
export interface ProfileContentProps {
userProfile: UserProfile;
}
export const ProfileContent = ({ userProfile }: ProfileContentProps) => {
const { t } = useTranslation("profile");
return (
<>
<p>{userProfile.username}</p>
</>
);
};

View file

@ -0,0 +1,7 @@
export const ProfileSkeleton = () => {
return (
<>
<p>Loading....</p>
</>
);
};

View file

@ -1,17 +1,27 @@
import { UserProfile } from "@types"; import { UserProfile } from "@types";
import { useState } from "react"; import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { useParams } from "react-router-dom"; import { useParams } from "react-router-dom";
import { setHeaderTitle } from "@renderer/features";
import { useAppDispatch } from "@renderer/hooks";
import { ProfileSkeleton } from "./profile-skeleton";
import { ProfileContent } from "./profile-content";
export const Profile = () => { export const Profile = () => {
const { username } = useParams(); const { username } = useParams();
const [userProfile, setUserProfile] = useState<UserProfile>(); const [userProfile, setUserProfile] = useState<UserProfile>();
const { t } = useTranslation("profile"); const dispatch = useAppDispatch();
return ( useEffect(() => {
<> window.electron.getUserProfile(username!).then((userProfile) => {
<p>Tela do usuarioooooooo</p> if (userProfile) {
</> dispatch(setHeaderTitle(userProfile.username));
); setUserProfile(userProfile);
}
});
}, [dispatch]);
if (!userProfile) return <ProfileSkeleton />;
return <ProfileContent userProfile={userProfile} />;
}; };