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
content.js
291 lines
| 1 | /* eslint-disable no-undef */ |
| 2 | import classnames from 'classnames'; |
| 3 | import { Button, Spinner, Icon } from '@wordpress/components'; |
| 4 | import { compose } from '@wordpress/compose'; |
| 5 | import { withDispatch, withSelect } from '@wordpress/data'; |
| 6 | import { Fragment, useEffect } from '@wordpress/element'; |
| 7 | import { rotateRight } from '@wordpress/icons'; |
| 8 | import { ENTER } from '@wordpress/keycodes'; |
| 9 | import TemplatesContent from './templates-content.js'; |
| 10 | import Export from './export.js'; |
| 11 | |
| 12 | import { |
| 13 | fetchTemplates, |
| 14 | fetchLibrary, |
| 15 | updateTemplate, |
| 16 | deleteTemplate, |
| 17 | duplicateTemplate, |
| 18 | } from './../data/templates-cloud/index.js'; |
| 19 | |
| 20 | const sortByOptions = { |
| 21 | date: window.tiTpc.library.filters.sortLabels.date, |
| 22 | template_name: window.tiTpc.library.filters.sortLabels.name, |
| 23 | modified: window.tiTpc.library.filters.sortLabels.modified, |
| 24 | }; |
| 25 | |
| 26 | const Content = ( { |
| 27 | setQuery, |
| 28 | getSearchQuery, |
| 29 | setSorting, |
| 30 | getOrder, |
| 31 | isSearch, |
| 32 | setSearch, |
| 33 | onImport, |
| 34 | isFetching, |
| 35 | isPreview, |
| 36 | currentTab, |
| 37 | preview, |
| 38 | setFetching, |
| 39 | } ) => { |
| 40 | const init = async ( search = getSearchQuery() ) => { |
| 41 | setFetching( true ); |
| 42 | |
| 43 | if ( search ) { |
| 44 | setSearch( true ); |
| 45 | } else { |
| 46 | setSearch( false ); |
| 47 | } |
| 48 | |
| 49 | const order = getOrder(); |
| 50 | if ( currentTab === 'templates' ) { |
| 51 | await fetchTemplates( { |
| 52 | search, |
| 53 | ...order, |
| 54 | } ); |
| 55 | } else { |
| 56 | await fetchLibrary( { |
| 57 | search, |
| 58 | ...order, |
| 59 | } ); |
| 60 | } |
| 61 | setFetching( false ); |
| 62 | }; |
| 63 | |
| 64 | useEffect( () => { |
| 65 | init(); |
| 66 | }, [ currentTab, getOrder() ] ); |
| 67 | |
| 68 | const isGeneral = currentTab === 'templates'; |
| 69 | |
| 70 | const onUpdateTemplate = async ( id, title ) => { |
| 71 | setFetching( true ); |
| 72 | await updateTemplate( { |
| 73 | template_id: id, |
| 74 | template_name: title, |
| 75 | } ); |
| 76 | setFetching( false ); |
| 77 | }; |
| 78 | |
| 79 | const onDelete = async ( id ) => { |
| 80 | setFetching( true ); |
| 81 | await deleteTemplate( id ); |
| 82 | setFetching( false ); |
| 83 | }; |
| 84 | |
| 85 | const onDuplicate = async ( id ) => { |
| 86 | setFetching( true ); |
| 87 | await duplicateTemplate( id ); |
| 88 | setFetching( false ); |
| 89 | }; |
| 90 | |
| 91 | if ( isPreview ) { |
| 92 | return ( |
| 93 | <div |
| 94 | className={ classnames( |
| 95 | 'dialog-message dialog-lightbox-message', |
| 96 | { |
| 97 | 'is-dark': |
| 98 | 'dark' === |
| 99 | elementor.settings.editorPreferences.model.get( |
| 100 | 'ui_theme' |
| 101 | ), |
| 102 | } |
| 103 | ) } |
| 104 | > |
| 105 | <div className="dialog-content dialog-lightbox-content"> |
| 106 | <div className="ti-tpc-template-library-preview"> |
| 107 | <iframe |
| 108 | title={ preview.template_name } |
| 109 | src={ preview.link || '' } |
| 110 | ></iframe> |
| 111 | |
| 112 | <div className="is-loading"> |
| 113 | <Icon icon={ rotateRight } /> |
| 114 | </div> |
| 115 | </div> |
| 116 | </div> |
| 117 | </div> |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | if ( isFetching ) { |
| 122 | return ( |
| 123 | <div |
| 124 | className={ classnames( |
| 125 | 'dialog-message dialog-lightbox-message', |
| 126 | { |
| 127 | 'is-dark': |
| 128 | 'dark' === |
| 129 | elementor.settings.editorPreferences.model.get( |
| 130 | 'ui_theme' |
| 131 | ), |
| 132 | } |
| 133 | ) } |
| 134 | > |
| 135 | <div className="dialog-content dialog-lightbox-content is-loading"> |
| 136 | <Spinner /> |
| 137 | </div> |
| 138 | </div> |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | if ( 'export' === currentTab ) { |
| 143 | return <Export />; |
| 144 | } |
| 145 | |
| 146 | return ( |
| 147 | <div |
| 148 | className={ classnames( 'dialog-message dialog-lightbox-message', { |
| 149 | 'is-dark': |
| 150 | 'dark' === |
| 151 | elementor.settings.editorPreferences.model.get( |
| 152 | 'ui_theme' |
| 153 | ), |
| 154 | } ) } |
| 155 | > |
| 156 | <div className="dialog-content dialog-lightbox-content"> |
| 157 | <div className="ti-tpc-template-library-templates"> |
| 158 | <div className="ti-tpc-template-library-templates-header"> |
| 159 | <div className="ti-tpc-template-library-templates-header-filters"> |
| 160 | { isGeneral && ( |
| 161 | <Fragment> |
| 162 | <div className="ti-tpc-template-library-templates-header-filters-label"> |
| 163 | { |
| 164 | window.tiTpc.library.filters |
| 165 | .sortLabel |
| 166 | } |
| 167 | </div> |
| 168 | |
| 169 | <div className="ti-tpc-template-library-templates-header-filters-filter"> |
| 170 | { Object.keys( sortByOptions ).map( |
| 171 | ( i ) => ( |
| 172 | <Button |
| 173 | key={ i } |
| 174 | className={ classnames( { |
| 175 | 'is-selected': |
| 176 | i === |
| 177 | getOrder().orderby, |
| 178 | 'is-asc': |
| 179 | 'ASC' === |
| 180 | getOrder().order, |
| 181 | } ) } |
| 182 | onClick={ () => { |
| 183 | const order = { |
| 184 | order: 'DESC', |
| 185 | orderby: i, |
| 186 | }; |
| 187 | |
| 188 | if ( |
| 189 | i === |
| 190 | getOrder().orderby |
| 191 | ) { |
| 192 | if ( |
| 193 | 'DESC' === |
| 194 | getOrder().order |
| 195 | ) { |
| 196 | order.order = |
| 197 | 'ASC'; |
| 198 | } |
| 199 | } |
| 200 | setSorting( { |
| 201 | ...order, |
| 202 | } ); |
| 203 | } } |
| 204 | > |
| 205 | { sortByOptions[ i ] } |
| 206 | </Button> |
| 207 | ) |
| 208 | ) } |
| 209 | </div> |
| 210 | </Fragment> |
| 211 | ) } |
| 212 | </div> |
| 213 | |
| 214 | <div className="ti-tpc-template-library-templates-header-search"> |
| 215 | <label |
| 216 | htmlFor="ti-tpc-template-library-filter-search" |
| 217 | className="elementor-screen-only" |
| 218 | > |
| 219 | { window.tiTpc.library.filters.searchLabel } |
| 220 | </label> |
| 221 | <input |
| 222 | id="ti-tpc-template-library-filter-search" |
| 223 | placeholder={ |
| 224 | window.tiTpc.library.filters.search |
| 225 | } |
| 226 | value={ getSearchQuery() } |
| 227 | onChange={ ( e ) => setQuery( e.target.value ) } |
| 228 | onKeyDown={ ( e ) => { |
| 229 | if ( e.keyCode === ENTER ) { |
| 230 | init(); |
| 231 | } |
| 232 | } } |
| 233 | /> |
| 234 | |
| 235 | { isSearch ? ( |
| 236 | <Button |
| 237 | onClick={ () => { |
| 238 | setQuery( '' ); |
| 239 | init( '' ); |
| 240 | } } |
| 241 | > |
| 242 | <i className="eicon-close"></i> |
| 243 | </Button> |
| 244 | ) : ( |
| 245 | <Button onClick={ () => init() }> |
| 246 | <i className="eicon-search"></i> |
| 247 | </Button> |
| 248 | ) } |
| 249 | </div> |
| 250 | </div> |
| 251 | |
| 252 | { [ 'templates', 'library' ].includes( currentTab ) && ( |
| 253 | <TemplatesContent |
| 254 | getSearchQuery={ () => getSearchQuery() } |
| 255 | getOrder={ getOrder } |
| 256 | setSorting={ setSorting } |
| 257 | onImport={ onImport } |
| 258 | onUpdateTemplate={ onUpdateTemplate } |
| 259 | onDelete={ onDelete } |
| 260 | onDuplicate={ onDuplicate } |
| 261 | isFetching={ isFetching } |
| 262 | isGeneral={ isGeneral } |
| 263 | /> |
| 264 | ) } |
| 265 | </div> |
| 266 | </div> |
| 267 | </div> |
| 268 | ); |
| 269 | }; |
| 270 | |
| 271 | export default compose( |
| 272 | withSelect( ( select ) => { |
| 273 | const { isFetching, isPreview, getCurrentTab, getPreview } = |
| 274 | select( 'tpc/elementor' ); |
| 275 | |
| 276 | return { |
| 277 | isFetching: isFetching(), |
| 278 | isPreview: isPreview(), |
| 279 | currentTab: getCurrentTab(), |
| 280 | preview: getPreview(), |
| 281 | }; |
| 282 | } ), |
| 283 | withDispatch( ( dispatch ) => { |
| 284 | const { setFetching } = dispatch( 'tpc/elementor' ); |
| 285 | |
| 286 | return { |
| 287 | setFetching, |
| 288 | }; |
| 289 | } ) |
| 290 | )( Content ); |
| 291 |