File tree Expand file tree Collapse file tree 4 files changed +17
-53
lines changed Expand file tree Collapse file tree 4 files changed +17
-53
lines changed Original file line number Diff line number Diff line change 1
1
import { debounce } from '@mui/material' ;
2
2
import { useEffect , useReducer , useState } from 'react' ;
3
- import services from './api/index ' ;
3
+ import { getInfoCity } from './api/weather ' ;
4
4
import './App.css' ;
5
5
import { ACTION_TYPE , apiReducer , INITIAL_STATE } from './utils/reducers/WeatherReducers' ;
6
6
@@ -35,15 +35,12 @@ function App() {
35
35
const getInfoWithCityName = async ( payload : FormSearchByName | FormSearchByLatLong ) => {
36
36
dispatch ( { type : ACTION_TYPE . FETCH_START } ) ;
37
37
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
+ } ) ;
47
44
} ;
48
45
49
46
return (
Load Diff This file was deleted.
Original file line number Diff line number Diff line change
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
+ } ;
Original file line number Diff line number Diff line change 1
- import axios , { AxiosError , AxiosResponse } from "axios" ;
2
1
import urlJoin from "url-join" ;
3
- import { API_STATUS } from "../constants/api" ;
4
2
5
3
const baseURL = process . env . REACT_APP_API_ENDPOINT || "" ;
6
4
7
- const linkApi = ( resource : string ) => {
5
+ export const linkApi = ( resource : string ) => {
8
6
return urlJoin ( baseURL , resource ) ;
9
7
} ;
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 ;
You can’t perform that action at this time.
0 commit comments