feat: refactor hydra api

This commit is contained in:
Zamitto 2024-07-10 21:35:39 -03:00
parent a81b016500
commit 6f70b529a2
9 changed files with 27 additions and 43 deletions

View file

@ -5,12 +5,7 @@ import { FriendRequest } from "@types";
const getFriendRequests = async ( const getFriendRequests = async (
_event: Electron.IpcMainInvokeEvent _event: Electron.IpcMainInvokeEvent
): Promise<FriendRequest[] | null> => { ): Promise<FriendRequest[] | null> => {
try { return HydraApi.get(`/profile/friend-requests`).catch(() => null);
const response = await HydraApi.get(`/profile/friend-requests`);
return response.data;
} catch (err) {
return null;
}
}; };
registerEvent("getFriendRequests", getFriendRequests); registerEvent("getFriendRequests", getFriendRequests);

View file

@ -9,9 +9,7 @@ const getMe = async (
_event: Electron.IpcMainInvokeEvent _event: Electron.IpcMainInvokeEvent
): Promise<UserProfile | null> => { ): Promise<UserProfile | null> => {
return HydraApi.get(`/profile/me`) return HydraApi.get(`/profile/me`)
.then((response) => { .then((me) => {
const me = response.data;
userAuthRepository.upsert( userAuthRepository.upsert(
{ {
id: 1, id: 1,

View file

@ -29,7 +29,7 @@ const updateProfile = async (
) => { ) => {
if (!newProfileImagePath) { if (!newProfileImagePath) {
return patchUserProfile(displayName).then( return patchUserProfile(displayName).then(
(response) => response.data as UserProfile (response) => response as UserProfile
); );
} }
@ -42,7 +42,7 @@ const updateProfile = async (
imageLength: fileSizeInBytes, imageLength: fileSizeInBytes,
}) })
.then(async (preSignedResponse) => { .then(async (preSignedResponse) => {
const { presignedUrl, profileImageUrl } = preSignedResponse.data; const { presignedUrl, profileImageUrl } = preSignedResponse;
const mimeType = await fileTypeFromFile(newProfileImagePath); const mimeType = await fileTypeFromFile(newProfileImagePath);
@ -56,7 +56,7 @@ const updateProfile = async (
.catch(() => undefined); .catch(() => undefined);
return patchUserProfile(displayName, profileImageUrl).then( return patchUserProfile(displayName, profileImageUrl).then(
(response) => response.data as UserProfile (response) => response as UserProfile
); );
}; };

View file

@ -10,8 +10,7 @@ const getUser = async (
userId: string userId: string
): Promise<UserProfile | null> => { ): Promise<UserProfile | null> => {
try { try {
const response = await HydraApi.get(`/user/${userId}`); const profile = await HydraApi.get(`/user/${userId}`);
const profile = response.data;
const recentGames = await Promise.all( const recentGames = await Promise.all(
profile.recentGames.map(async (game) => { profile.recentGames.map(async (game) => {

View file

@ -190,6 +190,7 @@ export class HydraApi {
await this.revalidateAccessTokenIfExpired(); await this.revalidateAccessTokenIfExpired();
return this.instance return this.instance
.get(url, this.getAxiosConfig()) .get(url, this.getAxiosConfig())
.then((response) => response.data)
.catch(this.handleUnauthorizedError); .catch(this.handleUnauthorizedError);
} }
@ -199,6 +200,7 @@ export class HydraApi {
await this.revalidateAccessTokenIfExpired(); await this.revalidateAccessTokenIfExpired();
return this.instance return this.instance
.post(url, data, this.getAxiosConfig()) .post(url, data, this.getAxiosConfig())
.then((response) => response.data)
.catch(this.handleUnauthorizedError); .catch(this.handleUnauthorizedError);
} }
@ -208,6 +210,7 @@ export class HydraApi {
await this.revalidateAccessTokenIfExpired(); await this.revalidateAccessTokenIfExpired();
return this.instance return this.instance
.put(url, data, this.getAxiosConfig()) .put(url, data, this.getAxiosConfig())
.then((response) => response.data)
.catch(this.handleUnauthorizedError); .catch(this.handleUnauthorizedError);
} }
@ -217,6 +220,7 @@ export class HydraApi {
await this.revalidateAccessTokenIfExpired(); await this.revalidateAccessTokenIfExpired();
return this.instance return this.instance
.patch(url, data, this.getAxiosConfig()) .patch(url, data, this.getAxiosConfig())
.then((response) => response.data)
.catch(this.handleUnauthorizedError); .catch(this.handleUnauthorizedError);
} }
@ -226,6 +230,7 @@ export class HydraApi {
await this.revalidateAccessTokenIfExpired(); await this.revalidateAccessTokenIfExpired();
return this.instance return this.instance
.delete(url, this.getAxiosConfig()) .delete(url, this.getAxiosConfig())
.then((response) => response.data)
.catch(this.handleUnauthorizedError); .catch(this.handleUnauthorizedError);
} }
} }

View file

@ -10,11 +10,7 @@ export const createGame = async (game: Game) => {
lastTimePlayed: game.lastTimePlayed, lastTimePlayed: game.lastTimePlayed,
}) })
.then((response) => { .then((response) => {
const { const { id: remoteId, playTimeInMilliseconds, lastTimePlayed } = response;
id: remoteId,
playTimeInMilliseconds,
lastTimePlayed,
} = response.data;
gameRepository.update( gameRepository.update(
{ objectID: game.objectID }, { objectID: game.objectID },

View file

@ -6,7 +6,7 @@ import { getSteamAppAsset } from "@main/helpers";
export const mergeWithRemoteGames = async () => { export const mergeWithRemoteGames = async () => {
return HydraApi.get("/games") return HydraApi.get("/games")
.then(async (response) => { .then(async (response) => {
for (const game of response.data) { for (const game of response) {
const localGame = await gameRepository.findOne({ const localGame = await gameRepository.findOne({
where: { where: {
objectID: game.objectId, objectID: game.objectId,

View file

@ -82,23 +82,25 @@ export function useUserDetails() {
[updateUserDetails] [updateUserDetails]
); );
const sendFriendRequest = useCallback(async (userId: string) => {
return window.electron.sendFriendRequest(userId);
}, []);
const updateFriendRequests = useCallback(async () => { const updateFriendRequests = useCallback(async () => {
const friendRequests = await window.electron.getFriendRequests(); const friendRequests = await window.electron.getFriendRequests();
dispatch(setFriendRequests(friendRequests)); dispatch(setFriendRequests(friendRequests));
}, [dispatch]); }, [dispatch]);
const sendFriendRequest = useCallback(
async (userId: string) => {
return window.electron
.sendFriendRequest(userId)
.then(() => updateFriendRequests());
},
[updateFriendRequests]
);
const updateFriendRequestState = useCallback( const updateFriendRequestState = useCallback(
async (userId: string, action: FriendRequestAction) => { async (userId: string, action: FriendRequestAction) => {
return window.electron return window.electron
.updateFriendRequest(userId, action) .updateFriendRequest(userId, action)
.then(() => {}) .then(() => updateFriendRequests());
.catch(() => {
console.log("falha no updateFriendsRequestState");
});
}, },
[updateFriendRequests] [updateFriendRequests]
); );

View file

@ -22,12 +22,8 @@ export const UserAddFriendsModal = ({
const navigate = useNavigate(); const navigate = useNavigate();
const { const { sendFriendRequest, updateFriendRequestState, friendRequests } =
sendFriendRequest, useUserDetails();
updateFriendRequests,
updateFriendRequestState,
friendRequests,
} = useUserDetails();
const { showSuccessToast, showErrorToast } = useToast(); const { showSuccessToast, showErrorToast } = useToast();
@ -35,7 +31,6 @@ export const UserAddFriendsModal = ({
setIsAddingFriend(true); setIsAddingFriend(true);
sendFriendRequest(friendCode) sendFriendRequest(friendCode)
.then(() => { .then(() => {
updateFriendRequests();
showSuccessToast(t("friend_request_sent")); showSuccessToast(t("friend_request_sent"));
}) })
.catch(() => { .catch(() => {
@ -47,13 +42,11 @@ export const UserAddFriendsModal = ({
}; };
const handleClickFriend = (userId: string) => { const handleClickFriend = (userId: string) => {
console.log("click friend"); //onClose();
onClose(); //navigate(`/user/${userId}`);
navigate(`/user/${userId}`);
}; };
const handleClickSeeProfile = () => { const handleClickSeeProfile = () => {
console.log("click see profile");
onClose(); onClose();
navigate(`/user/${friendCode}`); navigate(`/user/${friendCode}`);
}; };
@ -62,8 +55,6 @@ export const UserAddFriendsModal = ({
event: React.MouseEvent, event: React.MouseEvent,
userId: string userId: string
) => { ) => {
console.log("cancel");
event.preventDefault();
updateFriendRequestState(userId, "CANCEL") updateFriendRequestState(userId, "CANCEL")
.then(() => { .then(() => {
console.log("sucesso"); console.log("sucesso");
@ -77,8 +68,6 @@ export const UserAddFriendsModal = ({
event: React.MouseEvent, event: React.MouseEvent,
userId: string userId: string
) => { ) => {
console.log("accept friend request");
event.preventDefault();
updateFriendRequestState(userId, "ACCEPTED").catch(() => { updateFriendRequestState(userId, "ACCEPTED").catch(() => {
showErrorToast("Falha ao aceitar convite"); showErrorToast("Falha ao aceitar convite");
}); });