add execute fx

This commit is contained in:
madarsbiss 2021-09-22 19:05:00 +03:00
parent 17cf1f1cbb
commit 92d22cd650

View File

@ -15,43 +15,23 @@ import styles from "../styles/Home.module.css";
const App = () => { const App = () => {
const [input, setInput] = useState("Riga"); const [input, setInput] = useState("Riga");
const [systemUsed, setSystemUsed] = useState("metric"); const [execute, setExecute] = useState(true);
const [weatherData, setWeatherData] = useState(); const [weatherData, setWeatherData] = useState();
const [systemUsed, setSystemUsed] = useState("metric");
const getData = async () => {
const res = await fetch("api/data", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ input }),
});
const data = await res.json();
setWeatherData({ ...data });
setInput("");
};
useEffect(() => { useEffect(() => {
const getData = async () => {
const res = await fetch("api/data", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ input }),
});
const data = await res.json();
setWeatherData({ ...data });
setInput("");
};
getData(); getData();
}, []); }, [execute]);
// useEffect(() => {
// const getData = async () => {
// const res = await fetch("api/data", {
// method: "POST",
// headers: { "Content-Type": "application/json" },
// body: JSON.stringify({ input }),
// });
// const data = await res.json();
// setWeatherData({ ...data });
// setInput("");
// };
// getData();
// }, []);
const enterKeydown = (e) => {
if (e.keyCode === 13) {
getData();
}
};
const changeSystem = () => const changeSystem = () =>
systemUsed == "metric" systemUsed == "metric"
@ -80,7 +60,7 @@ const App = () => {
}} }}
onChange={(e) => setInput(e.target.value)} onChange={(e) => setInput(e.target.value)}
onKeyDown={(e) => { onKeyDown={(e) => {
enterKeydown(e); e.keyCode === 13 && setExecute(!execute);
e.target.placeholder = "Search a city..."; e.target.placeholder = "Search a city...";
}} }}
/> />
@ -94,7 +74,7 @@ const App = () => {
<Search <Search
onFocus={(e) => (e.target.value = "")} onFocus={(e) => (e.target.value = "")}
onChange={(e) => setInput(e.target.value)} onChange={(e) => setInput(e.target.value)}
onKeyDown={(e) => enterKeydown(e)} onKeyDown={(e) => e.keyCode === 13 && setExecute(!execute)}
/> />
</ErrorScreen> </ErrorScreen>
) : ( ) : (