<?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>Forem: Swahili Speaking Coder</title>
    <description>The latest articles on Forem by Swahili Speaking Coder (@babou360).</description>
    <link>https://forem.com/babou360</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%2F908974%2Fcdb487d9-a94d-465d-8e3d-f9288058d233.jpg</url>
      <title>Forem: Swahili Speaking Coder</title>
      <link>https://forem.com/babou360</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/babou360"/>
    <language>en</language>
    <item>
      <title>Asset(Image/Video) Picker with javascript(react/nextjs) on button click</title>
      <dc:creator>Swahili Speaking Coder</dc:creator>
      <pubDate>Thu, 23 Mar 2023 07:54:24 +0000</pubDate>
      <link>https://forem.com/babou360/assetimagevideo-picker-with-javascriptreactnextjs-on-button-click-1ol0</link>
      <guid>https://forem.com/babou360/assetimagevideo-picker-with-javascriptreactnextjs-on-button-click-1ol0</guid>
      <description>&lt;p&gt;Before we start i am going to put my social media links if you would like to follow me there&lt;br&gt;
Instagram &lt;a href="https://www.instagram.com/babou360"&gt;&lt;/a&gt;&lt;br&gt;
Twitter &lt;a href="https://www.twitter.com/babou360"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;and also my upwork profile if you want to work with me &lt;br&gt;
&lt;a href="https://www.upwork.com/freelancers/~011681294ec42b9963"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this blog post i am going to demonstrate how to create an asset picker on button click.&lt;br&gt;
Below is how the the ui of the picker looks like and how it works&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/AzjW7_u3mdw"&gt;
&lt;/iframe&gt;
 &lt;/p&gt;

&lt;p&gt;First of all we are going to create our app with next js a react framework&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-next-app assetpicker
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;make sure all the characters are in small letters because next js does not allow capital letters &lt;br&gt;
and during the creation of the app select yes for typescript on the terminal pop up because we are going to use typescript in this app&lt;br&gt;
After app creation we are going to install 3 npm packages which are react-icons, classnames and sass&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i react-icons classnames sass
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After installation go to pages and delete index.tsx content but not the file itself and we are going to start from scratch &lt;/p&gt;

&lt;p&gt;we are going to have 4 items in our setState which are image, selecta, current and names&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const [image, setImage] = useState&amp;lt;any&amp;gt;([])
 const [selecta, setSelecta] = useState&amp;lt;any&amp;gt;([])
 const [current, setCurrent] = useState(0)
 const [names, setNames] = useState&amp;lt;any&amp;gt;([])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;image this is the actual &lt;strong&gt;image **which is going to be displayed inside a div known as **selecta&lt;/strong&gt; and current is the current selected image which by default is the first one and names contains an array of names of all selected images this is going to help us to check if the image is already selected or not &lt;/p&gt;

