Skip to content
This repository has been archived by the owner on Jan 10, 2023. It is now read-only.

[Gutenberg] Add "Featured Destinations" homepage block #34

Merged
merged 16 commits into from
Apr 17, 2018

Conversation

miina
Copy link
Contributor

@miina miina commented Apr 6, 2018

Based on #12 and #31.

Aims to do the following:

  • Pull data dynamically to editor.
  • Sorts and renders the terms according to the uploaded images.
  • Register and add relevant meta to location term.

@miina
Copy link
Contributor Author

miina commented Apr 6, 2018

@DavidCramer Started adding the featured block within this commit: 9f745ae
Basically just registered the block and meta for amp_travel_featured and enabled fetching the terms via API by the value of the meta. Also added the logic for when there are not enough featured locations found.

What's missing:

  • Rendering for when there are enough featured locations found (Completely missing for frontend in PHP and partly missing in JS for editor);
  • Adding image-related metafield(s) for the user to upload the image;
  • Location term field to mark it being Featured;
  • Sorting the images for putting together the grid.

@miina miina changed the title [WIP] [Gutenberg] Add "Featured Destinations" homepage block [Gutenberg] Add "Featured Destinations" homepage block Apr 13, 2018
@miina miina requested a review from kienstra April 13, 2018 13:10
Copy link
Contributor

@kienstra kienstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice Work
Some Points Below

Hi @miina,
Good work here. Especially in setting up the grid:

featured-destinations-grid

<div class='form-field form-required location-cover-image-wrap'>
<label for='location-cover-image'><?php esc_html_e( 'Activity Cover Image' ); ?></label>
<input type='hidden' id='location-cover-image-value' class='small-text' name='location-cover-image-value' value='' />
<input type='button' id='location-cover-image' class='button location-cover-image-upload-button' value='<?php esc_html_e( 'Upload' ); ?>' />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could the "Upload" text for the button instead be something like "Select"? It's also possible to select an image from the media library.

upload-image-button

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the new "Select" text looks good.


} );

component.removeButton.on( 'click', function( event ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you have time, could you display this component.removeButton only if there's no selected image? But I'd call this a "nice to have."

It could be confusing to have a "Remove" button if there's no image to remove.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making this change, @miina. This works well.

/**
* Init.
*/
component.init = function init() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the selected image is persisting even after saving a new "Location"

  1. Go to /wp-admin/edit-tags.php?taxonomy=location
  2. Fill in all of the fields, and select an image

adding-new-location

  1. Click "Add New Location"
  2. Expected: the selected image doesn't display anymore, as the rest of the fields are empty
  3. Actual: the selected image still displays:

image-still-appears

But the workflow looks fine when editing a location:

edit-location

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this.

featuredLocations: '/wp/v2/locations?per_page=6&meta_value=1&meta_key=amp_travel_featured'
};
} )( ( { featuredLocations } ) => { // eslint-disable-line
const hasLocations = Array.isArray( featuredLocations.data ) && 6 === featuredLocations.data.length;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about making this

featuredLocations.data.length >= 6

instead of:

6 === featuredLocations.data.length

It might be easy for someone to change AMP_Travel_Blocks::$featured_locations_count, without noticing that this line requires it to be exactly 6.

*/
public function add_location_meta_fields() {
?>
<div class='form-field form-required location-cover-image-wrap'>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this method please use double quotes for the attributes, like:

<div class="form-field form-required location-cover-image-wrap">

And likewise for edit_location_meta_fields()

<td>
<?php wp_nonce_field( basename( __FILE__ ), 'travel_location_meta_nonce' ); ?>

<input type='hidden' id='location-cover-image-value' class='small-text' name='location-cover-image-value' value='<?php esc_attr( $cover_img_id ); ?>' />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<?php esc_attr( $cover_img_id ); ?>

This could probably use an echo at the beginning:

<?php echo esc_attr( $cover_img_id ); ?>

$location_img_srcset = wp_get_attachment_image_srcset( $location_img_id, 'full' );

$output .= '<a href="' . esc_url( get_term_link( $location['term_id'] ) ) . '" class="travel-featured-tile flex flex-auto relative travel-featured-color-' .
esc_html( $location_params[ $i ]['color'] ) . '">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

esc_html( $location_params[ $i ]['color'] 

Could this use esc_attr() instead, as this is for an attribute (class)

esc_html( $location_params[ $i ]['color'] ) . '">
<amp-img class="travel-object-cover flex-auto" layout="responsive" width="' .
esc_html( $location_params[ $i ]['width'] ) . '" height="' .
esc_html( $location_params[ $i ]['height'] ) . '" srcset="' . esc_html( $location_img_srcset ) . '" src="' . esc_url( $location_img_src[0] ) . '""></amp-img>
Copy link
Contributor

@kienstra kienstra Apr 13, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this line and the line above use esc_attr() for all of the attributes? But maybe there was an issue in using esc_attr() with the srcset.

icon="admin-post"
label={ __( 'Locations' ) }
>
{ __( 'Not enough featured locations found, add at least six to use the block.' ) }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be easier to use if the message mentioned "Location" terms, maybe like:

Not enough featured locations found. Please add at least six "Locations" terms, select an image, and check "Featured destination."

*
* @var int
*/
public static $featured_locations_count = 6;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class is instantiated, so would there be a need to make this static? If not, could you please remove static, and change this to something like:

const FEATURED_LOCATIONS_COUNT = 6;

Then, references to it could change from self::$featured_locations_count to self::FEATURED_LOCATIONS_COUNT

This is outside the diff of this PR, but could you please do the same thing to $popular_posts_count below?

@kienstra
Copy link
Contributor

kienstra commented Apr 14, 2018

Editor Styling

This is probably outside the scope of this PR. But we might revisit the styling of this block in the editor. The images appear much taller than on the front-end.

@miina
Copy link
Contributor Author

miina commented Apr 16, 2018

@kienstra Thanks for all the found issues! These should be addressed now, also improved the Gutenberg styling a little bit.

@kienstra
Copy link
Contributor

kienstra commented Apr 16, 2018

Approved, Other Than Merge Conflicts

Hi @miina,
Thanks for all of your work here. And thanks improving the styling of this "Featured Destinations" block in the editor.

Could you please resolve these merge conflicts? I caused them by merging in #16.

I tried to resolve them on my own, but it looks like editor-blocks.js will need to be rebuilt. And this command didn't seem to rebuild it properly:

npm run build

I probably missed something 😄

Thanks!

@miina
Copy link
Contributor Author

miina commented Apr 17, 2018

@kienstra, Merged develop & resolved conflicts, sorry for not noticing the conflicts before.

miina and others added 2 commits April 17, 2018 09:30
Before, there was an error in Gutenberg,
as renderStaticFeaturedBlock() wasn't defined.
This PR removed that function.
So change save() to return null,
as this looks to be a dynamic block.
AMP_Travel_Blocks::render_block_travel_featured()
renders the output on the front-end.
Also, compile js/editor-blocks.js, using:
npm run build.
@kienstra
Copy link
Contributor

Merging, Request For Sanity Check

Hi @miina,
Thanks, this block looks great. No need to apologize for not noticing the merge conflicts. I introduced them after your work day.

Could you please sanity-check this commit? I changed the save() function to return null. And I recompiled editor-blocks.js with npm run build.

@kienstra kienstra merged commit 8de32dd into develop Apr 17, 2018
@miina
Copy link
Contributor Author

miina commented Apr 18, 2018

Hey @kienstra, the commit you did looks good, thanks for that!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants