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 (
_event: Electron.IpcMainInvokeEvent
): Promise<FriendRequest[] | null> => {
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);

View file

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

View file

@ -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
);
};

View file

@ -10,8 +10,7 @@ const getUser = async (
userId: string
): Promise<UserProfile | null> => {
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) => {

View file

@ -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);
}
}

View file

@ -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 },

View file

@ -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,

View file

@ -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]
);

View file

@ -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");
});