| 1 |
import eventBus from '../eventBus.js'; |
| 2 |
import CustomSelect from '../Forms/CustomSelect.js'; |
| 3 |
import SyncFields from '../Forms/SyncFields.js'; |
| 4 |
import ApiMixin from '../Api/ApiMixin.js'; |
| 5 |
import {validate} from "../Plugins/Validation.js"; |
| 6 |
|
| 7 |
const template = ` |
| 8 |
<div v-if="currentStep === 0" class="header-title-ml"> |
| 9 |
<h1>Connect MailerLite with WooCommerce</h1> |
| 10 |
<p>Build custom segments, send automations, and track purchase activity in MailerLite. Enter your MailerLite account's API key to start using the integration.</p> |
| 11 |
</div> |
| 12 |
|
| 13 |
<div v-if="currentStep === 1"> |
| 14 |
<button @click="backToApi" type="button" id="woo_ml_wizard_back_step_1" class="btn btn-link-ml flex-start-ml flex-ml align-items-center-ml" style="margin-bottom: 1rem;"> |
| 15 |
<svg width="14" height="14" fill="none" style="margin-right: 0.25rem;" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"></path></svg> |
| 16 |
Back to API key</button> |
| 17 |
<div class="header-title-ml"> |
| 18 |
<h1 v-if="mlPlatform === 'rewrite'">Import your customers to MailerLite</h1> |
| 19 |
<h1 v-else>Select subscriber group</h1> |
| 20 |
<p>Subscribers from WooCommerce will join this MailerLite subscriber group.</p> |
| 21 |
</div> |
| 22 |
</div> |
| 23 |
|
| 24 |
<!-- Wizard content --> |
| 25 |
<div class="woo-ml-wizard-content"> |
| 26 |
<div v-if="currentStep === 0" id="woo-ml-wizard-step-one"> |
| 27 |
<div class="input-block-ml"> |
| 28 |
<label for="wooMlApiKey" class="settings-label mb-3-ml">API key</label> |
| 29 |
<div class="api-key-input"> |
| 30 |
<input ref="wooMlApiKey" type="text" name="api-key" placeholder="Enter your MailerLite API key" v-model="wooMlApiKey" :disabled="isLoading" data-testid="wizard-api-key"> |
| 31 |
<button @click="connectAccount" :class="{ 'woo-ml-button-loading': isLoading }" :disabled="isLoading" type="button" id="wooMlWizardApiKeyBtn" class="btn-primary-ml" data-testid="wizard-connect-account-btn"><span class="woo-ml-button-text">Connect account</span></button> |
| 32 |
</div> |
| 33 |
<div class="signup-link-ml"> |
| 34 |
<p>Don't you have a MailerLite account yet? Click to <a href="https://www.mailerlite.com/signup?utm_source=referral&utm_medium=woocommerce&utm_campaign=integration" target="_blank">Sign up</a></p> |
| 35 |
</div> |
| 36 |
</div> |
| 37 |
|
| 38 |
<div class="border-top-ml"> |
| 39 |
<div class="header-title-ml" style="margin-bottom: 0;"> |
| 40 |
<h2>Having trouble? Follow the instructions:</h2> |
| 41 |
<ol class="instructions-list"> |
| 42 |
<li>Access your MailerLite account and go to the <a href="https://dashboard.mailerlite.com/integrations" target="_blank" class="body-link-ml">Integrations tab.</a></li> |
| 43 |
<li>Locate the MailerLite API section and click Use and then Generate new token.</li> |
| 44 |
<li>Enter a name for the token such as "WooCommerce" and click Generate token.</li> |
| 45 |
<li>Copy the token and return to the Integrations page in WooCommerce. Paste your key in the API key box and click Connect account. |
| 46 |
</li> |
| 47 |
</ol> |
| 48 |
</div> |
| 49 |
<div style="display: none;"> |
| 50 |
<h3>Not a client of MailerLite? Click Sign up to create your account</h3> |
| 51 |
<a class="btn btn-secondary-ml" href="https://www.mailerlite.com/signup?utm_source=referral&utm_medium=woocommerce&utm_campaign=integration" target="_blank">Create account</a> |
| 52 |
</div> |
| 53 |
</div> |
| 54 |
<div class="border-top-ml"> |
| 55 |
<label class="settings-label-medium" style="margin:0;">Still having trouble connecting to your account? <a @click="openDebugLog" id="openDebugLog">Click here</a> for an advanced troubleshooting. </label> |
| 56 |
</div> |
| 57 |
</div> |
| 58 |
|
| 59 |
<div v-if="currentStep === 1"> |
| 60 |
<div id="woo-ml-wizard-step-two"> |
| 61 |
<label for="wooMlSubGroup" class="settings-label mb-3-ml">Group</label> |
| 62 |
<label class="input-mailerlite mb-2-ml" style="display: flex;"> |
| 63 |
<custom-select |
| 64 |
ref="wooMlSubGroupComponent" |
| 65 |
name="subscriber-group" |
| 66 |
style="width: 100%;" |
| 67 |
:options="groups" |
| 68 |
v-model="selectedGroup" |
| 69 |
placeholder="Select group" |
| 70 |
></custom-select> |
| 71 |
<button @click="createGroup" id="createGroupModal" type="button" class="btn-secondary-ml" style="margin-left: 0.5rem; white-space: nowrap;" data-testid="wizard-create-group-btn">Create group</button> |
| 72 |
</label> |
| 73 |
|
| 74 |
</div> |
| 75 |
<label class="settings-label-small mt-6-ml">{{ accountInfo }}</label> |
| 76 |
<div v-if="mlPlatform === 'classic'" class="border-top-ml"> |
| 77 |
<div id="woo-ml-wizard-step-two"> |
| 78 |
<div class="header-title-ml"> |
| 79 |
<h1>Consumer details</h1> |
| 80 |
<p>The Consumer key and Consumer secret are required for e-commerce automations to work in MailerLite Classic.</p> |
| 81 |
</div> |
| 82 |
<div class="form-group-ml vertical"> |
| 83 |
<label for="wooMlSubGroup" class="settings-label flex-ml align-items-center-ml mb-3-ml mt-0-ml">Consumer key |
| 84 |
</label> |
| 85 |
<label class="input-mailerlite mb-2-ml mt-0-ml"> |
| 86 |
<input ref="consumerKeyRef" type="text" name="consumer_key" class="woo-ml-form-checkbox text-input flex-start-ml" |
| 87 |
style="width: 100%;" v-model="consumerKey"> |
| 88 |
</label> |
| 89 |
<label class="settings-label-small mt-0-ml mb-0-ml">Find out how to generate secret <a href="https://docs.woocommerce.com/document/woocommerce-rest-api/" target="_blank">here.</a></label> |
| 90 |
</div> |
| 91 |
<div class="form-group-ml vertical"> |
| 92 |
<label for="wooMlSubGroup" class="settings-label flex-ml align-items-center-ml mb-3-ml mt-0-ml">Consumer secret |
| 93 |
</label> |
| 94 |
<label class="input-mailerlite mb-2-ml mt-0-ml"> |
| 95 |
<input ref="consumerSecretRef" type="text" name="consumer_secret" class="woo-ml-form-checkbox text-input flex-start-ml" |
| 96 |
style="width: 100%;" v-model="consumerSecret"> |
| 97 |
</label> |
| 98 |
<label class="settings-label-small mt-0-ml">Find out how to generate secret <a href="https://docs.woocommerce.com/document/woocommerce-rest-api/" target="_blank">here.</a></label> |
| 99 |
</div> |
| 100 |
</div> |
| 101 |
</div> |
| 102 |
|
| 103 |
<div class="form-group-ml vertical"> |
| 104 |
<label for="sync_fields" class="settings-label flex-ml align-items-center-ml mb-3-ml"> |
| 105 |
Synced fields |
| 106 |
<div class="tooltip-ml"> |
| 107 |
<span class="tooltiptext-ml">Select which fields you would like to sync. Please note that Email and Name fields are mandatory.</span> |
| 108 |
</div> |
| 109 |
</label> |
| 110 |
<label class="input-mailerlite" data-testid="wizard-sync-fields"> |
| 111 |
<sync-fields |
| 112 |
id="sync_fields" |
| 113 |
multiple="multiple" |
| 114 |
v-model="selectedSyncFields" |
| 115 |
placeholder="Click to select fields you want to sync" |
| 116 |
style="width: 100%;" |
| 117 |
:options="syncFields" |
| 118 |
:model-value="selectedSyncFields" |
| 119 |
/> |
| 120 |
</label> |
| 121 |
</div> |
| 122 |
|
| 123 |
<div class="settings-block" style="display: flex; justify-content: space-between; padding-top: 2rem;"> |
| 124 |
<button @click="startImport" :class="{ 'woo-ml-button-loading': isLoading }" :disabled="isLoading" id="startImport" type="button" class="btn-primary-ml" data-testid="wizard-start-import-btn"><span class="woo-ml-button-text">Next</span></button> |
| 125 |
</div> |
| 126 |
</div> |
| 127 |
</div> |
| 128 |
`; |
| 129 |
|
| 130 |
const Wizard = { |
| 131 |
components: { |
| 132 |
CustomSelect, |
| 133 |
SyncFields |
| 134 |
}, |
| 135 |
template, |
| 136 |
mixins: [ApiMixin], |
| 137 |
mounted() { |
| 138 |
const defaultValues = this.syncFields |
| 139 |
.filter((opt) => opt.default) |
| 140 |
.map((opt) => opt.value); |
| 141 |
this.selectedSyncFields = [...this.selectedSyncFields, ...defaultValues]; |
| 142 |
}, |
| 143 |
data() { |
| 144 |
return { |
| 145 |
wooMlApiKey: '', |
| 146 |
isLoading: false, |
| 147 |
mlPlatform: woo_mailerlite_admin_data.account.platform ?? 'rewrite', |
| 148 |
totalUntrackedResources: 0, |
| 149 |
accountName: woo_mailerlite_admin_data.account.accountName ?? '', |
| 150 |
consumerKey: '', |
| 151 |
consumerSecret: '', |
| 152 |
selectedSyncFields: [], |
| 153 |
currentStep: parseInt(woo_mailerlite_admin_data.currentStep) ?? 0, |
| 154 |
groups: [ |
| 155 |
{ |
| 156 |
value: 1, |
| 157 |
text: 'Group 1' |
| 158 |
}, |
| 159 |
{ |
| 160 |
value: 2, |
| 161 |
text: 'Group 2' |
| 162 |
}, |
| 163 |
{ |
| 164 |
value: 3, |
| 165 |
text: 'Group 3' |
| 166 |
}, |
| 167 |
], |
| 168 |
selectedGroup: null, |
| 169 |
syncFields: [ |
| 170 |
{ |
| 171 |
value: 'name', |
| 172 |
text: 'Name', |
| 173 |
default: true |
| 174 |
}, |
| 175 |
{ |
| 176 |
value: 'email', |
| 177 |
text: 'Email', |
| 178 |
default: true |
| 179 |
}, |
| 180 |
{ |
| 181 |
value: 'company', |
| 182 |
text: 'Company' |
| 183 |
}, |
| 184 |
{ |
| 185 |
value: 'city', |
| 186 |
text: 'City' |
| 187 |
}, |
| 188 |
{ |
| 189 |
value: 'zip', |
| 190 |
text: 'ZIP' |
| 191 |
}, |
| 192 |
{ |
| 193 |
value: 'state', |
| 194 |
text: 'State' |
| 195 |
}, |
| 196 |
{ |
| 197 |
value: 'country', |
| 198 |
text: 'Country' |
| 199 |
}, |
| 200 |
{ |
| 201 |
value: 'phone', |
| 202 |
text: 'Phone' |
| 203 |
}, |
| 204 |
], |
| 205 |
} |
| 206 |
}, |
| 207 |
computed: { |
| 208 |
accountInfo() { |
| 209 |
return this.accountName ? `Account: ${this.accountName}` : ''; |
| 210 |
}, |
| 211 |
}, |
| 212 |
methods: { |
| 213 |
|
| 214 |
backToApi() { |
| 215 |
this.currentStep = 0 |
| 216 |
eventBus.emit('wizard-step-event', 0) |
| 217 |
}, |
| 218 |
createGroup() { |
| 219 |
eventBus.emit('show-create-group-modal', this.selectedGroup) |
| 220 |
}, |
| 221 |
async connectAccount() { |
| 222 |
const apiKey = validate(this.$refs.wooMlApiKey, ['required'], 'API key is required'); |
| 223 |
if (!apiKey) { |
| 224 |
return |
| 225 |
} |
| 226 |
this.isLoading = true |
| 227 |
|
| 228 |
try { |
| 229 |
const data = await this.validateKey(this.wooMlApiKey); |
| 230 |
|
| 231 |
this.isLoading = false |
| 232 |
|
| 233 |
if (data.success) { |
| 234 |
this.currentStep = 1; |
| 235 |
this.mlPlatform = data.data.mlPlatform |
| 236 |
this.accountName = data.data.name |
| 237 |
|
| 238 |
eventBus.emit('wizard-step-event', this.currentStep) |
| 239 |
} else { |
| 240 |
eventBus.emit('alert-event', { error: data.data.message }); |
| 241 |
} |
| 242 |
} catch (err) { |
| 243 |
this.isLoading = false |
| 244 |
eventBus.emit('alert-event', { error: err.toString() }); |
| 245 |
} |
| 246 |
}, |
| 247 |
keyExists(key) { |
| 248 |
return this.syncFields.includes(key); |
| 249 |
}, |
| 250 |
async startImport() { |
| 251 |
try { |
| 252 |
this.isLoading = true |
| 253 |
const group = validate(this.$refs.wooMlSubGroupComponent.$refs.wooMlSubGroup, ['required'], 'Please select a group!') |
| 254 |
let consumerKey = true; |
| 255 |
let consumerSecret = true; |
| 256 |
if (this.$refs.consumerKeyRef) { |
| 257 |
consumerKey = validate(this.$refs.consumerKeyRef, ['required','startsWith:ck_'], {'required' : 'Please enter a consumer key!' , 'startsWith' : 'Please enter a valid consumer key'}) |
| 258 |
consumerSecret = validate(this.$refs.consumerSecretRef, ['required', 'startsWith:cs_'], {'required' : 'Please enter a consumer secret!' , 'startsWith' : 'Please enter a valid consumer secret'}) |
| 259 |
} |
| 260 |
|
| 261 |
if (!group || !consumerKey || !consumerSecret) { |
| 262 |
this.isLoading = false |
| 263 |
return |
| 264 |
} |
| 265 |
const response = await this.setupShop(this.selectedGroup, JSON.stringify(this.selectedSyncFields), this.consumerKey, this.consumerSecret); |
| 266 |
if (response.success) { |
| 267 |
eventBus.emit('group-saved', {'group': {'name' : response.data.group.name, 'id': response.data.group.id}, 'fields': this.selectedSyncFields}) |
| 268 |
eventBus.emit('wizard-step-event', 2) |
| 269 |
} |
| 270 |
this.isLoading = false |
| 271 |
} catch (err) { |
| 272 |
window.scrollTo(0,0); |
| 273 |
this.isLoading = false |
| 274 |
eventBus.emit('alert-event', { error: err.toString() }); |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
}, |
| 279 |
openDebugLog() { |
| 280 |
eventBus.emit('show-debug-modal'); |
| 281 |
} |
| 282 |
}, |
| 283 |
}; |
| 284 |
|
| 285 |
export default Wizard; |
| 286 |
|