Skip to content

Commit

Permalink
[GSW-624] feat: Add react-query common file
Browse files Browse the repository at this point in the history
  • Loading branch information
khiemldk committed Dec 7, 2023
1 parent 1832f73 commit aa36345
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/web/src/hooks/common/useForceRefetchQuery.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { useQueryClient } from "@tanstack/react-query";

export const useForceRefetchQuery = () => {
const client = useQueryClient();

return client.invalidateQueries.bind(client);
};
3 changes: 3 additions & 0 deletions packages/web/src/react-query/token/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './request';
export * from './types';
export * from './queries';
25 changes: 25 additions & 0 deletions packages/web/src/react-query/token/queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { TokenListResponse } from "@repositories/token";
import { UseQueryOptions, useQuery } from "@tanstack/react-query";
import { getTokenPrices, getTokens } from "./request";
import { QUERY_KEY } from "./types";

export const useGetTokensList = (
option?: UseQueryOptions<TokenListResponse, Error>
) => {
return useQuery<TokenListResponse, Error>({
queryKey: [QUERY_KEY.tokens],
queryFn: getTokens,
enabled: false,
option,
});
};

export const useGetTokenPrices = (
option?: UseQueryOptions<TokenListResponse, Error>
) => {
return useQuery<TokenListResponse, Error>({
queryKey: [QUERY_KEY.tokenPrices],
queryFn: getTokenPrices,
option,
});
};
23 changes: 23 additions & 0 deletions packages/web/src/react-query/token/request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { AxiosClient } from "@common/clients/network-client/axios-client";
import { TokenListResponse, TokenPriceListResponse } from "@repositories/token";

const API_URL = process.env.NEXT_PUBLIC_API_URL;

const request = new AxiosClient(API_URL);

export const getTokens = async (): Promise<TokenListResponse> => {
const response = await request.get<TokenListResponse>({
url: "/tokens",
});
if (response.data.tokens === null) {
return { tokens: [] };
}
return response.data;
};

export const getTokenPrices = async (): Promise<TokenPriceListResponse> => {
const response = await request.get<TokenPriceListResponse>({
url: "/token_prices",
});
return response.data;
};
7 changes: 7 additions & 0 deletions packages/web/src/react-query/token/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface IExampleTypeRequest {}
export interface IExampleTypeResponse {}

export enum QUERY_KEY {
tokens = "tokens",
tokenPrices = "token_prices",
}

0 comments on commit aa36345

Please sign in to comment.