PluginProbe
Elementor Website Builder – more than just a page builder / 3.8.0-beta1
Elementor Website Builder – more than just a page builder v3.8.0-beta1
4.3.4 4.3.3 4.3.2 4.3.1 4.3.0 4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 All 457 releases
elementor / app / modules / kit-library / assets / js / components / item-header.js

item-header.js in Elementor Website Builder – more than just a page builder 3.8.0-beta1, at app/modules/kit-library/assets/js/components/item-header.js

218 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import ApplyKitDialog from './apply-kit-dialog';
2 import ConnectDialog from './connect-dialog';
3 import Header from './layout/header';
4 import HeaderBackButton from './layout/header-back-button';
5 import Kit from '../models/kit';
6 import useDownloadLinkMutation from '../hooks/use-download-link-mutation';
7 import useKitCallToAction, { TYPE_PROMOTION, TYPE_CONNECT } from '../hooks/use-kit-call-to-action';
8 import { Dialog } from '@elementor/app-ui';
9 import { useMemo, useState } from 'react';
10 import { useSettingsContext } from '../context/settings-context';
11 import { appsEventTrackingDispatch } from 'elementor-app/event-track/apps-event-tracking';
12
13 import './item-header.scss';
14
15 /**
16 * Returns the right call to action button.
17 *
18 * @param {Kit} model
19 * @param {Object} root0
20 * @param {Function} root0.apply
21 * @param {Function} root0.onConnect
22 * @param {Function} root0.onClick
23 * @param {boolean} root0.isApplyLoading
24 * @return {Object} result
25 */
26 function useKitCallToActionButton( model, { apply, isApplyLoading, onConnect, onClick } ) {
27 const [ type, { subscriptionPlan } ] = useKitCallToAction( model.accessLevel );
28
29 return useMemo( () => {
30 if ( type === TYPE_CONNECT ) {
31 return {
32 id: 'connect',
33 text: __( 'Apply Kit', 'elementor' ), // The label is Apply kit but the this is connect button
34 hideText: false,
35 variant: 'contained',
36 color: 'primary',
37 size: 'sm',
38 onClick: ( e ) => {
39 onConnect( e );
40 onClick?.( e );
41 },
42 includeHeaderBtnClass: false,
43 };
44 }
45
46 if ( type === TYPE_PROMOTION && subscriptionPlan ) {
47 const getButtonURL = () => {
48 let url = subscriptionPlan.promotion_url;
49
50 if ( model.title ) {
51 // Remove special characters, replace spaces with '-' and convert url kit name to lowercase.
52 const cleanTitle = model.title.replace( /\s/g, '-' ).replace( /[^\w-]/g, '' ).toLowerCase();
53 url += `&utm_term=${ cleanTitle }`;
54 }
55
56 if ( model.id ) {
57 url += `&utm_content=${ model.id }`;
58 }
59
60 return url;
61 };
62
63 return {
64 id: 'promotion',
65 // Translators: %s is the subscription plan name.
66 text: __( 'Go %s', 'elementor' ).replace( '%s', subscriptionPlan.label ),
67 hideText: false,
68 variant: 'contained',
69 color: 'cta',
70 size: 'sm',
71 url: getButtonURL(),
72 target: '_blank',
73 includeHeaderBtnClass: false,
74 };
75 }
76
77 return {
78 id: 'apply',
79 text: __( 'Apply Kit', 'elementor' ),
80 className: 'e-kit-library__apply-button',
81 icon: isApplyLoading ? 'eicon-loading eicon-animation-spin' : '',
82 hideText: false,
83 variant: 'contained',
84 color: isApplyLoading ? 'disabled' : 'primary',
85 size: 'sm',
86 onClick: ( e ) => {
87 if ( ! isApplyLoading ) {
88 apply( e );
89 }
90
91 onClick?.( e );
92 },
93 includeHeaderBtnClass: false,
94 };
95 }, [ type, subscriptionPlan, isApplyLoading, apply ] );
96 }
97
98 export default function ItemHeader( props ) {
99 const { updateSettings } = useSettingsContext();
100
101 const [ isConnectDialogOpen, setIsConnectDialogOpen ] = useState( false );
102 const [ downloadLinkData, setDownloadLinkData ] = useState( null );
103 const [ error, setError ] = useState( false );
104 const kitData = {
105 kitName: props.model.title,
106 pageId: props.pageId,
107 };
108 const { mutate: apply, isLoading: isApplyLoading } = useDownloadLinkMutation(
109 props.model,
110 {
111 onSuccess: ( { data } ) => setDownloadLinkData( data ),
112 onError: ( errorResponse ) => {
113 if ( 401 === errorResponse.code ) {
114 elementorCommon.config.library_connect.is_connected = false;
115 elementorCommon.config.library_connect.current_access_level = 0;
116
117 updateSettings( {
118 is_library_connected: false,
119 access_level: 0,
120 } );
121
122 setIsConnectDialogOpen( true );
123
124 return;
125 }
126
127 setError( {
128 code: errorResponse.code,
129 message: __( 'Something went wrong.', 'elementor' ),
130 } );
131 },
132 },
133 );
134
135 const applyButton = useKitCallToActionButton( props.model, {
136 onConnect: () => setIsConnectDialogOpen( true ),
137 apply,
138 isApplyLoading,
139 onClick: () => {
140 return appsEventTrackingDispatch(
141 'kit-library/apply-kit',
142 {
143 kit_name: props.model.title,
144 element_position: 'app_header',
145 page_source: props.pageId,
146 event_type: 'click',
147 },
148 );
149 },
150 } );
151
152 const buttons = useMemo( () => [ applyButton, ...props.buttons ], [ props.buttons, applyButton ] );
153
154 return (
155 <>
156 {
157 error && (
158 <Dialog
159 title={ error.message }
160 text={ __( 'Nothing to worry about, just try again. If the problem continues, head over to the Help Center.', 'elementor' ) }
161 approveButtonText={ __( 'Learn More', 'elementor' ) }
162 approveButtonColor="link"
163 approveButtonUrl="http://go.elementor.com/app-kit-library-error/"
164 approveButtonOnClick={ () => setError( false ) }
165 dismissButtonText={ __( 'Got it', 'elementor' ) }
166 dismissButtonOnClick={ () => setError( false ) }
167 onClose={ () => setError( false ) }
168 />
169 )
170 }
171 {
172 downloadLinkData && <ApplyKitDialog
173 downloadLink={ downloadLinkData.data.download_link }
174 nonce={ downloadLinkData.meta.nonce }
175 onClose={ () => setDownloadLinkData( null ) }
176 />
177 }
178 {
179 isConnectDialogOpen && <ConnectDialog
180 pageId={ props.pageId }
181 onClose={ () => setIsConnectDialogOpen( false ) }
182 onSuccess={ ( data ) => {
183 const accessLevel = data.kits_access_level || data.access_level || 0;
184
185 elementorCommon.config.library_connect.is_connected = true;
186 elementorCommon.config.library_connect.current_access_level = accessLevel;
187
188 updateSettings( {
189 is_library_connected: true,
190 access_level: accessLevel, // BC: Check for 'access_level' prop
191 } );
192
193 if ( data.access_level < props.model.accessLevel ) {
194 return;
195 }
196
197 apply();
198 } }
199 onError={ ( message ) => setError( { message } ) }
200 />
201 }
202 <Header
203 startColumn={ <HeaderBackButton { ...kitData } /> }
204 centerColumn={ props.centerColumn }
205 buttons={ buttons }
206 { ...kitData }
207 />
208 </>
209 );
210 }
211
212 ItemHeader.propTypes = {
213 model: PropTypes.instanceOf( Kit ).isRequired,
214 centerColumn: PropTypes.node,
215 buttons: PropTypes.arrayOf( PropTypes.object ),
216 pageId: PropTypes.string,
217 };
218