| 1 |
import { useRef, useContext, useState } from 'react'; |
| 2 |
import { useNavigate } from '@reach/router'; |
| 3 |
import { OnboardingContext } from '../context/context'; |
| 4 |
import Connect from '../utils/connect'; |
| 5 |
import Layout from '../components/layout/layout'; |
| 6 |
import PageContentLayout from '../components/layout/page-content-layout'; |
| 7 |
|
| 8 |
export default function Account() { |
| 9 |
const { state, updateState, getStateObjectToUpdate } = useContext( OnboardingContext ), |
| 10 |
[ noticeState, setNoticeState ] = useState( null ), |
| 11 |
navigate = useNavigate(), |
| 12 |
pageId = 'account', |
| 13 |
nextStep = state.isHelloThemeActivated ? 'siteName' : 'hello', |
| 14 |
actionButtonRef = useRef(), |
| 15 |
alreadyHaveAccountLinkRef = useRef(); |
| 16 |
|
| 17 |
let skipButton; |
| 18 |
|
| 19 |
if ( 'completed' !== state.steps[ pageId ] ) { |
| 20 |
skipButton = { |
| 21 |
text: __( 'Skip', 'elementor' ), |
| 22 |
}; |
| 23 |
} |
| 24 |
|
| 25 |
let pageTexts = {}; |
| 26 |
|
| 27 |
if ( state.isLibraryConnected ) { |
| 28 |
pageTexts = { |
| 29 |
firstLine: __( 'To get the most out of Elementor, we\'ll help you take your first steps:', 'elementor' ), |
| 30 |
listItems: [ |
| 31 |
__( 'Set your site\'s theme', 'elementor' ), |
| 32 |
__( 'Give your site a name & logo', 'elementor' ), |
| 33 |
__( 'Choose how to start creating', 'elementor' ), |
| 34 |
], |
| 35 |
}; |
| 36 |
} else { |
| 37 |
pageTexts = { |
| 38 |
firstLine: __( 'To get the most out of Elementor, we’ll connect your account.', 'elementor' ) + |
| 39 |
' ' + __( 'Then you can:', 'elementor' ), |
| 40 |
listItems: [ |
| 41 |
__( 'Choose from countless professional templates', 'elementor' ), |
| 42 |
__( 'Manage your site with our handy dashboard', 'elementor' ), |
| 43 |
__( 'Take part in the community forum, share & grow together', 'elementor' ), |
| 44 |
], |
| 45 |
}; |
| 46 |
} |
| 47 |
|
| 48 |
// If the user is not connected, the on-click action is handled by the <Connect> component, so there is no onclick |
| 49 |
// property. |
| 50 |
const actionButton = { |
| 51 |
role: 'button', |
| 52 |
}; |
| 53 |
|
| 54 |
if ( state.isLibraryConnected ) { |
| 55 |
actionButton.text = __( 'Let’s do it', 'elementor' ); |
| 56 |
|
| 57 |
actionButton.onClick = () => { |
| 58 |
elementorCommon.events.dispatchEvent( { |
| 59 |
event: 'next', |
| 60 |
version: '', |
| 61 |
details: { |
| 62 |
placement: elementorAppConfig.onboarding.eventPlacement, |
| 63 |
step: state.currentStep, |
| 64 |
}, |
| 65 |
} ); |
| 66 |
|
| 67 |
updateState( getStateObjectToUpdate( state, 'steps', pageId, 'completed' ) ); |
| 68 |
|
| 69 |
navigate( 'onboarding/' + nextStep ); |
| 70 |
}; |
| 71 |
} else { |
| 72 |
actionButton.text = __( 'Create my account', 'elementor' ); |
| 73 |
actionButton.href = elementorAppConfig.onboarding.urls.signUp + elementorAppConfig.onboarding.utms.connectCta; |
| 74 |
actionButton.ref = actionButtonRef; |
| 75 |
actionButton.onClick = () => { |
| 76 |
elementorCommon.events.dispatchEvent( { |
| 77 |
event: 'create account', |
| 78 |
version: '', |
| 79 |
details: { |
| 80 |
placement: elementorAppConfig.onboarding.eventPlacement, |
| 81 |
source: 'cta', |
| 82 |
}, |
| 83 |
} ); |
| 84 |
}; |
| 85 |
} |
| 86 |
|
| 87 |
const connectSuccessCallback = ( data ) => { |
| 88 |
const stateToUpdate = getStateObjectToUpdate( state, 'steps', pageId, 'completed' ); |
| 89 |
|
| 90 |
stateToUpdate.isLibraryConnected = true; |
| 91 |
|
| 92 |
elementorCommon.config.library_connect.is_connected = true; |
| 93 |
elementorCommon.config.library_connect.current_access_level = data.kits_access_level || data.access_level || 0; |
| 94 |
|
| 95 |
updateState( stateToUpdate ); |
| 96 |
|
| 97 |
elementorCommon.events.dispatchEvent( { |
| 98 |
event: 'indication prompt', |
| 99 |
version: '', |
| 100 |
details: { |
| 101 |
placement: elementorAppConfig.onboarding.eventPlacement, |
| 102 |
step: state.currentStep, |
| 103 |
action_state: 'success', |
| 104 |
action: 'connect account', |
| 105 |
}, |
| 106 |
} ); |
| 107 |
|
| 108 |
setNoticeState( { |
| 109 |
type: 'success', |
| 110 |
icon: 'eicon-check-circle-o', |
| 111 |
message: 'Alrighty - your account is connected.', |
| 112 |
} ); |
| 113 |
|
| 114 |
navigate( 'onboarding/' + nextStep ); |
| 115 |
}; |
| 116 |
|
| 117 |
const connectFailureCallback = () => { |
| 118 |
elementorCommon.events.dispatchEvent( { |
| 119 |
event: 'indication prompt', |
| 120 |
version: '', |
| 121 |
details: { |
| 122 |
placement: elementorAppConfig.onboarding.eventPlacement, |
| 123 |
step: state.currentStep, |
| 124 |
action_state: 'failure', |
| 125 |
action: 'connect account', |
| 126 |
}, |
| 127 |
} ); |
| 128 |
|
| 129 |
setNoticeState( { |
| 130 |
type: 'error', |
| 131 |
icon: 'eicon-warning', |
| 132 |
message: __( 'Oops, the connection failed. Try again.', 'elementor' ), |
| 133 |
} ); |
| 134 |
|
| 135 |
navigate( 'onboarding/' + nextStep ); |
| 136 |
}; |
| 137 |
|
| 138 |
return ( |
| 139 |
<Layout pageId={ pageId } nextStep={ nextStep }> |
| 140 |
<PageContentLayout |
| 141 |
image={ elementorCommon.config.urls.assets + 'images/app/onboarding/Illustration_Account.svg' } |
| 142 |
title={ __( 'You\'re here! Let\'s set things up.', 'elementor' ) } |
| 143 |
actionButton={ actionButton } |
| 144 |
skipButton={ skipButton } |
| 145 |
noticeState={ noticeState } |
| 146 |
> |
| 147 |
{ actionButton.ref && ! state.isLibraryConnected && |
| 148 |
<Connect |
| 149 |
buttonRef={ actionButton.ref } |
| 150 |
successCallback={ ( data ) => connectSuccessCallback( data ) } |
| 151 |
errorCallback={ connectFailureCallback } |
| 152 |
/> } |
| 153 |
<span> |
| 154 |
{ pageTexts.firstLine } |
| 155 |
</span> |
| 156 |
<ul> |
| 157 |
{ pageTexts.listItems.map( ( listItem, index ) => { |
| 158 |
return <li key={ 'listItem' + index }>{ listItem }</li>; |
| 159 |
} ) } |
| 160 |
</ul> |
| 161 |
</PageContentLayout> |
| 162 |
{ |
| 163 |
! state.isLibraryConnected && ( |
| 164 |
<div className="e-onboarding__footnote"> |
| 165 |
<p> |
| 166 |
{ __( 'Already have one?', 'elementor' ) + ' ' } |
| 167 |
<a |
| 168 |
ref={ alreadyHaveAccountLinkRef } |
| 169 |
href={ elementorAppConfig.onboarding.urls.connect + elementorAppConfig.onboarding.utms.connectCtaLink } |
| 170 |
onClick={ () => { |
| 171 |
elementorCommon.events.dispatchEvent( { |
| 172 |
event: 'connect account', |
| 173 |
version: '', |
| 174 |
details: { |
| 175 |
placement: elementorAppConfig.onboarding.eventPlacement, |
| 176 |
}, |
| 177 |
} ); |
| 178 |
} } |
| 179 |
> |
| 180 |
{ __( 'Connect your account', 'elementor' ) } |
| 181 |
</a> |
| 182 |
</p> |
| 183 |
<Connect |
| 184 |
buttonRef={ alreadyHaveAccountLinkRef } |
| 185 |
successCallback={ connectSuccessCallback } |
| 186 |
errorCallback={ connectFailureCallback } |
| 187 |
/> |
| 188 |
</div> |
| 189 |
) |
| 190 |
} |
| 191 |
</Layout> |
| 192 |
); |
| 193 |
} |
| 194 |
|