Skip to content

Commit

Permalink
Fixed potentially extra calling of unread endpoint for anon users
Browse files Browse the repository at this point in the history
  • Loading branch information
dkildar committed Nov 30, 2024
1 parent 654e89c commit 605ff66
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
10 changes: 0 additions & 10 deletions src/api/private-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,16 +130,6 @@ export const getCurrencyRates = (): Promise<{
};
}> => appAxios.get(apiBase("/private-api/market-data/latest")).then((resp: any) => resp.data);

export const getUnreadNotificationCount = (username: string): Promise<number> => {
const data = { code: getAccessToken(username) };

return data.code
? appAxios
.post(apiBase(`/private-api/notifications/unread`), data)
.then((resp) => resp.data.count)
: Promise.resolve(0);
};

export interface UserImage {
created: string;
timestamp: number;
Expand Down
18 changes: 16 additions & 2 deletions src/api/queries/notifications/notification-unread-count-query.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
import { useQuery } from "@tanstack/react-query";
import { QueryIdentifiers } from "@/core/react-query";
import { useGlobalStore } from "@/core/global-store";
import { getUnreadNotificationCount } from "@/api/private-api";
import { getAccessToken } from "@/utils";
import { appAxios } from "@/api/axios";
import { apiBase } from "@/api/helper";

export function useNotificationUnreadCountQuery() {
const activeUser = useGlobalStore((state) => state.activeUser);

return useQuery({
queryKey: [QueryIdentifiers.NOTIFICATIONS_UNREAD_COUNT, activeUser?.username],
queryFn: () => getUnreadNotificationCount(activeUser!.username),
queryFn: () => {
if (!activeUser?.username) {
return;
}

const data = { code: getAccessToken(activeUser?.username) };

return data.code
? appAxios
.post(apiBase(`/private-api/notifications/unread`), data)
.then((resp) => resp.data.count)
: Promise.resolve(0);
},
enabled: !!activeUser,
initialData: 0,
refetchInterval: 60000
Expand Down

0 comments on commit 605ff66

Please sign in to comment.