<?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: Caleb Alessandro Rodriguez</title>
    <description>The latest articles on DEV Community by Caleb Alessandro Rodriguez (@sirdigo).</description>
    <link>https://dev.to/sirdigo</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%2F759871%2F36a1e03f-0424-443f-98ba-821843ac5f6a.jpg</url>
      <title>DEV Community: Caleb Alessandro Rodriguez</title>
      <link>https://dev.to/sirdigo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sirdigo"/>
    <language>en</language>
    <item>
      <title>Looping Over Objects.</title>
      <dc:creator>Caleb Alessandro Rodriguez</dc:creator>
      <pubDate>Mon, 25 Jul 2022 16:05:59 +0000</pubDate>
      <link>https://dev.to/sirdigo/looping-over-objects-5hmm</link>
      <guid>https://dev.to/sirdigo/looping-over-objects-5hmm</guid>
      <description>&lt;p&gt;Ever wanted to loop over an object and change all of the values? Like when you're building a react app with state and you have multiple inputs. Now maybe you want to clear all of the state with a clear button? Let me show you one way I came by from the amazing &lt;a href="https://wesbos.com/"&gt;Wes Bos&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I will be showing this in vanilla JS.&lt;/p&gt;

&lt;p&gt;First, let's make the object&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const product1 = {
   name: 'Giant Sour Gummy Worm',
   price: 35.00,
   description: 'A giant gummy worm coated with sour powder and waiting to be eaten!',
   image: 'https://images.vat19.com/worlds-largest-sour-gummy-worm/worlds-largest-gummy-worm-sour-flavor-1.jpg',
   availability: 'FOR_SALE',
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have the object we need to loop over it by changing it to an array. Using &lt;code&gt;Object.entries()&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;Object.entries(product1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we &lt;code&gt;console.log()&lt;/code&gt; that we should get something like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  [ 'name', 'Giant Sour Gummy Worm' ],
  [ 'price', 35 ],
  [
    'description',
    'A giant gummy worm dunked in sour powder and waiting to be eaten!'
  ],
  [
    'image',
    'https://images.vat19.com/worlds-largest-sour-gummy-worm/worlds-largest-gummy-worm-sour-flavor-1.jpg'
  ],
  [ 'availability', 'FOR_SALE' ]
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do you see how each array has two items? The first is the key and the second is the value!&lt;/p&gt;

&lt;p&gt;Now if we simply &lt;code&gt;map()&lt;/code&gt; over the array we can change all of the inner array values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const mappedProduct = Object.entries(
    product1
    ).map(([key, value]) =&amp;gt; [key, ''])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we map over it now we must destructor the key and value with [key, value]. When we want to change all values we enter a new value like so [key, 'new value']. In this case we're just clearing the values so I left an empty string to keep it blank.&lt;/p&gt;

&lt;p&gt;Now &lt;code&gt;console.log(mappedProduct)&lt;/code&gt; will give us&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  [ 'name', '' ],
  [ 'price', '' ],
  [ 'description', '' ],
  [ 'image', '' ],
  [ 'availability', '' ]
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Awesome now all we have to do is change it back to an object! This is super simple all we need is &lt;code&gt;Object.fromEntries()&lt;/code&gt;. Everything has to be wrapped in it, Like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const clearedProduct = Object.fromEntries(
  Object.entries(
    product1
  ).map(([key, value]) =&amp;gt; [key, ''])
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And with that we're done, if we &lt;code&gt;console.log(clearedProduct)&lt;/code&gt; we'll get this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{ 
  name: '', 
  price: '', 
  description: '', 
  image: '', 
  availability: '' 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you're still having some trouble visualizing this, here is a &lt;a href="https://replit.com/@SirDigoDigo/Loooping-Over-Objects?v=1"&gt;replit I made&lt;/a&gt;  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TLDR: Using &lt;code&gt;Object.entries()&lt;/code&gt; and &lt;code&gt;Object.fromEntries()&lt;/code&gt; we can loop over an object by turning it into an array and then back into an object&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;Object.fromEntries(
  Object.entries(
    objectYouWantToLoopOver
  ).map(([key, value]) =&amp;gt; [key, ''])
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>react</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Fetching data depending on the date.</title>
      <dc:creator>Caleb Alessandro Rodriguez</dc:creator>
      <pubDate>Wed, 09 Mar 2022 19:17:13 +0000</pubDate>
      <link>https://dev.to/sirdigo/fetching-data-depending-on-the-date-4jp2</link>
      <guid>https://dev.to/sirdigo/fetching-data-depending-on-the-date-4jp2</guid>
      <description>&lt;p&gt;In the past three weeks of building my capstone project for a software engineering boot camp I'm close to graduating from, I came upon an issue(one of many). Every day I needed to fetch a prompt from my rails backend based on what date that prompt instance has. In this Dev-Blog, I'll show you what I came up with. I'm sure there's a better way, but this is mine.&lt;br&gt;
&lt;strong&gt;Note: I'm using a JS react frontend and a ruby on rails backend&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's start with the backend. First set up your resources.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails g resource Prompt title date:date&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;make sure to use a date table when generating them. Once that's finished you can go ahead and migrate.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;rails db:migrate&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Okay cool, now let's look in the Prompt controller. You should have something like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class PromptsController &amp;lt; ApplicationController

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

&lt;/div&gt;



&lt;p&gt;Before we forgot to go into the config file and down somewhere near the bottom should be a routes.rb file. Go ahead and add a new route to it, name it whatever you want, I'm going to name it...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;get '/prompts/:date', to: 'prompts#show_prompt_by_date'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's go ahead and make the method for grabbing the data by the date.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class PromptsController &amp;lt; ApplicationController

  def show_prompt_by_date 
     #grabbing the specific prompt using the params
     prompt = Prompt.find_by(date: params[:date])

     #rendering/error handling
     if prompt
        render json: prompt, status: :ok
     else
        render json: { errors: ["Prompt not found"] }, status: :not_found
     end
  end

  private

  #being safe
  def prompt_params
     params.permit(:date)
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you're using sterilizers go ahead and add date and whatever other table you want to show up in front of the attributes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class PromptSerializer &amp;lt; ActiveModel::Serializer
  attributes :title, :date
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you aren't then when you render the prompt render it with includes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;render json: prompt, include[:title, :date, status: :ok
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After all that lets add some seed data&lt;br&gt;
&lt;strong&gt;seeds.rb&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;Prompt.destroy_all

Prompt.create!(title: "July, 15th, 2020", date: 7152022)
Prompt.create!(title: "March, 9th, 2020", date: 392022)
Prompt.create!(title: "March, 10th, 2020", date: 3102022)

puts "done seeding"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now go ahead and seed!&lt;br&gt;
&lt;code&gt;rails db:seed&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That's it for the backend!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now for the frontend.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First in whatever component you want to fetch the data in you'll need to set up a few things, I'll be using the hooks useState(for grabbing state) and useEffect(for fetching whenever window is loaded). Let's import that and also get and format the current date.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;App.js&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;import React, { useState, useEffetc } from "react";

function App(){
  //setting up our useState for assigning a prompt 
  const [prompt, setPrompt] = useState([])

  //grabs the current date/time
  const currentDate = new Date();

  //formatting our date for fetching
  const date = `${current.getMonth()+1}${current.getDate()}${current.getFullYear()}`;

  return(
    &amp;lt;div&amp;gt;nothing here yet&amp;lt;/div&amp;gt;
  )
}

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now all that is left to do is set up our useEffect/fetch and do something with the fetched data. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;App.js&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;useEffect(() =&amp;gt; {
  //your url might be different, such as: http://localhost:4000/prompts/${date}

  fetch(`/prompts/${date}`)
  .then((r) =&amp;gt; r.json())
  .then(data =&amp;gt; {
     setPrompt(data)
     fetchPosts(data.id)
   })
}, [date]);

//doing something with the data
return (
  &amp;lt;div&amp;gt;{prompt.title} : {prompt.date}&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's pretty much it, run your backend with &lt;code&gt;rails start&lt;/code&gt; and your front with &lt;code&gt;npm start&lt;/code&gt;. Hopefully, this was helpful, thank you for reading.&lt;/p&gt;

</description>
      <category>react</category>
      <category>rails</category>
      <category>beginners</category>
      <category>reacthooks</category>
    </item>
    <item>
      <title>How to use mouse coordinates to make an image button with React.</title>
      <dc:creator>Caleb Alessandro Rodriguez</dc:creator>
      <pubDate>Sat, 08 Jan 2022 07:44:42 +0000</pubDate>
      <link>https://dev.to/sirdigo/how-to-use-mouse-coordinates-to-make-an-image-button-with-react-5fmb</link>
      <guid>https://dev.to/sirdigo/how-to-use-mouse-coordinates-to-make-an-image-button-with-react-5fmb</guid>
      <description>&lt;p&gt;Starting my Phase 2 Project for the Software Engineering course I'm enrolled in at Flatiron school I was honestly a bit unsure and even scared about one obstacle, finding and using mouse coordinates. Starting off my project partners and I came up with the idea to make a simple point and clicker game. We were then informed that a good way to do this would be to select a section of the window and using coordinates we could turn that given area into a button. Before I get into this I would like to say I'm only a student and my knowledge of JS and React is not big. Secondly, finding the mouse coordinates are very simple but instead of writing out a whole new, probably worse explanation, here is the explanation I followed by &lt;a href="https://codedaily.io/tutorials/Create-a-useMousePosition-Hook-with-useEffect-and-useState-in-React" rel="noopener noreferrer"&gt;Jason Brown&lt;/a&gt;. Anyways let's get into it.&lt;/p&gt;

&lt;p&gt;Assuming you followed Mr. Brown's tutorial you should have a &lt;code&gt;useMousePosition&lt;/code&gt; component all ready to go. I'm going to use the art I made from the project as an example and introduce you to Slippy.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5xfxxcik95jb3gj2fj9s.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5xfxxcik95jb3gj2fj9s.gif" alt="Slippy Asleep"&gt;&lt;/a&gt;&lt;br&gt;
Above you can see currently Slippy is sleeping, I would like to be able to click on the &lt;em&gt;WAKE&lt;/em&gt; button to well wake him up. Thanks to &lt;code&gt;useMousePosition&lt;/code&gt;, the coordinates of the button, and some conditional logic I can do so, lets go ahead and put this image of Slippy in an &lt;code&gt;useState&lt;/code&gt; after importing both the image and hook.&lt;/p&gt;

&lt;p&gt;Let's start by grabbing the coordinates of each corner we want.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foo0lawex3n9nz9luur3g.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foo0lawex3n9nz9luur3g.gif" alt="Showing how I'd collect coordinates"&gt;&lt;/a&gt;&lt;br&gt;
You should end up with four different numbers, two for the x position and two for the y position. &lt;strong&gt;Make sure to write those numbers down, and separate them according to which x and y go together&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Now before anything else, you need to import &lt;code&gt;useMousePosition&lt;/code&gt; into the component you want to use it in and save it to a variable as a value to be used later on. I also like to take it a step further and destructor and save x and y as well, like so.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React { useState } from "react";
import slippySleeping from "./images/slippySleeping.gif";
import { useMousePosition } from "./useMousePosition";

App function(){
  const [slippy, setSlippy] = usestate(slippySleeping)

  const position = useMousePosition();
  const x = position.x;
  const y = position.y;

  return (
   &amp;lt;img src={slippy} alt="Slippy Sleeping" /&amp;gt;
  );
};

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have this set up we can add an on click to the image tag and link a function inside of it. Let's call the function &lt;code&gt;wakeSlippy&lt;/code&gt;. This function will hold the conditional logic and change the previous state to a new state, with that in mind make sure to import your second image. You should end up with something like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React { useState } from "react";
import slippySleeping from "./images/slippySleeping.gif";
import awakeSlippy from "./images/awakeSlippy.gif";
import { useMousePosition } from "./useMousePosition";

App function(){
  const [slippy, setSlippy] = usestate(slippySleeping)

  const position = useMousePosition();
  const x = position.x;
  const y = position.y;

  function wakeSlippy(){
    setSlippy(awakeSlippy);
  }

  return (
   &amp;lt;img src={slippy} onClick={wakeSlippy} alt="Slippy Sleeping" /&amp;gt;
  );
};

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What we have right now will change the image no matter where we click on the image, let's fix that. Remember those coordinates? Well, it's time to use them. The conditional logic should look something like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(x &amp;lt; biggerNumX &amp;amp;&amp;amp; y &amp;lt; biggerNumY &amp;amp;&amp;amp; x &amp;gt; lesserNumX &amp;amp;&amp;amp; y &amp;gt; lesserNumY) { 
  setSlippy(awakeSlippy;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What we've done here is find the corners of the button and make&lt;br&gt;
a square with the conditional logic. Now all you need to do is put it into the &lt;code&gt;wakeSleepy&lt;/code&gt; function and you should have something like this.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz9bb5t7r2ngfnav941x9.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz9bb5t7r2ngfnav941x9.gif" alt="Slippy Awake"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;If you wanted to put Slippy back to bed all you'd need to do is get the coordinates for the bed and add some more conditional logic and that's it, pretty simple! One final word, this method of making buttons can be pretty unreliable unless you update your code for all screen sizes.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>react</category>
    </item>
    <item>
      <title>First Day at Flatiron and my biggest struggle.</title>
      <dc:creator>Caleb Alessandro Rodriguez</dc:creator>
      <pubDate>Thu, 25 Nov 2021 07:20:55 +0000</pubDate>
      <link>https://dev.to/sirdigo/first-day-at-flatiron-and-my-biggest-struggle-4nga</link>
      <guid>https://dev.to/sirdigo/first-day-at-flatiron-and-my-biggest-struggle-4nga</guid>
      <description>&lt;p&gt;Up to this point my day online, in Zoom calls, and working through labs(Assignments) had been pretty good. I had been working through labs with little to no trouble for the most part &lt;strong&gt;Until&lt;/strong&gt; &lt;em&gt;phase-1-first-class-functions-lab&lt;/em&gt;. The lab started of moderately normal, it wasn't "easy" but it wasn't super hard. The thing with Flatiron and its assignments is that the README.md files, explaining what to do but not completely telling, are worded in a way that you have to read over and over to understand. but maybe that's just me being literate and Flatiron trying to teach me to be literate with code. Just being a beginner I don't understand enough yet. In the end I spent too much time on this part of the code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const selectingDrivers = [returnFirstTwoDrivers, returnLastTwoDrivers];

 const createFareMultiplier = i =&amp;gt; {
     return function(i) {
         return i * i;
     }
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;But&lt;/strong&gt; it was one of the best feelings in the world completing it.&lt;/p&gt;

&lt;p&gt;P.S: Excuse my subpar grammar, I don't write/blog much.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
