content.js
1 year ago
filters.js
1 year ago
header.js
1 year ago
import-modal.js
1 year ago
list-item.js
1 year ago
notices.js
5 years ago
pagination.js
5 years ago
preview.js
1 year ago
publish-button.js
2 years ago
template-predefine.js
2 years ago
templates-content.js
1 year ago
tpc-templates-button.js
2 years ago
list-item.js
393 lines
| 1 | /* eslint-disable camelcase */ |
| 2 | import classnames from 'classnames'; |
| 3 | |
| 4 | import { |
| 5 | // cloudUpload, |
| 6 | check, |
| 7 | edit, |
| 8 | page, |
| 9 | trash, |
| 10 | update, |
| 11 | } from '@wordpress/icons'; |
| 12 | import { __ } from '@wordpress/i18n'; |
| 13 | import { |
| 14 | Button, |
| 15 | Icon, |
| 16 | Popover, |
| 17 | TextControl, |
| 18 | Tooltip, |
| 19 | } from '@wordpress/components'; |
| 20 | import { useDispatch } from '@wordpress/data'; |
| 21 | import { useState } from '@wordpress/element'; |
| 22 | |
| 23 | import { |
| 24 | updateTemplate, |
| 25 | deleteTemplate, |
| 26 | // duplicateTemplate, |
| 27 | importTemplate, |
| 28 | fetchLibrary, |
| 29 | } from './../data/templates-cloud/index'; |
| 30 | |
| 31 | const ListItem = ( { |
| 32 | sortingOrder, |
| 33 | layout, |
| 34 | item, |
| 35 | importBlocks, |
| 36 | deletable, |
| 37 | } ) => { |
| 38 | const { togglePreview, setPreviewData } = useDispatch( 'tpc/block-editor' ); |
| 39 | const [ isLoading, setLoading ] = useState( false ); |
| 40 | const [ isEditing, setEditing ] = useState( false ); |
| 41 | const [ itemName, setItemName ] = useState( item.template_name ); |
| 42 | |
| 43 | const importItem = async () => { |
| 44 | setLoading( 'importing' ); |
| 45 | const data = await importTemplate( item.template_id ); |
| 46 | |
| 47 | if ( data.__file && data.content && 'wp_export' === data.__file ) { |
| 48 | importBlocks( data.content, item.meta || [], item.template_type ); |
| 49 | } |
| 50 | setLoading( false ); |
| 51 | }; |
| 52 | |
| 53 | const updateItem = async () => { |
| 54 | setLoading( 'updating' ); |
| 55 | await updateTemplate( |
| 56 | { |
| 57 | template_id: item.template_id, |
| 58 | template_name: itemName || item.template_name, |
| 59 | }, |
| 60 | sortingOrder |
| 61 | ); |
| 62 | await fetchLibrary( sortingOrder ); |
| 63 | setLoading( false ); |
| 64 | setEditing( ! isEditing ); |
| 65 | }; |
| 66 | |
| 67 | /* const duplicateItem = async () => { |
| 68 | setLoading( 'duplicating' ); |
| 69 | await duplicateTemplate( item.template_id ); |
| 70 | setLoading( false ); |
| 71 | };*/ |
| 72 | |
| 73 | const deleteItem = async () => { |
| 74 | if ( |
| 75 | ! window.confirm( |
| 76 | __( |
| 77 | 'Are you sure you want to delete this template?', |
| 78 | 'templates-patterns-collection' |
| 79 | ) |
| 80 | ) |
| 81 | ) { |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | setLoading( 'deleting' ); |
| 86 | await deleteTemplate( item.template_id, sortingOrder ); |
| 87 | setLoading( false ); |
| 88 | }; |
| 89 | |
| 90 | const importPreview = async () => { |
| 91 | togglePreview(); |
| 92 | setPreviewData( { |
| 93 | type: 'library', |
| 94 | item, |
| 95 | } ); |
| 96 | }; |
| 97 | |
| 98 | if ( 'grid' === layout ) { |
| 99 | const style = { backgroundImage: `url(${ item.template_thumbnail })` }; |
| 100 | |
| 101 | return ( |
| 102 | <div key={ item.template_id } className="table-grid"> |
| 103 | <div |
| 104 | style={ style } |
| 105 | className={ classnames( 'grid-preview', { |
| 106 | 'is-loading': isEditing || false !== isLoading, |
| 107 | } ) } |
| 108 | > |
| 109 | <div className="preview-actions"> |
| 110 | <Button |
| 111 | isSecondary |
| 112 | disabled={ false !== isLoading } |
| 113 | onClick={ importPreview } |
| 114 | > |
| 115 | { __( 'Preview', 'templates-patterns-collection' ) } |
| 116 | </Button> |
| 117 | <Button |
| 118 | isPrimary |
| 119 | isBusy={ 'importing' === isLoading } |
| 120 | disabled={ false !== isLoading } |
| 121 | onClick={ importItem } |
| 122 | > |
| 123 | { __( 'Import', 'templates-patterns-collection' ) } |
| 124 | </Button> |
| 125 | |
| 126 | { deletable && ( |
| 127 | <div className="preview-controls"> |
| 128 | { ! item.link && ( |
| 129 | <Button |
| 130 | label={ __( |
| 131 | 'Edit', |
| 132 | 'templates-patterns-collection' |
| 133 | ) } |
| 134 | icon={ |
| 135 | 'updating' === isLoading |
| 136 | ? update |
| 137 | : edit |
| 138 | } |
| 139 | disabled={ |
| 140 | isEditing || false !== isLoading |
| 141 | } |
| 142 | className={ classnames( { |
| 143 | 'is-loading': |
| 144 | 'updating' === isLoading, |
| 145 | } ) } |
| 146 | onClick={ () => |
| 147 | setEditing( ! isEditing ) |
| 148 | } |
| 149 | > |
| 150 | { isEditing && ( |
| 151 | <Popover |
| 152 | onFocusOutside={ () => |
| 153 | setEditing( ! isEditing ) |
| 154 | } |
| 155 | className="controls-popover" |
| 156 | inline |
| 157 | > |
| 158 | <div className="popover-content"> |
| 159 | <TextControl |
| 160 | label={ __( |
| 161 | 'Template Name', |
| 162 | 'templates-patterns-collection' |
| 163 | ) } |
| 164 | value={ itemName } |
| 165 | onChange={ setItemName } |
| 166 | /> |
| 167 | |
| 168 | <Button |
| 169 | label={ __( |
| 170 | 'Update', |
| 171 | 'templates-patterns-collection' |
| 172 | ) } |
| 173 | icon={ |
| 174 | 'updating' === |
| 175 | isLoading |
| 176 | ? update |
| 177 | : check |
| 178 | } |
| 179 | disabled={ |
| 180 | false !== isLoading |
| 181 | } |
| 182 | className={ classnames( |
| 183 | { |
| 184 | 'is-loading': |
| 185 | 'updating' === |
| 186 | isLoading, |
| 187 | } |
| 188 | ) } |
| 189 | onClick={ updateItem } |
| 190 | /> |
| 191 | </div> |
| 192 | </Popover> |
| 193 | ) } |
| 194 | </Button> |
| 195 | ) } |
| 196 | |
| 197 | { /*<Button |
| 198 | label={ __( 'Duplicate' ) } |
| 199 | icon={ |
| 200 | 'duplicating' === isLoading |
| 201 | ? update |
| 202 | : group |
| 203 | } |
| 204 | disabled={ false !== isLoading } |
| 205 | className={ classnames( { |
| 206 | 'is-loading': |
| 207 | 'duplicating' === isLoading, |
| 208 | } ) } |
| 209 | onClick={ duplicateItem } |
| 210 | />*/ } |
| 211 | |
| 212 | <Button |
| 213 | label={ __( |
| 214 | 'Delete', |
| 215 | 'templates-patterns-collection' |
| 216 | ) } |
| 217 | icon={ |
| 218 | 'deleting' === isLoading |
| 219 | ? update |
| 220 | : trash |
| 221 | } |
| 222 | disabled={ false !== isLoading } |
| 223 | className={ classnames( { |
| 224 | 'is-loading': 'deleting' === isLoading, |
| 225 | } ) } |
| 226 | onClick={ deleteItem } |
| 227 | /> |
| 228 | </div> |
| 229 | ) } |
| 230 | </div> |
| 231 | </div> |
| 232 | |
| 233 | <div className="card-footer"> |
| 234 | <p>{ item.template_name }</p> |
| 235 | { item.template_type === 'fse' && ( |
| 236 | <div className="type-label">FSE</div> |
| 237 | ) } |
| 238 | </div> |
| 239 | </div> |
| 240 | ); |
| 241 | } |
| 242 | |
| 243 | const actionClasses = classnames( 'actions', { |
| 244 | 'no-controls': ! deletable, |
| 245 | } ); |
| 246 | |
| 247 | return ( |
| 248 | <div key={ item.template_id } className="table-row"> |
| 249 | <div className="row-title"> |
| 250 | <Icon icon={ page } /> |
| 251 | { isEditing ? ( |
| 252 | <TextControl |
| 253 | label={ __( |
| 254 | 'Template Name', |
| 255 | 'templates-patterns-collection' |
| 256 | ) } |
| 257 | hideLabelFromVision |
| 258 | value={ itemName } |
| 259 | onChange={ setItemName } |
| 260 | /> |
| 261 | ) : ( |
| 262 | item.template_name |
| 263 | ) } |
| 264 | </div> |
| 265 | |
| 266 | { item.template_type === 'fse' && ( |
| 267 | <div className="row-type"> |
| 268 | <div className="type-label">FSE</div> |
| 269 | </div> |
| 270 | ) } |
| 271 | |
| 272 | { deletable && ( |
| 273 | <div className="row-controls"> |
| 274 | { item.link ? ( |
| 275 | <Tooltip |
| 276 | text={ __( 'This template is synced to a page.', 'templates-patterns-collection' ) } |
| 277 | > |
| 278 | <Button |
| 279 | label={ __( |
| 280 | 'Edit', |
| 281 | 'templates-patterns-collection' |
| 282 | ) } |
| 283 | icon={ edit } |
| 284 | disabled={ true } |
| 285 | > |
| 286 | { __( |
| 287 | 'Edit', |
| 288 | 'templates-patterns-collection' |
| 289 | ) } |
| 290 | </Button> |
| 291 | </Tooltip> |
| 292 | ) : ( |
| 293 | <Button |
| 294 | label={ |
| 295 | isEditing |
| 296 | ? __( |
| 297 | 'Update', |
| 298 | 'templates-patterns-collection' |
| 299 | ) |
| 300 | : __( |
| 301 | 'Edit', |
| 302 | 'templates-patterns-collection' |
| 303 | ) |
| 304 | } |
| 305 | icon={ |
| 306 | isEditing |
| 307 | ? 'updating' === isLoading |
| 308 | ? update |
| 309 | : check |
| 310 | : edit |
| 311 | } |
| 312 | disabled={ false !== isLoading } |
| 313 | className={ classnames( { |
| 314 | 'is-loading': 'updating' === isLoading, |
| 315 | } ) } |
| 316 | onClick={ |
| 317 | isEditing |
| 318 | ? updateItem |
| 319 | : () => setEditing( ! isEditing ) |
| 320 | } |
| 321 | > |
| 322 | { isEditing |
| 323 | ? __( |
| 324 | 'Update', |
| 325 | 'templates-patterns-collection' |
| 326 | ) |
| 327 | : __( |
| 328 | 'Edit', |
| 329 | 'templates-patterns-collection' |
| 330 | ) } |
| 331 | </Button> |
| 332 | ) } |
| 333 | |
| 334 | { /*<Button |
| 335 | label={ __( 'Duplicate' ) } |
| 336 | icon={ 'duplicating' === isLoading ? update : group } |
| 337 | disabled={ false !== isLoading } |
| 338 | className={ classnames( { |
| 339 | 'is-loading': 'duplicating' === isLoading, |
| 340 | } ) } |
| 341 | onClick={ duplicateItem } |
| 342 | />*/ } |
| 343 | |
| 344 | <Button |
| 345 | label={ __( |
| 346 | 'Delete', |
| 347 | 'templates-patterns-collection' |
| 348 | ) } |
| 349 | icon={ 'deleting' === isLoading ? update : trash } |
| 350 | disabled={ false !== isLoading } |
| 351 | className={ classnames( { |
| 352 | 'is-loading': 'deleting' === isLoading, |
| 353 | } ) } |
| 354 | onClick={ deleteItem } |
| 355 | > |
| 356 | { 'deleting' === isLoading |
| 357 | ? __( |
| 358 | 'Deleting', |
| 359 | 'templates-patterns-collection' |
| 360 | ) + '...' |
| 361 | : __( 'Delete', 'templates-patterns-collection' ) } |
| 362 | </Button> |
| 363 | { /* <Button |
| 364 | label={ __( 'Sync' ) } |
| 365 | icon={ cloudUpload } |
| 366 | onClick={ () => console.log( 'Upload to cloud.' ) } |
| 367 | /> */ } |
| 368 | </div> |
| 369 | ) } |
| 370 | <div className={ actionClasses }> |
| 371 | <Button |
| 372 | isSecondary |
| 373 | disabled={ false !== isLoading } |
| 374 | onClick={ importPreview } |
| 375 | > |
| 376 | { __( 'Preview', 'templates-patterns-collection' ) } |
| 377 | </Button> |
| 378 | |
| 379 | <Button |
| 380 | isPrimary |
| 381 | isBusy={ 'importing' === isLoading } |
| 382 | disabled={ false !== isLoading } |
| 383 | onClick={ importItem } |
| 384 | > |
| 385 | { __( 'Import', 'templates-patterns-collection' ) } |
| 386 | </Button> |
| 387 | </div> |
| 388 | </div> |
| 389 | ); |
| 390 | }; |
| 391 | |
| 392 | export default ListItem; |
| 393 |