Skip to content

Commit

Permalink
Merge pull request #2 from Thorfin69/main
Browse files Browse the repository at this point in the history
Updated functions
  • Loading branch information
s-shemmee authored Jul 12, 2024
2 parents ccd2b08 + 8564e56 commit 09a447a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
19 changes: 7 additions & 12 deletions src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import Forecast from "./Forecast";
import "../styles.css";
import '@fortawesome/fontawesome-free/css/all.min.css';


function App() {
const [query, setQuery] = useState();
const [query, setQuery] = useState("");
const [weather, setWeather] = useState({
loading: true,
data: {},
Expand All @@ -27,7 +26,7 @@ function App() {
"August",
"September",
"October",
"Nocvember",
"November",
"December"
];
const days = [
Expand All @@ -41,16 +40,14 @@ function App() {
];

const currentDate = new Date();
const date = `${days[currentDate.getDay()]} ${currentDate.getDate()} ${
months[currentDate.getMonth()]
}`;
const date = `${days[currentDate.getDay()]} ${currentDate.getDate()} ${months[currentDate.getMonth()]
}`;
return date;
};

//new search function
const search = async (event) => {
if (event.key === "Enter") {
event.preventDefault();
setQuery("");
event.preventDefault();
if (event.type === "click" || (event.type === "keypress" && event.key === "Enter")) {
setWeather({ ...weather, loading: true });
const apiKey = "b03a640e5ef6980o4da35b006t5f2942";
const url = `https://api.shecodes.io/weather/v1/current?query=${query}&key=${apiKey}`;
Expand All @@ -63,7 +60,6 @@ function App() {
})
.catch((error) => {
setWeather({ ...weather, data: {}, error: true });
setQuery("");
console.log("error", error);
});
}
Expand All @@ -76,7 +72,6 @@ function App() {

try {
const response = await axios.get(url);

setWeather({ data: response.data, loading: false, error: false });
} catch (error) {
setWeather({ data: {}, loading: false, error: true });
Expand Down
11 changes: 9 additions & 2 deletions src/components/SearchEngine.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import React from "react";

function SearchEngine({ query, setQuery, search }) {
//handler function
const handleKeyPress = (e) => {
if (e.key === 'Enter') {
search(e);
}
};

return (
<div className="SearchEngine">
<input
Expand All @@ -10,9 +17,9 @@ function SearchEngine({ query, setQuery, search }) {
name="query"
value={query}
onChange={(e) => setQuery(e.target.value)}
onKeyPress={search}
onKeyPress={handleKeyPress}
/>
<button><i className="fas fa-search" style={{ fontSize: "18px" }}></i></button>
<button onClick={search}><i className="fas fa-search" style={{ fontSize: "18px" }}></i></button>
</div>
);
}
Expand Down

0 comments on commit 09a447a

Please sign in to comment.