<?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: valera2022</title>
    <description>The latest articles on DEV Community by valera2022 (@valera2022).</description>
    <link>https://dev.to/valera2022</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%2F1004989%2Fa35bb3f7-e3af-41b2-8548-5a7acf01b630.png</url>
      <title>DEV Community: valera2022</title>
      <link>https://dev.to/valera2022</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/valera2022"/>
    <language>en</language>
    <item>
      <title>How Does A Sinatra API Looks Like In Ruby?</title>
      <dc:creator>valera2022</dc:creator>
      <pubDate>Sun, 09 Jul 2023 00:18:28 +0000</pubDate>
      <link>https://dev.to/valera2022/how-does-a-sinatra-api-looks-like-in-ruby-59m</link>
      <guid>https://dev.to/valera2022/how-does-a-sinatra-api-looks-like-in-ruby-59m</guid>
      <description>&lt;p&gt;Sinatra offers us a way to build Routes for Our Front-end and back-end to connect with each other.&lt;/p&gt;

&lt;p&gt;First Let us explain some basic terms before we divide into details.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is MVC?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It stands for Model View Controller.&lt;br&gt;
The view will be the Browser where the user sends some requests to the back-end. These requests are handled by the middle man which is the controller(aka Sinatra in our case). Our controller then request the Model(Ruby with Active record in our case) to view/change the data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is Active Record?&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Active Record is the M in MVC - the model - which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database. It is an implementation of the Active Record pattern which itself is a description of an Object Relational Mapping system."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;What is Sinatra?&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Sinatra is a small web framework that provides a Domain Specific Language (or   DSL) implemented in Ruby. Blake Mizerany created it and provides a lightweight option for developing simple web applications. Sinatra is Rack-based, which means it uses Rack under the hood and can use many tools designed to work with Rack. It's been used by companies such as Apple, BBC, GitHub, LinkedIn, and more."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;What is an API?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;API stands for &lt;strong&gt;A&lt;/strong&gt;pplication &lt;strong&gt;P&lt;/strong&gt;rogramming &lt;strong&gt;I&lt;/strong&gt;nterface.&lt;br&gt;
An API allows you to access resources from Your own backend or Other people's Data. API can Make the programmer’s life easier since we could use resources from other’s people/companies and implement them in our code.&lt;/p&gt;
&lt;h2&gt;
  
  
  Let us talk about Modules
&lt;/h2&gt;

&lt;p&gt;as we defined before Module/s have the responsibility for changing or viewing the data.&lt;/p&gt;

&lt;p&gt;In Ruby, we make classes that are modules of data tables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Supervisor &amp;lt; ActiveRecord::Base
    has_many :workers
    def destroy_all
        Doctor.destroy_all
  end


end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Worker &amp;lt; ActiveRecord::Base
    belongs_to :supervisor
    def destroy_all
          Patient.destroy_all
    end


end

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These modules are related to one another through the keywords has many and belongs to.&lt;/p&gt;

&lt;p&gt;Sinatra is gonna interact with modules and the modules will handle the data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let us talk about Sinatra
&lt;/h2&gt;

&lt;p&gt;Sinatra’s syntax is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;get  “/workers” do
  workers = Worker.all
  workers.to_json()
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We start with an HTTP verb. “/workers” will be our URL. This is how Sinatra handles each different HTTP request So If I send a get request from the browser or my front-end The Ruby inside this specific Route will come into action.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;RESTful Routes&lt;/strong&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;REST stands for Representational State Transfer which provides a way of mapping HTTP verbs (get, post, put, delete) and CRUD actions (create, read, update, delete) together. It is a convention for defining routes and when something follows the rest principle it is known as RESTFUL.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So It is convection for Our Routes.&lt;br&gt;
Let’s say we want to Send a Get request, my Route will be named in this matter:&lt;/p&gt;

&lt;p&gt;So It is convection for Our Routes.&lt;br&gt;
Let’s say we want to Send a Get request, my Route will be named in this matter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  get  “/workers” do
      workers = Worker.all
      workers.to_json()
  end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is an Index Route meaning it is not one single Item but a List of all the workers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Or A POST request:&lt;/em&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;post '/workers' do
    supervisor = Supervisor.find_by(id: params[:supervisor_id] )
    worker = doctor.workers.create(
      name: params[:name],
      dob: params[:dob],
      doh: params[:doh],
      ins: params[:ins],
      sick_days: params[:sick_days],
      security_level: params[:security_level],
      vacation: params[:vacation],
      pay: params[:pay],
      note: params[:note]

    )
