Refactoring the control to be reusable
[MAILPOET-2676]
This commit is contained in:
committed by
amine-mp
parent
1026b07495
commit
27bf71bad9
16
assets/js/src/common/controls/call_api.ts
Normal file
16
assets/js/src/common/controls/call_api.ts
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
import MailPoet from 'mailpoet';
|
||||||
|
|
||||||
|
export default async function ({ endpoint, action, data }) {
|
||||||
|
try {
|
||||||
|
const res = await MailPoet.Ajax.post({
|
||||||
|
api_version: (window as any).mailpoet_api_version,
|
||||||
|
endpoint,
|
||||||
|
action,
|
||||||
|
data,
|
||||||
|
});
|
||||||
|
return { success: true, res };
|
||||||
|
} catch (res) {
|
||||||
|
const error = res.errors.map((e) => e.message);
|
||||||
|
return { success: false, error };
|
||||||
|
}
|
||||||
|
}
|
@@ -1,13 +1,21 @@
|
|||||||
import { Action, Settings } from './types';
|
import { select } from '@wordpress/data';
|
||||||
|
import { STORE_NAME } from '.';
|
||||||
|
import { Action } from './types';
|
||||||
|
|
||||||
export function setSetting(path: string[], value: any): Action {
|
export function setSetting(path: string[], value: any): Action {
|
||||||
return { type: 'SET_SETTING', path, value };
|
return { type: 'SET_SETTING', path, value };
|
||||||
}
|
}
|
||||||
|
|
||||||
export function* saveSettings(data: Settings) {
|
export function* saveSettings() {
|
||||||
yield { type: 'SAVE_STARTED' };
|
yield { type: 'SAVE_STARTED' };
|
||||||
const error = yield { type: 'SEND_DATA_TO_API', data };
|
const data = select(STORE_NAME).getSettings();
|
||||||
if (error) {
|
const { success, error } = yield {
|
||||||
|
type: 'CALL_API',
|
||||||
|
endpoint: 'settings',
|
||||||
|
method: 'save',
|
||||||
|
data,
|
||||||
|
};
|
||||||
|
if (!success) {
|
||||||
return { type: 'SAVE_FAILED', error };
|
return { type: 'SAVE_FAILED', error };
|
||||||
}
|
}
|
||||||
yield { type: 'TRACK_SETTINGS_SAVED', data };
|
yield { type: 'TRACK_SETTINGS_SAVED', data };
|
||||||
|
@@ -1,18 +1,4 @@
|
|||||||
import MailPoet from 'mailpoet';
|
export { default as CALL_API } from 'common/controls/call_api';
|
||||||
|
|
||||||
export async function SEND_DATA_TO_API({ data }) {
|
|
||||||
try {
|
|
||||||
await MailPoet.Ajax.post({
|
|
||||||
api_version: (window as any).mailpoet_api_version,
|
|
||||||
endpoint: 'settings',
|
|
||||||
action: 'set',
|
|
||||||
data,
|
|
||||||
});
|
|
||||||
return false;
|
|
||||||
} catch (res) {
|
|
||||||
return res.errors.map((e) => e.message);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function TRACK_SETTINGS_SAVED({ data }) {
|
export function TRACK_SETTINGS_SAVED({ data }) {
|
||||||
// ...
|
// ...
|
||||||
|
Reference in New Issue
Block a user