<?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: Velcruza</title>
    <description>The latest articles on DEV Community by Velcruza (@velcruza).</description>
    <link>https://dev.to/velcruza</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%2F743717%2Feac2d2f8-337f-4577-92ec-1319df08f376.png</url>
      <title>DEV Community: Velcruza</title>
      <link>https://dev.to/velcruza</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/velcruza"/>
    <language>en</language>
    <item>
      <title>How to display different components based on user authentication</title>
      <dc:creator>Velcruza</dc:creator>
      <pubDate>Wed, 02 Feb 2022 02:56:16 +0000</pubDate>
      <link>https://dev.to/velcruza/how-to-display-different-components-based-on-user-authentication-8o5</link>
      <guid>https://dev.to/velcruza/how-to-display-different-components-based-on-user-authentication-8o5</guid>
      <description>&lt;p&gt;Hey there! Ever want to display different components based on whether or not your user is logged in? Well, you're in the right spot. Let's dive right in.&lt;/p&gt;

&lt;p&gt;To start off, you're going to want your two different components that you'll be displaying based on user authentication. For this example the two components I'm using are &lt;code&gt;&amp;lt;LoggedOut/&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;LoggedIn/&amp;gt;&lt;/code&gt;. Next step is we need a route on the backend to render current user info. For this example the route I used is &lt;code&gt;/me&lt;/code&gt; and heres what that looks like in my user controller:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def me
        if current_user
            render json: current_user, status: :ok
        else
            render json: {error: "Not logged in"}, status: :unauthorized
        end
    end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From here, what we're going to be doing is fetching to this route &lt;code&gt;/me&lt;/code&gt; to find out if our user is logged in, and if our user is logged in we're going set our current user to that and render our page based on that. That probably didn't make much sense but lets step through the code together.&lt;br&gt;
In our App.js here is what we do:&lt;br&gt;
We're starting off by setting 2 states&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [currentUser, setCurrentUser] = useState(null);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we're going to fetch to our &lt;code&gt;/me&lt;/code&gt; route to figure out if our user is logged in or not, and based on that info: if the user logged in we're setting our current user to that data, and if not then our current user remains null:&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("/me", {
      credentials: "include",
    }).then((res) =&amp;gt; {
      if (res.ok) {
        res.json().then((user) =&amp;gt; {
          setCurrentUser(user);
        });
      }
    });
  }, []);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;then in our &lt;code&gt;return()&lt;/code&gt; we're going to dynamically render the elements based on if currentUser is a truthy value or a falsey value:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return (
      &amp;lt;&amp;gt;
        {currentUser ? (&amp;lt;LoggedIn/&amp;gt;) : (&amp;lt;LoggedOut/&amp;gt;)}
      &amp;lt;/&amp;gt;
  );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and that's all you need!&lt;/p&gt;

&lt;p&gt;You can change the names for LoggedIn and LoggedOut to be whatever components you want to render based on if you're user is logged in or not. I hope this helped you out in any way and appreciate you following to the end!&lt;/p&gt;

</description>
      <category>react</category>
      <category>rails</category>
    </item>
    <item>
      <title>How to create simple group chats using Rails</title>
      <dc:creator>Velcruza</dc:creator>
      <pubDate>Fri, 28 Jan 2022 07:14:06 +0000</pubDate>
      <link>https://dev.to/velcruza/how-to-create-simple-group-chats-using-rails-45gf</link>
      <guid>https://dev.to/velcruza/how-to-create-simple-group-chats-using-rails-45gf</guid>
      <description>&lt;p&gt;Hey! Have you ever thought about implementing group chats in your project? Well, you're in the right place. I'm going to walk you step by step through the process of creating simple group chats using Rails backend. Let's get right into it.&lt;/p&gt;

