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,
|
||||
SetSubscriberCountActionType,
|
||||
UpdateSegmentActionData,
|
||||
Segment,
|
||||
} from '../types';
|
||||
import { storeName } from './constants';
|
||||
|
||||
@ -156,10 +157,11 @@ export function* handleSave(segmentId?: number): Generator<{
|
||||
|
||||
export function* createFromTemplate(): Generator<{
|
||||
type: string;
|
||||
segment?: AnyFormItem;
|
||||
segment?: Segment;
|
||||
}> {
|
||||
MailPoet.Modal.loading(true);
|
||||
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([]);
|
||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
||||
// @ts-ignore -- I don't know how to configure typescript to understand this
|
||||
|
@ -1,7 +1,7 @@
|
||||
import { MailPoet } from 'mailpoet';
|
||||
import { assign, has } from 'lodash/fp';
|
||||
|
||||
import { AnyFormItem } from '../types';
|
||||
import { AnyFormItem, Segment } from '../types';
|
||||
import { isErrorResponse } from '../../../ajax';
|
||||
|
||||
function convertSavedData(data: Record<string, string | number>): AnyFormItem {
|
||||
@ -53,7 +53,7 @@ export async function SAVE_SEGMENT(actionData): Promise<{
|
||||
success: boolean;
|
||||
error?: string[];
|
||||
}> {
|
||||
const segment: AnyFormItem = actionData.segment;
|
||||
const segment: Segment = actionData.segment;
|
||||
try {
|
||||
const response = await MailPoet.Ajax.post({
|
||||
api_version: MailPoet.apiVersion,
|
||||
@ -61,7 +61,10 @@ export async function SAVE_SEGMENT(actionData): Promise<{
|
||||
action: 'save',
|
||||
data: segment,
|
||||
});
|
||||
|
||||
segment.id = response.data.id;
|
||||
segment.name = response.data.name;
|
||||
|
||||
return {
|
||||
success: true,
|
||||
};
|
||||
|
@ -181,6 +181,7 @@ export type Segment = {
|
||||
description?: string;
|
||||
filters_connect?: SegmentConnectTypes;
|
||||
filters?: AnyFormItem[];
|
||||
force_creation?: boolean;
|
||||
};
|
||||
|
||||
export type AnyFormItem =
|
||||
|
@ -38,6 +38,11 @@ class SegmentSaveController {
|
||||
public function save(array $data = []): SegmentEntity {
|
||||
$id = isset($data['id']) ? (int)$data['id'] : null;
|
||||
$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'] ?? '';
|
||||
$filtersData = $this->filterDataMapper->map($data);
|
||||
|
||||
|
Reference in New Issue
Block a user