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
template-predefine.js
172 lines
| 1 | import { __ } from '@wordpress/i18n'; |
| 2 | import { Button, TextControl, PanelBody } from '@wordpress/components'; |
| 3 | import classnames from 'classnames'; |
| 4 | import PublishButton from './publish-button'; |
| 5 | import { getTemplate } from '../data/templates-cloud'; |
| 6 | import Notices from './notices'; |
| 7 | |
| 8 | const TemplatePredefine = ( { |
| 9 | templateData, |
| 10 | setTemplateData, |
| 11 | canPredefine, |
| 12 | setLoading, |
| 13 | createErrorNotice, |
| 14 | createSuccessNotice, |
| 15 | isLoading, |
| 16 | saveMeta, |
| 17 | } ) => { |
| 18 | /** |
| 19 | * Set the screenshot URL. |
| 20 | * |
| 21 | * @param {string} url Screenshot URL. |
| 22 | */ |
| 23 | const setScreeShotUrl = ( url ) => { |
| 24 | setTemplateData( { |
| 25 | ...templateData, |
| 26 | _ti_tpc_screenshot_url: url, |
| 27 | } ); |
| 28 | }; |
| 29 | |
| 30 | /** |
| 31 | * Set the published status. |
| 32 | * |
| 33 | * @param {boolean} published Published status. |
| 34 | */ |
| 35 | const setPublished = ( published ) => { |
| 36 | setTemplateData( { |
| 37 | ...templateData, |
| 38 | _ti_tpc_published: published, |
| 39 | } ); |
| 40 | }; |
| 41 | |
| 42 | /** |
| 43 | * Refresh the template data. |
| 44 | * |
| 45 | * @return {Promise<void>} |
| 46 | */ |
| 47 | const refreshData = async () => { |
| 48 | setLoading( 'publishing' ); |
| 49 | try { |
| 50 | await getTemplate( templateData._ti_tpc_template_id ).then( |
| 51 | ( results ) => { |
| 52 | if ( |
| 53 | templateData._ti_tpc_template_id === results.template_id |
| 54 | ) { |
| 55 | if ( results.template_thumbnail ) { |
| 56 | setScreeShotUrl( results.template_thumbnail ); |
| 57 | saveMeta( { |
| 58 | ...templateData, |
| 59 | _ti_tpc_screenshot_url: |
| 60 | results.template_thumbnail, |
| 61 | } ); |
| 62 | } |
| 63 | createSuccessNotice( |
| 64 | __( |
| 65 | 'Template Data Refreshed.', |
| 66 | 'templates-patterns-collection' |
| 67 | ), |
| 68 | { |
| 69 | type: 'snackbar', |
| 70 | } |
| 71 | ); |
| 72 | } |
| 73 | } |
| 74 | ); |
| 75 | } catch ( error ) { |
| 76 | createErrorNotice( |
| 77 | __( |
| 78 | 'Something happened when refreshing the template data.', |
| 79 | 'templates-patterns-collection' |
| 80 | ) |
| 81 | ); |
| 82 | } |
| 83 | setLoading( false ); |
| 84 | }; |
| 85 | |
| 86 | return ( |
| 87 | <PanelBody> |
| 88 | <h4> |
| 89 | { __( 'Publish Settings', 'templates-patterns-collection' ) } |
| 90 | </h4> |
| 91 | <TextControl |
| 92 | label={ __( |
| 93 | 'Screenshot URL', |
| 94 | 'templates-patterns-collection' |
| 95 | ) } |
| 96 | value={ templateData._ti_tpc_screenshot_url || '' } |
| 97 | help={ __( |
| 98 | 'Use `{generate_ss}` to publish this and have a screenshot automatically generated. Otherwise use the url to point to an image location for the template preview.', |
| 99 | 'templates-patterns-collection' |
| 100 | ) } |
| 101 | type="url" |
| 102 | onChange={ setScreeShotUrl } |
| 103 | /> |
| 104 | <TextControl |
| 105 | label={ __( 'Site Slug', 'templates-patterns-collection' ) } |
| 106 | value={ templateData._ti_tpc_site_slug } |
| 107 | help={ __( |
| 108 | 'Use `general` to publish this as a global template. Otherwise use the starter site slug to make it available as a single page for the starter site.', |
| 109 | 'templates-patterns-collection' |
| 110 | ) } |
| 111 | type="url" |
| 112 | onChange={ ( newValue ) => |
| 113 | setTemplateData( { |
| 114 | ...templateData, |
| 115 | _ti_tpc_site_slug: newValue, |
| 116 | } ) |
| 117 | } |
| 118 | /> |
| 119 | <TextControl |
| 120 | label={ __( 'Previewer URL', 'templates-patterns-collection' ) } |
| 121 | value={ templateData._ti_tpc_previewer_url || '' } |
| 122 | help={ __( |
| 123 | 'Provide a URL to a previewer for this template. This will be used to display a preview of the template in the editor.', |
| 124 | 'templates-patterns-collection' |
| 125 | ) } |
| 126 | type="url" |
| 127 | onChange={ ( newValue ) => |
| 128 | setTemplateData( { |
| 129 | ...templateData, |
| 130 | _ti_tpc_previewer_url: newValue, |
| 131 | } ) |
| 132 | } |
| 133 | /> |
| 134 | <PublishButton |
| 135 | canPredefine={ canPredefine } |
| 136 | setLoading={ setLoading } |
| 137 | templateData={ { |
| 138 | ...templateData, |
| 139 | link: templateData._ti_tpc_previewer_url, |
| 140 | } } |
| 141 | setScreenshotURL={ setScreeShotUrl } |
| 142 | setPublished={ setPublished } |
| 143 | saveMeta={ saveMeta } |
| 144 | published={ templateData._ti_tpc_published || false } |
| 145 | createErrorNotice={ createErrorNotice } |
| 146 | createSuccessNotice={ createSuccessNotice } |
| 147 | isLoading={ isLoading } |
| 148 | /> |
| 149 | { templateData._ti_tpc_published && ( |
| 150 | <Button |
| 151 | isLink |
| 152 | icon="image-rotate" |
| 153 | onClick={ refreshData } |
| 154 | disabled={ false !== isLoading } |
| 155 | className={ classnames( { |
| 156 | 'is-loading': 'publishing' === isLoading, |
| 157 | } ) } |
| 158 | style={ { |
| 159 | marginLeft: '12px', |
| 160 | textDecoration: 'none', |
| 161 | } } |
| 162 | > |
| 163 | { __( 'Refresh', 'templates-patterns-collection' ) } |
| 164 | </Button> |
| 165 | ) } |
| 166 | <Notices /> |
| 167 | </PanelBody> |
| 168 | ); |
| 169 | }; |
| 170 | |
| 171 | export default TemplatePredefine; |
| 172 |