&lt;p&gt;Apart from those we are going to have 3 functions addSelecta, onChange and deleteData&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const addSelecta = () =&amp;gt; {
    let image1 = selecta.length
    if(selecta.length &amp;gt; image.length){
      alert('not allowed')
      return 
    }else if( selecta.length &amp;gt;= 5){
      alert('maximum images 5 reached')
      return 
    }else{
      selecta.push(`image${image1+1}`)
      setSelecta(selecta.map((item:any)=&amp;gt; item))
    }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in this function we are adding a button to select image and that div is going to display a plus icon if there is no image selected and and image otherwise.&lt;br&gt;
Every Time we click a button we are going to add an item named image plus selecta.length icreament so selecta item is going to be image0, image1 etc in an array&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const onChange = (e: React.ChangeEvent&amp;lt;HTMLInputElement&amp;gt;) =&amp;gt; {
    const one:any = e.target.files
    const two = URL.createObjectURL(one[0])
    if(names.includes(one[0].name)){
      alert('asset available')
      return 
    }else{
      image.push(two)
      names.push(one[0].name)
      setImage(image.map((item:any)=&amp;gt; item))
      setNames(names.map((item:any)=&amp;gt; item))
    }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;in in onchange function this is all about selecting assets but there is more to that.&lt;br&gt;
we are going to have a constant value named &lt;strong&gt;one **which is simply an array assignment of assets.&lt;br&gt;
and the second constant value is **two&lt;/strong&gt; which is simply a url creation for selected files number one.&lt;br&gt;
we are using URL.createObjectURL(one[0]) because we cannot display files as files files on the web unless they are BLOB so this method is simply a creation of a BLOB url&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const deleteData = (image1:any, selecta1:any) =&amp;gt; {
    const imageIndex = image.indexOf(image1)
    const selectaIndex = selecta.indexOf(selecta1)

    if(selecta.includes(selecta1)){
      selecta.splice(selectaIndex,1)
      image.splice(imageIndex,1)
      names.splice(image1.name,1)
      setSelecta(selecta.map((item:any)=&amp;gt; item))
      setImage(image.map((item:any)=&amp;gt; item))
      setNames(names.map((item:any)=&amp;gt; item))
    }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The third function &lt;strong&gt;deleteData&lt;/strong&gt; we are going to pass 2 items along with it the first one is *&lt;em&gt;image **which is the image index of that selected image plus the selecta index of the selecta array.&lt;br&gt;
Then we are going to get indexes of those two **imageIndex **and **selectaIndex *&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;and then we are going to check if the selecta array includes the selected selecta if true we are going to delete the selecta, image and name and after that we are going to set them with new array so as to update the ui because if we do not do that the changes will not be reflected on the ui&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;div className={styles.home}&amp;gt;
      &amp;lt;div className={styles.item}&amp;gt;
        &amp;lt;div className={styles.hero}&amp;gt;
          &amp;lt;div className={styles.thumbs}&amp;gt;
            {
              image.map((item:any, index:number)=&amp;gt; (
                &amp;lt;div className={cx(styles.dot, current===index &amp;amp;&amp;amp; styles.active)} onClick={()=&amp;gt; setCurrent(index)}&amp;gt;&amp;lt;/div&amp;gt;
              ))
            }
          &amp;lt;/div&amp;gt;
          {
            image.length &amp;lt; 1
            ? null
            : &amp;lt;img src={image[current]} alt="" /&amp;gt;
          }
        &amp;lt;/div&amp;gt;
        &amp;lt;div className={styles.action}&amp;gt;
          &amp;lt;div className={styles.left}&amp;gt;
            {
              selecta.map((item:any, index:number)=&amp;gt; (
                &amp;lt;div className={styles.item}&amp;gt;
                  {
                    index+1 &amp;gt; image.length
                    ?&amp;lt;div className={styles.picker}&amp;gt;
                      &amp;lt;input type="file"
                      id='file'
                      style={{display:'none'}} 
                      onChange={e =&amp;gt; onChange(e)}
                      /&amp;gt;
                      &amp;lt;label htmlFor="file"&amp;gt;
                        &amp;lt;BiPlus className={styles.icon} size={25}/&amp;gt;
                      &amp;lt;/label&amp;gt;
                    &amp;lt;/div&amp;gt;
                    : &amp;lt;img src={image[index]} alt=""  onClick={()=&amp;gt; deleteData(image[index], item)}/&amp;gt;
                  }
                &amp;lt;/div&amp;gt;
              ))
            }
          &amp;lt;/div&amp;gt;
          &amp;lt;div className={styles.right}&amp;gt;
            &amp;lt;div className={styles.add} onClick={() =&amp;gt; addSelecta()}&amp;gt;
              &amp;lt;BiPlus className={styles.icon}/&amp;gt;
            &amp;lt;/div&amp;gt;
          &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After all that we need to write our code we are going to have home as our main div and inside it there is item and inside item we are having hero section which is the big image.&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 className={styles.hero}&amp;gt;
          &amp;lt;div className={styles.thumbs}&amp;gt;
            {
              image.map((item:any, index:number)=&amp;gt; (
                &amp;lt;div className={cx(styles.dot, current===index &amp;amp;&amp;amp; styles.active)} onClick={()=&amp;gt; setCurrent(index)}&amp;gt;&amp;lt;/div&amp;gt;
              ))
            }
          &amp;lt;/div&amp;gt;
          {
            image.length &amp;lt; 1
            ? null
            : &amp;lt;img src={image[current]} alt="" /&amp;gt;
          }
        &amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside the hero section we have thumbs which are the small dots indicating the selected image and unselected ones.&lt;/p&gt;

&lt;p&gt;and then we have the action div with left and right divs the left is an array of selectas and the right one is the button to add selectas.&lt;br&gt;
inside the selecta we are going to check if selecta has an image or not if not we are going to show a button to select the image &lt;/p&gt;

&lt;p&gt;here is the full code &lt;/p&gt;

&lt;p&gt;Index.tsx&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 styles from './home.module.scss'
import { BiPlus } from 'react-icons/bi'
import cx from 'classnames'

function Home() {
  const [image, setImage] = useState&amp;lt;any&amp;gt;([])
  const [selecta, setSelecta] = useState&amp;lt;any&amp;gt;([])
  const [current, setCurrent] = useState(0)
  const [names, setNames] = useState&amp;lt;any&amp;gt;([])

  const addSelecta = () =&amp;gt; {
    let image1 = selecta.length
    if(selecta.length &amp;gt; image.length){
      alert('not allowed')
      return 
    }else if( selecta.length &amp;gt;= 5){
      alert('maximum images 5 reached')
      return 
    }else{
      selecta.push(`image${image1+1}`)
      setSelecta(selecta.map((item:any)=&amp;gt; item))
    }
  }

  const onChange = (e: React.ChangeEvent&amp;lt;HTMLInputElement&amp;gt;) =&amp;gt; {
    const one:any = e.target.files
    const two = URL.createObjectURL(one[0])
    if(names.includes(one[0].name)){
      alert('asset available')
      return 
    }else{
      image.push(two)
      names.push(one[0].name)
      setImage(image.map((item:any)=&amp;gt; item))
      setNames(names.map((item:any)=&amp;gt; item))
    }
  }

  const deleteData = (image1:any, selecta1:any) =&amp;gt; {
    const imageIndex = image.indexOf(image1)
    const selectaIndex = selecta.indexOf(selecta1)

    if(selecta.includes(selecta1)){
      selecta.splice(selectaIndex,1)
      image.splice(imageIndex,1)
      names.splice(image1.name,1)
      setSelecta(selecta.map((item:any)=&amp;gt; item))
      setImage(image.map((item:any)=&amp;gt; item))
      setNames(names.map((item:any)=&amp;gt; item))
    }
  }
  return (
    &amp;lt;div className={styles.home}&amp;gt;
      &amp;lt;div className={styles.item}&amp;gt;
        &amp;lt;div className={styles.hero}&amp;gt;
          &amp;lt;div className={styles.thumbs}&amp;gt;
            {
              image.map((item:any, index:number)=&amp;gt; (
                &amp;lt;div className={cx(styles.dot, current===index &amp;amp;&amp;amp; styles.active)} onClick={()=&amp;gt; setCurrent(index)}&amp;gt;&amp;lt;/div&amp;gt;
              ))
            }
          &amp;lt;/div&amp;gt;
          {
            image.length &amp;lt; 1
            ? null
            : &amp;lt;img src={image[current]} alt="" /&amp;gt;
          }
        &amp;lt;/div&amp;gt;
        &amp;lt;div className={styles.action}&amp;gt;
          &amp;lt;div className={styles.left}&amp;gt;
            {
              selecta.map((item:any, index:number)=&amp;gt; (
                &amp;lt;div className={styles.item}&amp;gt;
                  {
                    index+1 &amp;gt; image.length
                    ?&amp;lt;div className={styles.picker}&amp;gt;
                      &amp;lt;input type="file"
                      id='file'
                      style={{display:'none'}} 
                      onChange={e =&amp;gt; onChange(e)}
                      /&amp;gt;
                      &amp;lt;label htmlFor="file"&amp;gt;
                        &amp;lt;BiPlus className={styles.icon} size={25}/&amp;gt;
                      &amp;lt;/label&amp;gt;
                    &amp;lt;/div&amp;gt;
                    : &amp;lt;img src={image[index]} alt=""  onClick={()=&amp;gt; deleteData(image[index], item)}/&amp;gt;
                  }
                &amp;lt;/div&amp;gt;
              ))
            }
          &amp;lt;/div&amp;gt;
          &amp;lt;div className={styles.right}&amp;gt;
            &amp;lt;div className={styles.add} onClick={() =&amp;gt; addSelecta()}&amp;gt;
              &amp;lt;BiPlus className={styles.icon}/&amp;gt;
            &amp;lt;/div&amp;gt;
          &amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}

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

&lt;/div&gt;



&lt;p&gt;home.module.scss&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.home{
    width: 100vw;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;

    .item{
        width: 50%;

        .hero{
            width: 100%;
            height: 400px;
            background: black;
            display: flex;
            align-items: center;
            justify-content: center;
            position: relative;

            img{
                width: 90%;
                height: 90%;
                object-fit: cover;
            }
            .thumbs{
                position: absolute;
                bottom: 0;
                display: flex;
                align-items: center;
                justify-content: center;

                .dot{
                    width: 10px;
                    height: 10px;
                    margin: 5px;
                    background: white;
                    cursor: pointer;

                    &amp;amp;.active{
                        background: crimson;
                    }
                }
            }
        }

        .action{
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: space-between;
            margin: 10px 0;

            .left{
                width: 90%;
                display: flex;
                align-items: center;
                justify-content: flex-start;

                .item{
                    width: 100px;
                    height: 100px;
                    background: crimson;
                    margin-right: 10px;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    cursor: pointer;

                    img{
                        width: 100%;
                        height: 100%;
                        object-fit: cover;
                    }
                }
            }
            .right{
                width: 10%;

                .add{
                    background: crimson;
                    border-radius: 50%;
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    width: 40px;
                    height: 40px;
                    cursor: pointer;

                    .icon{
                        color: white;
                    }
                }
            }
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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