Skip to content

Commit e8dbda4

Browse files
committed
Merge branch 'feat/mailerlite-new-actions' into rishad-dev-tmp
2 parents e458d21 + bf6e12b commit e8dbda4

File tree

8 files changed

+261
-184
lines changed

8 files changed

+261
-184
lines changed

frontend-dev/src/components/AllIntegrations/MailerLite/EditMailerLite.jsx

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable no-param-reassign */
22

3-
import { useState } from 'react'
3+
import { useEffect, useState } from 'react'
44
import { useNavigate, useParams } from 'react-router-dom'
55
import { useRecoilState, useRecoilValue } from 'recoil'
66
import { $actionConf, $formFields, $newFlow } from '../../../GlobalStates'
@@ -13,6 +13,7 @@ import { saveActionConf } from '../IntegrationHelpers/IntegrationHelpers'
1313
import IntegrationStepThree from '../IntegrationHelpers/IntegrationStepThree'
1414
import { checkMappedFields, handleInput } from './MailerLiteCommonFunc'
1515
import MailerLiteIntegLayout from './MailerLiteIntegLayout'
16+
import { create } from 'mutative'
1617

1718
function EditMailerLite({ allIntegURL }) {
1819
const navigate = useNavigate()
@@ -45,6 +46,16 @@ function EditMailerLite({ allIntegURL }) {
4546
})
4647
}
4748

