123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <!--Banner-->
<section class='container-fluid p-0 position-relative'>
  <div class='cover-photo'></div>
  <div class='overlay'></div>
  <div class='banner-title text-center'>
    <h1><i class='fas fa-handshake text-success'></i></h1>
    <h2 class='text-white'>Your Connection Starts Here</h2>
  </div>
</section>

<!--Navbar-->
<section class='container-fluid p-0 sticky-top shadow-lg' id='navbar-group'>
  <div class='row text-center m-0'>
    <a href='#navbar-group' class='nav-col col-3' id='all'>
      <i class='fas fa-users'></i>
      <span class='d-none d-lg-inline pl-3'>all</span>
    </a>
    <a href='#navbar-group' class='nav-col col-3' id='male'>
      <i class='fas fa-mars'></i>
      <span class='d-none d-lg-inline pl-3'>male</span>
    </a>
    <a href='#navbar-group' class='nav-col col-3' id='female'>
      <i class='fas fa-venus'></i>
      <span class='d-none d-lg-inline pl-3'>female</span>
    </a>
    <a href='#navbar-group' class='nav-col col-3' id='random'>
      <i class='fas fa-random'></i>
      <span class='d-none d-lg-inline pl-3'>Random Pick</span>
    </a>
  </div>
</section>

<!--Data Panel-->
<div class='container mt-5'>
  <div id='data-panel' class='row'>
    <!--Display Profile Card-->
  </div>
</div>

<!-- Modal-->
<div class='modal fade' id='profileDetail' tabindex='-1' role='dialog' aria-labelledby='exampleModalLongTitle' aria-hidden='true'>
  <div class='modal-dialog modal-dialog-centered modal-lg' role='document'>
    <div class='modal-content'>
      <div class='modal-body'>
        <div class='container-fluid'>
          <div class='row'>
            <!--Avatar-->
            <div class='col-12 col-lg-4'>
              <img id='profile-detail-image' class='w-100 h-auto' src='' alt='profile photo' class='img-fluid'>
            </div>
            <!--Profile Detail Info-->
            <div class='col-12 col-lg-8 py-4 text-secondary position-relative' id='profile-detail-info'>
              <h1 class='mb-4 text-dark' id='profile-detail-name'></h1>
              <p id='profile-detail-age'></p>
              <p id='profile-detail-birth'></p>
              <p id='profile-detail-region'></p>
              <ul class='text-center'>
                <li><img src='' alt='country flag' id='profile-detail-flag'></li>
                <li class='mt-3'><span class='badge' id='profile-detail-gender'></span></li>
              </ul>
            </div>
          </div>
        </div>
      </div>
      <div class='modal-footer text-white'>
        <a type='button' class='btn btn-primary' id='profile-detail-connect' href=''><i class='far fa-envelope pr-2'></i>Connect
          Now</a>
        <a type='button' class='btn btn-secondary' data-dismiss='modal'>Close</a>
      </div>
    </div>
  </div>
</div>

<!--Fixed Check Favorite Button-->
<a href='#navbar-group' role='button' class='btn btn-danger shadow-lg text-white' id='my-favorite'>
  <i class='fas fa-heart pr-1'></i>
  <span id='favorite-number'>0</span>
  <span class='ml-2'><i class='far fa-hand-point-right'></i>Check Your Favorite</span>
</a>
            
          
!
            
              /*Initiallization*/
* {
  box-sizing: border-box;
}

body {
  background: #f0f0f0;
}

li {
  list-style-type: none;
}

a,
a:hover {
  text-decoration: none;
}

/*Banner Section*/
.cover-photo {
  width: 100%;
  height: 500px;
  background-image: url("https://images.pexels.com/photos/296881/pexels-photo-296881.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260");
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
}

.overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.3);
}

.banner-title {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-shadow: 1px 1px #505050;
  width: 90%;
  font-size: 16px;
}

.banner-title h1 {
  font-size: 4em;
}

.banner-title h2 {
  font-size: 3em;
}

/*Navbar Section*/
#navbar-group {
  font-size: 16px;
}

.nav-col {
  padding: .4em 0;
  font-size: 2em;
  font-weight: 600;
  color: white;
  text-shadow: 1px 1px #505050;
  text-transform: capitalize;
  background: #888888;
}

.nav-col:hover {
  color: white;
  background: #282828;
  -webkit-transition: background 0.2s linear;
  transition: background 0.2s linear;
}

