1+ // Import CSS.
2+ import './style.scss' ;
3+ import './editor.scss' ;
4+ import { EcwidIcons } from '../icons.js' ;
5+
6+
7+ if ( ! EcwidGutenbergParams . isDemoStore ) {
8+
9+ const { __ , _x } = wp . i18n ; // Import __() from wp.i18n
10+
11+ const {
12+ BlockControls,
13+ registerBlockType,
14+ } = wp . blocks ;
15+
16+ const {
17+ InspectorControls,
18+ } = wp . editor ;
19+
20+ const {
21+ PanelBody,
22+ ToggleControl,
23+ } = wp . components ;
24+
25+ const { withState } = wp . compose ;
26+
27+ const {
28+ Fragment
29+ } = wp . element ;
30+
31+ registerBlockType ( 'ec-store/buynow' , {
32+ title : __ ( 'Buy Now Button' , 'ecwid-shopping-cart' ) ,
33+ icon : EcwidIcons . button ,
34+ category : 'ec-store' , // Block category — Group blocks together based on common traits E.g. common, formatting, layout widgets, embed.
35+ attributes : {
36+ id : { type : 'integer' } ,
37+ show_price_on_button : { type : 'boolean' , default : true } ,
38+ center_align : { type : 'boolean' , default : true }
39+ } ,
40+ description : __ ( 'Display a buy button' , 'ecwid-shopping-cart' ) ,
41+ supports : {
42+ customClassName : false ,
43+ className : false ,
44+ html : false ,
45+ align : true ,
46+ alignWide : false ,
47+ isPrivate : ! EcwidGutenbergParams . isApiAvailable
48+ } ,
49+
50+ /**
51+ * The edit function describes the structure of your block in the context of the editor.
52+ * This represents what the editor will render when the block is used.
53+ *
54+ * The "edit" property must be a valid function.
55+ *
56+ * @link https://wordpress.org/gutenberg/handbook/block-api/block-edit-save/
57+ */
58+ edit : function ( props ) {
59+
60+ const { attributes } = props ;
61+
62+ const saveCallback = function ( params ) {
63+
64+ const attributes = {
65+ 'id' : params . newProps . id
66+ } ;
67+
68+ EcwidGutenbergParams . products [ params . newProps . id ] = {
69+ name : params . newProps . product . name ,
70+ imageUrl : params . newProps . product . thumb
71+ } ;
72+
73+ params . originalProps . setAttributes ( attributes ) ;
74+ } ;
75+
76+ const editor = < div className = "ec-store-block ec-store-block-buynow" >
77+ { ! attributes . id &&
78+ < div >
79+ < div className = "image" >
80+ </ div >
81+
82+ < div className = "button-container" >
83+ < button className = "button ec-store-block-button" onClick = { ( ) => { var params = { 'saveCallback' :saveCallback , 'props' : props } ; ecwid_open_product_popup ( params ) ; } } > { EcwidGutenbergParams . chooseProduct } </ button >
84+ </ div >
85+ </ div >
86+ }
87+
88+ { attributes . id &&
89+ < div className = "image" >
90+ </ div >
91+ }
92+ </ div > ;
93+
94+ function buildToggle ( props , name , label ) {
95+ return < ToggleControl
96+ label = { label }
97+ checked = { props . attributes [ name ] }
98+ onChange = { ( ) => props . setAttributes ( { [ name ] : ! props . attributes [ name ] } ) }
99+ />
100+ }
101+
102+ function openEcwidProductPopup ( props ) {
103+ ecwid_open_product_popup ( { 'saveCallback' : saveCallback , 'props' : props } ) ;
104+ }
105+
106+ return ( [
107+ editor ,
108+ < InspectorControls >
109+ { attributes . id &&
110+ < div >
111+ < div className = "ec-store-inspector-row" >
112+ < label className = "ec-store-inspector-subheader" > { __ ( 'Linked product' , 'ecwid-shopping-cart' ) } </ label >
113+ </ div >
114+
115+ < div className = "ec-store-inspector-row" >
116+
117+ { EcwidGutenbergParams . products && EcwidGutenbergParams . products [ attributes . id ] &&
118+ < label > { EcwidGutenbergParams . products [ attributes . id ] . name } </ label >
119+ }
120+
121+ < button className = "button" onClick = { ( ) => openEcwidProductPopup ( props ) } > { __ ( 'Change' , 'ecwid-shopping-cart' ) } </ button >
122+ </ div >
123+ </ div >
124+ }
125+ { ! attributes . id &&
126+ < div className = "ec-store-inspector-row" >
127+ < button className = "button" onClick = { ( ) => openEcwidProductPopup ( props ) } > { __ ( 'Choose product' , 'ecwid-shopping-cart' ) } </ button >
128+ </ div >
129+ }
130+
131+ < br />
132+ < PanelBody title = { __ ( 'Appearance' , 'ecwid-shopping-cart' ) } initialOpen = { false } >
133+ { buildToggle ( props , 'show_price_on_button' , __ ( 'Show price inside the «Buy now» button' , 'ecwid-shopping-cart' ) ) }
134+ { buildToggle ( props , 'center_align' , __ ( 'Center align on a page' , 'ecwid-shopping-cart' ) ) }
135+ </ PanelBody >
136+ </ InspectorControls >
137+ ] ) ;
138+ } ,
139+
140+ save : function ( props ) {
141+ return false ;
142+ } ,
143+
144+ } ) ;
145+
146+ }
0 commit comments