patient.to_json

end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the create Route.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;How About A Patch?&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;patch  “/workers/:id” do
      worker = Worker.find(params[:id])
      worker.update(
          name: params[:name]
)
      workers.to_json()
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the Update Route.&lt;/p&gt;

&lt;p&gt;If we wanted to &lt;strong&gt;&lt;em&gt;delete&lt;/em&gt;&lt;/strong&gt; one of them, we will send a delete request we will “catch it” with something like :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;delete '/workers/:id' do 
    worker = Worker.find(params[:id])

    worker.destroy()


  end 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the Destroy Route.&lt;/p&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;h2&gt;
  
  
  Common mistakes and debugging tips.
&lt;/h2&gt;

&lt;p&gt;**&lt;/p&gt;

&lt;p&gt;You can check if your API ends are working by putting a binding.pry in it and see If a request may to it triggers binding.pry( you can use Postman if you don’t have the corresponding fetch on the front-end).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;post '/workers' do
    supervisor = Supervisor.find_by(id: params[:supervisor_id] )
    worker = doctor.workers.create(
      name: params[:name],
      dob: params[:dob],
      doh: params[:doh],
      ins: params[:ins],
      sick_days: params[:sick_days],
      security_level: params[:security_level],
      vacation: params[:vacation],
      pay: params[:pay],
      note: params[:note]



    )
    worker.to_json
    binding.pry

end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If it does not open the pry console on the back end, check if there are any issues with your front-end request.&lt;/p&gt;

&lt;p&gt;Another Silly thing that could mess up your response from the server is that last line &lt;em&gt;.to_json&lt;/em&gt; . Make sure this statement is in there.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;To recap:&lt;/strong&gt;&lt;br&gt;
Sinatra works as a middle between the View aka the Browser and the Module/s.&lt;br&gt;
The module (ruby with active record) view or change data in the data base and send the response back.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Resources:&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.geeksforgeeks.org/restful-routes-in-node-js/"&gt;https://www.geeksforgeeks.org/restful-routes-in-node-js/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://guides.rubyonrails.org/active_record_basics.html"&gt;https://guides.rubyonrails.org/active_record_basics.html&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.redhat.com/en/topics/api/what-are-application-programming-interfaces"&gt;https://www.redhat.com/en/topics/api/what-are-application-programming-interfaces&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Data Handling And Transfer Between Components and Database.</title>
      <dc:creator>valera2022</dc:creator>
      <pubDate>Sat, 13 May 2023 18:41:00 +0000</pubDate>
      <link>https://dev.to/valera2022/-data-handling-and-transfer-between-components-and-database-3bep</link>
      <guid>https://dev.to/valera2022/-data-handling-and-transfer-between-components-and-database-3bep</guid>
      <description>

&lt;p&gt;One of the basic things you need to know when working with React is how to transfer and deal with data from one component to another.&lt;br&gt;
Let's start with some definitions before we go on further.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;- What is State?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The state is data that has the ability to change due to a user's actions. Data in state could be added, deleted, or completely overwritten by a user's action.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;- What are props?&lt;/em&gt;&lt;br&gt;
we use props to pass data between components. we send props down to child components.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;- What Is Lifting State?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;When implementing state we need to figure out where is going to be.&lt;br&gt;
The best place for this is a parent component from which we can send data to the child components that will need the same state. This is What we call lifting state.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;- What Is Inverse Data Flow?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;We know we can send data down through props but how could we do the inverse?&lt;br&gt;
well. We simply pass down a callback function and call it in the child component with the data.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;- What is CRUD?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Each of the words stands for something.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;C : Create,&lt;/li&gt;
&lt;li&gt;R : Read,&lt;/li&gt;
&lt;li&gt;U:  Update,&lt;/li&gt;
&lt;li&gt;D: Delete.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each represents different operations/interactions we can make to a database.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Get Request&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To make a request to a server or an API we can use something called fetch.&lt;/p&gt;