&lt;p&gt;To start off you're going to want to setup the controllers, models and serializers for your backend using 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;rails g resource User name
rails g resource Group name
rails g resource Message text references:user references:group
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once you have that setup you want to go over to your User and Group models to make sure to add has_many :messages to their models. After doing that here's what your models should look like.&lt;/p&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User &amp;lt; ApplicationRecord
  has_many :messages
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Group &amp;lt; ApplicationRecord
  has_many :messages
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Message &amp;lt; ApplicationRecord
  belongs_to :user
  belongs_to :group
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next you're going to want to setup your Messages Controller with an index and create and it should look like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MessagesController &amp;lt; ApplicationController
    def index
        render json: Message.all, status: :ok
    end

    def create
        new_message = Message.create!(message_params)
        render json: new_message, status: :created
    end

    private

    def message_params
        params.permit(:group_id, :text, :user_id)
    end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now on your frontend when you fetch('/messages') with a get request it will return and array of objects (messages) that are already put in order of creation. From here you can filter this on your frontend to figure out what group/user the message belongs to. (on the other hand you can also do this on the backend with setting up custom routes and serializers to allow you to fetch to a route that will return messages that belong to a specific group). When creating a new message you just send a post request to your '/messages' with the user_id, group_id and text in the body object.&lt;/p&gt;

&lt;p&gt;Hope this helped you a bit to get started on setting up some group chats!&lt;/p&gt;

</description>
      <category>rails</category>
      <category>react</category>
    </item>
    <item>
      <title>Using setInterval() in Javascript</title>
      <dc:creator>Velcruza</dc:creator>
      <pubDate>Wed, 05 Jan 2022 19:51:59 +0000</pubDate>
      <link>https://dev.to/velcruza/using-setinterval-in-javascript-3cai</link>
      <guid>https://dev.to/velcruza/using-setinterval-in-javascript-3cai</guid>
      <description>&lt;p&gt;In my last project I wanted to set production per second and have my "factories" produce resources on a set time interval. Unfortunately I did not have enough time to figure out then so I decided to go back and look into how to use setInterval() in Javascript.&lt;/p&gt;

&lt;p&gt;setInterval() repeatedly calls a function with a fixed time delay between each call. So you can utilize to do something like I mentioned above (producing x amount of resources every x seconds) or for animations! You can use it in a way to have your picture move from one position to the other every x amount of seconds (hint hint... you might maybe see this in our upcoming project). &lt;/p&gt;

&lt;p&gt;Let's get into how to use setInterval() in your code now.&lt;/p&gt;

&lt;p&gt;setInterval() takes in a callback function and a delay in milliseconds. The method returns an interval ID so you can remove it later by calling clearInterval().&lt;/p&gt;

&lt;p&gt;basic example of the syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let intervalId = setInterval(callBackFn, 1000)

