Skip to content

Commit 6e6d088

Browse files
committed
refactor: mailerLite authorization
1 parent 2921e79 commit 6e6d088

File tree

4 files changed

+94
-25
lines changed

4 files changed

+94
-25
lines changed

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

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { useState } from 'react'
33
import { __ } from '../../../Utils/i18nwrap'
44
import LoaderSm from '../../Loaders/LoaderSm'
55
import Note from '../../Utilities/Note'
6-
import { mailerliteRefreshFields } from './MailerLiteCommonFunc'
6+
import { authorization, mailerliteRefreshFields } from './MailerLiteCommonFunc'
77
import tutorialLinks from '../../../Utils/StaticData/tutorialLinks'
88
import TutorialLink from '../../Utilities/TutorialLink'
99

@@ -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,7 +34,7 @@ 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] = ''
@@ -135,17 +135,7 @@ export default function MailerLiteAuthorization({
135135
{!isInfo && (
136136
<div>
137137
<button
138-
onClick={() =>
139-
mailerliteRefreshFields(
140-
mailerLiteConf,
141-
setMailerLiteConf,
142-
setError,
143-
setisAuthorized,
144-
loading,
145-
setLoading,
146-
'authorization'
147-
)
148-
}
138+
onClick={() => authorization(mailerLiteConf, setError, setIsAuthorized, loading, setLoading)}
149139
className="btn btcd-btn-lg purple sh-sm flx"
150140
type="button"
151141
disabled={

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

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ export const handleInput = (
2323
setMailerLiteConf({ ...newConf })
2424
}
2525

26-
export const generateMappedField = (mailerLiteConf) => {
27-
const requiredFlds = mailerLiteConf?.mailerLiteFields.filter((fld) => fld.required === true)
26+
export const generateMappedField = mailerLiteConf => {
27+
const requiredFlds = mailerLiteConf?.mailerLiteFields.filter(fld => fld.required === true)
2828
return requiredFlds.length > 0
29-
? requiredFlds.map((field) => ({
29+
? requiredFlds.map(field => ({
3030
formField: '',
3131
mailerLiteFormField: field.key
3232
}))
3333
: [{ formField: '', mailerLiteFormField: '' }]
3434
}
3535

36-
export const checkMappedFields = (mailerLiteConf) => {
36+
export const checkMappedFields = mailerLiteConf => {
3737
const mappedFields = mailerLiteConf?.field_map
3838
? mailerLiteConf.field_map.filter(
39-
(mappedField) =>
39+
mappedField =>
4040
!mappedField.formField ||
4141
!mappedField.mailerLiteFormField ||
4242
(!mappedField.formField === 'custom' && !mappedField.customValue)
@@ -47,11 +47,43 @@ export const checkMappedFields = (mailerLiteConf) => {
4747
}
4848
return true
4949
}
50+
51+
export const authorization = (confTmp, setError, setIsAuthorized, loading, setLoading) => {
52+
if (!confTmp.auth_token) {
53+
setError({
54+
auth_token: !confTmp.auth_token ? __("API Key can't be empty", 'bit-integrations') : ''
55+
})
56+
57+
return
58+
}
59+
setError({})
60+
setLoading({ ...loading, auth: true })
61+
62+
const requestParams = {
63+
auth_token: confTmp.auth_token,
64+
version: confTmp.version
65+
}
66+
67+
bitsFetch(requestParams, 'mailerlite_authorization').then(result => {
68+
setLoading({ ...loading, auth: false })
69+
70+
if (result && result.success) {
71+
setIsAuthorized(true)
72+
73+
toast.success(__('Authorized Successfully', 'bit-integrations'))
74+
75+
return
76+
}
77+
78+
toast.error(__('Authorized failed', 'bit-integrations'))
79+
})
80+
}
81+
5082
export const mailerliteRefreshFields = (
5183
confTmp,
5284
setConf,
5385
setError,
54-
setisAuthorized,
86+
setIsAuthorized,
5587
loading,
5688
setLoading,
5789
type
@@ -74,14 +106,14 @@ export const mailerliteRefreshFields = (
74106
version: confTmp.version
75107
}
76108

77-
bitsFetch(requestParams, 'mailerlite_refresh_fields').then((result) => {
109+
bitsFetch(requestParams, 'mailerlite_refresh_fields').then(result => {
78110
if (result && result.success) {
79111
const newConf = { ...confTmp }
80112
if (result.data) {
81113
newConf.mailerLiteFields = result.data
82114
}
83115
setConf(newConf)
84-
setisAuthorized(true)
116+
setIsAuthorized(true)
85117
if (type === 'authorization') {
86118
setLoading({ ...loading, auth: false })
87119
toast.success(__('Authorized Successfully', 'bit-integrations'))
@@ -109,7 +141,7 @@ export const getAllGroups = (confTmp, setConf, loding, setLoading) => {
109141
version: confTmp.version
110142
}
111143

112-
bitsFetch(requestParams, 'mailerlite_fetch_all_groups').then((result) => {
144+
bitsFetch(requestParams, 'mailerlite_fetch_all_groups').then(result => {
113145
if (result && result.success) {
114146
const newConf = { ...confTmp }
115147
if (result.data) {

includes/Actions/MailerLite/MailerLiteController.php

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ public function fetchAllGroups($refreshFieldsRequestParams)
3131
400
3232
);
3333
}
34-
// var_dump('fjkasfhjklas');
35-
// die;
34+
3635
if ('v2' === $refreshFieldsRequestParams->version) {
3736
$apiKey = $refreshFieldsRequestParams->auth_token;
3837
$endpoint = self::$_baseUrlV2 . 'groups';
@@ -79,6 +78,34 @@ public function fetchAllGroups($refreshFieldsRequestParams)
7978
}
8079
}
8180

81+
public function authorization($refreshFieldsRequestParams)
82+
{
83+
if (empty($refreshFieldsRequestParams->auth_token) || empty($refreshFieldsRequestParams->version)) {
84+
wp_send_json_error(
85+
__(
86+
'Requested parameter is empty',
87+
'bit-integrations'
88+
),
89+
400
90+
);
91+
}
92+
93+
$api = self::getApiVersionInfo($refreshFieldsRequestParams);
94+
95+
$response = HttpHelper::get($api['endpoint'], null, $api['header']);
96+
97+
if (HttpHelper::$responseCode == 200) {
98+
wp_send_json_success('Authorization Successful', 200);
99+
100+
return;
101+
}
102+
103+
wp_send_json_error(
104+
$response->message ?? $response ?? 'Authorization Failed',
105+
400
106+
);
107+
}
108+
82109
public function mailerliteRefreshFields($refreshFieldsRequestParams)
83110
{
84111
if (empty($refreshFieldsRequestParams->auth_token)) {
@@ -192,4 +219,23 @@ public function execute($integrationData, $fieldValues)
192219

193220
return $mailerliteApiResponse;
194221
}
222+
223+
private static function getApiVersionInfo($refreshFieldsRequestParams)
224+
{
225+
if ('v2' === $refreshFieldsRequestParams->version) {
226+
return [
227+
'endpoint' => self::$_baseUrlV2 . 'subscribers',
228+
'header' => [
229+
'Authorization' => 'Bearer ' . $refreshFieldsRequestParams->auth_token,
230+
]
231+
];
232+
}
233+
234+
return [
235+
'endpoint' => self::$_baseUrlV1 . 'me',
236+
'header' => [
237+
'X-Mailerlite-Apikey' => $refreshFieldsRequestParams->auth_token,
238+
]
239+
];
240+
}
195241
}

includes/Actions/MailerLite/Routes.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
use BitCode\FI\Actions\MailerLite\MailerLiteController;
88
use BitCode\FI\Core\Util\Route;
99

10+
Route::post('mailerlite_authorization', [MailerLiteController::class, 'authorization']);
1011
Route::post('mailerlite_fetch_all_groups', [MailerLiteController::class, 'fetchAllGroups']);
1112
Route::post('mailerlite_refresh_fields', [MailerLiteController::class, 'mailerliteRefreshFields']);

0 commit comments

Comments
 (0)