&lt;p&gt;When we are dealing with data on react we usually set this data to state.&lt;br&gt;
let's say we have a fetch get request :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; useEffect(()=&amp;gt;{
    fetch("http://localhost:8000/data") 
    .then (response =&amp;gt; response.json())
    .then( data=&amp;gt;{ setData(data)})
},[])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;fetch returns a promise. A promise is a representation of an asynchronous calculation.&lt;/p&gt;

&lt;p&gt;We see before the last line that we send data to State. This will keep our data deposit for easy retrieval. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;What's the next thing from here On Out?&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;once we get State we can send data to any child component in need of it since we are lifting state.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;return(&amp;lt;Child data={data}/&amp;gt;)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Let's also set up a function so we can retrieve data from another child component&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function handleChildData(formData){   
 setData(formData)   
}

&amp;lt;SecondChid OnParent={handleChildData} /&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way we could implement inverse data flow. The second child sends data up to the parent.&lt;/p&gt;

&lt;p&gt;Our second child happens to be a controlled form :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function SecondChild({onParent}){

const [name,setName]= useState("")
const [email,setEmail] = useState("")
const [date,setDate] = useState(“”)
const [img,setImg] = useState(“”)

function handleSubmit(e){
   e.preventDefault()
   const formData ={
       name: name,
       email: email,
       date: date,
       img: img
}
    onParent(formData)
}
&amp;lt;form onSubmit={handleSubmit}&amp;gt; 
        &amp;lt;div&amp;gt;
         &amp;lt;input  type="text" name="name" value={name} onChange= 
          {e=&amp;gt; setState(e.target.value) } placeholder="Name"  /&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div&amp;gt;
          &amp;lt;input  type="text" name="email" value={email}onChange= 
            {e=&amp;gt; setEmail(e.target.value)} placeholder ="Author"/&amp;gt;
        &amp;lt;/div&amp;gt;
        &amp;lt;div&amp;gt;
          &amp;lt;input type="text" name="date" value={date} onChange= 
             {e=&amp;gt;setDate(e.target.value)} placeholder="Date" /&amp;gt;
        &amp;lt;/div&amp;gt;
          &amp;lt;div&amp;gt;
           &amp;lt;input  type="text" name="img" value={img} onChange=                                    
              {e=&amp;gt;setImg(e.target.value)}placeholder="Image" /&amp;gt;
          &amp;lt;/div&amp;gt;
           &amp;lt;input type="submit" value="Submit"  /&amp;gt;
        &amp;lt;/form&amp;gt;

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The controlled form will keep track of the input value a user inputs and update the assigned State for each of them. Then we can package it in a object and send it up towards the parent component.&lt;br&gt;
Then update State with our child's data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function handleChildData(formData){   
setData(formData)   
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since we have the data on the parent component we could, as usual, do a POST request so the original data and update it with the new one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function handleChildData(formData){   
   fetch("http://localhost:8000/data",{
      method: "POST",
      headers: 
      { "Content-Type": "application/json" },
      body: JSON.stringify(formData
      )})
    .then(r=&amp;gt; r.json())
    .then(dat=&amp;gt; setData([...data,dat]))
}}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or perhaps a Patch request&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fetch(`http://localhost:8000/data/${item.id}`,{
  method:"PATCH",
  headers: {
    "Content-type": "application/json",
  },
  body:JSON.stringify(
    {
      name: “Goes By” + name ,
       email: “the email is” + email,
       date: date + 1) }
   )})
  .then(r =&amp;gt; r.json())
  .then((updatedItem)=&amp;gt; (console.log(updatedItem)));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A Patch request updates or changes some data we already have in the database. Not to be confused with a Post request which adds or creates data unto the database.&lt;/p&gt;

&lt;p&gt;We also have a Delete request which will be The D in CRUD. The delete request is to eliminate data on the database:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; fetch(`http://localhost:8000/data/${item.id}`, {
      method: "DELETE",
    })
      .then((r) =&amp;gt; r.json())
      .then(() =&amp;gt; onDeleteItem(item));
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this article, we have gone over some of the most common practices for handling and transferring data In React. I hope This was found helpful.&lt;/p&gt;

