Skip to content

Commit

Permalink
throw error catch
Browse files Browse the repository at this point in the history
  • Loading branch information
hamzakaya committed Sep 12, 2021
1 parent c19f4f3 commit 5cc47d3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 19 deletions.
11 changes: 4 additions & 7 deletions src/api/openweathermap.ts
Original file line number Diff line number Diff line change
@@ -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));
Expand All @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions src/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,9 @@ export interface IExtendedForecastData {
description: string;
};
}


export type ResponseType = {
weather: IWeatherData;
forecast: IExtendedForecastData[];
};
2 changes: 1 addition & 1 deletion src/components/HavaDurumu/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
21 changes: 15 additions & 6 deletions src/pages/Anasayfa.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -40,8 +43,14 @@ const Anasayfa = () => {
<>
<Header />
<Search callback={konumGonder} />
<HavaDurumu />
<HaftalikHavaDurumu />

{!isError && (
<>
<HavaDurumu />
<HaftalikHavaDurumu />
</>
)}

</>
);
};
Expand Down
19 changes: 14 additions & 5 deletions src/store/reducers/havadurumu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
},
Expand All @@ -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
);
Expand Down

1 comment on commit 5cc47d3

@vercel
Copy link

@vercel vercel bot commented on 5cc47d3 Sep 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.