This repository has been archived by the owner on Jan 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from xwp/feature/23-popular_block
[Gutenberg] Add Popular homepage block.
- Loading branch information
Showing
5 changed files
with
485 additions
and
148 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,148 +4,8 @@ | |
* Internal block libraries. | ||
*/ | ||
const { __ } = wp.i18n; | ||
const { registerBlockType } = wp.blocks; | ||
|
||
import { deamplify } from '../amp-transformer'; | ||
|
||
/** | ||
* Render the popular block. | ||
* | ||
* @return {wp.element.Component} Rendered component. | ||
*/ | ||
const renderStaticPopularBlock = () => { | ||
return ( | ||
<section className='travel-popular pb4 pt3 relative'> | ||
<header className='max-width-3 mx-auto px1 md-px2'> | ||
<h3 className='h1 bold line-height-2 md-hide lg-hide' aria-hidden='true'>Top Adventures<br/>Near You</h3> | ||
<h3 className='h1 bold line-height-2 xs-hide sm-hide center'>Top Adventures Near You</h3> | ||
</header> | ||
<div className='overflow-scroll'> | ||
<div className='travel-overflow-container'> | ||
<div className='flex px1 md-px2 mxn1'> | ||
<div className='m1 mt3 mb2'> | ||
<div className='travel-popular-tilt-right mb1'> | ||
<div className='relative travel-results-result'> | ||
<a className='travel-results-result-link block relative' href='#'> | ||
<amp-img class='block rounded' width='346' height='200' noloading='' src={ travelGlobals.themeUrl + '/img/surf-day.jpg' } srcset={ travelGlobals.themeUrl + '/img/[email protected] 2x, ' + travelGlobals.themeUrl + '/img/surf-day.jpg 1x' }></amp-img> | ||
</a> | ||
<div className='travel-results-result-like absolute top-0 right-0'> | ||
<div className='p1'> | ||
<label className='travel-like'> | ||
<input type='checkbox' className='absolute'/> | ||
<div className='travel-like-hearts circle inline-block relative'> | ||
<div className='travel-like-heart-tiny travel-like-heart-solid absolute'></div> | ||
<div className='travel-like-heart-tiny travel-like-heart-solid absolute'></div> | ||
<div className='travel-like-heart-tiny travel-like-heart-solid absolute'></div> | ||
<div className='travel-like-heart travel-like-heart-white absolute mx-auto '></div> | ||
<div className='travel-like-heart travel-like-heart-solid absolute mx-auto '></div> | ||
<div className='travel-like-heart travel-like-heart-outline absolute mx-auto '></div> | ||
</div> | ||
</label> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<div className='h2 line-height-2 mb1'> | ||
<span className='travel-results-result-text'>Surf Day. Board and Wetsuits Included in Price!</span> | ||
<span className='travel-results-result-subtext h3'>•</span> | ||
<span className='travel-results-result-subtext h3'>$ </span><span className='black bold'>100</span> | ||
</div> | ||
|
||
<div className='h4 line-height-2'> | ||
<div className='inline-block relative mr1 h3 line-height-2'> | ||
<div className='travel-results-result-stars green'>★★★★★</div> | ||
</div> | ||
<span className='travel-results-result-subtext mr1'>241 Reviews</span> | ||
<span className='travel-results-result-subtext'><svg className='travel-icon' viewBox='0 0 77 100'><g fill='none' fillRule='evenodd'><path stroke='currentColor' strokeWidth='7.5' d='M38.794 93.248C58.264 67.825 68 49.692 68 38.848 68 22.365 54.57 9 38 9S8 22.364 8 38.85c0 10.842 9.735 28.975 29.206 54.398a1 1 0 0 0 1.588 0z'></path><circle cx='38' cy='39' r='10' fill='currentColor'></circle></g></svg> Oaxaca</span> | ||
</div> | ||
</div> | ||
<div className='m1 mt3 mb2'> | ||
<div className='travel-results-result relative mb1'> | ||
<div className='relative travel-results-result'> | ||
<a className='travel-results-result-link block relative' href='#'> | ||
<amp-img class='block rounded' width='346' height='200' noloading='' src={ travelGlobals.themeUrl + '/imgdiscover-electronic-scene.jpg' } srcset={ travelGlobals.themeUrl + '/img/[email protected] 2x, ' + travelGlobals.themeUrl + '/img/discover-electronic-scene.jpg 1x' }></amp-img> | ||
</a> | ||
<div className='travel-results-result-flags absolute top-0 left-0'> | ||
<div className='p1'><span className='travel-pill bold'>NEW</span></div> | ||
</div> | ||
<div className='travel-results-result-like absolute top-0 right-0'> | ||
<div className='p1'> | ||
<label className='travel-like'> | ||
<input type='checkbox' className='absolute'/> | ||
<div className='travel-like-hearts circle inline-block relative'> | ||
<div className='travel-like-heart-tiny travel-like-heart-solid absolute'></div> | ||
<div className='travel-like-heart-tiny travel-like-heart-solid absolute'></div> | ||
<div className='travel-like-heart-tiny travel-like-heart-solid absolute'></div> | ||
<div className='travel-like-heart travel-like-heart-white absolute mx-auto '></div> | ||
<div className='travel-like-heart travel-like-heart-solid absolute mx-auto '></div> | ||
<div className='travel-like-heart travel-like-heart-outline absolute mx-auto '></div> | ||
</div> | ||
</label> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<div className='h2 line-height-2 mb1'> | ||
<span className='travel-results-result-text'>Discover Oaxaca‘s Electronic Music Scene with a Top DJ</span> | ||
<span className='travel-results-result-subtext h3'>•</span> | ||
<span className='travel-results-result-subtext h3'>$ </span><span className='black bold'>113</span> | ||
</div> | ||
|
||
<div className='h4 line-height-2'> | ||
<div className='inline-block relative mr1 h3 line-height-2'> | ||
<div className='travel-results-result-stars green'>★★★★★</div> | ||
</div> | ||
<span className='travel-results-result-subtext mr1'>42 Reviews</span> | ||
<span className='travel-results-result-subtext'><svg className='travel-icon' viewBox='0 0 77 100'><g fill='none' fillRule='evenodd'><path stroke='currentColor' strokeWidth='7.5' d='M38.794 93.248C58.264 67.825 68 49.692 68 38.848 68 22.365 54.57 9 38 9S8 22.364 8 38.85c0 10.842 9.735 28.975 29.206 54.398a1 1 0 0 0 1.588 0z'></path><circle cx='38' cy='39' r='10' fill='currentColor'></circle></g></svg> Oaxaca</span> | ||
</div> | ||
</div> | ||
<div className='m1 mt3 mb2'> | ||
<div className='travel-popular-tilt-left mb1'> | ||
<div className='relative travel-results-result'> | ||
<a className='travel-results-result-link block relative' href='#'> | ||
<amp-img class='block rounded' width='346' height='200' noloading='' src={ travelGlobals.themeUrl + '/img/skateboard-around-city.jpg' } srcset={ travelGlobals.themeUrl + '/img/[email protected] 2x, ' + travelGlobals.themeUrl + '/img/skateboard-around-city.jpg 1x' }></amp-img> | ||
</a> | ||
<div className='travel-results-result-like absolute top-0 right-0'> | ||
<div className='p1'> | ||
<label className='travel-like'> | ||
<input type='checkbox' className='absolute'/> | ||
<div className='travel-like-hearts circle inline-block relative'> | ||
<div className='travel-like-heart-tiny travel-like-heart-solid absolute'></div> | ||
<div className='travel-like-heart-tiny travel-like-heart-solid absolute'></div> | ||
<div className='travel-like-heart-tiny travel-like-heart-solid absolute'></div> | ||
<div className='travel-like-heart travel-like-heart-white absolute mx-auto '></div> | ||
<div className='travel-like-heart travel-like-heart-solid absolute mx-auto '></div> | ||
<div className='travel-like-heart travel-like-heart-outline absolute mx-auto '></div> | ||
</div> | ||
</label> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<div className='h2 line-height-2 mb1'> | ||
<span className='travel-results-result-text'>Skateboard Around the City</span> | ||
<span className='travel-results-result-subtext h3'>•</span> | ||
<span className='travel-results-result-subtext h3'>$ </span><span className='black bold'>92</span> | ||
</div> | ||
|
||
<div className='h4 line-height-2'> | ||
<div className='inline-block relative mr1 h3 line-height-2'> | ||
<div className='travel-results-result-stars green'>★★★★★</div> | ||
</div> | ||
<span className='travel-results-result-subtext mr1'>17 Reviews</span> | ||
<span className='travel-results-result-subtext'><svg className='travel-icon' viewBox='0 0 77 100'><g fill='none' fillRule='evenodd'><path stroke='currentColor' strokeWidth='7.5' d='M38.794 93.248C58.264 67.825 68 49.692 68 38.848 68 22.365 54.57 9 38 9S8 22.364 8 38.85c0 10.842 9.735 28.975 29.206 54.398a1 1 0 0 0 1.588 0z'></path><circle cx='38' cy='39' r='10' fill='currentColor'></circle></g></svg> Mexico City</span> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</section> | ||
); | ||
}; | ||
const { registerBlockType, RichText } = wp.blocks; | ||
const { Placeholder, withAPIData } = wp.components; | ||
|
||
/** | ||
* Register block. | ||
|
@@ -162,11 +22,77 @@ export default registerBlockType( | |
__( 'Travel' ) | ||
], | ||
|
||
edit() { | ||
return deamplify( renderStaticPopularBlock() ); | ||
}, | ||
edit: withAPIData( () => { | ||
return { | ||
popularPosts: '/wp/v2/adventures?per_page=3&orderby=meta_value_num&meta_key=amp_travel_rating&_embed' | ||
}; | ||
} )( ( { popularPosts } ) => { // eslint-disable-line | ||
const popularPostsCount = 3; | ||
const hasAdventures = Array.isArray( popularPosts.data ) && popularPosts.data.length; | ||
if ( ! hasAdventures || popularPostsCount !== popularPosts.data.length ) { | ||
return ( | ||
<Placeholder key='placeholder' icon='admin-post' label={ __( 'Adventures' ) } > | ||
{ __( 'Not enough adventures with ratings found, add at least 3 to use the block.' ) } | ||
</Placeholder> | ||
); | ||
} | ||
|
||
const adventures = popularPosts.data; | ||
const popularClasses = [ | ||
'travel-popular-tilt-right', | ||
'travel-results-result', | ||
'travel-popular-tilt-left' | ||
]; | ||
|
||
return ( | ||
<section className='travel-popular pb4 pt3 relative'> | ||
<header className='max-width-3 mx-auto px1 md-px2'> | ||
<h3 className='h1 bold line-height-2'>{ __( 'Top Adventures' ) }</h3> | ||
</header> | ||
<div className='overflow-scroll'> | ||
<div className='travel-overflow-container'> | ||
<div className='flex px1 md-px2 mxn1'> | ||
{ adventures.map( ( adventure, i ) => // eslint-disable-line | ||
<div key='adventure' className='m1 mt3 mb2'> | ||
<div className={ popularClasses[ i ] + ' mb1' }> | ||
<div className='relative travel-results-result'> | ||
<a className='travel-results-result-link block relative' href={ adventure.link }> | ||
<img src={ adventure._embedded['wp:featuredmedia'][0].source_url } className='block rounded' width='346' height='200' ></img> | ||
</a> | ||
</div> | ||
</div> | ||
|
||
<div className='h2 line-height-2 mb1'> | ||
<span className='travel-results-result-text'>{ adventure.title.rendered }</span> | ||
<span className='travel-results-result-subtext h3'>•</span> | ||
<span className='travel-results-result-subtext h3'>$ </span><span className='black bold'>{ adventure.meta.amp_travel_price }</span> | ||
</div> | ||
|
||
<div className='h4 line-height-2'> | ||
<div className='inline-block relative mr1 h3 line-height-2'> | ||
<div className='travel-results-result-stars green'> | ||
{ [ ...Array( parseInt( adventure.meta.amp_travel_rating ) ) ].map( () => | ||
'★' | ||
) } | ||
</div> | ||
</div> | ||
<span className='travel-results-result-subtext mr1'>{ adventure.meta.amp_travel_reviews } Reviews</span> | ||
<span className='travel-results-result-subtext'><svg className='travel-icon' viewBox='0 0 77 100'><g fill='none' fillRule='evenodd'><path stroke='currentColor' strokeWidth='7.5' d='M38.794 93.248C58.264 67.825 68 49.692 68 38.848 68 22.365 54.57 9 38 9S8 22.364 8 38.85c0 10.842 9.735 28.975 29.206 54.398a1 1 0 0 0 1.588 0z'></path><circle cx='38' cy='39' r='10' fill='currentColor'></circle></g></svg> | ||
{ adventure.meta.amp_travel_location } | ||
</span> | ||
</div> | ||
</div> | ||
) } | ||
</div> | ||
</div> | ||
</div> | ||
</section> | ||
); | ||
}), | ||
save() { | ||
return renderStaticPopularBlock(); | ||
|
||
// Handled by PHP. | ||
return null; | ||
} | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.