Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event dates block and sort UI #12

Merged
merged 26 commits into from
Dec 15, 2020
Merged
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
05aa6ae
feat: adjust hover/active style of the components-button
thomasguillot Nov 6, 2020
9e3d05f
fix: autocomplete-tokenfield style to match search suggestions
thomasguillot Nov 6, 2020
fdc9e7e
chore: delete unneeded save.js from Curated List block
dkoo Nov 9, 2020
c6fe929
feat: sort UI for curated lists
dkoo Nov 16, 2020
abd1dfc
feat: improved listing item placeholder design
dkoo Nov 23, 2020
854fa3e
feat: better sort options
dkoo Nov 23, 2020
1f2b771
fix: meta handling; add hide author option
dkoo Nov 23, 2020
413b52c
feat: respect "hide author" setting even when list is showing authors
dkoo Nov 24, 2020
afd1200
feat: add date controls to Event listings
dkoo Nov 25, 2020
eb0b1e3
chore: remove unused meta fields
dkoo Nov 25, 2020
c9e3d8e
chore: start event date sort by option (TODO)
dkoo Dec 1, 2020
7266f47
chore: guard against incorrect meta type
dkoo Dec 1, 2020
92e6ba4
feat: create Event Dates block and add as sort option in lists
dkoo Dec 3, 2020
80914aa
fix: date range with times on same day
dkoo Dec 3, 2020
3786cae
chore: keep meta functions together
dkoo Dec 3, 2020
3c6d958
chore: remove unneeded date display in editor
dkoo Dec 3, 2020
fee8fc1
feat: more intelligent event dates in excerpt
dkoo Dec 3, 2020
e21bc05
fix: errors and warnings if endDate is empty
dkoo Dec 3, 2020
a017dc6
fix: show event dates in excerpts in editor, too
dkoo Dec 3, 2020
4a1e160
chore: remove now-redundant template_include method from Blocks class
dkoo Dec 4, 2020
b076e32
fix: unused namespace import
dkoo Dec 4, 2020
5b3f887
fix: prevent AMP tree-shaking from removing sort UI CSS
dkoo Dec 9, 2020
eb89dac
feat: hacky way to prevent focusing the List Container wrapper block
dkoo Dec 9, 2020
b3262e9
fix: hide, don't remove, load more button after getting all posts
dkoo Dec 9, 2020
d2dc44f
fix: block crash when creating a new Specific Listings list
dkoo Dec 10, 2020
597581f
fix: clear floats on Curated List block in editor
dkoo Dec 12, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions amp/curated-list/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
let isFetching = false;
let isEndOfData = false;
buildLoadMoreHandler( document.querySelector( '.newspack-listings__curated-list' ) );
buildSortHandler( document.querySelector( '.newspack-listings__curated-list' ) );
function buildLoadMoreHandler( blockWrapperEl ) {
const btnEl = blockWrapperEl.querySelector( '[data-next]' );
if ( ! btnEl ) return;
Expand Down Expand Up @@ -43,6 +44,59 @@ function buildLoadMoreHandler( blockWrapperEl ) {
}
} );
}
function buildSortHandler( blockWrapperEl ) {
const sortUi = blockWrapperEl.querySelector( '.newspack-listings__sort-ui' );
const sortBy = blockWrapperEl.querySelector( '.newspack-listings__sort-select-control' );
const sortOrder = blockWrapperEl.querySelectorAll( 'input' );
const btnEl = blockWrapperEl.querySelector( '[data-next]' );
if ( ! sortBy || ! sortOrder.length || ! sortUi ) return;
const triggers = Array.prototype.concat.call( Array.prototype.slice.call( sortOrder ), [
sortBy,
] );
const postsContainerEl = blockWrapperEl.querySelector( '.newspack-listings__list-container' );
const restURL = sortUi.getAttribute( 'data-url' );
let isFetching = false;
let _sortBy = sortUi.querySelector( '[selected]' ).value;
let _order = sortUi.querySelector( '[checked]' ).value;
const sortHandler = e => {
if ( isFetching ) return false;
isFetching = true;
blockWrapperEl.classList.remove( 'is-error' );
blockWrapperEl.classList.add( 'is-loading' );
if ( e.target.tagName.toLowerCase() === 'select' ) {
_sortBy = e.target.value;
} else {
_order = e.target.value;
}
if ( 'post__in' === e.target.value ) {
Array.prototype.forEach.call( sortOrder, button => button.setAttribute( 'disabled' ) );
} else {
Array.prototype.forEach.call( sortOrder, button => button.removeAttribute( 'disabled' ) );
}
const requestURL = `${ restURL }&${ encodeURIComponent(
'query[sortBy]'
) }=${ _sortBy }&${ encodeURIComponent( 'query[order]' ) }=${ _order }`;
apiFetchWithRetry( { url: requestURL, onSuccess, onError }, 3 );
function onSuccess( data, next ) {
if ( ! isPostsDataValid( data ) ) return onError();
postsContainerEl.textContent = '';
data.forEach( item => {
const tempDIV = document.createElement( 'div' );
tempDIV.innerHTML = item.html.trim();
postsContainerEl.appendChild( tempDIV.childNodes[ 0 ] );
} );
if ( next && btnEl ) btnEl.setAttribute( 'data-next', next );
isFetching = false;
blockWrapperEl.classList.remove( 'is-loading' );
}
function onError() {
isFetching = false;
blockWrapperEl.classList.remove( 'is-loading' );
blockWrapperEl.classList.add( 'is-error' );
}
};
triggers.forEach( trigger => trigger.addEventListener( 'change', sortHandler ) );
}
function apiFetchWithRetry( options, n ) {
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = () => {
Expand Down
20 changes: 15 additions & 5 deletions includes/class-newspack-listings-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,17 @@ public static function build_listings_query( $query, $args = [] ) {
$args['order'] = $query['order'];
}
if ( ! empty( $query['sortBy'] ) ) {
$args['orderby'] = $query['sortBy'];
if ( 'event_date' === $query['sortBy'] ) {
$args['orderby'] = 'meta_value';
$args['meta_type'] = 'DATE';
$args['meta_key'] = 'newspack_listings_event_start_date';
} else {
$args['orderby'] = $query['sortBy'];
}
}

if ( ! empty( $query['post__in'] ) ) {
$args['post__in'] = $query['post__in'];
}

if (
Expand Down Expand Up @@ -231,7 +241,7 @@ function( $post ) use ( $attributes, $fields, $is_amp, $next_page, $query ) {

// if $fields includes html, get rendered HTML for the post.
if ( in_array( 'html', $fields ) && ! empty( $attributes ) ) {
$html = Blocks::template_include(
$html = Utils\template_include(
'listing',
[
'attributes' => $attributes,
Expand All @@ -257,8 +267,8 @@ function( $post ) use ( $attributes, $fields, $is_amp, $next_page, $query ) {
$item['tags'] = get_the_terms( $post->ID, Core::NEWSPACK_LISTINGS_TAG );
}

// If $fields includes author, get the post author.
if ( in_array( 'author', $fields ) ) {
// If $fields includes author and the post isn't set to hide author, get the post author.
if ( in_array( 'author', $fields ) && empty( get_post_meta( $post->ID, 'newspack_listings_hide_author', true ) ) ) {
$item['author'] = get_the_author_meta( 'display_name', $post->post_author );
}

Expand Down Expand Up @@ -311,7 +321,7 @@ function( $post ) use ( $attributes, $fields, $is_amp, $next_page, $query ) {
}

if ( ! empty( $next_url ) ) {
$response->header( 'next-url', esc_url( $next_url ) );
$response->header( 'next-url', $next_url );
}

return $response;
Expand Down
26 changes: 2 additions & 24 deletions includes/class-newspack-listings-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,15 @@ public static function manage_editor_assets() {
'post_type_label' => get_post_type_object( $post_type )->labels->singular_name,
'post_type' => $post_type,
'post_types' => $post_types,
'meta_fields' => Core::get_meta_fields( $post_type ),
'taxonomies' => [
'category' => Core::NEWSPACK_LISTINGS_CAT,
'tag' => Core::NEWSPACK_LISTINGS_TAG,
],

// If we don't have ANY listings that can be added to a list yet, alert the editor so we can show messaging.
'no_listings' => 0 === $total_count,
'date_format' => get_option( 'date_format' ),
'time_format' => get_option( 'time_format' ),
]
);

Expand Down Expand Up @@ -237,29 +238,6 @@ public static function register_block_patterns() {
}
}
}

/**
* Loads a template with given data in scope.
*
* @param string $template Name of the template to be included.
* @param array $data Data to be passed into the template to be included.
* @param string $path (Optional) Path to the folder containing the template.
* @return string
*/
public static function template_include( $template, $data = [], $path = NEWSPACK_LISTINGS_PLUGIN_FILE . 'src/templates/' ) {
if ( ! strpos( $template, '.php' ) ) {
$template = $template . '.php';
}
$path .= $template;
if ( ! is_file( $path ) ) {
return '';
}
ob_start();
include $path;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
}

Newspack_Listings_Blocks::instance();
Loading