|
| 1 | +/** |
| 2 | + * Copyright 2020 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * External dependencies |
| 19 | + */ |
| 20 | +import { get, isUndefined, pickBy } from 'lodash'; |
| 21 | + |
| 22 | +/** |
| 23 | + * WordPress dependencies |
| 24 | + */ |
| 25 | +import { createHigherOrderComponent } from '@wordpress/compose'; |
| 26 | +import { withSelect } from '@wordpress/data'; |
| 27 | +import { useEffect, useState } from '@wordpress/element'; |
| 28 | + |
| 29 | +/** |
| 30 | + * Internal dependencies |
| 31 | + */ |
| 32 | +import './style.css'; |
| 33 | +import NoPosts from './components/no-posts'; |
| 34 | +import PostsList from './components/posts-list'; |
| 35 | +import { getPosts } from '../../utils/api'; |
| 36 | + |
| 37 | +/** |
| 38 | + * Edit component. |
| 39 | + * |
| 40 | + * @param {Object} props - Component props. |
| 41 | + * |
| 42 | + * @return {Function} A functional component. |
| 43 | + */ |
| 44 | +const Edit = props => { |
| 45 | + const { postsToDisplay } = props; |
| 46 | + const hasPosts = Array.isArray( postsToDisplay ) && postsToDisplay.length; |
| 47 | + |
| 48 | + if ( ! hasPosts ) { |
| 49 | + return <NoPosts { ...props } />; |
| 50 | + } |
| 51 | + |
| 52 | + return <PostsList { ...props } />; |
| 53 | +}; |
| 54 | + |
| 55 | +/** |
| 56 | + * @type {Function} A functional component. |
| 57 | + */ |
| 58 | +const EditWithSelect = withSelect( ( select, props ) => { |
| 59 | + const { |
| 60 | + category, |
| 61 | + postsToShow, |
| 62 | + posts, |
| 63 | + orderby, |
| 64 | + displayFeaturedImage, |
| 65 | + style, |
| 66 | + } = props.attributes; |
| 67 | + const { name } = props; |
| 68 | + let { fetchedPosts } = props; |
| 69 | + |
| 70 | + const featuredImageSizeSlug = style === 'list' ? 'medium' : 'large'; |
| 71 | + |
| 72 | + const { getEntityRecords, getMedia } = select( 'core' ); |
| 73 | + |
| 74 | + let queryArgs = { |
| 75 | + categories: category, |
| 76 | + per_page: postsToShow, |
| 77 | + }; |
| 78 | + |
| 79 | + if ( name === 'material/hand-picked-posts' ) { |
| 80 | + queryArgs = { |
| 81 | + include: posts.map( Number ), |
| 82 | + per_page: posts.length, |
| 83 | + orderby: 'date', |
| 84 | + order: 'desc', |
| 85 | + }; |
| 86 | + |
| 87 | + if ( orderby ) { |
| 88 | + if ( orderby === 'title' ) { |
| 89 | + queryArgs.orderby = 'title'; |
| 90 | + queryArgs.order = 'asc'; |
| 91 | + } else if ( orderby === 'popularity' ) { |
| 92 | + queryArgs.orderby = 'comment_count'; |
| 93 | + queryArgs.order = 'desc'; |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + const fetchedPostsQuery = pickBy( |
| 99 | + queryArgs, |
| 100 | + value => ! isUndefined( value ) |
| 101 | + ); |
| 102 | + |
| 103 | + fetchedPosts = Array.isArray( fetchedPosts ) |
| 104 | + ? fetchedPosts |
| 105 | + : getEntityRecords( 'postType', 'post', fetchedPostsQuery ); |
| 106 | + |
| 107 | + return { |
| 108 | + postsToDisplay: ! Array.isArray( fetchedPosts ) |
| 109 | + ? fetchedPosts |
| 110 | + : fetchedPosts.map( post => { |
| 111 | + if ( displayFeaturedImage && post.featured_media ) { |
| 112 | + const image = getMedia( post.featured_media ); |
| 113 | + let url = get( |
| 114 | + image, |
| 115 | + [ 'media_details', 'sizes', featuredImageSizeSlug, 'source_url' ], |
| 116 | + null |
| 117 | + ); |
| 118 | + if ( ! url ) { |
| 119 | + url = get( image, 'source_url', null ); |
| 120 | + } |
| 121 | + return { ...post, featuredImageSourceUrl: url }; |
| 122 | + } |
| 123 | + return post; |
| 124 | + } ), |
| 125 | + }; |
| 126 | +} )( Edit ); |
| 127 | + |
| 128 | +export default EditWithSelect; |
| 129 | + |
| 130 | +const withGetPosts = createHigherOrderComponent( WrappedComponent => { |
| 131 | + return props => { |
| 132 | + const { category, posts, orderby, postType } = props.attributes; |
| 133 | + |
| 134 | + const queryArgs = { |
| 135 | + categories: category, |
| 136 | + include: posts.map( Number ), |
| 137 | + per_page: posts.length, |
| 138 | + orderby: 'date', |
| 139 | + order: 'desc', |
| 140 | + }; |
| 141 | + |
| 142 | + if ( orderby ) { |
| 143 | + if ( orderby === 'title' ) { |
| 144 | + queryArgs.orderby = 'title'; |
| 145 | + queryArgs.order = 'asc'; |
| 146 | + } else if ( orderby === 'popularity' ) { |
| 147 | + queryArgs.orderby = 'comment_count'; |
| 148 | + queryArgs.order = 'desc'; |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + const [ postsToDisplay, setPostsToDisplay ] = useState( [] ); |
| 153 | + |
| 154 | + useEffect( () => { |
| 155 | + getPosts( { selected: posts, postType, queryArgs } ) |
| 156 | + .then( data => { |
| 157 | + setPostsToDisplay( data ); |
| 158 | + } ) |
| 159 | + .catch( e => { |
| 160 | + console.error( e ); |
| 161 | + } ); |
| 162 | + }, [] ); // eslint-disable-line react-hooks/exhaustive-deps |
| 163 | + |
| 164 | + return <WrappedComponent { ...props } fetchedPosts={ postsToDisplay } />; |
| 165 | + }; |
| 166 | +}, 'withGetPosts' ); |
| 167 | + |
| 168 | +export const EditWithGetPosts = withGetPosts( EditWithSelect ); |
0 commit comments