49+
useEffect(() => {
50+
if (!mailerLiteConf?.action) {
51+
setMailerLiteConf(prev =>
52+
create(prev, draftConf => {
53+
draftConf.action = 'add_subscriber'
54+
})
55+
)
56+
}
57+
}, [])
58+
4859
return (
4960
<div style={{ width: 900 }}>
5061
<SnackMsg snack={snack} setSnackbar={setSnackbar} />
@@ -53,7 +64,7 @@ function EditMailerLite({ allIntegURL }) {
5364
<b className="wdt-200 d-in-b">{__('Integration Name:', 'bit-integrations')}</b>
5465
<input
5566
className="btcd-paper-inp w-5"
56-
onChange={(e) => handleInput(e, mailerLiteConf, setMailerLiteConf)}
67+
onChange={e => handleInput(e, mailerLiteConf, setMailerLiteConf, loading, setLoading)}
5768
name="name"
5869
value={mailerLiteConf.name}
5970
type="text"
@@ -66,9 +77,7 @@ function EditMailerLite({ allIntegURL }) {
6677
<MailerLiteIntegLayout
6778
formID={flow.triggered_entity_id}
6879
formFields={formField}
69-
handleInput={(e) =>
70-
handleInput(e, mailerLiteConf, setMailerLiteConf, setLoading, setSnackbar)
71-
}
80+
handleInput={e => handleInput(e, mailerLiteConf, setMailerLiteConf, loading, setLoading)}
7281
mailerLiteConf={mailerLiteConf}
7382
setMailerLiteConf={setMailerLiteConf}
7483
loading={loading}

frontend-dev/src/components/AllIntegrations/MailerLite/MailerLite.jsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ function MailerLite({ formFields, setFlow, flow, allIntegURL }) {
3535
mailerLiteFields: [],
3636
groups: [],
3737
group_ids: [],
38-
actions: {}
38+
action: ''
3939
})
4040

4141
const saveConfig = () => {
@@ -96,22 +96,24 @@ function MailerLite({ formFields, setFlow, flow, allIntegURL }) {
9696
style={{ ...(step === 2 && { width: 900, height: 'auto', overflow: 'visible' }) }}>
9797
<MailerLiteIntegLayout
9898
formFields={formFields}
99-
handleInput={e => handleInput(e, mailerLiteConf, setMailerLiteConf, setLoading, setSnackbar)}
99+
handleInput={e => handleInput(e, mailerLiteConf, setMailerLiteConf, loading, setLoading)}
100100
mailerLiteConf={mailerLiteConf}
101101
setMailerLiteConf={setMailerLiteConf}
102102
loading={loading}
103103
setLoading={setLoading}
104104
setSnackbar={setSnackbar}
105105
/>
106106

107-
<button
108-
onClick={() => nextPage(3)}
109-
// disabled={!mailerLiteConf?.recipient_id}
110-
className="btn f-right btcd-btn-lg purple sh-sm flx"
111-
type="button">
112-
{__('Next', 'bit-integrations')} &nbsp;
113-
<div className="btcd-icn icn-arrow_back rev-icn d-in-b" />
114-
</button>
107+
{mailerLiteConf?.action && (
108+
<button
109+
onClick={() => nextPage(3)}
110+
disabled={!checkMappedFields(mailerLiteConf)}
111+
className="btn f-right btcd-btn-lg purple sh-sm flx"
112+
type="button">
113+
{__('Next', 'bit-integrations')} &nbsp;
114+
<div className="btcd-icn icn-arrow_back rev-icn d-in-b" />
115+
</button>
116+
)}
115117
</div>
116118

117119
{/* STEP 3 */}

frontend-dev/src/components/AllIntegrations/MailerLite/MailerLiteAuthorization.jsx

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
/* eslint-disable no-unused-expressions */
22
import { useState } from 'react'
33
import { __ } from '../../../Utils/i18nwrap'
4+
import tutorialLinks from '../../../Utils/StaticData/tutorialLinks'
45
import LoaderSm from '../../Loaders/LoaderSm'
56
import Note from '../../Utilities/Note'
6-
import { mailerliteRefreshFields } from './MailerLiteCommonFunc'
7-
import tutorialLinks from '../../../Utils/StaticData/tutorialLinks'
87
import TutorialLink from '../../Utilities/TutorialLink'
8+
import { authorization } from './MailerLiteCommonFunc'
99

1010
export default function MailerLiteAuthorization({
1111
mailerLiteConf,
@@ -17,7 +17,7 @@ export default function MailerLiteAuthorization({
1717
setSnackbar,
1818
isInfo
1919
}) {
20-
const [isAuthorized, setisAuthorized] = useState(false)
20+
const [isAuthorized, setIsAuthorized] = useState(false)
2121
const [error, setError] = useState({ name: '', auth_token: '' })
2222
const { mailerLite } = tutorialLinks
2323

@@ -34,14 +34,15 @@ export default function MailerLiteAuthorization({
3434
!mailerLiteConf?.default
3535
setstep(2)
3636
}
37-
const handleInput = (e) => {
37+
const handleInput = e => {
3838
const newConf = { ...mailerLiteConf }
3939
const rmError = { ...error }
4040
rmError[e.target.name] = ''
4141
newConf[e.target.name] = e.target.value
4242
setError(rmError)
4343
setMailerLiteConf(newConf)
4444
}
45+
4546
const note = `
4647
<h4>${__('Step of generate API token:', 'bit-integrations')}</h4>
4748
<ul>
@@ -135,17 +136,7 @@ export default function MailerLiteAuthorization({
135136
{!isInfo && (
136137
<div>
137138
<button
138-
onClick={() =>
139-
mailerliteRefreshFields(
140-
mailerLiteConf,
141-
setMailerLiteConf,
142-
setError,
143-
setisAuthorized,
144-
loading,
145-
setLoading,
146-
'authorization'
147-
)
148-
}
139+
onClick={() => authorization(mailerLiteConf, setIsAuthorized, loading, setLoading)}
149140
className="btn btcd-btn-lg purple sh-sm flx"
150141
type="button"
151142
disabled={

frontend-dev/src/components/AllIntegrations/MailerLite/MailerLiteCommonFunc.js

Lines changed: 89 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,36 @@
22
import toast from 'react-hot-toast'
33
import { __ } from '../../../Utils/i18nwrap'
44
import bitsFetch from '../../../Utils/bitsFetch'
5+
import { create } from 'mutative'
56

6-
export const handleInput = (
7-
e,
8-
mailerLiteConf,
9-
setMailerLiteConf,
10-
setLoading,
11-
setSnackbar,
12-
isNew,
13-
error,
14-
setError
15-
) => {
16-
const newConf = { ...mailerLiteConf }
17-
const { name } = e.target
18-
if (e.target.value !== '') {
19-
newConf[name] = e.target.value
20-
} else {
21-
delete newConf[name]
22-
}
23-
setMailerLiteConf({ ...newConf })
7+
export const handleInput = (e, mailerLiteConf, setMailerLiteConf, loading, setLoading) => {
8+
const { name, value } = e.target
9+
10+
setMailerLiteConf(prev =>
11+
create(prev, draftConf => {
12+
draftConf[name] = value
13+
14+
if (name === 'action' && value !== '') {
15+
mailerliteRefreshFields(draftConf, setMailerLiteConf, loading, setLoading)
16+
}
17+
})
18+
)
2419
}
2520

26-
export const generateMappedField = (mailerLiteConf) => {
27-
const requiredFlds = mailerLiteConf?.mailerLiteFields.filter((fld) => fld.required === true)
21+
export const generateMappedField = mailerLiteConf => {
22+
const requiredFlds = mailerLiteConf?.mailerLiteFields.filter(fld => fld.required === true)
2823
return requiredFlds.length > 0
29-
? requiredFlds.map((field) => ({
24+
? requiredFlds.map(field => ({
3025
formField: '',
3126
mailerLiteFormField: field.key
3227
}))
3328
: [{ formField: '', mailerLiteFormField: '' }]
3429
}
3530

36-
export const checkMappedFields = (mailerLiteConf) => {
31+
export const checkMappedFields = mailerLiteConf => {
3732
const mappedFields = mailerLiteConf?.field_map
3833
? mailerLiteConf.field_map.filter(
39-
(mappedField) =>
34+
mappedField =>
4035
!mappedField.formField ||
4136
!mappedField.mailerLiteFormField ||
4237
(!mappedField.formField === 'custom' && !mappedField.customValue)
@@ -47,57 +42,89 @@ export const checkMappedFields = (mailerLiteConf) => {
4742
}
4843
return true
4944
}
50-
export const mailerliteRefreshFields = (
51-
confTmp,
52-
setConf,
53-
setError,
54-
setisAuthorized,
55-
loading,
56-
setLoading,
57-
type
58-
) => {
45+
46+
export const authorization = (confTmp, setIsAuthorized, loading, setLoading) => {
5947
if (!confTmp.auth_token) {
60-
setError({
61-
auth_token: !confTmp.auth_token ? __("API Key can't be empty", 'bit-integrations') : ''
62-
})
48+
toast.error(__("API Key can't be empty", 'bit-integrations'))
49+
6350
return
6451
}
65-
setError({})
66-
if (type === 'authorization') {
67-
setLoading({ ...loading, auth: true })
68-
} else {
69-
setLoading({ ...loading, field: true })
70-
}
52+
53+
setLoading({ ...loading, auth: true })
7154

7255
const requestParams = {
7356
auth_token: confTmp.auth_token,
7457
version: confTmp.version
7558
}
7659

77-
bitsFetch(requestParams, 'mailerlite_refresh_fields').then((result) => {
60+
bitsFetch(requestParams, 'mailerlite_authorization').then(result => {
61+
setLoading({ ...loading, auth: false })
62+
7863
if (result && result.success) {
79-
const newConf = { ...confTmp }
80-
if (result.data) {
81-
newConf.mailerLiteFields = result.data
82-
}
83-
setConf(newConf)
84-
setisAuthorized(true)
85-
if (type === 'authorization') {
86-
setLoading({ ...loading, auth: false })
87-
toast.success(__('Authorized Successfully', 'bit-integrations'))
88-
} else {
89-
setLoading({ ...loading, field: false })
90-
toast.success(__('Fields refresh successfully', 'bit-integrations'))
91-
}
64+
setIsAuthorized(true)
65+
66+
toast.success(__('Authorized Successfully', 'bit-integrations'))
67+
9268
return
9369
}
94-
if (type === 'authorization') {
95-
setLoading({ ...loading, auth: false })
96-
toast.error(__('Authorized failed', 'bit-integrations'))
97-
} else {
98-
setLoading({ ...loading, field: false })
99-
toast.error(__('Fields refresh failed', 'bit-integrations'))
70+
71+
toast.error(__('Authorized failed', 'bit-integrations'))
72+
})
73+
}
74+
75+
export const mailerliteRefreshFields = (confTmp, setConf, loading, setLoading) => {
76+
if (!confTmp.auth_token) {
77+
toast.error(__("API Key can't be empty", 'bit-integrations'))
78+
79+
return
80+
}
81+
82+
setLoading({ ...loading, field: true })
83+
84+
if (confTmp?.action !== 'add_subscriber') {
85+
setLoading({ ...loading, field: false })
86+
87+
setConf(prev =>
88+
create(prev, draftConf => {
89+
draftConf.mailerLiteFields = [
90+
{
91+
key: 'email',
92+
label: 'Email',
93+
required: true
94+
}
95+
]
96+
97+
draftConf.field_map = generateMappedField(draftConf)
98+
})
99+
)
100+
101+
toast.success(__('Fields refresh successfully', 'bit-integrations'))
102+
103+
return
104+
}
105+
106+
const requestParams = {
107+
auth_token: confTmp.auth_token,
108+
version: confTmp.version
109+
}
110+
111+
bitsFetch(requestParams, 'mailerlite_refresh_fields').then(result => {
112+
setLoading({ ...loading, field: false })
113+
114+
if (result && result.success) {
115+
setConf(prev =>
116+
create(prev, draftConf => {
117+
draftConf.mailerLiteFields = result.data
118+
draftConf.field_map = generateMappedField(draftConf)
119+
})
120+
)
121+
122+
toast.success(__('Fields refresh successfully', 'bit-integrations'))
123+
124+
return
100125
}
126+
127+
toast.error(__('Fields refresh failed', 'bit-integrations'))
101128
})
102129
}
103130

@@ -109,7 +136,7 @@ export const getAllGroups = (confTmp, setConf, loding, setLoading) => {
109136
version: confTmp.version
110137
}
111138

112-
bitsFetch(requestParams, 'mailerlite_fetch_all_groups').then((result) => {
139+
bitsFetch(requestParams, 'mailerlite_fetch_all_groups').then(result => {
113140
if (result && result.success) {
114141
const newConf = { ...confTmp }
115142
if (result.data) {

0 commit comments

Comments
 (0)