From 9bd464227f9667024c199f08ebb2dedaa04048df Mon Sep 17 00:00:00 2001 From: aviral hatwal <119874648+cyberviking5@users.noreply.github.com> Date: Thu, 5 Oct 2023 03:27:25 +0530 Subject: [PATCH] Weather App (#635) * Weather App * cleanup * Removing consoles * Cleanup --- .../kitchen-sink/src/examples/redux-todo.tsx | 1 - .../kitchen-sink/src/examples/weather-app.tsx | 149 ++++++++++++++++++ packages/kitchen-sink/src/main.tsx | 10 +- 3 files changed, 157 insertions(+), 3 deletions(-) create mode 100644 packages/kitchen-sink/src/examples/weather-app.tsx diff --git a/packages/kitchen-sink/src/examples/redux-todo.tsx b/packages/kitchen-sink/src/examples/redux-todo.tsx index 6fbb0ae8bf..074e29bebb 100644 --- a/packages/kitchen-sink/src/examples/redux-todo.tsx +++ b/packages/kitchen-sink/src/examples/redux-todo.tsx @@ -68,7 +68,6 @@ const MainTodoApp = block(() => { const TodoApp = () => { const [input, setInput] = useState(''); - const count = useSelector( (state: { todo: typeof initialState }) => state.todo.count, ); diff --git a/packages/kitchen-sink/src/examples/weather-app.tsx b/packages/kitchen-sink/src/examples/weather-app.tsx new file mode 100644 index 0000000000..faaba8ba43 --- /dev/null +++ b/packages/kitchen-sink/src/examples/weather-app.tsx @@ -0,0 +1,149 @@ +import styled from 'styled-components'; +import { useState } from 'react'; +import React from 'react'; +import { block } from 'million/react'; + +const Condition = styled.span` + margin: 20px auto; + text-transform: capitalize; + font-size: 14px; + & span { + font-size: 28px; + } +`; + +const SearchBox = styled.form` + display: flex; + flex-direction: row; + justify-content: space-evenly; + margin: 20px; + border: black solid 1px; + border-radius: 2px; + + & input { + padding: 10px; + font-size: 14px; + border: none; + outline: none; + font-family: Montserrat; + font-weight: bold; + } + & button { + background-color: blue; + font-size: 14px; + padding: 0 10px; + color: white; + border: none; + outline: none; + cursor: pointer; + font-family: Montserrat; + font-weight: bold; + } +`; +const ChooseCityLabel = styled.span` + color: white; + margin: 10px auto; + font-size: 18px; + font-weight: bold; +`; +const WelcomeWeatherLogo = styled.img` + width: 140px; + height: 140px; + margin: 40px auto; +`; + +type Weather = { + city: string; + weatherDesc: string | undefined; + temp: any; +}; + +const WeatherComponent = block(({ city, weatherDesc, temp }: Weather) => { + return ( +
+ + + {typeof temp === 'number' ? ( + + {`${Math.floor(temp - 273)}°C`} + + ) : ( +

NOT DEFINED

+ )} +
+ {` | ${weatherDesc}`} +
+

{city.toUpperCase()}

+
+ ); +}); + +export default function Weather() { + const [city, updatecity] = useState(null); + const handleChange = (event: React.ChangeEvent) => { + updatecity(event.target.value); + }; + const [weather, updateWeather] = useState(); + const [temp, updatetemp] = useState(); + const [weatherDesc, updateWeatherDesc] = useState(); + const fetchWeather = async (e: React.FormEvent) => { + e.preventDefault(); + const response = await fetch( + `https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=fe4feefa8543e06d4f3c66d92c61b69c`, + ); + const data = await response.json(); + updateWeather(data.weather[0].main); + updateWeatherDesc(data.weather[0].description); + updatetemp(data.main.temp); + }; + return ( +
+ + Weather App + + {city && weather ? ( + <> + + + ) : ( + <> + + Find Weather of your city + { + e.preventDefault(); + await fetchWeather(e); + }} + > + + + + + )} +
+ ); +} diff --git a/packages/kitchen-sink/src/main.tsx b/packages/kitchen-sink/src/main.tsx index af6eb2c249..437aff7c5d 100644 --- a/packages/kitchen-sink/src/main.tsx +++ b/packages/kitchen-sink/src/main.tsx @@ -26,7 +26,10 @@ type Module = { default: ComponentType }; Object.entries(import.meta.glob('./examples/*.{tsx,jsx}')).map( async ([key, mod]) => [ - key.replace('./examples/', '').replace('.tsx', '').replace('.jsx', ''), + key + .replace('./examples/', '') + .replace('.tsx', '') + .replace('.jsx', ''), mod as () => Promise, ] as const, ), @@ -67,7 +70,10 @@ type Module = { default: ComponentType }; key={key} disabled={hasSelected && selected === index} > - {key.replace('./examples/', '').replace('.tsx', '').replace('.jsx', '')} + {key + .replace('./examples/', '') + .replace('.tsx', '') + .replace('.jsx', '')} ); })}{' '}