/*Data Panel Section: General*/
#data-panel figure {
  overflow: hidden;
}

#data-panel img.card-img-top {
  cursor: pointer;
  -webkit-transform: scale(1);
  transform: scale(1);
}

#data-panel img.card-img-top:hover {
  opacity: 0.7;
  -webkit-transition: all 0.1s linear;
  -moz-transition: all 0.1s linear;
  -o-transition: all 0.1s linear;
  transition: all 0.1s linear;
  -webkit-transform: scale(1.1);
  transform: scale(1.1);
}

#data-panel i.fa-heart {
  cursor: pointer;
  transition: all 0.1s linear;
}

.gray-heart {
  color: #c0c0c0;
}

/*Data Panel Section: Favorite*/
#data-panel i.fa-heart:hover,
.red-heart {
  color: #c80000;
  text-shadow: 2px 2px #e0e0e0;
}

#empty-message {
  margin: 3em auto 0 auto;
  font-size: 16px;
  color: #b0b0b0;
  text-shadow: 1px 1px #888888;
}

#empty-message h1 {
  font-size: 4em;
}

#empty-message h3 {
  font-size: 1.8em;
}

/*Modal Section*/
#profile-detail-info {
  font-size: 16px;
}

#profile-detail-info h1 {
  font-size: 1.9em;
}

#profile-detail-info p {
  margin: 0;
  font-size: 1.1em;
  line-height: 1.8em;
}

#profile-detail-info ul {
  position: absolute;
  top: 28px;
  right: 25px;
}

#profile-detail-flag {
  width: 2.5em;
  height: auto;
}

#profile-detail-gender {
  background: #e8e8e8;
}

/*Fixed Check Favorite Button*/
#my-favorite {
  position: fixed;
  bottom: 30px;
  right: -217px;
  padding: 0.8em 0.5em;
  font-size: 20px;
  opacity: 0.7;
}

#my-favorite:hover {
  right: 0;
  opacity: 1;
  -webkit-transition: all 0.3s ease;
  transition: all 0.3s ease;
}

/*Large Devices*/
@media screen and (min-width: 992px) {
  /*Banner Section*/
  .banner-title {
    font-size: 20px;
  }

  /*Navbar Section*/
  #navbar-group {
    font-size: 12px;
  }

  /*Modal Section*/
  #profile-detail-info {
    font-size: 19px;
  }

  /*Fixed Check Favorite Button*/
  #empty-message {
    font-size: 22px;
  }
}

/*Extra Large Devices*/
@media screen and (min-width: 1200px) {
  /*Banner Section*/
  .banner-title {
    font-size: 25px;
  }
}

            
          
