From 6f70b529a224510fab91064d029dd9b651ce1d30 Mon Sep 17 00:00:00 2001 From: Zamitto <167933696+zamitto@users.noreply.github.com> Date: Wed, 10 Jul 2024 21:35:39 -0300 Subject: [PATCH] feat: refactor hydra api --- .../events/profile/get-friend-requests.ts | 7 +------ src/main/events/profile/get-me.ts | 4 +--- src/main/events/profile/update-profile.ts | 6 +++--- src/main/events/user/get-user.ts | 3 +-- src/main/services/hydra-api.ts | 5 +++++ src/main/services/library-sync/create-game.ts | 6 +----- .../library-sync/merge-with-remote-games.ts | 2 +- src/renderer/src/hooks/use-user-details.ts | 18 ++++++++++-------- .../src/pages/user/user-add-friends-modal.tsx | 19 ++++--------------- 9 files changed, 27 insertions(+), 43 deletions(-) diff --git a/src/main/events/profile/get-friend-requests.ts b/src/main/events/profile/get-friend-requests.ts index 0e75efba..2c04b865 100644 --- a/src/main/events/profile/get-friend-requests.ts +++ b/src/main/events/profile/get-friend-requests.ts @@ -5,12 +5,7 @@ import { FriendRequest } from "@types"; const getFriendRequests = async ( _event: Electron.IpcMainInvokeEvent ): Promise => { - try { - const response = await HydraApi.get(`/profile/friend-requests`); - return response.data; - } catch (err) { - return null; - } + return HydraApi.get(`/profile/friend-requests`).catch(() => null); }; registerEvent("getFriendRequests", getFriendRequests); diff --git a/src/main/events/profile/get-me.ts b/src/main/events/profile/get-me.ts index 83463680..6e451134 100644 --- a/src/main/events/profile/get-me.ts +++ b/src/main/events/profile/get-me.ts @@ -9,9 +9,7 @@ const getMe = async ( _event: Electron.IpcMainInvokeEvent ): Promise => { return HydraApi.get(`/profile/me`) - .then((response) => { - const me = response.data; - + .then((me) => { userAuthRepository.upsert( { id: 1, diff --git a/src/main/events/profile/update-profile.ts b/src/main/events/profile/update-profile.ts index fe79d345..e6671992 100644 --- a/src/main/events/profile/update-profile.ts +++ b/src/main/events/profile/update-profile.ts @@ -29,7 +29,7 @@ const updateProfile = async ( ) => { if (!newProfileImagePath) { return patchUserProfile(displayName).then( - (response) => response.data as UserProfile + (response) => response as UserProfile ); } @@ -42,7 +42,7 @@ const updateProfile = async ( imageLength: fileSizeInBytes, }) .then(async (preSignedResponse) => { - const { presignedUrl, profileImageUrl } = preSignedResponse.data; + const { presignedUrl, profileImageUrl } = preSignedResponse; const mimeType = await fileTypeFromFile(newProfileImagePath); @@ -56,7 +56,7 @@ const updateProfile = async ( .catch(() => undefined); return patchUserProfile(displayName, profileImageUrl).then( - (response) => response.data as UserProfile + (response) => response as UserProfile ); }; diff --git a/src/main/events/user/get-user.ts b/src/main/events/user/get-user.ts index 596df084..7b4c0aa8 100644 --- a/src/main/events/user/get-user.ts +++ b/src/main/events/user/get-user.ts @@ -10,8 +10,7 @@ const getUser = async ( userId: string ): Promise => { try { - const response = await HydraApi.get(`/user/${userId}`); - const profile = response.data; + const profile = await HydraApi.get(`/user/${userId}`); const recentGames = await Promise.all( profile.recentGames.map(async (game) => { diff --git a/src/main/services/hydra-api.ts b/src/main/services/hydra-api.ts index 1abae98c..f74403ce 100644 --- a/src/main/services/hydra-api.ts +++ b/src/main/services/hydra-api.ts @@ -190,6 +190,7 @@ export class HydraApi { await this.revalidateAccessTokenIfExpired(); return this.instance .get(url, this.getAxiosConfig()) + .then((response) => response.data) .catch(this.handleUnauthorizedError); } @@ -199,6 +200,7 @@ export class HydraApi { await this.revalidateAccessTokenIfExpired(); return this.instance .post(url, data, this.getAxiosConfig()) + .then((response) => response.data) .catch(this.handleUnauthorizedError); } @@ -208,6 +210,7 @@ export class HydraApi { await this.revalidateAccessTokenIfExpired(); return this.instance .put(url, data, this.getAxiosConfig()) + .then((response) => response.data) .catch(this.handleUnauthorizedError); } @@ -217,6 +220,7 @@ export class HydraApi { await this.revalidateAccessTokenIfExpired(); return this.instance .patch(url, data, this.getAxiosConfig()) + .then((response) => response.data) .catch(this.handleUnauthorizedError); } @@ -226,6 +230,7 @@ export class HydraApi { await this.revalidateAccessTokenIfExpired(); return this.instance .delete(url, this.getAxiosConfig()) + .then((response) => response.data) .catch(this.handleUnauthorizedError); } } diff --git a/src/main/services/library-sync/create-game.ts b/src/main/services/library-sync/create-game.ts index c0e8b1f8..b66a1897 100644 --- a/src/main/services/library-sync/create-game.ts +++ b/src/main/services/library-sync/create-game.ts @@ -10,11 +10,7 @@ export const createGame = async (game: Game) => { lastTimePlayed: game.lastTimePlayed, }) .then((response) => { - const { - id: remoteId, - playTimeInMilliseconds, - lastTimePlayed, - } = response.data; + const { id: remoteId, playTimeInMilliseconds, lastTimePlayed } = response; gameRepository.update( { objectID: game.objectID }, diff --git a/src/main/services/library-sync/merge-with-remote-games.ts b/src/main/services/library-sync/merge-with-remote-games.ts index 2162ea58..2a6b5bb5 100644 --- a/src/main/services/library-sync/merge-with-remote-games.ts +++ b/src/main/services/library-sync/merge-with-remote-games.ts @@ -6,7 +6,7 @@ import { getSteamAppAsset } from "@main/helpers"; export const mergeWithRemoteGames = async () => { return HydraApi.get("/games") .then(async (response) => { - for (const game of response.data) { + for (const game of response) { const localGame = await gameRepository.findOne({ where: { objectID: game.objectId, diff --git a/src/renderer/src/hooks/use-user-details.ts b/src/renderer/src/hooks/use-user-details.ts index ecf66276..ceb2df3f 100644 --- a/src/renderer/src/hooks/use-user-details.ts +++ b/src/renderer/src/hooks/use-user-details.ts @@ -82,23 +82,25 @@ export function useUserDetails() { [updateUserDetails] ); - const sendFriendRequest = useCallback(async (userId: string) => { - return window.electron.sendFriendRequest(userId); - }, []); - const updateFriendRequests = useCallback(async () => { const friendRequests = await window.electron.getFriendRequests(); dispatch(setFriendRequests(friendRequests)); }, [dispatch]); + const sendFriendRequest = useCallback( + async (userId: string) => { + return window.electron + .sendFriendRequest(userId) + .then(() => updateFriendRequests()); + }, + [updateFriendRequests] + ); + const updateFriendRequestState = useCallback( async (userId: string, action: FriendRequestAction) => { return window.electron .updateFriendRequest(userId, action) - .then(() => {}) - .catch(() => { - console.log("falha no updateFriendsRequestState"); - }); + .then(() => updateFriendRequests()); }, [updateFriendRequests] ); diff --git a/src/renderer/src/pages/user/user-add-friends-modal.tsx b/src/renderer/src/pages/user/user-add-friends-modal.tsx index a5462c5b..b61f293c 100644 --- a/src/renderer/src/pages/user/user-add-friends-modal.tsx +++ b/src/renderer/src/pages/user/user-add-friends-modal.tsx @@ -22,12 +22,8 @@ export const UserAddFriendsModal = ({ const navigate = useNavigate(); - const { - sendFriendRequest, - updateFriendRequests, - updateFriendRequestState, - friendRequests, - } = useUserDetails(); + const { sendFriendRequest, updateFriendRequestState, friendRequests } = + useUserDetails(); const { showSuccessToast, showErrorToast } = useToast(); @@ -35,7 +31,6 @@ export const UserAddFriendsModal = ({ setIsAddingFriend(true); sendFriendRequest(friendCode) .then(() => { - updateFriendRequests(); showSuccessToast(t("friend_request_sent")); }) .catch(() => { @@ -47,13 +42,11 @@ export const UserAddFriendsModal = ({ }; const handleClickFriend = (userId: string) => { - console.log("click friend"); - onClose(); - navigate(`/user/${userId}`); + //onClose(); + //navigate(`/user/${userId}`); }; const handleClickSeeProfile = () => { - console.log("click see profile"); onClose(); navigate(`/user/${friendCode}`); }; @@ -62,8 +55,6 @@ export const UserAddFriendsModal = ({ event: React.MouseEvent, userId: string ) => { - console.log("cancel"); - event.preventDefault(); updateFriendRequestState(userId, "CANCEL") .then(() => { console.log("sucesso"); @@ -77,8 +68,6 @@ export const UserAddFriendsModal = ({ event: React.MouseEvent, userId: string ) => { - console.log("accept friend request"); - event.preventDefault(); updateFriendRequestState(userId, "ACCEPTED").catch(() => { showErrorToast("Falha ao aceitar convite"); });