function callBackFn(){
   console.log("This message will log every one second")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This example would log "This message will log every one second" to the console every one second.&lt;/p&gt;

&lt;p&gt;Here is an example of using the clearInterval() method with setInterval() to be able to start/stop the function from executing every x seconds:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let nIntervId;

function changeColor() {
  // check if already an interval has been set up
  if (!nIntervId) {
    nIntervId = setInterval(flashText, 1000);
  }
}

function flashText() {
  const oElem = document.getElementById("my_box");
  if (oElem.className === "go") {
    oElem.className = "stop";
  } else {
    oElem.className = "go";
  }
}

function stopTextColor() {
  clearInterval(nIntervId);
  // release our intervalID from the variable
  nIntervId = null; 
}

document.getElementById("start").addEventListener("click", changeColor);
document.getElementById("stop").addEventListener("click", stopTextColor);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Things to be careful of when using the setInterval() method:&lt;br&gt;
-If you're using it to manipulate data from a fetch request you have to keep in mind that the fetch request itself takes sometime&lt;br&gt;
-Using it to setState can also get really funky (which is what happened with my project, therefore didn't end up using it) &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How mapping out project components can help you</title>
      <dc:creator>Velcruza</dc:creator>
      <pubDate>Wed, 17 Nov 2021 21:12:26 +0000</pubDate>
      <link>https://dev.to/velcruza/how-mapping-out-project-components-can-help-you-54i1</link>
      <guid>https://dev.to/velcruza/how-mapping-out-project-components-can-help-you-54i1</guid>
      <description>&lt;p&gt;I started off doing my react projects on the fly without building out a map for myself of components and where State needs to go and where I'm passing props from/to. I started running into a lot of trouble with where I should be putting my states or where I can pass data from depending on where I fetch it etc. I decided then to learn to map out my components to help me be able to properly understand the structure of project.&lt;/p&gt;

&lt;p&gt;Let us use a shopping website as our example. If I wanted to build something like that, it would need a lot of components. Initially what I did was try and think about the layout in my head, so let's try and do that here:&lt;br&gt;
-I need some TopNav and BottomNav that will be rendered on every page/route of my website&lt;br&gt;
-I also need a MainPage which will contain my ShopList, FeaturedList, and RecommendedList.&lt;br&gt;
-Those then will contain their children items such as ShopItems, FeaturedItems, and RecommendedItems.&lt;br&gt;
-I also need a CartPage that will display my cart and cost which will contain InventoryList(which has InventoryItems as a child) and CostDetails as well&lt;/p&gt;

&lt;p&gt;Obviously you can keep going with this and it will get really complicated even if you write it out on paper. (That's with me not even mentioned state, props and fetching data... that's a whole other mess)&lt;/p&gt;

&lt;p&gt;A much better way to do this is to sketch out a graph of your components and their children. I made a quick sketch of the same example that I used above, and now it will probably make sense.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5gU5j-09--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ni6nnra0zmyjzif41nt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5gU5j-09--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ni6nnra0zmyjzif41nt.png" alt="Image description" width="880" height="706"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I used the Drawio extension in VScode which is super easy and really nice to use for project planning. I really recommend you always take some time before starting your project to map out your components and their children and where you want your data to be fetched etc. before even touching a piece of code. This 100% will improve your time efficiency when it comes to actually coding and also your understanding of your own code.&lt;/p&gt;

</description>
      <category>react</category>
      <category>drawio</category>
    </item>
    <item>
      <title>Using setTimeOut() in JavaScript</title>
      <dc:creator>Velcruza</dc:creator>
      <pubDate>Wed, 03 Nov 2021 06:31:16 +0000</pubDate>
      <link>https://dev.to/velcruza/using-settimeout-in-javascript-33gm</link>
      <guid>https://dev.to/velcruza/using-settimeout-in-javascript-33gm</guid>
      <description>&lt;p&gt;Sometimes in your code delaying a function's actions can be really useful which is what I'm going to be talking about today!&lt;/p&gt;

&lt;p&gt;Imagine the following piece of code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log("Hey, ")
console.log("you?")
console.log("how ")
console.log("are ")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This would return us "Hey, you?how are ". Which doesn't really make any sense. (Obviously, the simplest/quickest fix would be to just reorganize our console.log or even combine it into one line but try to keep an open mind). A JavaScript function called "setTimeOut()" allows us to delay a function's actions after a specified number of milliseconds. setTimeOut() usually takes in a callback function or an anonymous function as one of its parameters and a number as it's other parameter.&lt;br&gt;
For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setTimeOut(() =&amp;gt; {console.log("delayed message")}, 500)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This would delay our "delayed message" by 500 milliseconds.&lt;/p&gt;

&lt;p&gt;So, going back to our initial piece of code that we had used as our example. We can now use setTimeOut() on our console.log("you?") to make sure it gets delayed until after the other two messages have already been logged to our console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log("Hey, ")
setTimeOut(() =&amp;gt; {console.log("you?")}, 500)
console.log("how ")
console.log("are ")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now this should return us "Hey, how are you?".&lt;/p&gt;

&lt;p&gt;This was just a simple example of how you can utilize the setTimeOut() function in JavaScript. One thing that I've used it for is delaying a function call until I have fetched all the data from a public api that I was then using in said function. There are many other ways that you can implement this function into your code (if needed) to help you out if you're struggling with anything rendering/executing/etc. before you actually want it to.&lt;/p&gt;

&lt;p&gt;One note to make though: figuring out the exact amount of milliseconds you want something to be delayed by can be quite difficult which is a downside of this. In the example I used above, realistically our computer doesn't need 500ms to log the other three messages to our console, but to a human, this isn't very noticeable.&lt;/p&gt;

&lt;p&gt;Hope you guys enjoyed my little post about setTimeOut() and hopefully this will be useful to you someday :) !&lt;/p&gt;

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