&lt;p&gt;RESOURCES:&lt;br&gt;
Flanagan,D.(2020)._JavaScript The Definite Guide: Master the World's Most-Used Programming language.&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/"&gt;https://developer.mozilla.org/en-US/&lt;/a&gt;.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Playing With HTML Elements: Issues That Might Arise While Listening to Form Elements and fetching data.</title>
      <dc:creator>valera2022</dc:creator>
      <pubDate>Tue, 10 Jan 2023 03:04:45 +0000</pubDate>
      <link>https://dev.to/valera2022/playing-with-html-elements-issues-that-might-arise-while-listening-to-form-elements-and-fetching-data-5cl8</link>
      <guid>https://dev.to/valera2022/playing-with-html-elements-issues-that-might-arise-while-listening-to-form-elements-and-fetching-data-5cl8</guid>
      <description>&lt;h2&gt;
  
  
  Solving refresh issues with submit events.
&lt;/h2&gt;

&lt;p&gt;Preventing the refresh default action of a Submit event might sound straightforward but as I have found out it is not as intuitive as it might sound.&lt;/p&gt;

&lt;p&gt;Let’s start with some basics. What is an event?&lt;/p&gt;

&lt;p&gt;The Web Browser fires an event when something happens to the page. Be it when you click, submit, hoover, etc over something.&lt;/p&gt;

&lt;p&gt;In Javascript,  The syntax of an event has three main components ;&lt;/p&gt;

&lt;p&gt;Event target: The target is the HTML element to which we point the action of the event.&lt;/p&gt;

&lt;p&gt;Event type: This is a string that defines what kind of event we are using. Be it “click”,”keydown”,” submit”, “focus”, etc.&lt;/p&gt;

&lt;p&gt;Event handler: It is a function that responds to an event. This function is invoked by the browser. This gets triggered once we make contact with the event target.&lt;/p&gt;

&lt;p&gt;Having this down, let’s start with the problems:&lt;/p&gt;

&lt;h2&gt;
  
  
  You are not looking at the big picture type of issue.
&lt;/h2&gt;

&lt;p&gt;Given the below form:&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;div id ="container1"&amp;gt; 
    &amp;lt;form id = "inputs" &amp;gt;&amp;lt;input id = "search"type = text label = "name" &amp;gt;
        &amp;lt;input id = "sub" type = submit label = "submit "&amp;gt;
    &amp;lt;/form&amp;gt;
   &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If my intention was to add a submit event to this form, the intuitive thing will be to grab the Submit button:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let submitBtn = document.querySelector(“#sub”)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So when I click on the submit button the Event will trigger and our handler will cancel out refreshing the page with preventDefault().&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;submitBtn.addEventListener(“submit”, event =&amp;gt; 
     event.preventDefault())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;It will not work&lt;/strong&gt; this way.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;What is the issue?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The browser's default action of refreshing the page is targeting the form itself, not only the button. &lt;/p&gt;

&lt;p&gt;We need to target the whole form.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let formulario = document.querySelector(“#inputs”)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;formulario.addEventListener(“submit”, event =&amp;gt; 
  event.preventDefault())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Problem solved!&lt;/p&gt;

&lt;h2&gt;
  
  
  Solving an issue with fetch and form data.
&lt;/h2&gt;

&lt;p&gt;Now that we have the refresh of the form solved we might encounter some minor headaches while trying to use the text input of the form.&lt;/p&gt;

&lt;p&gt;But first, let’s go back to basics.&lt;/p&gt;

&lt;p&gt;_what is an API? _&lt;br&gt;
It stands for Application Programming Interface. Basically, it is data from other people/companies that the public is given access to.&lt;/p&gt;

