content.js
1 year ago
export.js
1 year ago
header.js
1 year ago
template.js
5 years ago
templates-content.js
1 year ago
templates-content.js
185 lines
| 1 | import useInfiniteScroll from 'react-infinite-scroll-hook'; |
| 2 | import classnames from 'classnames'; |
| 3 | import { Button, Placeholder, Spinner } from '@wordpress/components'; |
| 4 | import { withSelect } from '@wordpress/data'; |
| 5 | import { Fragment, useState } from '@wordpress/element'; |
| 6 | import { __ } from '@wordpress/i18n'; |
| 7 | |
| 8 | import Template from './template.js'; |
| 9 | |
| 10 | import { |
| 11 | fetchTemplates, |
| 12 | fetchLibrary, |
| 13 | } from './../data/templates-cloud/index.js'; |
| 14 | |
| 15 | const sortByOptions = { |
| 16 | template_name: window.tiTpc.library.filters.sortLabels.name, |
| 17 | date: window.tiTpc.library.filters.sortLabels.date, |
| 18 | modified: window.tiTpc.library.filters.sortLabels.modified, |
| 19 | }; |
| 20 | |
| 21 | const TemplatesContent = ( { |
| 22 | getSearchQuery, |
| 23 | getOrder, |
| 24 | setSorting, |
| 25 | onImport, |
| 26 | onUpdateTemplate, |
| 27 | onDelete, |
| 28 | onDuplicate, |
| 29 | isGeneral, |
| 30 | items, |
| 31 | currentPage, |
| 32 | totalPages, |
| 33 | } ) => { |
| 34 | const [ isLoading, setLoading ] = useState( false ); |
| 35 | |
| 36 | const onLoadMore = async () => { |
| 37 | if ( currentPage === totalPages ) { |
| 38 | return; |
| 39 | } |
| 40 | |
| 41 | setLoading( true ); |
| 42 | |
| 43 | const order = getOrder(); |
| 44 | if ( isGeneral ) { |
| 45 | await fetchTemplates( { |
| 46 | search: getSearchQuery(), |
| 47 | page: currentPage + 1, |
| 48 | isScroll: true, |
| 49 | ...order, |
| 50 | } ); |
| 51 | } else { |
| 52 | await fetchLibrary( { |
| 53 | search: getSearchQuery(), |
| 54 | page: currentPage + 1, |
| 55 | isScroll: true, |
| 56 | ...order, |
| 57 | } ); |
| 58 | } |
| 59 | setLoading( false ); |
| 60 | }; |
| 61 | |
| 62 | const infiniteRef = useInfiniteScroll( { |
| 63 | loading: isLoading, |
| 64 | hasNextPage: currentPage !== totalPages, |
| 65 | onLoadMore, |
| 66 | threshold: 1, |
| 67 | } ); |
| 68 | |
| 69 | return ( |
| 70 | <div |
| 71 | className={ classnames( |
| 72 | 'ti-tpc-template-library-templates-container', |
| 73 | { |
| 74 | 'is-table': ! isGeneral, |
| 75 | } |
| 76 | ) } |
| 77 | ref={ infiniteRef } |
| 78 | > |
| 79 | { isGeneral ? ( |
| 80 | items.map( ( item ) => ( |
| 81 | <Template |
| 82 | key={ item.template_id } |
| 83 | item={ item } |
| 84 | id={ item.template_id } |
| 85 | title={ item.template_name } |
| 86 | thumbnail={ item.template_thumbnail } |
| 87 | onImport={ onImport } |
| 88 | /> |
| 89 | ) ) |
| 90 | ) : ( |
| 91 | <Fragment> |
| 92 | <div className="ti-tpc-template-library-templates-table-header"> |
| 93 | { Object.keys( sortByOptions ).map( ( i ) => ( |
| 94 | <div |
| 95 | key={ i } |
| 96 | className="ti-tpc-template-library-templates-table-column" |
| 97 | > |
| 98 | <Button |
| 99 | className={ classnames( { |
| 100 | 'is-selected': i === getOrder().orderby, |
| 101 | 'is-asc': 'ASC' === getOrder().order, |
| 102 | } ) } |
| 103 | onClick={ () => { |
| 104 | const order = { |
| 105 | order: 'DESC', |
| 106 | orderby: i, |
| 107 | }; |
| 108 | |
| 109 | if ( i === getOrder().orderby ) { |
| 110 | if ( 'DESC' === getOrder().order ) { |
| 111 | order.order = 'ASC'; |
| 112 | } |
| 113 | } |
| 114 | setSorting( { |
| 115 | ...order, |
| 116 | } ); |
| 117 | } } |
| 118 | > |
| 119 | { sortByOptions[ i ] } |
| 120 | </Button> |
| 121 | </div> |
| 122 | ) ) } |
| 123 | <div className="ti-tpc-template-library-templates-table-column"> |
| 124 | <Button> |
| 125 | { |
| 126 | window.tiTpc.library.filters.sortLabels |
| 127 | .actions |
| 128 | } |
| 129 | </Button> |
| 130 | </div> |
| 131 | </div> |
| 132 | |
| 133 | { items.map( ( item ) => ( |
| 134 | <Template |
| 135 | table={ true } |
| 136 | key={ item.template_id } |
| 137 | item={ item } |
| 138 | id={ item.template_id } |
| 139 | title={ item.template_name } |
| 140 | meta={ item.meta || '' } |
| 141 | onImport={ onImport } |
| 142 | onUpdateTemplate={ onUpdateTemplate } |
| 143 | onDelete={ onDelete } |
| 144 | onDuplicate={ ( id ) => onDuplicate( id ) } |
| 145 | /> |
| 146 | ) ) } |
| 147 | </Fragment> |
| 148 | ) } |
| 149 | |
| 150 | { 0 === items.length && |
| 151 | ( isGeneral ? ( |
| 152 | <p> |
| 153 | { __( |
| 154 | 'No templates found. Check again later!', |
| 155 | 'templates-patterns-collection' |
| 156 | ) } |
| 157 | </p> |
| 158 | ) : ( |
| 159 | <p style={ { marginLeft: '10px' } }> |
| 160 | { __( |
| 161 | 'No templates available. Try adding few templates.', |
| 162 | 'templates-patterns-collection' |
| 163 | ) } |
| 164 | </p> |
| 165 | ) ) } |
| 166 | |
| 167 | { isLoading && ( |
| 168 | <Placeholder> |
| 169 | <Spinner /> |
| 170 | </Placeholder> |
| 171 | ) } |
| 172 | </div> |
| 173 | ); |
| 174 | }; |
| 175 | |
| 176 | export default withSelect( ( select, { isGeneral } ) => { |
| 177 | const library = isGeneral |
| 178 | ? select( 'tpc/elementor' ).getTemplates() |
| 179 | : select( 'tpc/elementor' ).getLibrary(); |
| 180 | |
| 181 | const { items = [], currentPage, totalPages } = library; |
| 182 | |
| 183 | return { items, currentPage, totalPages }; |
| 184 | } )( TemplatesContent ); |
| 185 |