From 5cc47d3b523516d3f1ee4a44c9694a38d94d340a Mon Sep 17 00:00:00 2001 From: hamzakaya Date: Sun, 12 Sep 2021 16:47:57 +0300 Subject: [PATCH] throw error catch --- src/api/openweathermap.ts | 11 ++++------- src/api/types.ts | 6 ++++++ src/components/HavaDurumu/index.tsx | 2 +- src/pages/Anasayfa.tsx | 21 +++++++++++++++------ src/store/reducers/havadurumu.ts | 19 ++++++++++++++----- 5 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/api/openweathermap.ts b/src/api/openweathermap.ts index 7ff1c83..6296f39 100644 --- a/src/api/openweathermap.ts +++ b/src/api/openweathermap.ts @@ -1,12 +1,7 @@ import { API_BASE_URL, API_KEY, gunAdi, kelvinToCelcius, queryString, trToEng } from "../utils"; -import { IExtendedForecastData, IWeatherData } from "./types"; +import { IExtendedForecastData, IWeatherData, ResponseType } from "./types"; const CACHE_DATA = new Map(); -type ResponseType = { - weather: IWeatherData; - forecast: IExtendedForecastData[]; -}; - export const openweathermap = async (sehir: string | object): ResponseType => { const KEY = JSON.stringify(sehir); if (CACHE_DATA.has(KEY)) return JSON.parse(CACHE_DATA.get(KEY)); @@ -32,7 +27,9 @@ export const openweathermap = async (sehir: string | object): ResponseType => { haftalikHavaDurumu: haftalikData, }); }) - .catch((err) => console.error(err)); + .catch((err) => { + return reject(err.message); + }); CACHE_DATA.set(KEY, JSON.stringify(result)); return result; diff --git a/src/api/types.ts b/src/api/types.ts index 88f8e08..33c9400 100644 --- a/src/api/types.ts +++ b/src/api/types.ts @@ -36,3 +36,9 @@ export interface IExtendedForecastData { description: string; }; } + + +export type ResponseType = { + weather: IWeatherData; + forecast: IExtendedForecastData[]; +}; \ No newline at end of file diff --git a/src/components/HavaDurumu/index.tsx b/src/components/HavaDurumu/index.tsx index 87b1d3d..fa9db34 100644 --- a/src/components/HavaDurumu/index.tsx +++ b/src/components/HavaDurumu/index.tsx @@ -27,7 +27,7 @@ import { havaDurumuDetay } from "../../store/reducers/havadurumu"; const HavaDurumu: React.FC = () => { const dispatch = useDispatch(); - const { weather, dereceTipi, recieved, isLoading, isError } = + const { weather, dereceTipi, recieved, isLoading } = useSelector(havaDurumuDetay); if (isLoading || !recieved) return null; diff --git a/src/pages/Anasayfa.tsx b/src/pages/Anasayfa.tsx index 853eacc..f5b42cd 100644 --- a/src/pages/Anasayfa.tsx +++ b/src/pages/Anasayfa.tsx @@ -14,21 +14,24 @@ import { const Anasayfa = () => { const dispatch = useDispatch(); const dispatchData = (data) => dispatch(getData(data)); - const { loading } = useSelector((state: AppStore) => ({ + const { loading, isError } = useSelector((state: AppStore) => ({ loading: state.data.isLoading, + isError: state.data.isError, })); useLayoutEffect(() => { - konumGonder(); + if (!isError) konumGonder(); }, []); const konumGonder = useCallback(async (sehirAdi) => { if (!sehirAdi) { - await getPosition() + return await getPosition() .then(({ coords: { latitude: lat, longitude: lon } }) => dispatchData({ lat, lon }) ) - .catch((err) => dispatchData(DEFAULT_CITY)); + .catch((err) => { + dispatchData(DEFAULT_CITY); + }); } return dispatchData(sehirAdi); @@ -40,8 +43,14 @@ const Anasayfa = () => { <>
- - + + {!isError && ( + <> + + + + )} + ); }; diff --git a/src/store/reducers/havadurumu.ts b/src/store/reducers/havadurumu.ts index 2a89a0c..4a68d87 100644 --- a/src/store/reducers/havadurumu.ts +++ b/src/store/reducers/havadurumu.ts @@ -6,7 +6,11 @@ import { } from "@reduxjs/toolkit"; import { AppStore } from ".."; import { openweathermap } from "../../api/openweathermap"; -import { IExtendedForecastData, IWeatherData } from "../../api/types"; +import { + IExtendedForecastData, + IWeatherData, + ResponseType, +} from "../../api/types"; export interface IWeatherState { weatherData: IWeatherData; @@ -47,9 +51,15 @@ const havaDurumuSlice = createSlice({ state.isLoading = true; }, [getData.fulfilled]: (state: IWeatherState, action: Action) => { - const { weather, forecast } = action.payload; - state.weatherData = weather; - state.extendedWeatherData = forecast; + if (!!action?.payload) { + const { weather, forecast }: ResponseType = action.payload; + state.weatherData = weather; + state.extendedWeatherData = forecast; + state.isError = false; + } else { + state.isError = true; + } + state.isLoading = false; state.isRecieved = true; }, @@ -75,7 +85,6 @@ export const havaDurumuDetay = createSelector( isLoading: state.data.isLoading, weather: state.data.weatherData, recieved: state.data.isRecieved, - isError: state.data.isError, }), (state) => state );