DemoSiteTemplatesImport.js
3 years ago
FeedbackNotice.js
1 year ago
Filters.js
1 year ago
ImportTemplatesModal.js
1 year ago
Library.js
1 year ago
ListItem.js
1 year ago
PreviewFrame.js
1 year ago
common.js
2 years ago
ListItem.js
354 lines
| 1 | /* eslint-disable camelcase */ |
| 2 | |
| 3 | import { check, edit, page, trash, update } from '@wordpress/icons'; |
| 4 | import { __ } from '@wordpress/i18n'; |
| 5 | import { Button, Icon, TextControl, Tooltip } from '@wordpress/components'; |
| 6 | import api from '@wordpress/api'; |
| 7 | |
| 8 | import { useState } from '@wordpress/element'; |
| 9 | import classnames from 'classnames'; |
| 10 | |
| 11 | import { |
| 12 | updateTemplate, |
| 13 | // duplicateTemplate, |
| 14 | deleteTemplate, |
| 15 | } from './common'; |
| 16 | |
| 17 | const ListItem = ( { |
| 18 | sortingOrder, |
| 19 | item, |
| 20 | loadTemplates, |
| 21 | userTemplate, |
| 22 | grid, |
| 23 | onPreview, |
| 24 | onImport, |
| 25 | upsell = false, |
| 26 | } ) => { |
| 27 | const [ isLoading, setLoading ] = useState( false ); |
| 28 | const [ isEditing, setEditing ] = useState( false ); |
| 29 | const [ itemName, setItemName ] = useState( item.template_name ); |
| 30 | |
| 31 | const updateItem = async ( e ) => { |
| 32 | e.preventDefault(); |
| 33 | const { template_id, template_name } = item; |
| 34 | setLoading( 'updating' ); |
| 35 | await updateTemplate( template_id, itemName || template_name ).then( |
| 36 | ( r ) => { |
| 37 | if ( r.success ) { |
| 38 | setEditing( ! isEditing ); |
| 39 | setLoading( false ); |
| 40 | } |
| 41 | } |
| 42 | ); |
| 43 | }; |
| 44 | |
| 45 | /*const duplicateItem = async () => { |
| 46 | setLoading( 'duplicating' ); |
| 47 | await duplicateTemplate( item.template_id ).then( ( r ) => { |
| 48 | if ( r.success ) { |
| 49 | loadTemplates(); |
| 50 | } |
| 51 | } ); |
| 52 | setLoading( false ); |
| 53 | };*/ |
| 54 | |
| 55 | /** |
| 56 | * Remove the template from the global option. |
| 57 | * |
| 58 | * @param {string} templateId The template ID. |
| 59 | */ |
| 60 | const removeFromFseGlobalOption = ( templateId ) => { |
| 61 | const settings = new api.models.Settings(); |
| 62 | |
| 63 | settings.fetch().then( ( response ) => { |
| 64 | const newSettings = Object.keys( |
| 65 | response.templates_patterns_collection_fse_templates |
| 66 | ).reduce( ( acc, key ) => { |
| 67 | const template = |
| 68 | response.templates_patterns_collection_fse_templates[ key ]; |
| 69 | const updatedTemplate = { ...template }; |
| 70 | |
| 71 | if ( template._ti_tpc_template_id === templateId ) { |
| 72 | // Skip the item you want to delete |
| 73 | return acc; |
| 74 | } |
| 75 | |
| 76 | acc[ key ] = updatedTemplate; |
| 77 | return acc; |
| 78 | }, {} ); |
| 79 | |
| 80 | settings.save( { |
| 81 | templates_patterns_collection_fse_templates: newSettings, |
| 82 | } ); |
| 83 | } ); |
| 84 | }; |
| 85 | |
| 86 | const deleteItem = async () => { |
| 87 | if ( |
| 88 | ! window.confirm( |
| 89 | __( 'Are you sure you want to delete this template?', 'templates-patterns-collection' ) |
| 90 | ) |
| 91 | ) { |
| 92 | return false; |
| 93 | } |
| 94 | |
| 95 | setLoading( 'deleteing' ); |
| 96 | |
| 97 | deleteTemplate( item.template_id ).then( ( r ) => { |
| 98 | if ( r.success ) { |
| 99 | loadTemplates( { |
| 100 | page: 0, |
| 101 | ...sortingOrder, |
| 102 | } ); |
| 103 | if ( item.template_type === 'fse' ) { |
| 104 | removeFromFseGlobalOption( item.template_id ); |
| 105 | } |
| 106 | setLoading( false ); |
| 107 | } |
| 108 | } ); |
| 109 | }; |
| 110 | |
| 111 | const handlePreview = () => { |
| 112 | onPreview( item.link ); |
| 113 | }; |
| 114 | |
| 115 | const actionClasses = classnames( 'actions', { |
| 116 | 'no-controls': ! userTemplate, |
| 117 | } ); |
| 118 | |
| 119 | if ( grid ) { |
| 120 | const style = { backgroundImage: `url(${ item.template_thumbnail })` }; |
| 121 | |
| 122 | return ( |
| 123 | <div key={ item.template_id } className="table-grid"> |
| 124 | <div |
| 125 | style={ style } |
| 126 | className={ classnames( 'grid-preview', { |
| 127 | 'is-loading': isEditing || false !== isLoading, |
| 128 | } ) } |
| 129 | > |
| 130 | <div className="preview-actions"> |
| 131 | { ! userTemplate && item.link && ( |
| 132 | <Button |
| 133 | isSecondary |
| 134 | disabled={ false !== isLoading } |
| 135 | onClick={ handlePreview } |
| 136 | > |
| 137 | { __( 'Preview', 'templates-patterns-collection' ) } |
| 138 | </Button> |
| 139 | ) } |
| 140 | { ! upsell && ( |
| 141 | <Button |
| 142 | isPrimary |
| 143 | isBusy={ 'importing' === isLoading } |
| 144 | disabled={ false !== isLoading } |
| 145 | onClick={ onImport } |
| 146 | > |
| 147 | { __( 'Import', 'templates-patterns-collection' ) } |
| 148 | </Button> |
| 149 | ) } |
| 150 | |
| 151 | { userTemplate && ( |
| 152 | <div className="preview-controls"> |
| 153 | { ! item.link && ( |
| 154 | <Button |
| 155 | label={ __( 'Edit', 'templates-patterns-collection' ) } |
| 156 | icon={ |
| 157 | 'updating' === isLoading |
| 158 | ? update |
| 159 | : edit |
| 160 | } |
| 161 | disabled={ |
| 162 | isEditing || false !== isLoading |
| 163 | } |
| 164 | className={ classnames( { |
| 165 | 'is-loading': |
| 166 | 'updating' === isLoading, |
| 167 | } ) } |
| 168 | onClick={ () => |
| 169 | setEditing( ! isEditing ) |
| 170 | } |
| 171 | /> |
| 172 | ) } |
| 173 | |
| 174 | { /*<Button |
| 175 | label={ __( 'Duplicate' ) } |
| 176 | icon={ |
| 177 | 'duplicating' === isLoading |
| 178 | ? update |
| 179 | : group |
| 180 | } |
| 181 | disabled={ false !== isLoading } |
| 182 | className={ classnames( { |
| 183 | 'is-loading': |
| 184 | 'duplicating' === isLoading, |
| 185 | } ) } |
| 186 | onClick={ duplicateItem } |
| 187 | />*/ } |
| 188 | |
| 189 | <Button |
| 190 | label={ __( 'Delete', 'templates-patterns-collection' ) } |
| 191 | icon={ |
| 192 | 'deleteing' === isLoading |
| 193 | ? update |
| 194 | : trash |
| 195 | } |
| 196 | disabled={ false !== isLoading } |
| 197 | className={ classnames( { |
| 198 | 'is-loading': 'deleteing' === isLoading, |
| 199 | } ) } |
| 200 | onClick={ deleteItem } |
| 201 | /> |
| 202 | </div> |
| 203 | ) } |
| 204 | </div> |
| 205 | </div> |
| 206 | |
| 207 | <div className="card-footer"> |
| 208 | { isEditing ? ( |
| 209 | <form onSubmit={ updateItem }> |
| 210 | <TextControl |
| 211 | value={ itemName } |
| 212 | onChange={ setItemName } |
| 213 | /> |
| 214 | <Button |
| 215 | type="submit" |
| 216 | label={ __( 'Update', 'templates-patterns-collection' ) } |
| 217 | icon={ |
| 218 | 'updating' === isLoading ? update : check |
| 219 | } |
| 220 | disabled={ false !== isLoading } |
| 221 | className={ classnames( { |
| 222 | 'is-loading': 'updating' === isLoading, |
| 223 | } ) } |
| 224 | /> |
| 225 | </form> |
| 226 | ) : ( |
| 227 | <> |
| 228 | <p>{ itemName }</p> |
| 229 | { item.template_type === 'fse' && ( |
| 230 | <div className="type-label">FSE</div> |
| 231 | ) } |
| 232 | </> |
| 233 | ) } |
| 234 | </div> |
| 235 | </div> |
| 236 | ); |
| 237 | } |
| 238 | |
| 239 | return ( |
| 240 | <div key={ item.template_id } className="table-row"> |
| 241 | <div className="title"> |
| 242 | <Icon icon={ page } /> |
| 243 | { isEditing ? ( |
| 244 | <TextControl |
| 245 | label={ __( 'Template Name', 'templates-patterns-collection' ) } |
| 246 | hideLabelFromVision |
| 247 | value={ itemName } |
| 248 | onChange={ setItemName } |
| 249 | /> |
| 250 | ) : ( |
| 251 | itemName |
| 252 | ) } |
| 253 | </div> |
| 254 | |
| 255 | { item.template_type === 'fse' && ( |
| 256 | <div className="type"> |
| 257 | <div className="type-label">FSE</div> |
| 258 | </div> |
| 259 | ) } |
| 260 | |
| 261 | { userTemplate && ( |
| 262 | <div className="controls"> |
| 263 | { item.link ? ( |
| 264 | <Tooltip |
| 265 | text={ __( 'This template is synced to a page.', 'templates-patterns-collection' ) } |
| 266 | > |
| 267 | <Button |
| 268 | label={ __( 'Edit', 'templates-patterns-collection' ) } |
| 269 | icon={ edit } |
| 270 | disabled={ true } |
| 271 | > |
| 272 | { __( 'Edit', 'templates-patterns-collection' ) } |
| 273 | </Button> |
| 274 | </Tooltip> |
| 275 | ) : ( |
| 276 | <Button |
| 277 | label={ isEditing ? __( 'Update', 'templates-patterns-collection' ) : __( 'Edit', 'templates-patterns-collection' ) } |
| 278 | icon={ |
| 279 | isEditing |
| 280 | ? 'updating' === isLoading |
| 281 | ? update |
| 282 | : check |
| 283 | : edit |
| 284 | } |
| 285 | disabled={ false !== isLoading } |
| 286 | className={ classnames( { |
| 287 | 'is-loading': 'updating' === isLoading, |
| 288 | } ) } |
| 289 | onClick={ ( e ) => { |
| 290 | if ( isEditing ) { |
| 291 | updateItem( e ).then( () => |
| 292 | setEditing( ! isEditing ) |
| 293 | ); |
| 294 | return; |
| 295 | } |
| 296 | setEditing( ! isEditing ); |
| 297 | } } |
| 298 | > |
| 299 | { isEditing ? __( 'Update', 'templates-patterns-collection' ) : __( 'Edit', 'templates-patterns-collection' ) } |
| 300 | </Button> |
| 301 | ) } |
| 302 | |
| 303 | { /* <Button |
| 304 | label={ __( 'Duplicate' ) } |
| 305 | icon={ 'duplicating' === isLoading ? update : group } |
| 306 | disabled={ false !== isLoading } |
| 307 | className={ classnames( { |
| 308 | 'is-loading': 'duplicating' === isLoading, |
| 309 | } ) } |
| 310 | onClick={ duplicateItem } |
| 311 | /> |
| 312 | */ } |
| 313 | <Button |
| 314 | label={ __( 'Delete', 'templates-patterns-collection' ) } |
| 315 | icon={ 'deleteing' === isLoading ? update : trash } |
| 316 | disabled={ false !== isLoading } |
| 317 | className={ classnames( { |
| 318 | 'is-loading': 'deleteing' === isLoading, |
| 319 | } ) } |
| 320 | onClick={ deleteItem } |
| 321 | > |
| 322 | { 'deleting' === isLoading |
| 323 | ? __( 'Deleting', 'templates-patterns-collection' ) + '...' |
| 324 | : __( 'Delete', 'templates-patterns-collection' ) } |
| 325 | </Button> |
| 326 | </div> |
| 327 | ) } |
| 328 | |
| 329 | <div className={ actionClasses }> |
| 330 | { ! userTemplate && item.link && ( |
| 331 | <Button |
| 332 | isSecondary |
| 333 | disabled={ false !== isLoading } |
| 334 | onClick={ handlePreview } |
| 335 | > |
| 336 | { __( 'Preview', 'templates-patterns-collection' ) } |
| 337 | </Button> |
| 338 | ) } |
| 339 | |
| 340 | <Button |
| 341 | isPrimary |
| 342 | isBusy={ 'importing' === isLoading } |
| 343 | onClick={ onImport } |
| 344 | disabled={ false !== isLoading } |
| 345 | > |
| 346 | { __( 'Import', 'templates-patterns-collection' ) } |
| 347 | </Button> |
| 348 | </div> |
| 349 | </div> |
| 350 | ); |
| 351 | }; |
| 352 | |
| 353 | export default ListItem; |
| 354 |