Add a random string when creating a segment if the name already exists
This ensures that it is possible to create multiple segments from the same template. [MAILPOET-5394]
This commit is contained in:
committed by
Aschepikov
parent
dba4ba4dfc
commit
f806b3362c
@ -13,6 +13,7 @@ import {
|
|||||||
SubscriberCount,
|
SubscriberCount,
|
||||||
SetSubscriberCountActionType,
|
SetSubscriberCountActionType,
|
||||||
UpdateSegmentActionData,
|
UpdateSegmentActionData,
|
||||||
|
Segment,
|
||||||
} from '../types';
|
} from '../types';
|
||||||
import { storeName } from './constants';
|
import { storeName } from './constants';
|
||||||
|
|
||||||
@ -156,10 +157,11 @@ export function* handleSave(segmentId?: number): Generator<{
|
|||||||
|
|
||||||
export function* createFromTemplate(): Generator<{
|
export function* createFromTemplate(): Generator<{
|
||||||
type: string;
|
type: string;
|
||||||
segment?: AnyFormItem;
|
segment?: Segment;
|
||||||
}> {
|
}> {
|
||||||
MailPoet.Modal.loading(true);
|
MailPoet.Modal.loading(true);
|
||||||
const segment = select(storeName).getSegment();
|
const segment = select(storeName).getSegment();
|
||||||
|
segment.force_creation = true; // create segment with a random name if one with the same name already exists
|
||||||
yield setErrors([]);
|
yield setErrors([]);
|
||||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||||
// @ts-ignore -- I don't know how to configure typescript to understand this
|
// @ts-ignore -- I don't know how to configure typescript to understand this
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { MailPoet } from 'mailpoet';
|
import { MailPoet } from 'mailpoet';
|
||||||
import { assign, has } from 'lodash/fp';
|
import { assign, has } from 'lodash/fp';
|
||||||
|
|
||||||
import { AnyFormItem } from '../types';
|
import { AnyFormItem, Segment } from '../types';
|
||||||
import { isErrorResponse } from '../../../ajax';
|
import { isErrorResponse } from '../../../ajax';
|
||||||
|
|
||||||
function convertSavedData(data: Record<string, string | number>): AnyFormItem {
|
function convertSavedData(data: Record<string, string | number>): AnyFormItem {
|
||||||
@ -53,7 +53,7 @@ export async function SAVE_SEGMENT(actionData): Promise<{
|
|||||||
success: boolean;
|
success: boolean;
|
||||||
error?: string[];
|
error?: string[];
|
||||||
}> {
|
}> {
|
||||||
const segment: AnyFormItem = actionData.segment;
|
const segment: Segment = actionData.segment;
|
||||||
try {
|
try {
|
||||||
const response = await MailPoet.Ajax.post({
|
const response = await MailPoet.Ajax.post({
|
||||||
api_version: MailPoet.apiVersion,
|
api_version: MailPoet.apiVersion,
|
||||||
@ -61,7 +61,10 @@ export async function SAVE_SEGMENT(actionData): Promise<{
|
|||||||
action: 'save',
|
action: 'save',
|
||||||
data: segment,
|
data: segment,
|
||||||
});
|
});
|
||||||
|
|
||||||
segment.id = response.data.id;
|
segment.id = response.data.id;
|
||||||
|
segment.name = response.data.name;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
success: true,
|
success: true,
|
||||||
};
|
};
|
||||||
|
@ -181,6 +181,7 @@ export type Segment = {
|
|||||||
description?: string;
|
description?: string;
|
||||||
filters_connect?: SegmentConnectTypes;
|
filters_connect?: SegmentConnectTypes;
|
||||||
filters?: AnyFormItem[];
|
filters?: AnyFormItem[];
|
||||||
|
force_creation?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type AnyFormItem =
|
export type AnyFormItem =
|
||||||
|
@ -38,6 +38,11 @@ class SegmentSaveController {
|
|||||||
public function save(array $data = []): SegmentEntity {
|
public function save(array $data = []): SegmentEntity {
|
||||||
$id = isset($data['id']) ? (int)$data['id'] : null;
|
$id = isset($data['id']) ? (int)$data['id'] : null;
|
||||||
$name = $data['name'] ?? '';
|
$name = $data['name'] ?? '';
|
||||||
|
|
||||||
|
if (!$this->segmentsRepository->isNameUnique($name, null) && isset($data['force_creation']) && $data['force_creation'] === 'true') {
|
||||||
|
$name = $name . ' (' . wp_generate_password(12, false) . ')';
|
||||||
|
}
|
||||||
|
|
||||||
$description = $data['description'] ?? '';
|
$description = $data['description'] ?? '';
|
||||||
$filtersData = $this->filterDataMapper->map($data);
|
$filtersData = $this->filterDataMapper->map($data);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user