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 11import { debounce } from '@mui/material' ;
22import { useEffect , useReducer , useState } from 'react' ;
3- import services from './api/index ' ;
3+ import { getInfoCity } from './api/weather ' ;
44import './App.css' ;
55import { 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 (
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" ;
21import urlJoin from "url-join" ;
3- import { API_STATUS } from "../constants/api" ;
42
53const 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 ;
You can’t perform that action at this time.
0 commit comments