&lt;p&gt;This data is usually packed in Json.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;what is JSON?&lt;/em&gt;&lt;br&gt;
It stands for JavaScript Object Notation.&lt;br&gt;
JSON is a form of data format which is largely used on the web.&lt;br&gt;
Ex of JSON format:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; `
    {{% embed  %}
        "name":"Penguin",
        "imageurl":"https://external-content.duckduckgo.com/iu/?u=http%3A%2F%2Fcdn.roaring.earth%2Fwp-content%2Fuploads%2F2016%2F03%2FMagellanic_penguin_Valdes_Peninsula-Photo-by-longhorndave.jpg&amp;amp;f=1&amp;amp;nofb=1&amp;amp;ipt=ce483fcdf5447fffef6460a3054096805e8546f89cd919fcb08f98bc4b6d18d2&amp;amp;ipo=images",
        "description":"are a group of aquatic flightless birds. They live almost exclusively in the Southern Hemisphere",
        "donations": "0"
    },

    {
        "name":"Bald eagle",
        "imageurl":"https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Furbanraptorconservancy.org%2Fwp-content%2Fuploads%2F2019%2F03%2FBE3g.jpg&amp;amp;f=1&amp;amp;nofb=1&amp;amp;ipt=f966af2a73c049b28f680a28877b6271aeffe1f3447aebaeecf31c51333222e3&amp;amp;ipo=images",
        "description":" a bird of prey found in North America. ",
        "donations":"0"

    },
    {
        "name":"Red bird of paradise",
        "imageurl":"https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fc2.staticflickr.com%2F8%2F7239%2F7188557664_1230086a7b_b.jpg&amp;amp;f=1&amp;amp;nofb=1&amp;amp;ipt=56253f9696e04f2990ed10501f36a090d38a456bc0f0af85938d85bb337ee5d1&amp;amp;ipo=images",
        "description":"up to 33 cm long, brown and yellow with a dark brown iris, grey legs and yellow bill.",
        "donations":"0"

    },
    {
        "name":"Snow Leopard",
        "imageurl":"https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fidsb.tmgrup.com.tr%2Fly%2Fuploads%2Fimages%2F2020%2F05%2F25%2Fthumbs%2F800x531%2F37398.jpg%3Fv%3D1590571602&amp;amp;f=1&amp;amp;nofb=1&amp;amp;ipt=96668444752a472d250a282bd208ce016ad1645ef96a8b275a47c3de95360c92&amp;amp;ipo=images",
        "description":"native to the mountain ranges of Central and South Asia. It is listed as Vulnerable on the IUCN Red List because the global population is estimated to number fewer than 10,000",
        "donations":"0"

    }

`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;What is a  fetch request?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;It’s a method to retrieve data from the net. We retrieve JSON data with this method.&lt;br&gt;
 Example :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getData (){
            fetch(URL)
            .then(response =&amp;gt; response.json())
            .then(data =&amp;gt; {
                console.log( data ))

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  You are looking at the elephant from afar type of issue.
&lt;/h2&gt;

&lt;p&gt;Let us say we want to fetch some data with a user's input.&lt;/p&gt;

&lt;p&gt;we can start by getting the form input text element.&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;form id='forms'&amp;gt;
      &amp;lt;input id='searchit' type='text' name='search'&amp;gt;
      &amp;lt;input id="sub" type='submit' name='submit'/&amp;gt;
    &amp;lt;/form&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;let texto = document.querySelector(“#searchit”)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We could send it over to fetch with :&lt;br&gt;
&lt;code&gt;getdata(texto)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;assuming:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getData (input){
            fetch('https://soemwhere.com/nothere/${input}')
            .then(response =&amp;gt; response.json())
            .then(data =&amp;gt; {
                console.log( data ))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;as you see we do some interpolation to inject input text in the fetch request&lt;/p&gt;

&lt;p&gt;&lt;code&gt;fetch('https://soemwhere.com/nothere/${input}')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This will not work&lt;/strong&gt; as intended.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;What's the issue?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;we are targeting the text input alright but the thing is we are actually grabbing the element itself:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;input id='searchit' type='text' name='search'&amp;gt;&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;What we really want is the value of whatever the user writes in the input type text&lt;/p&gt;

&lt;p&gt;So instead of this :&lt;br&gt;&lt;br&gt;
&lt;code&gt;getdata(texto)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We will do something like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;getdata(texto.value)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In this case, I want to focus on the elephant's writings instead of the whole elephant.&lt;/p&gt;

&lt;p&gt;Problem solved!&lt;/p&gt;

&lt;p&gt;These issues are not as difficult once you see them, but in my learning experience as a beginner, issues like this will trip you over and make you spend a lot of time in your code because you missed them.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;RESOURCES:&lt;/strong&gt;&lt;/em&gt;&lt;br&gt;
Flanagan,D.(2020)._JavaScript The Definite Guide: Master the World's Most-Used Programming language.&lt;br&gt;
&lt;a href="https://developer.mozilla.org/en-US/" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>developer</category>
      <category>resources</category>
    </item>
  </channel>
</rss>
