Skip to content

Commit

Permalink
#44 - Add navigate to location functions
Browse files Browse the repository at this point in the history
  • Loading branch information
asharonbaltazar committed Dec 13, 2022
1 parent b5e35c8 commit 1e88abf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
15 changes: 11 additions & 4 deletions src/pages/search/GeolocationButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ import { useAppDispatch } from '@store';
import { getGeolocationGeocode } from '@slices/searchSlice.thunks';
import { getWeather } from '@slices/weatherSlice.thunks';
import { unwrapResult } from '@reduxjs/toolkit';
import { useNavigate } from 'react-router-dom';

export const GeolocationButton = () => {
const dispatch = useAppDispatch();
const navigate = useNavigate();

const getGeoLocation = async () =>
dispatch(getGeolocationGeocode())
.then(unwrapResult)
.then((geolocationGeocode) => dispatch(getWeather(geolocationGeocode)));
const getGeoLocation = async () => {
navigate('/');

const geolocationGeocode = unwrapResult(
await dispatch(getGeolocationGeocode())
);

return dispatch(getWeather(geolocationGeocode));
};

return (
<div className="mt-3 md:mt-5">
Expand Down
8 changes: 5 additions & 3 deletions src/pages/search/LocationResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ export const LocationResults = () => {
const { locations, recentLocations, loading, errors } = useSearch();

const getGeocoding = async (text: Text, placeId: string) => {
navigate('/');

const geocodeLocation = unwrapResult(await dispatch(getGeocode(placeId)));

return dispatch(getWeather(geocodeLocation))
.then(() => navigate('/'))
.then(() => dispatch(setRecentLocation({ text, placeId })));
return dispatch(getWeather(geocodeLocation)).then(() =>
dispatch(setRecentLocation({ text, placeId }))
);
};

if (loading) {
Expand Down
13 changes: 6 additions & 7 deletions src/pages/search/Search.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect } from 'react';
import { useCallback, useEffect } from 'react';
import { LocationInput } from '@pages/search/LocationInput';
import { LocationSelection } from '@pages/search/LocationSelection';
import { GoogleAttribution } from '@pages/search/GoogleAttribution';
Expand All @@ -9,12 +9,11 @@ import { resetLocations } from '@slices/searchSlice';
export const Search = () => {
const dispatch = useAppDispatch();

useEffect(
() => () => {
dispatch(resetLocations());
},
[dispatch]
);
const resetLocationsOnExit = useCallback(() => {
dispatch(resetLocations());
}, [dispatch]);

useEffect(() => resetLocationsOnExit, [resetLocationsOnExit]);

return (
<div className="flex h-screen flex-col">
Expand Down

0 comments on commit 1e88abf

Please sign in to comment.