content.js
5 years ago
export.js
5 years ago
filters.js
5 years ago
header.js
1 year ago
list-item.js
5 years ago
pagination.js
5 years ago
preview.js
1 year ago
templates-content.js
5 years ago
list-item.js
213 lines
| 1 | /* eslint-disable no-undef */ |
| 2 | /* eslint-disable camelcase */ |
| 3 | import classnames from 'classnames'; |
| 4 | |
| 5 | import { check, edit, page, trash, update } from '@wordpress/icons'; |
| 6 | import { Button, Icon, TextControl, Tooltip } from '@wordpress/components'; |
| 7 | import { useDispatch } from '@wordpress/data'; |
| 8 | import { useState } from '@wordpress/element'; |
| 9 | |
| 10 | import { |
| 11 | updateTemplate, |
| 12 | deleteTemplate, |
| 13 | fetchLibrary, |
| 14 | } from './../data/templates-cloud/index'; |
| 15 | |
| 16 | const ListItem = ( { layout, item, importTemplate, deletable } ) => { |
| 17 | const { togglePreview, setPreviewData } = useDispatch( 'tpc/beaver' ); |
| 18 | const [ isLoading, setLoading ] = useState( false ); |
| 19 | const [ isEditing, setEditing ] = useState( false ); |
| 20 | const [ itemName, setItemName ] = useState( item.template_name ); |
| 21 | |
| 22 | const updateItem = async () => { |
| 23 | setLoading( 'updating' ); |
| 24 | await updateTemplate( { |
| 25 | template_id: item.template_id, |
| 26 | template_name: itemName || item.template_name, |
| 27 | } ); |
| 28 | await fetchLibrary(); |
| 29 | setLoading( false ); |
| 30 | setEditing( ! isEditing ); |
| 31 | }; |
| 32 | |
| 33 | const deleteItem = async () => { |
| 34 | if ( ! window.confirm( window.tiTpc.library.deleteItem ) ) { |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | setLoading( 'deleting' ); |
| 39 | await deleteTemplate( item.template_id ); |
| 40 | setLoading( false ); |
| 41 | }; |
| 42 | |
| 43 | const importPreview = async () => { |
| 44 | togglePreview(); |
| 45 | setPreviewData( { |
| 46 | type: 'library', |
| 47 | item, |
| 48 | } ); |
| 49 | }; |
| 50 | |
| 51 | if ( 'grid' === layout ) { |
| 52 | const style = { backgroundImage: `url(${ item.template_thumbnail })` }; |
| 53 | |
| 54 | return ( |
| 55 | <div key={ item.template_id } className="table-grid"> |
| 56 | <div |
| 57 | style={ style } |
| 58 | className={ classnames( 'grid-preview', { |
| 59 | 'is-loading': isEditing || false !== isLoading, |
| 60 | } ) } |
| 61 | > |
| 62 | <div className="preview-actions"> |
| 63 | { ! deletable && item.link && ( |
| 64 | <Button |
| 65 | isSecondary |
| 66 | disabled={ false !== isLoading } |
| 67 | onClick={ importPreview } |
| 68 | > |
| 69 | { window.tiTpc.library.actions.preview } |
| 70 | </Button> |
| 71 | ) } |
| 72 | |
| 73 | <Button |
| 74 | isPrimary |
| 75 | isBusy={ 'importing' === isLoading } |
| 76 | disabled={ false !== isLoading } |
| 77 | onClick={ () => importTemplate( item.template_id ) } |
| 78 | > |
| 79 | { window.tiTpc.library.actions.import } |
| 80 | </Button> |
| 81 | |
| 82 | { deletable && ( |
| 83 | <div className="preview-controls"> |
| 84 | <Button |
| 85 | label={ |
| 86 | window.tiTpc.library.actions.delete |
| 87 | } |
| 88 | icon={ |
| 89 | 'deleting' === isLoading |
| 90 | ? update |
| 91 | : trash |
| 92 | } |
| 93 | disabled={ false !== isLoading } |
| 94 | className={ classnames( { |
| 95 | 'is-loading': 'deleting' === isLoading, |
| 96 | } ) } |
| 97 | onClick={ deleteItem } |
| 98 | /> |
| 99 | </div> |
| 100 | ) } |
| 101 | </div> |
| 102 | </div> |
| 103 | |
| 104 | <div className="card-footer"> |
| 105 | <p>{ item.template_name }</p> |
| 106 | </div> |
| 107 | </div> |
| 108 | ); |
| 109 | } |
| 110 | |
| 111 | const actionClasses = classnames( 'actions', { |
| 112 | 'no-controls': ! deletable, |
| 113 | } ); |
| 114 | |
| 115 | return ( |
| 116 | <div key={ item.template_id } className="table-row"> |
| 117 | <div className="row-title"> |
| 118 | <Icon icon={ page } /> |
| 119 | { isEditing ? ( |
| 120 | <TextControl |
| 121 | label={ window.tiTpc.exporter.textLabel } |
| 122 | hideLabelFromVision |
| 123 | value={ itemName } |
| 124 | onChange={ setItemName } |
| 125 | /> |
| 126 | ) : ( |
| 127 | item.template_name |
| 128 | ) } |
| 129 | </div> |
| 130 | |
| 131 | { deletable && ( |
| 132 | <div className="row-controls"> |
| 133 | { item.link ? ( |
| 134 | <Tooltip text={ window.tiTpc.library.synced }> |
| 135 | <Button |
| 136 | label={ window.tiTpc.library.actions.edit } |
| 137 | icon={ edit } |
| 138 | disabled={ true } |
| 139 | > |
| 140 | { window.tiTpc.library.actions.edit } |
| 141 | </Button> |
| 142 | </Tooltip> |
| 143 | ) : ( |
| 144 | <Button |
| 145 | label={ |
| 146 | isEditing |
| 147 | ? window.tiTpc.library.actions.update |
| 148 | : window.tiTpc.library.actions.edit |
| 149 | } |
| 150 | icon={ |
| 151 | isEditing |
| 152 | ? 'updating' === isLoading |
| 153 | ? update |
| 154 | : check |
| 155 | : edit |
| 156 | } |
| 157 | disabled={ false !== isLoading } |
| 158 | className={ classnames( { |
| 159 | 'is-loading': 'updating' === isLoading, |
| 160 | } ) } |
| 161 | onClick={ |
| 162 | isEditing |
| 163 | ? updateItem |
| 164 | : () => setEditing( ! isEditing ) |
| 165 | } |
| 166 | > |
| 167 | { isEditing |
| 168 | ? window.tiTpc.library.actions.update |
| 169 | : window.tiTpc.library.actions.edit } |
| 170 | </Button> |
| 171 | ) } |
| 172 | |
| 173 | <Button |
| 174 | label={ window.tiTpc.library.actions.delete } |
| 175 | icon={ 'deleting' === isLoading ? update : trash } |
| 176 | disabled={ false !== isLoading } |
| 177 | className={ classnames( { |
| 178 | 'is-loading': 'deleting' === isLoading, |
| 179 | } ) } |
| 180 | onClick={ deleteItem } |
| 181 | > |
| 182 | { 'deleting' === isLoading |
| 183 | ? window.tiTpc.library.actions.deleting + '...' |
| 184 | : window.tiTpc.library.actions.delete } |
| 185 | </Button> |
| 186 | </div> |
| 187 | ) } |
| 188 | <div className={ actionClasses }> |
| 189 | { ! deletable && item.link && ( |
| 190 | <Button |
| 191 | isSecondary |
| 192 | disabled={ false !== isLoading } |
| 193 | onClick={ importPreview } |
| 194 | > |
| 195 | { window.tiTpc.library.actions.preview } |
| 196 | </Button> |
| 197 | ) } |
| 198 | |
| 199 | <Button |
| 200 | isPrimary |
| 201 | isBusy={ 'importing' === isLoading } |
| 202 | disabled={ false !== isLoading } |
| 203 | onClick={ () => importTemplate( item.template_id ) } |
| 204 | > |
| 205 | { window.tiTpc.library.actions.import } |
| 206 | </Button> |
| 207 | </div> |
| 208 | </div> |
| 209 | ); |
| 210 | }; |
| 211 | |
| 212 | export default ListItem; |
| 213 |