Skip to content

Commit e6c453c

Browse files
author
Le Tuan Kiet
committed
[RTT-04] Integrate new config API
2 parents 77e21bf + 38bdc66 commit e6c453c

File tree

4 files changed

+17
-53
lines changed

4 files changed

+17
-53
lines changed

src/App.tsx

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { debounce } from '@mui/material';
22
import { useEffect, useReducer, useState } from 'react';
3-
import services from './api/index';
3+
import { getInfoCity } from './api/weather';
44
import './App.css';
55
import { ACTION_TYPE, apiReducer, INITIAL_STATE } from './utils/reducers/WeatherReducers';
66

@@ -35,15 +35,12 @@ function App() {
3535
const getInfoWithCityName = async (payload: FormSearchByName | FormSearchByLatLong) => {
3636
dispatch({ type: ACTION_TYPE.FETCH_START });
3737

38-
await services.weather
39-
.index(payload)
40-
.then(({ data: weatherInfo }) => {
41-
dispatch({ type: ACTION_TYPE.FETCH_SUCCESS, payload: weatherInfo });
42-
setIsSuccess(true);
43-
})
44-
.catch((e: ApiError) => {
45-
dispatch({ type: ACTION_TYPE.FETCH_FAILED, errorMsg: e.message });
46-
});
38+
await getInfoCity(payload).then((weatherInfo) =>{
39+
dispatch({ type: ACTION_TYPE.FETCH_SUCCESS, payload: weatherInfo });
40+
setIsSuccess(true);
41+
}).catch((e: ApiError) => {
42+
dispatch({ type: ACTION_TYPE.FETCH_FAILED, errorMsg: e.message });
43+
});
4744
};
4845

4946
return (

src/api/index.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/api/weather.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import axios from "axios";
2+
import API from "../utils/constants/api";
3+
import { linkApi } from "../utils/service/api";
4+
5+
export const getInfoCity = async (
6+
params: FormSearchByName | FormSearchByLatLong
7+
): Promise<ApiResponse> => {
8+
return axios.get(linkApi(API.WEATHER), { params }).then((d) => d.data);
9+
};

src/utils/service/api.ts

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,7 @@
1-
import axios, { AxiosError, AxiosResponse } from "axios";
21
import urlJoin from "url-join";
3-
import { API_STATUS } from "../constants/api";
42

53
const baseURL = process.env.REACT_APP_API_ENDPOINT || "";
64

7-
const linkApi = (resource: string) => {
5+
export const linkApi = (resource: string) => {
86
return urlJoin(baseURL, resource);
97
};
10-
11-
const handleError = (error: AxiosError) => {
12-
if (
13-
!error.response?.status ||
14-
[API_STATUS.HTTP_500_SERVER].includes(error.response?.status)
15-
) {
16-
console.log(error.response);
17-
} else {
18-
return error.response.data;
19-
}
20-
};
21-
22-
class RestService<T = any, R = any> {
23-
constructor(protected resource: string) {}
24-
25-
index(params?: R) {
26-
return new Promise<AxiosResponse<T>>((resolve, rejects) => {
27-
axios
28-
.get<T>(linkApi(this.resource), {
29-
params,
30-
})
31-
.then(resolve)
32-
.catch((error: AxiosError) => {
33-
rejects(handleError(error));
34-
});
35-
});
36-
}
37-
}
38-
39-
export default RestService;

0 commit comments

Comments
 (0)