!
            
              (function(){  
  const BASE_URL = 'https://lighthouse-user-api.herokuapp.com'
  const INDEX_URL = BASE_URL + '/api/v1/users/'
  const data = []
  const navbarGroup = document.querySelector('#navbar-group')
  const dataPanel = document.querySelector('#data-panel')
  const myFavorite = document.querySelector('#my-favorite')
  let onFavoritePage = false
  
  //Send request to Index API
  axios
    .get(INDEX_URL)
    .then(response => {
      response.data.results.forEach(profileData => {
        data.push({...profileData, favorite: false})
      })
      displayProfile(data)
    })
    .catch(error => console.log(error))
  
  //Add event listener to navbarGroup
  navbarGroup.addEventListener('click', event => {
    const target = event.target
    const targetParent = target.parentElement
    if (target.matches('#all') || targetParent.matches('#all')){
      displayProfile(data)
    } else if(target.matches('#male') || targetParent.matches('#male')) {
      const maleData = data.filter(person => person.gender === 'male')
      displayProfile(maleData)
    } else if(target.matches('#female') || targetParent.matches('#female')) {
      const femaleData = data.filter(person => person.gender === 'female')
      displayProfile(femaleData)
    } else {
      displayProfile(chooseRandomProfiles(4))
    }
    onFavoritePage = false
  })
  
  //Add event listener to dataPanel
  dataPanel.addEventListener('click', event => {
    if (event.target.matches('.card-img-top')) {
      showProfileDetail(event.target.dataset.id)
    } else if (event.target.matches('.fa-heart')) {
      event.target.classList.toggle('red-heart')
      updateFavoriteStatus(event.target.dataset.id)
      
      //if the user is on the favorite page remove that profile
      if (onFavoritePage) {
        let profile = event.target.closest('#profile')
        removeFavoriteProfile(profile)
      }
    }
  })
  
  //Add event listener to Fixed Favorite Button
  myFavorite.addEventListener('click', () => {
    const favoriteData = data.filter(person => person.favorite )
    favoriteData.length !== 0 ? displayProfile(favoriteData) : displayEmptyMessage()
    onFavoritePage = true
  })
  
  function displayProfile(data) {
    dataPanel.innerHTML = data.map(person => {
      //if the profile has been selected as favorite add red-heart class
      redHeartClass = person.favorite === true ? 'red-heart' : ''
      //return display content
      return `
        <div class='col-md-6 col-xl-4 mb-4' id='profile'>
          <div class='card h-100 shadow'>
            <figure>
              <img src=${person.avatar} class='card-img-top' alt='profile photo' data-toggle='modal' data-target='#profileDetail' data-id='${person.id}'>
            </figure>
            <div class='card-body text-capitalize row'>
              <div class='col-10'>
                <h5 class='card-title'>${person.name} ${person.surname}</h5>
                <p class='card-text text-secondary'>
                  <i class='fas fa-map-marker-alt mr-2'></i>${person.region}
                </p>
              </div>
              <div class='col-2'>
                <h2><i class='fas fa-heart gray-heart ${redHeartClass}' data-id='${person.id}'></i></h2>
              </div>
            </div>
            <div class='card-footer text-muted text-center'>
              <a href='mailto:${person.email}' class='card-link'><i class='far fa-envelope pr-2'></i>Connect Now</a>
            </div>
          </div>
        </div>
      `
    }).join('')
  }
  
  //randomly find a specific number of profiles
  function chooseRandomProfiles(num) {
    const randomData = []
    for (let i = 0; i < num; i++) {
      let randomIndex = Math.floor(Math.random() * data.length)
      randomData.push(data[randomIndex])
    }
    return randomData
  }
  
  function showProfileDetail(id) {
    //get elements
    const modalImage = document.getElementById('profile-detail-image')
    const modalName = document.getElementById('profile-detail-name')
    const modalAge = document.getElementById('profile-detail-age')
    const modalBirth = document.getElementById('profile-detail-birth')
    const modalRegion = document.getElementById('profile-detail-region')
    const modalGender = document.getElementById('profile-detail-gender')
    const modalEmail = document.getElementById('profile-detail-connect')
    
    //set request url
    const SHOW_API = INDEX_URL + id
    
    //send request to SHOW API
    axios
      .get(SHOW_API)
      .then(response => {
        const data = response.data
        modalImage.src = data.avatar
        modalName.textContent = `${data.name} ${data.surname}`
        modalAge.textContent = `Age: ${data.age}`
        modalBirth.textContent = `Born in: ${data.birthday}`
        modalRegion.textContent = `Nationality: ${data.region}`
        modalEmail.href = `mailto:${data.email}`
        modalGender.textContent = (data.gender === 'male') ? 'Male' : 'Female'
        createFlag(data.region)
    })
  }
  
  function createFlag(region) {
    //modify country name for special regions
    if (region === 'England') region = 'United Kingdom'
    
    const modalFlag = document.getElementById('profile-detail-flag')
    const BASE_URL = 'https://restcountries.eu'
    const FLAG_URL = BASE_URL + '/rest/v2/name/' + region + '?fields=flag'
   
    //send request to REST Countries API
    axios
      .get(FLAG_URL)
      .then(response => modalFlag.src = response.data[0].flag)
      .catch(error => {     
          modalFlag.src = ''
          console.log(error)
      })
  }
  
  function updateFavoriteStatus(id) {
    //update profile data's favorite status 
    data[id-1].favorite = data[id-1].favorite ? false : true
    
    //update the number on check favorite button
    updateFavoriteNumber()
  }
  
  function updateFavoriteNumber() {
    const number = data.filter(profile => profile.favorite).length
    document.getElementById('favorite-number').textContent = number
  }
  
  function removeFavoriteProfile(profile) {
    profile.parentElement.removeChild(profile)
    if (dataPanel.children.length === 0) displayEmptyMessage()
  }
  
  //if no one is selected as favorite, display empty message
  function displayEmptyMessage() {
    dataPanel.innerHTML = `
      <div class='text-center' id='empty-message'>
        <h1><i class='far fa-grin-beam-sweat'></i></h1>
        <h3>No one has been chosen</h3>
      </div>
    `
  }
})()
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console