Giter Club home page Giter Club logo

react-weather-application's Introduction

react-weather-application's People

Contributors

anthonyhad avatar zetabug avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

react-weather-application's Issues

ES6 Convertion

App component is currently bloated and using old JS syntax


Steps:

  • Convert functions to arrow functions
  • Use Object destruction for props
  • Event handlers affecting a single state in one line code, such as the input
  • Not needed variable declaration removal, conditional ternary on return JSX

Solution:

APP COMPONENT

const App = () => {
  const [data, setData] = useState(null);
  const [place, setPlace] = useState(null);
  const [search, setSearch] = useState("");
  const [location, setLocation] = useState("");
  const [isLoading, setIsLoading] = useState(false);

  const geoHandler = async () => {
    setSearch("");
    setIsLoading(true);
    await navigator.geolocation.getCurrentPosition((position) => {
      const crd = position.coords;
      const latitude = crd.latitude;
      const longitude = crd.longitude;
      setLocation(`${latitude},${longitude}`);
    });
  };

  const fetchData = useCallback(async (search, location) => {
    const options = {
      method: "GET",
      headers: {
        "X-RapidAPI-Key": "0173291af0msh62b3ca25953f210p13d732jsn66b4d9f97708",
        "X-RapidAPI-Host": "weatherapi-com.p.rapidapi.com",
      },
    };
    const url = `https://weatherapi-com.p.rapidapi.com/current.json?q=${
      search || location
    }`;
    const response = await fetch(url, options);
    const responseData = await response.json();
    return responseData;
  }, []);

  useEffect(() => {
    fetchData(search, location).then((responseData) => {
      setPlace(responseData.location);
      setData(responseData.current);
      setIsLoading(false);
    });
  }, [location, search, fetchData]);

  return (
    <>
      <div className="container">
        <h1>WEATHER APPLICATION</h1>
        <div className="search-sec">
          <input
            type="text"
            value={search}
            placeholder="Search by City..."
            onChange={(e) => setSearch(e.target.value)}
          />
        </div>
        <div>
          <p>or</p>
        </div>
        <div>
          <button onClick={geoHandler}>Find me!</button>
        </div>
        <br />
        <br />
        {isLoading && <LoadingIndicator />}
        {data ? <Weather place={place} data={data} /> : <p>no data found 😬</p>}
      </div>
      <span className="credit">Ranvir@zetabug/github</span>
    </>
  );
};

export default App;

WEATHER COMPONENT

const Weather = ({ place, data }) => {
  return (
    <div className="output-sec">
      <div className="location">
        {place.name}, {place.region}
      </div>
      <img src={data.condition.icon} alt="" />
      <div className="sky-status">{data.condition.text}</div>
      <div className="temp">Temperature : {data.temp_c}°C</div>
      <div className="humidity">Humidity : {data.humidity}</div>
    </div>
  );
};

If interested, review and merge PR
Thank you =)

Dark mode

hi, @zetabug 👋

I want to add dark mode to the website. Can I start working on that?

⚠️ SECURITY ISSUE: Hide the RapidAPI key

⚠️ HIGH PRIORITY ISSUE

The problem

The RapidAPI key is exposed in the code, which means that anyone can take it and do a large amount of requests, surpassing the free plan limit and causing financial problems to the owner of the key.

The solution

In development (local): create an environment variable (ex: RAPID_API_KEY), and assign your RapidAPI key as its value. Make sure the .env file is listed on .gitignore so the key don't gets exposed in the public repository. Learn more about environment variables in React on this article.
In production (Netlify): create an environment variable (learn more in the Netlify documentation) with the same name and value as the local environment variable.

Observations

This is not the ideal solution, because the key will be still present in the request and anyone will be able to see it on the DevTools network tab. The ideal solution would be create a backend to access the API with that key. But at least the provided solution is a best practice and removes your key from GitHub.

Contact

E-mail: [email protected]

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.