Skip to content

Commit

Permalink
Clear currently fetched weather data when making a new search
Browse files Browse the repository at this point in the history
  • Loading branch information
ThuHtooSan committed Jan 15, 2024
1 parent 16c9162 commit 4949846
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/components/SearchBar/SearchBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import { icon } from '@fortawesome/fontawesome-svg-core/import.macro';
import { useEffect, useRef, useState } from 'react';
import './search-bar.scss';
import { useNavigate } from 'react-router-dom';
import { useAppContext } from '../../hooks';

const SearchBar = () => {
const { dispatch } = useAppContext();
const navigate = useNavigate();
const [input, setInput] = useState('');
const inputRef = useRef<HTMLInputElement>(null!);
Expand All @@ -20,6 +22,7 @@ const SearchBar = () => {
if (e.key === 'Enter') {
navigate(`/weather?city=${input.trim()}`);
setInput('');
dispatch({ type: 'RESET_WEATHER' });
}
};

Expand Down
5 changes: 5 additions & 0 deletions src/state/reducer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ const reducer = (state: State, action: Action): State => {
...state,
error: null,
};
case 'RESET_WEATHER':
return {
...state,
weather: { current: null, forecast: null },
};
default: {
return state;
}
Expand Down
7 changes: 6 additions & 1 deletion src/state/reducer.types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ export type Action =
| FetchFailureAction;

type NoPayloadActions = {
type: 'RESTORE_UNIT' | 'TOGGLE_UNIT' | 'FETCH_START' | 'DISMISS_ERROR';
type:
| 'RESTORE_UNIT'
| 'TOGGLE_UNIT'
| 'FETCH_START'
| 'DISMISS_ERROR'
| 'RESET_WEATHER';
};

type FetchWeatherDataSuccessAction = {
Expand Down

0 comments on commit 4949846

Please sign in to comment.