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

[RNMobile] Latest Posts Block v1 Support in Mobile #20301

Merged
merged 42 commits into from
Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
10c612d
Display basic support block for Latest Posts
chipsnyder Feb 12, 2020
cab1265
Update UI of Latest Posts Placeholder
chipsnyder Feb 12, 2020
03cc326
Add Post Content Control to the Latest post config menu
chipsnyder Feb 12, 2020
87ecea7
Add support for Post Content Settings and Post Meta Data settings
chipsnyder Feb 14, 2020
de24d67
Expose QueryControls to Mobile
chipsnyder Feb 14, 2020
e47bd1d
Add Post Sorting and options for Latest Post Block
chipsnyder Feb 14, 2020
b9061fa
Refactor LatestPost to allow hooks in component mounting for api requ…
chipsnyder Feb 14, 2020
7909604
Fix Style issue
chipsnyder Feb 14, 2020
10fc0b3
Add Network call on Latest-Posts to fetch categories
chipsnyder Feb 17, 2020
ca93565
Reorder Options in Latest Posts
chipsnyder Feb 17, 2020
d5298dc
Show configure menu in Latests Posts on focus and on tap of configure
chipsnyder Feb 17, 2020
ba029e0
Reorganize consts
chipsnyder Feb 17, 2020
88fb931
Reorganize consts
chipsnyder Feb 17, 2020
5df8293
Mmove web refrences to use defined constants
chipsnyder Feb 17, 2020
5946ec7
Merge remote-tracking branch 'origin/master' into rnmobile/issue-1746…
chipsnyder Feb 18, 2020
443f890
Reorder Latest Post Settings to match Web
chipsnyder Feb 18, 2020
047a63c
Adjust setting for Categories selection
chipsnyder Feb 18, 2020
6e7cc90
Fix Prettier issue
chipsnyder Feb 18, 2020
00f6f00
Resolve PHP style issue
chipsnyder Feb 18, 2020
464980a
Adjust data parsing for Android
chipsnyder Feb 19, 2020
ffdf99b
Adjust the way of identifying the way to parse categoriesList
chipsnyder Feb 19, 2020
2860f17
Remove Clear Settings and rename the configure button to customize
chipsnyder Feb 20, 2020
6e4a4aa
Remove function that automatically prompts the settings when tapping …
chipsnyder Feb 20, 2020
bd0ed3d
Implement text and control changes from design feedback
chipsnyder Feb 20, 2020
eb076f3
Adjust separators for Latest Post
chipsnyder Feb 20, 2020
28df7ec
Adjust latest-post post content label to be more excerpt centric, and…
chipsnyder Feb 24, 2020
3a92495
Update label of excerpt length on latest-posts
chipsnyder Feb 24, 2020
a5f46eb
Merge remote-tracking branch 'origin/master' into rnmobile/issue-1746…
chipsnyder Feb 24, 2020
dffeecc
Adjust casing of customize label
chipsnyder Feb 24, 2020
bdc6698
Align customize style with add media empty state
chipsnyder Feb 24, 2020
8b05644
Fix style issue
chipsnyder Feb 24, 2020
202f08f
Merge remote-tracking branch 'origin/master' into rnmobile/issue-1746…
chipsnyder Feb 26, 2020
858f2cf
Move set attribute events out of the render function
chipsnyder Feb 26, 2020
842ee12
Add developer comment for platform differences
chipsnyder Feb 27, 2020
debfdf1
Refactor syntax on set functions
chipsnyder Feb 27, 2020
76a3f4a
Modify inspector controls to follow the style of the attribute setters
chipsnyder Feb 27, 2020
7793ad7
Update design of block title
chipsnyder Feb 27, 2020
f7e2bbb
Update spacing around block
chipsnyder Feb 27, 2020
af72d61
Add type checking to block non array responses from categoreis request
chipsnyder Feb 28, 2020
24fa26f
Merge branch 'master' into rnmobile/issue-1746-latest-post
chipsnyder Mar 11, 2020
a0f99b0
Merge remote-tracking branch 'origin/master' into rnmobile/issue-1746…
chipsnyder Mar 11, 2020
5d511e7
Merge remote-tracking branch 'origin/master' into rnmobile/issue-1746…
chipsnyder Mar 11, 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
1 change: 1 addition & 0 deletions packages/block-library/src/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export const registerCoreBlocks = () => {
button,
spacer,
shortcode,
latestPosts,
geriux marked this conversation as resolved.
Show resolved Hide resolved
devOnly( verse ),
cover,
].forEach( registerBlock );
Expand Down
72 changes: 72 additions & 0 deletions packages/block-library/src/latest-posts/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"name": "core/latest-posts",
"category": "widgets",
"attributes": {
"align": {
"type": "string",
"enum": ["left", "center", "right", "wide", "full"]
},
"className": {
"type": "string"
},
"categories": {
"type": "string"
},
"postsToShow": {
"type": "number",
"default": 5
},
"displayPostContent": {
"type": "boolean",
"default": false
},
"displayPostContentRadio": {
"type": "string",
"default": "excerpt"
},
"excerptLength": {
"type": "number",
"default": 55
},
"displayPostDate": {
"type": "boolean",
"default": false
},
"postLayout": {
"type": "string",
"default": "list"
},
"columns": {
"type": "number",
"default": 3
},
"order": {
"type": "string",
"default": "desc"
},
"orderBy": {
"type": "string",
"default": "date"
},
"displayFeaturedImage": {
"type": "boolean",
"default": false
},
"featuredImageAlign": {
"type": "string",
"enum": ["left", "center", "right"]
},
"featuredImageSizeSlug": {
"type": "string",
"default":"thumbnail"
},
"featuredImageSizeWidth": {
"type": "number",
"default":null
},
"featuredImageSizeHeight": {
"type": "number",
"default":null
}
}
}
3 changes: 3 additions & 0 deletions packages/block-library/src/latest-posts/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const MIN_EXCERPT_LENGTH = 10;
export const MAX_EXCERPT_LENGTH = 100;
export const MAX_POSTS_COLUMNS = 6;
14 changes: 11 additions & 3 deletions packages/block-library/src/latest-posts/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,21 @@ import {
import { withSelect } from '@wordpress/data';
import { pin, list, grid } from '@wordpress/icons';

/**
* Internal dependencies
*/
import {
MIN_EXCERPT_LENGTH,
MAX_EXCERPT_LENGTH,
MAX_POSTS_COLUMNS,
} from './constants';

/**
* Module Constants
*/
const CATEGORIES_LIST_QUERY = {
per_page: -1,
};
const MAX_POSTS_COLUMNS = 6;

class LatestPostsEdit extends Component {
constructor() {
Expand Down Expand Up @@ -133,8 +141,8 @@ class LatestPostsEdit extends Component {
onChange={ ( value ) =>
setAttributes( { excerptLength: value } )
}
min={ 10 }
max={ 100 }
min={ MIN_EXCERPT_LENGTH }
max={ MAX_EXCERPT_LENGTH }
/>
) }
</PanelBody>
Expand Down
254 changes: 254 additions & 0 deletions packages/block-library/src/latest-posts/edit.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,254 @@
/**
* External dependencies
*/
import { TouchableWithoutFeedback, View, Text } from 'react-native';

/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { compose, withPreferredColorScheme } from '@wordpress/compose';
import { withDispatch } from '@wordpress/data';
import { coreBlocks } from '@wordpress/block-library';
import { __ } from '@wordpress/i18n';
import { postList as icon } from '@wordpress/icons';
import { InspectorControls } from '@wordpress/block-editor';
import { fetchRequest } from 'react-native-gutenberg-bridge';
Copy link
Contributor

Choose a reason for hiding this comment

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

This shouldn't be used directly and instead, it should use the GB apiFecth interface. This will provide support for retries and cache of the data.

The only thing needed is to add the /wp/v2/categories to the white list here: https://github.com/wordpress-mobile/gutenberg-mobile/blob/e288c45e882db9808e0396715f379edb07f32416/src/api-fetch-setup.js#L44

@chipsnyder if you need help please ping me or @etoledom.

Copy link
Contributor Author

@chipsnyder chipsnyder Mar 19, 2020

Choose a reason for hiding this comment

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

Thanks @SergioEstevao! I'll make a PR either today and push it up.

import {
Icon,
PanelBody,
ToggleControl,
RangeControl,
QueryControls,
} from '@wordpress/components';

/**
* Internal dependencies
*/
import styles from './style.scss';
import { MIN_EXCERPT_LENGTH, MAX_EXCERPT_LENGTH } from './constants';

class LatestPostsEdit extends Component {
constructor() {
super( ...arguments );
this.state = {
categoriesList: [],
};
this.onSetDisplayPostContent = this.onSetDisplayPostContent.bind(
this
);
this.onSetDisplayPostContentRadio = this.onSetDisplayPostContentRadio.bind(
this
);
this.onSetExcerptLength = this.onSetExcerptLength.bind( this );
this.onSetDisplayPostDate = this.onSetDisplayPostDate.bind( this );
this.onSetOrder = this.onSetOrder.bind( this );
this.onSetOrderBy = this.onSetOrderBy.bind( this );
this.onSetPostsToShow = this.onSetPostsToShow.bind( this );
this.onSetCategories = this.onSetCategories.bind( this );
this.getInspectorControls = this.getInspectorControls.bind( this );
}

componentDidMount() {
this.isStillMounted = true;
this.fetchRequest = fetchRequest( '/wp/v2/categories' )
.then( ( categoriesList ) => {
if ( this.isStillMounted ) {
// TODO: remove this check after `fetchRequest` types are made consist across platforms
// (see: https://github.com/wordpress-mobile/gutenberg-mobile/issues/1961)
if ( typeof categoriesList === 'string' ) {
mkevins marked this conversation as resolved.
Show resolved Hide resolved
this.setState( {
categoriesList: JSON.parse( categoriesList ),
} );
} else {
this.setState( { categoriesList } );
}
}
} )
.catch( () => {
if ( this.isStillMounted ) {
this.setState( { categoriesList: [] } );
}
} );
}

componentWillUnmount() {
this.isStillMounted = false;
}

onSetDisplayPostContent( value ) {
const { setAttributes } = this.props;
setAttributes( { displayPostContent: value } );
}

onSetDisplayPostContentRadio( value ) {
const { setAttributes } = this.props;
setAttributes( {
displayPostContentRadio: value ? 'excerpt' : 'full_post',
} );
}

onSetExcerptLength( value ) {
const { setAttributes } = this.props;
setAttributes( { excerptLength: value } );
}

onSetDisplayPostDate( value ) {
const { setAttributes } = this.props;
setAttributes( { displayPostDate: value } );
}

onSetOrder( value ) {
const { setAttributes } = this.props;
setAttributes( { order: value } );
}

onSetOrderBy( value ) {
const { setAttributes } = this.props;
setAttributes( { orderBy: value } );
}

onSetPostsToShow( value ) {
const { setAttributes } = this.props;
setAttributes( { postsToShow: value } );
}

onSetCategories( value ) {
const { setAttributes } = this.props;
setAttributes( {
categories: '' !== value ? value.toString() : undefined,
} );
}

getInspectorControls() {
const { attributes } = this.props;
const {
displayPostContent,
displayPostContentRadio,
excerptLength,
displayPostDate,
order,
orderBy,
postsToShow,
categories,
} = attributes;

const { categoriesList } = this.state;
const displayExcerptPostContent = displayPostContentRadio === 'excerpt';

return (
<InspectorControls>
<PanelBody title={ __( 'Post content settings' ) }>
<ToggleControl
label={ __( 'Show post content' ) }
checked={ displayPostContent }
onChange={ this.onSetDisplayPostContent }
separatorType={
displayPostContent ? 'fullWidth' : 'none'
}
/>
{ displayPostContent && (
<ToggleControl
label={ __( 'Only show excerpt' ) }
checked={ displayExcerptPostContent }
onChange={ this.onSetDisplayPostContentRadio }
separatorType={
displayExcerptPostContent ? 'fullWidth' : 'none'
}
/>
) }
{ displayPostContent && displayExcerptPostContent && (
<RangeControl
label={ __( 'Excerpt length (words)' ) }
value={ excerptLength }
onChange={ this.onSetExcerptLength }
min={ MIN_EXCERPT_LENGTH }
max={ MAX_EXCERPT_LENGTH }
separatorType="none"
/>
) }
</PanelBody>

<PanelBody title={ __( 'Post meta settings' ) }>
<ToggleControl
label={ __( 'Display post date' ) }
checked={ displayPostDate }
onChange={ this.onSetDisplayPostDate }
separatorType="none"
/>
</PanelBody>

<PanelBody title={ __( 'Sorting and filtering' ) }>
<QueryControls
{ ...{ order, orderBy } }
numberOfItems={ postsToShow }
categoriesList={ categoriesList }
selectedCategoryId={
undefined !== categories ? Number( categories ) : ''
}
onOrderChange={ this.onSetOrder }
onOrderByChange={ this.onSetOrderBy }
onCategoryChange={ this.onSetCategories }
onNumberOfItemsChange={ this.onSetPostsToShow }
/>
</PanelBody>
</InspectorControls>
);
}

render() {
const {
getStylesFromColorScheme,
name,
openGeneralSidebar,
isSelected,
} = this.props;

const blockType = coreBlocks[ name ];

const blockStyle = getStylesFromColorScheme(
styles.latestPostBlock,
styles.latestPostBlockDark
);

const iconStyle = getStylesFromColorScheme(
styles.latestPostBlockIcon,
styles.latestPostBlockIconDark
);

const titleStyle = getStylesFromColorScheme(
styles.latestPostBlockMessage,
styles.latestPostBlockMessageDark
);

return (
<TouchableWithoutFeedback
accessible={ ! isSelected }
disabled={ ! isSelected }
onPress={ openGeneralSidebar }
>
<View style={ blockStyle }>
{ this.getInspectorControls() }
<Icon icon={ icon } { ...iconStyle } />
<Text style={ titleStyle }>
{ blockType.settings.title }
</Text>
<Text style={ styles.latestPostBlockSubtitle }>
{ __( 'CUSTOMIZE' ) }
</Text>
</View>
</TouchableWithoutFeedback>
);
}
}

export default compose( [
withDispatch( ( dispatch ) => {
const { openGeneralSidebar } = dispatch( 'core/edit-post' );

return {
openGeneralSidebar: () => openGeneralSidebar( 'edit-post/block' ),
};
} ),
withPreferredColorScheme,
] )( LatestPostsEdit );
5 changes: 3 additions & 2 deletions packages/block-library/src/latest-posts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import { postList as icon } from '@wordpress/icons';
* Internal dependencies
*/
import edit from './edit';
import metadata from './block.json';

export const name = 'core/latest-posts';
const { name } = metadata;
export { metadata, name };

export const settings = {
title: __( 'Latest Posts' ),
description: __( 'Display a list of your most recent posts.' ),
icon,
category: 'widgets',
keywords: [ __( 'recent posts' ) ],
supports: {
align: true,
Expand Down
Loading