ArinzeGit / Advice-Generator-App

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Advice generator app

This is my solution to the 'Advice generator app' challenge on Frontend Mentor. Frontend Mentor challenges help me improve my coding skills by building realistic projects.

Table of contents

Overview

The challenge

Users should be able to:

  • View the optimal layout for the site depending on their device's screen size
  • See hover states for all interactive elements on the page
  • Generate a new piece of advice by clicking the dice icon

Screenshot

Desktop version:
Advice Generator App Screenshot1

Mobile version:
Advice Generator App Screenshot2

Links

  • Solution URL: View the GitHub repository here
  • Live Site URL: I have hosted the project on GitHub Pages here

My process

Built with

  • Semantic HTML5 markup
  • CSS custom properties
  • Flexbox - CSS web layout model
  • TypeScript - Programming language that extends JavaScript
  • React - JavaScript library
  • Node.js - JavaScript runtime environment
  • Vite - React build tool with local development server
  • Advice Slip JSON API - API that generates random advice

What I learned

  • I learned how to consume APIs. After reading the documentation, I used the fetch() function to make HTTP requests to the Advice Slip JSON API endpoints. On receiving response, data is processed to extract the advice and ID used to update the state of the app. I also wrote code to handle errors.
    • I also learned that I could pass the function that makes the API call and updates the state to the button as props so that the button can be used to trigger the state change. In my case, the getAdvice function.
import { useState } from "react";
import "./App.css";
import AdviceCard from "./components/AdviceCard";
import Button from "./components/Button";
import PatternDividerDesktop from "./components/PatternDividerDesktop";
import PatternDividerMobile from "./components/PatternDividerMobile";

const App = () => {
  const [id, setId] = useState(117);

  const [advice, setAdvice] = useState(
    "It is easy to sit up and take notice, what's difficult is getting up and taking action."
  );

  const getAdvice = () => {
    fetch("https://api.adviceslip.com/advice")
      .then((response) => response.json())
      .then((data) => {
        const newId = data.slip.id;
        setId(newId);
        const newAdvice = data.slip.advice;
        setAdvice(newAdvice);
        console.log(newId, newAdvice);
      })
      .catch((error) => {
        console.log("Error fetching advice:", error);
      });
  };

  return (
    <div className="app">
      <main>
        <div className="card-container">
          <AdviceCard adviceId={id}>{advice}</AdviceCard>
          <div className="pattern-divider-desktop">
            <PatternDividerDesktop />
          </div>
          <div className="pattern-divider-mobile">
            <PatternDividerMobile />
          </div>
          <div className="button-container">
            <Button onclick={getAdvice} />
          </div>
        </div>
      </main>
    </div>
  );
};

export default App;
  • I learned that letter-spacing in CSS can be used to set the space between letters in a text. Before now, I thought it was fixed and dependent on the font-family.

Continued development

  • Consuming APIs
  • CSS Flexbox
  • Media queries
  • Typescript
  • React

I found these techniques very useful. I will continue focusing on them in future projects to refine and perfect them.

Useful resources

Author

About


Languages

Language:TypeScript 57.5%Language:CSS 30.0%Language:JavaScript 6.6%Language:HTML 5.9%