<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: rharshit82</title>
    <description>The latest articles on DEV Community by rharshit82 (@rharshit82).</description>
    <link>https://dev.to/rharshit82</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F731336%2Fd5c30d54-89f9-4afc-9d08-4018917fe201.jpeg</url>
      <title>DEV Community: rharshit82</title>
      <link>https://dev.to/rharshit82</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rharshit82"/>
    <language>en</language>
    <item>
      <title>Building a Simple Weather Dashboard with JavaScript and Mock APIs</title>
      <dc:creator>rharshit82</dc:creator>
      <pubDate>Sun, 02 Jun 2024 17:53:41 +0000</pubDate>
      <link>https://dev.to/rharshit82/building-a-simple-weather-dashboard-with-javascript-and-mock-apis-2k7i</link>
      <guid>https://dev.to/rharshit82/building-a-simple-weather-dashboard-with-javascript-and-mock-apis-2k7i</guid>
      <description>&lt;p&gt;Creating a weather dashboard is a great way to practice working with APIs and building user interfaces. In this tutorial, we'll build a simple weather dashboard using JavaScript and a Mock Weather API. This will allow us to fetch and display weather data without needing access to a live weather API. The &lt;a href="https://mockx.net"&gt;Mock API&lt;/a&gt; will simulate real-world scenarios, making it an excellent tool for development and testing.&lt;br&gt;
We will be using &lt;a href="https://mockx.net/docs/weather-api"&gt;Dummy Weather API &lt;/a&gt;for this project.&lt;br&gt;
Prerequisites&lt;br&gt;
To follow along with this tutorial, you'll need:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Basic knowledge of HTML, CSS, and JavaScript.&lt;/li&gt;
&lt;li&gt;A code editor (like VSCode).&lt;/li&gt;
&lt;li&gt;A web browser.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Setting Up the Project
&lt;/h2&gt;

&lt;p&gt;First, let's set up a simple HTML page that will serve as our weather dashboard.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;
&amp;lt;head&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
    &amp;lt;title&amp;gt;Weather Dashboard&amp;lt;/title&amp;gt;
    &amp;lt;style&amp;gt;
        body {
            font-family: Arial, sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            padding: 20px;
        }
        .weather {
            border: 1px solid #ccc;
            padding: 20px;
            border-radius: 5px;
            margin-top: 20px;
            width: 300px;
            text-align: center;
        }
    &amp;lt;/style&amp;gt;
&amp;lt;/head&amp;gt;
&amp;lt;body&amp;gt;
    &amp;lt;h1&amp;gt;Weather Dashboard&amp;lt;/h1&amp;gt;
    &amp;lt;input type="text" id="city" placeholder="Enter city name"&amp;gt;
    &amp;lt;button id="getWeather"&amp;gt;Get Weather&amp;lt;/button&amp;gt;
    &amp;lt;div id="weatherContainer"&amp;gt;&amp;lt;/div&amp;gt;

    &amp;lt;script src="app.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Writing JavaScript to Fetch Weather Data
&lt;/h2&gt;

&lt;p&gt;Next, we'll write JavaScript to fetch weather data from the Mock API and display it on our dashboard.&lt;/p&gt;

&lt;p&gt;Create a file named app.js and add the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.getElementById('getWeather').addEventListener('click', getWeather);

async function getWeather() {
    const city = document.getElementById('city').value;
    if (!city) {
        alert('Please enter a city name');
        return;
    }

    try {
        const response = await fetch(`https://api.mockx.net/weather/search?q=${city}`);
        const data = await response.json();
        displayWeather(data.weathers);
    } catch (error) {
        console.error('Error fetching weather data:', error);
    }
}

function displayWeather(weathers) {
    const weatherContainer = document.getElementById('weatherContainer');
    weatherContainer.innerHTML = ''; // Clear previous weather data

    if (weathers.length === 0) {
        weatherContainer.innerHTML = '&amp;lt;p&amp;gt;No weather data found for the entered city.&amp;lt;/p&amp;gt;';
        return;
    }

    weathers.forEach(weather =&amp;gt; {
        const weatherDiv = document.createElement('div');
        weatherDiv.className = 'weather';
        weatherDiv.innerHTML = `
            &amp;lt;h2&amp;gt;${weather.location.city}, ${weather.location.country}&amp;lt;/h2&amp;gt;
            &amp;lt;p&amp;gt;Temperature: ${weather.current_weather.temperature}°C&amp;lt;/p&amp;gt;
            &amp;lt;p&amp;gt;Condition: ${weather.current_weather.weather_condition}&amp;lt;/p&amp;gt;
            &amp;lt;p&amp;gt;Humidity: ${weather.current_weather.humidity}%&amp;lt;/p&amp;gt;
            &amp;lt;p&amp;gt;Wind Speed: ${weather.current_weather.wind_speed} km/h&amp;lt;/p&amp;gt;
            &amp;lt;p&amp;gt;Air Quality: ${weather.air_quality.category} (${weather.air_quality.index})&amp;lt;/p&amp;gt;
        `;
        weatherContainer.appendChild(weatherDiv);
    });
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Explanation
&lt;/h2&gt;

&lt;p&gt;HTML Structure: We have a simple HTML structure with an input field for the city name, a button to trigger the weather fetch, and a container to display the weather data.&lt;/p&gt;

&lt;p&gt;Fetching Weather Data: When the button is clicked, the getWeather function is called. This function fetches weather data from the Mock API based on the entered city name.&lt;/p&gt;

&lt;p&gt;Displaying Weather Data: The displayWeather function takes the fetched weather data and dynamically creates HTML elements to display the information.&lt;/p&gt;

&lt;h2&gt;
  
  
  Running the Application
&lt;/h2&gt;

&lt;p&gt;To run the application, simply open the index.html file in your web browser. Enter a city name and click the "Get Weather" button to see the weather data for that city.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Using Mock APIs is a fantastic way to practice working with real-world data without needing access to live servers. In this tutorial, we built a simple weather dashboard using JavaScript and a Mock Weather API. This approach allows you to test and develop your applications in a controlled environment.&lt;/p&gt;

&lt;p&gt;For more information on Mock APIs, you can check out &lt;a href="https://mockx.net"&gt;MockX&lt;/a&gt;.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
