Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ticket #1 #4

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
REACT_APP_WEATHER_API_KEY="b03a640e5ef6980o4da35b006t5f2942"
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
.env
6 changes: 6 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Ignore all MDX files to prevent unwanted JSX line wrapping
# https://jasongerbes.com/blog/mdx-remove-unwanted-paragraph-tags
*.mdx
dist
build
node_modules
4 changes: 4 additions & 0 deletions prettier.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
printWidth: 100,
tabWidth: 2,
};
79 changes: 36 additions & 43 deletions src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import "../styles.css";
import '@fortawesome/fontawesome-free/css/all.min.css';

function App() {
const [query, setQuery] = useState("");
const [query, setQuery] = useState("Rabat");
const [weather, setWeather] = useState({
loading: true,
data: {},
error: false
error: false,
});

const toDate = () => {
Expand All @@ -27,48 +27,47 @@ function App() {
"September",
"October",
"November",
"December"
];
const days = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
"December",
];
const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];

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) => {
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}`;

await axios
.get(url)
.then((res) => {
console.log("res", res);
setWeather({ data: res.data, loading: false, error: false });
})
.catch((error) => {
setWeather({ ...weather, data: {}, error: true });
console.log("error", error);
});
useEffect(() => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
async (position) => {
const { latitude, longitude } = position.coords;
try {
const response = await axios.get(
`https://nominatim.openstreetmap.org/reverse?lat=${latitude}&lon=${longitude}&format=json`
);
const data = await response.data;
const city = data.address.city || data.address.town || data.address.village;
setQuery(city);
} catch (err) {
console.error("Failed to fetch city data:", err);
}
},
(err) => {
console.error("Geolocation error:", err.message);
}
);
} else {
console.error("Geolocation is not supported by your browser");
}
};
}, []);


useEffect(() => {
const fetchData = async () => {
const apiKey = "b03a640e5ef6980o4da35b006t5f2942";
const url = `https://api.shecodes.io/weather/v1/current?query=Rabat&key=${apiKey}`;
const apiKey = process.env.REACT_APP_WEATHER_API_KEY;
const url = `https://api.shecodes.io/weather/v1/current?query=${query}&key=${apiKey}`;

try {
const response = await axios.get(url);
Expand All @@ -78,15 +77,12 @@ function App() {
console.log("error", error);
}
};

fetchData();
}, []);
}, [query]);

return (
<div className="App">

{/* SearchEngine component */}
<SearchEngine query={query} setQuery={setQuery} search={search} />
<SearchEngine query={query} setQuery={setQuery} />

{weather.loading && (
<>
Expand All @@ -101,15 +97,12 @@ function App() {
<br />
<br />
<span className="error-message">
<span style={{ fontFamily: "font" }}>
Sorry city not found, please try again.
</span>
<span style={{ fontFamily: "font" }}>Sorry city not found, please try again.</span>
</span>
</>
)}

{weather && weather.data && weather.data.condition && (
// Forecast component
<Forecast weather={weather} toDate={toDate} />
)}
</div>
Expand Down
31 changes: 16 additions & 15 deletions src/components/SearchEngine.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
import React from "react";
import React, { useState } from "react";

function SearchEngine({ query, setQuery, search }) {
//handler function
const handleKeyPress = (e) => {
if (e.key === 'Enter') {
search(e);
}
function SearchEngine({ query, setQuery }) {
const [value, setValue] = useState("");

const handleSubmit = (e) => {
e.preventDefault();
setQuery(value);
};

return (
<div className="SearchEngine">
<form className="SearchEngine" onSubmit={handleSubmit}>
<input
type="text"
className="city-search"
placeholder="enter city name"
name="query"
value={query}
onChange={(e) => setQuery(e.target.value)}
onKeyPress={handleKeyPress}
placeholder="Enter city name"
name="city"
value={value}
onChange={(e) => setValue(e.target.value)}
/>
<button onClick={search}><i className="fas fa-search" style={{ fontSize: "18px" }}></i></button>
</div>
<button type="submit">
<i className="fas fa-search" style={{ fontSize: "18px" }}></i>
</button>
</form>
);
}

Expand Down