Skip to content

Commit

Permalink
Refactor useDeviceLocales usage
Browse files Browse the repository at this point in the history
  • Loading branch information
mgonzalezg9 committed May 20, 2024
1 parent 2be2f5b commit a218ecb
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
22 changes: 17 additions & 5 deletions src/screens/LocationRequest/LocationRequestBody.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import Colors from "@/constants/Colors";
import { SweatherErrorCode } from "@/error/error-logic";
import useDeviceLocales from "@/i18n/hooks/useDeviceLocales";
import useWallpaper from "@/wallpaper/hooks/useWallpaper";
import useWeatherForecast from "@/weather/hooks/useWeatherForecast";
import useWeatherForecast, {
LocationQuery,
} from "@/weather/hooks/useWeatherForecast";
import { useNavigation } from "@react-navigation/native";
import React, { useEffect, useState } from "react";
import { ActivityIndicator, StyleSheet, View } from "react-native";
Expand All @@ -10,15 +13,24 @@ import SearchSection from "./SearchSection";

const LocationRequestBody: React.FC = () => {
const navigation = useNavigation();
const [location, setLocation] = useState({} as never);
const [geoData, setGeoData] = useState<Omit<LocationQuery, "device">>(
{} as never
);
const [locationDenied, setLocationDenied] = useState(false);

const { locale, timeZone } = useDeviceLocales();
const {
weather,
forecast,
loading: loadingWeather,
error: errorWeather,
} = useWeatherForecast(location);
} = useWeatherForecast({
...geoData,
device: {
locale,
timeZone,
},
});
const { wallpaper, loading: loadingWallpaper } = useWallpaper(
weather?.geolocation?.city
);
Expand All @@ -37,7 +49,7 @@ const LocationRequestBody: React.FC = () => {
if (locationDenied) {
setLocationDenied(false);
}
}, [location]);
}, [geoData]);

return (
<View style={styles.searchContainer}>
Expand All @@ -50,7 +62,7 @@ const LocationRequestBody: React.FC = () => {
) : (
<>
<SearchSection
onSearch={setLocation}
onSearch={setGeoData}
onLocationDeny={() => setLocationDenied(true)}
/>
{errorWeather || locationDenied ? (
Expand Down
4 changes: 1 addition & 3 deletions src/weather/hooks/useWeatherForecast.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { SweatherErrorCode } from "@/error/error-logic";
import useDeviceLocales from "@/i18n/hooks/useDeviceLocales";
import {
getCurrentWeather,
getHourlyForecast,
Expand Down Expand Up @@ -40,9 +39,8 @@ const getWeatherAndForecast = async ({
export default function useWeatherForecast({
location,
coordinates,
device: { locale, timeZone },
}: LocationQuery) {
const { timeZone, locale } = useDeviceLocales();

const [weather, setWeather] = useState<Weather>();
const [forecast, setForecast] = useState<Forecast>();
const [isLoading, setLoading] = useState<boolean>(false);
Expand Down

0 comments on commit a218ecb

Please sign in to comment.