Skip to content

Commit

Permalink
Created an action creator to handle the coordinates recieved from the…
Browse files Browse the repository at this point in the history
… openstreetmap api
  • Loading branch information
hmdNetizen committed Jul 2, 2021
1 parent ebbfb10 commit 6bdf22c
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 16 deletions.
20 changes: 14 additions & 6 deletions src/components/SearchField.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ import { useState, useEffect } from "react";
import { MdSearch, MdClose, MdLocationOn } from "react-icons/md";
import { useActions } from "../components/hooks/useActions";
import { useTypedSelector } from "./hooks/useTypedSelector";
import { locationDataset } from "../store/reducers/weatherReducer";

interface Props {
openSearch: boolean;
setOpenSearch: (search: boolean) => void;
}

const SearchField: React.FC<Props> = ({ setOpenSearch }) => {
const { getLocationFromMap } = useActions();
const { getLocationFromMap, clearLocationFromMap, getLocationCoords } =
useActions();
const { mapLocation } = useTypedSelector((state) => state.weather);

const [inputText, setInputText] = useState("");
const [autoComplete, setAutoComplete] = useState([]);

const handleInputChange = (
event: React.ChangeEvent<HTMLInputElement>
Expand All @@ -25,13 +26,16 @@ const SearchField: React.FC<Props> = ({ setOpenSearch }) => {
event.preventDefault();

if (!inputText) {
setAutoComplete([]);
return;
}
};

useEffect(() => {
getLocationFromMap(inputText);
if (inputText) {
getLocationFromMap(inputText);
} else {
clearLocationFromMap();
}

// eslint-disable-next-line
}, [inputText]);
Expand All @@ -58,8 +62,12 @@ const SearchField: React.FC<Props> = ({ setOpenSearch }) => {
</form>
{mapLocation.length > 0 && (
<ul className="search__list">
{mapLocation.map((location) => (
<li className="search__list__item" key={location.placeId}>
{mapLocation.map((location: locationDataset, index: number) => (
<li
className="search__list__item"
key={index}
onClick={() => getLocationCoords(location)}
>
<MdLocationOn className="search__list__item__icon" />{" "}
<span className="search__list__item__text">
{location.display_name}
Expand Down
1 change: 1 addition & 0 deletions src/sass/abstracts/_variables.scss
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ $color-dark-grey: #616475;
$color-light-blue: #3c47e9;
$color-light-blue-1: #3a44d8;
$color-light-blue-2: #343ec5;
$color-light-blue-3: #bfc3fc;
$color-dark-blue: #110e3c;
$color-dark-blue-1: #0f0b53;
$color-yellow: #ffec65;
5 changes: 5 additions & 0 deletions src/sass/components/_searchField.scss
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
&__list {
width: 100%;
background: $color-white;
margin-top: 0.5em;

&__item {
display: flex;
Expand All @@ -73,6 +74,10 @@
&:not(:last-of-type) {
border-bottom: 1px solid $color-grey-light;
}

&:hover {
background: rgba($color-light-blue-1, 0.2);
}
}

&__item__icon {
Expand Down
5 changes: 5 additions & 0 deletions src/sass/index.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/sass/index.css.map

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions src/store/action-creators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const fetchWeatherData =
}
};

// This fetches data corresponding to the input text from openstreetmap api
export const getLocationFromMap =
(position: string) => async (dispatch: Dispatch) => {
const location = position.split(" ").join("+");
Expand All @@ -48,6 +49,24 @@ export const getLocationFromMap =
}
};

// This clears off the data from openstreetmap api
export const clearLocationFromMap = () => (dispatch: Dispatch) => {
dispatch({
type: ActionTypes.CLEAR_LOCATION_FROM_MAP,
});
};

export const getLocationCoords =
(coords: { lat: string; lon: string }) => (dispatch: Dispatch) => {
dispatch({
type: ActionTypes.GET_LOCATION_COORDINATES,
payload: {
lat: coords.lat,
long: coords.lon,
},
});
};

export const getDegreeCelsius = () => (dispatch: Dispatch) =>
dispatch({
type: ActionTypes.WEATHER_DEGREE_IS_CELSIUS,
Expand Down
2 changes: 2 additions & 0 deletions src/store/action-types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ export enum ActionTypes {
WEATHER_DEGREE_IS_CELSIUS = "WEATHER_DEGREE_IS_CELSIUS",
WEATHER_DEGREE_IS_FAHRENHEIT = "WEATHER_DEGREE_IS_FAHRENHEIT",
GET_LOCATION_FROM_MAP = "GET_LOCATION_FROM_MAP",
CLEAR_LOCATION_FROM_MAP = "CLEAR_LOCATION_FROM_MAP",
GET_LOCATION_COORDINATES = "GET_LOCATION_COORDINATES",
}
16 changes: 15 additions & 1 deletion src/store/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,25 @@ interface getLocationFromMapAction {
payload: locationDataset[];
}

interface clearLocationFromMapAction {
type: ActionTypes.CLEAR_LOCATION_FROM_MAP;
}

interface getLocationCoordsAction {
type: ActionTypes.GET_LOCATION_COORDINATES;
payload: {
lat: string;
lon: string;
};
}

export type Action =
| GetWeatherDataAction
| GetWeatherDataSuccessAction
| GetWeatherDataFailureAction
| WeatherDegreeIsCelsiusAction
| weatherDegreeIsFahrenheitAction
| SearchedWeatherDataSuccessAction
| getLocationFromMapAction;
| getLocationFromMapAction
| clearLocationFromMapAction
| getLocationCoordsAction;
35 changes: 27 additions & 8 deletions src/store/reducers/weatherReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,27 @@ export interface locationDataset {
display_name: string;
}

interface WeatherState {
loading: boolean;
weatherData: WeatherDataset | null;
error: string | null;
isCelsius: boolean;
mapLocation: locationDataset[] | [];
coords: {
lat: string;
lon: string;
} | null;
}

const initialState = {
loading: false,
weatherData: null,
error: null,
isCelsius: true,
coords: null,
mapLocation: [],
};

interface WeatherState {
loading: boolean;
weatherData: WeatherDataset | null;
error: string | null;
isCelsius: boolean;
mapLocation: locationDataset[];
}

const weatherReducer = (
state: WeatherState = initialState,
action: Action
Expand Down Expand Up @@ -86,6 +91,20 @@ const weatherReducer = (
error: null,
mapLocation: action.payload,
};
case ActionTypes.CLEAR_LOCATION_FROM_MAP:
return {
...state,
loading: false,
error: null,
mapLocation: [],
};
case ActionTypes.GET_LOCATION_COORDINATES:
return {
...state,
loading: false,
error: null,
coords: action.payload,
};
default:
return state;
}
Expand Down

0 comments on commit 6bdf22c

Please sign in to comment.