Use store for count of subscribers
[MAILPOET-3469]
This commit is contained in:
@@ -8,6 +8,8 @@ import {
|
|||||||
SetSegmentActionType,
|
SetSegmentActionType,
|
||||||
SetErrorsActionType,
|
SetErrorsActionType,
|
||||||
SetSegmentFilerActionType,
|
SetSegmentFilerActionType,
|
||||||
|
SubscriberCount,
|
||||||
|
SetSubscriberCountActionType,
|
||||||
} from '../types';
|
} from '../types';
|
||||||
|
|
||||||
export function setSegment(segment: AnyFormItem): SetSegmentActionType {
|
export function setSegment(segment: AnyFormItem): SetSegmentActionType {
|
||||||
@@ -66,6 +68,13 @@ export function updateSegmentFilterFromEvent(
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function updateSubscriberCount(data: SubscriberCount): SetSubscriberCountActionType {
|
||||||
|
return {
|
||||||
|
type: Actions.UPDATE_SUBSCRIBER_COUNT,
|
||||||
|
subscriberCount: data,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export function* pageLoaded(segmentId?: number): Generator<{
|
export function* pageLoaded(segmentId?: number): Generator<{
|
||||||
type: string;
|
type: string;
|
||||||
segmentId?: number;
|
segmentId?: number;
|
||||||
|
@@ -4,8 +4,9 @@ import {
|
|||||||
ActionType,
|
ActionType,
|
||||||
SetSegmentActionType,
|
SetSegmentActionType,
|
||||||
SetErrorsActionType,
|
SetErrorsActionType,
|
||||||
StateType,
|
|
||||||
SetSegmentFilerActionType,
|
SetSegmentFilerActionType,
|
||||||
|
SetSubscriberCountActionType,
|
||||||
|
StateType,
|
||||||
} from '../types';
|
} from '../types';
|
||||||
|
|
||||||
function setSegment(state: StateType, action: SetSegmentActionType): StateType {
|
function setSegment(state: StateType, action: SetSegmentActionType): StateType {
|
||||||
@@ -39,6 +40,14 @@ function updateSegmentFilter(state: StateType, action: SetSegmentFilerActionType
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function updateSubscriberCount(state: StateType, action: SetSubscriberCountActionType): StateType {
|
||||||
|
const oldCount = state.subscriberCount;
|
||||||
|
return {
|
||||||
|
...state,
|
||||||
|
subscriberCount: assign(oldCount, action.subscriberCount),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
export const createReducer = (defaultState: StateType) => (
|
export const createReducer = (defaultState: StateType) => (
|
||||||
state: StateType = defaultState,
|
state: StateType = defaultState,
|
||||||
action: ActionType
|
action: ActionType
|
||||||
@@ -49,6 +58,8 @@ export const createReducer = (defaultState: StateType) => (
|
|||||||
case Actions.UPDATE_SEGMENT: return updateSegment(state, action as SetSegmentActionType);
|
case Actions.UPDATE_SEGMENT: return updateSegment(state, action as SetSegmentActionType);
|
||||||
case Actions.UPDATE_SEGMENT_FILTER:
|
case Actions.UPDATE_SEGMENT_FILTER:
|
||||||
return updateSegmentFilter(state, action as SetSegmentFilerActionType);
|
return updateSegmentFilter(state, action as SetSegmentFilerActionType);
|
||||||
|
case Actions.UPDATE_SUBSCRIBER_COUNT:
|
||||||
|
return updateSubscriberCount(state, action as SetSubscriberCountActionType);
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
|
@@ -5,7 +5,7 @@ import {
|
|||||||
GroupFilterValue,
|
GroupFilterValue,
|
||||||
Segment,
|
Segment,
|
||||||
StateType,
|
StateType,
|
||||||
SubscriberActionTypes,
|
SubscriberActionTypes, SubscriberCount,
|
||||||
WindowCustomFields,
|
WindowCustomFields,
|
||||||
WindowEditableRoles,
|
WindowEditableRoles,
|
||||||
WindowNewslettersList,
|
WindowNewslettersList,
|
||||||
@@ -46,6 +46,9 @@ export const getCustomFieldsList = (state: StateType): WindowCustomFields => (
|
|||||||
export const getSegment = (state: StateType): Segment => (
|
export const getSegment = (state: StateType): Segment => (
|
||||||
state.segment
|
state.segment
|
||||||
);
|
);
|
||||||
|
export const getSubscriberCount = (state: StateType): SubscriberCount => (
|
||||||
|
state.subscriberCount
|
||||||
|
);
|
||||||
export const getSegmentFilter = (state: StateType, index: number): AnyFormItem | undefined => {
|
export const getSegmentFilter = (state: StateType, index: number): AnyFormItem | undefined => {
|
||||||
let found: AnyFormItem | undefined;
|
let found: AnyFormItem | undefined;
|
||||||
if (!Array.isArray(state.segment.filters)) {
|
if (!Array.isArray(state.segment.filters)) {
|
||||||
|
@@ -40,6 +40,9 @@ export const createStore = (): void => {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
},
|
},
|
||||||
|
subscriberCount: {
|
||||||
|
loading: false,
|
||||||
|
},
|
||||||
errors: [],
|
errors: [],
|
||||||
allAvailableFilters: getAvailableFilters(window.mailpoet_can_use_woocommerce_subscriptions),
|
allAvailableFilters: getAvailableFilters(window.mailpoet_can_use_woocommerce_subscriptions),
|
||||||
};
|
};
|
||||||
|
@@ -1,36 +1,32 @@
|
|||||||
import React, { useEffect, useState } from 'react';
|
import React, { useEffect } from 'react';
|
||||||
import MailPoet from 'mailpoet';
|
import MailPoet from 'mailpoet';
|
||||||
import { useSelect } from '@wordpress/data';
|
import { useSelect, useDispatch } from '@wordpress/data';
|
||||||
|
|
||||||
import { isFormValid } from './validator';
|
import { isFormValid } from './validator';
|
||||||
import { loadCount } from './subscribers_calculator';
|
import { loadCount } from './subscribers_calculator';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
Segment,
|
Segment,
|
||||||
|
SubscriberCount,
|
||||||
} from './types';
|
} from './types';
|
||||||
|
|
||||||
interface SubscriberCount {
|
|
||||||
count?: number;
|
|
||||||
loading?: boolean;
|
|
||||||
errors?: string[];
|
|
||||||
}
|
|
||||||
|
|
||||||
const SubscribersCounter: React.FunctionComponent = () => {
|
const SubscribersCounter: React.FunctionComponent = () => {
|
||||||
const [subscribersCount, setSubscribersCount] = useState<SubscriberCount>({
|
|
||||||
loading: false,
|
|
||||||
count: undefined,
|
|
||||||
errors: undefined,
|
|
||||||
});
|
|
||||||
|
|
||||||
const segment: Segment = useSelect(
|
const segment: Segment = useSelect(
|
||||||
(select) => select('mailpoet-dynamic-segments-form').getSegment(),
|
(select) => select('mailpoet-dynamic-segments-form').getSegment(),
|
||||||
[]
|
[]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const subscribersCount: SubscriberCount = useSelect(
|
||||||
|
(select) => select('mailpoet-dynamic-segments-form').getSubscriberCount(),
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
|
const { updateSubscriberCount } = useDispatch('mailpoet-dynamic-segments-form');
|
||||||
|
|
||||||
const serializedSegment = JSON.stringify(segment);
|
const serializedSegment = JSON.stringify(segment);
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
function load(loadItem: Segment): void {
|
function load(loadItem: Segment): void {
|
||||||
setSubscribersCount({
|
updateSubscriberCount({
|
||||||
loading: true,
|
loading: true,
|
||||||
count: undefined,
|
count: undefined,
|
||||||
errors: undefined,
|
errors: undefined,
|
||||||
@@ -43,26 +39,26 @@ const SubscribersCounter: React.FunctionComponent = () => {
|
|||||||
finished.count = response.count;
|
finished.count = response.count;
|
||||||
finished.errors = response.errors;
|
finished.errors = response.errors;
|
||||||
}
|
}
|
||||||
setSubscribersCount(finished);
|
updateSubscriberCount(finished);
|
||||||
}, (errorResponse) => {
|
}, (errorResponse) => {
|
||||||
const finished = {} as SubscriberCount;
|
const finished = {} as SubscriberCount;
|
||||||
const errors = errorResponse.errors.map((error) => error.message);
|
const errors = errorResponse.errors.map((error) => error.message);
|
||||||
finished.loading = false;
|
finished.loading = false;
|
||||||
finished.count = undefined;
|
finished.count = undefined;
|
||||||
finished.errors = errors;
|
finished.errors = errors;
|
||||||
setSubscribersCount(finished);
|
updateSubscriberCount(finished);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isFormValid(segment.filters)) {
|
if (isFormValid(segment.filters)) {
|
||||||
load(segment);
|
load(segment);
|
||||||
} else {
|
} else {
|
||||||
setSubscribersCount({
|
updateSubscriberCount({
|
||||||
count: undefined,
|
count: undefined,
|
||||||
loading: false,
|
loading: false,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, [segment, serializedSegment]);
|
}, [segment, serializedSegment, updateSubscriberCount]);
|
||||||
|
|
||||||
if (subscribersCount.errors) {
|
if (subscribersCount.errors) {
|
||||||
return (
|
return (
|
||||||
|
@@ -98,6 +98,12 @@ export type AnyFormItem =
|
|||||||
WooCommerceSubscriptionFormItem |
|
WooCommerceSubscriptionFormItem |
|
||||||
EmailFormItem;
|
EmailFormItem;
|
||||||
|
|
||||||
|
export interface SubscriberCount {
|
||||||
|
count?: number;
|
||||||
|
loading?: boolean;
|
||||||
|
errors?: string[];
|
||||||
|
}
|
||||||
|
|
||||||
export type OnFilterChange = (value: AnyFormItem, filterIndex: number) => void;
|
export type OnFilterChange = (value: AnyFormItem, filterIndex: number) => void;
|
||||||
|
|
||||||
export type WindowEditableRoles = {
|
export type WindowEditableRoles = {
|
||||||
@@ -163,6 +169,7 @@ export interface StateType {
|
|||||||
wooCountries: WindowWooCommerceCountries;
|
wooCountries: WindowWooCommerceCountries;
|
||||||
customFieldsList: WindowCustomFields;
|
customFieldsList: WindowCustomFields;
|
||||||
segment: Segment,
|
segment: Segment,
|
||||||
|
subscriberCount: SubscriberCount,
|
||||||
errors: string[],
|
errors: string[],
|
||||||
allAvailableFilters: GroupFilterValue[],
|
allAvailableFilters: GroupFilterValue[],
|
||||||
}
|
}
|
||||||
@@ -172,6 +179,7 @@ export enum Actions {
|
|||||||
SET_ERRORS = 'SET_ERRORS',
|
SET_ERRORS = 'SET_ERRORS',
|
||||||
UPDATE_SEGMENT = 'UPDATE_SEGMENT',
|
UPDATE_SEGMENT = 'UPDATE_SEGMENT',
|
||||||
UPDATE_SEGMENT_FILTER = 'UPDATE_SEGMENT_FILTER',
|
UPDATE_SEGMENT_FILTER = 'UPDATE_SEGMENT_FILTER',
|
||||||
|
UPDATE_SUBSCRIBER_COUNT = 'UPDATE_SUBSCRIBER_COUNT',
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ActionType {
|
export interface ActionType {
|
||||||
@@ -187,6 +195,10 @@ export interface SetSegmentFilerActionType extends ActionType {
|
|||||||
filterIndex: number;
|
filterIndex: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface SetSubscriberCountActionType extends ActionType {
|
||||||
|
subscriberCount: SubscriberCount;
|
||||||
|
}
|
||||||
|
|
||||||
export interface SetErrorsActionType extends ActionType {
|
export interface SetErrorsActionType extends ActionType {
|
||||||
errors: string[];
|
errors: string[];
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user