-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* feat : Add HTTP client module * feat : Add QueryClient and QueryClientProvider for React Query * feat : Add API and hook for fetching topics * refactor : Refactor authorization header in http.ts * feat : Add useTopicsQuery hook to TopicCardContainer * refactor : Refactor TopicCard and TopicCardContainer components eliminate useState,useEffect in TopicCardContainer
- Loading branch information
Showing
6 changed files
with
62 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios'; | ||
|
||
const API_POSTFIX = 'api'; | ||
const BASE_URL = process.env.APP_URL || `https://mapbefine.com/${API_POSTFIX}`; | ||
|
||
const token = localStorage.getItem('userToken'); | ||
|
||
const axiosInstance = axios.create({ | ||
baseURL: BASE_URL, | ||
headers: token ? { Authorization: `Bearer ${token}` } : {}, | ||
}); | ||
|
||
export interface HttpClient extends AxiosInstance { | ||
get<T = unknown>(url: string, config?: AxiosRequestConfig): Promise<T>; | ||
post<T = unknown>( | ||
url: string, | ||
data?: any, | ||
config?: AxiosRequestConfig, | ||
): Promise<T>; | ||
patch<T = unknown>( | ||
url: string, | ||
data?: any, | ||
config?: AxiosRequestConfig, | ||
): Promise<T>; | ||
put<T = unknown>( | ||
url: string, | ||
data?: any, | ||
config?: AxiosRequestConfig, | ||
): Promise<T>; | ||
delete<T = unknown>(url: string, config?: AxiosRequestConfig): Promise<T>; | ||
} | ||
|
||
export const http: HttpClient = axiosInstance; | ||
|
||
http.interceptors.response.use((res) => res.data); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { TopicCardProps } from '../types/Topic'; | ||
import { http } from './http'; | ||
|
||
export const getTopics = (url: string) => http.get<TopicCardProps[]>(url); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
|
||
import { getTopics } from '../../api'; | ||
|
||
const useTopicsQuery = (url: string) => { | ||
const { data: topics, refetch: refetchTopics } = useQuery({ | ||
queryKey: ['topics', url], | ||
queryFn: () => getTopics(url), | ||
}); | ||
return { topics, refetchTopics }; | ||
}; | ||
|
||
export default useTopicsQuery; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters