Add calling API on the click
[MAILPOET-3646]
This commit is contained in:
@ -6,14 +6,27 @@ type Props = {
|
|||||||
cacheCalculation: string;
|
cacheCalculation: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleRecalculate = () => {
|
export function SubscribersCacheMessage({ cacheCalculation }: Props): JSX.Element {
|
||||||
window.location.reload();
|
|
||||||
};
|
|
||||||
|
|
||||||
const SubscribersCacheMessage = ({ cacheCalculation }: Props): JSX.Element => {
|
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const datetimeDiff = new Date().getTime() - new Date(cacheCalculation).getTime();
|
const datetimeDiff = new Date().getTime() - new Date(cacheCalculation).getTime();
|
||||||
const minutes = Math.floor((datetimeDiff / 1000) / 60);
|
const minutes = Math.floor((datetimeDiff / 1000) / 60);
|
||||||
|
|
||||||
|
const handleRecalculate = () => {
|
||||||
|
setLoading(true);
|
||||||
|
MailPoet.Ajax.post({
|
||||||
|
api_version: MailPoet.apiVersion,
|
||||||
|
endpoint: 'settings',
|
||||||
|
action: 'recalculateSubscribersCountsCache',
|
||||||
|
}).done(() => {
|
||||||
|
window.location.reload();
|
||||||
|
}).fail((response) => {
|
||||||
|
MailPoet.Notice.error(
|
||||||
|
response.errors.map((error) => error.message),
|
||||||
|
{ scroll: true }
|
||||||
|
);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="mailpoet-subscribers-cache-notice">
|
<div className="mailpoet-subscribers-cache-notice">
|
||||||
{MailPoet.I18n.t('subscribersCountWereCalculated')}
|
{MailPoet.I18n.t('subscribersCountWereCalculated')}
|
||||||
@ -24,10 +37,7 @@ const SubscribersCacheMessage = ({ cacheCalculation }: Props): JSX.Element => {
|
|||||||
type="button"
|
type="button"
|
||||||
variant="secondary"
|
variant="secondary"
|
||||||
dimension="small"
|
dimension="small"
|
||||||
onClick={() => {
|
onClick={handleRecalculate}
|
||||||
setLoading(true);
|
|
||||||
handleRecalculate();
|
|
||||||
}}
|
|
||||||
withSpinner={loading}
|
withSpinner={loading}
|
||||||
>
|
>
|
||||||
{MailPoet.I18n.t('recalculateNow')}
|
{MailPoet.I18n.t('recalculateNow')}
|
||||||
@ -35,6 +45,4 @@ const SubscribersCacheMessage = ({ cacheCalculation }: Props): JSX.Element => {
|
|||||||
<div className="mailpoet-gap" />
|
<div className="mailpoet-gap" />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
}
|
||||||
|
|
||||||
export { SubscribersCacheMessage };
|
|
||||||
|
@ -4,6 +4,7 @@ namespace MailPoet\API\JSON\v1;
|
|||||||
|
|
||||||
use MailPoet\API\JSON\Endpoint as APIEndpoint;
|
use MailPoet\API\JSON\Endpoint as APIEndpoint;
|
||||||
use MailPoet\API\JSON\Error as APIError;
|
use MailPoet\API\JSON\Error as APIError;
|
||||||
|
use MailPoet\Cache\TransientCache;
|
||||||
use MailPoet\Config\AccessControl;
|
use MailPoet\Config\AccessControl;
|
||||||
use MailPoet\Config\ServicesChecker;
|
use MailPoet\Config\ServicesChecker;
|
||||||
use MailPoet\Cron\Workers\InactiveSubscribers;
|
use MailPoet\Cron\Workers\InactiveSubscribers;
|
||||||
@ -13,6 +14,8 @@ use MailPoet\Entities\ScheduledTaskEntity;
|
|||||||
use MailPoet\Form\FormMessageController;
|
use MailPoet\Form\FormMessageController;
|
||||||
use MailPoet\Mailer\MailerLog;
|
use MailPoet\Mailer\MailerLog;
|
||||||
use MailPoet\Newsletter\Sending\ScheduledTasksRepository;
|
use MailPoet\Newsletter\Sending\ScheduledTasksRepository;
|
||||||
|
use MailPoet\Segments\SegmentsRepository;
|
||||||
|
use MailPoet\Segments\SegmentSubscribersRepository;
|
||||||
use MailPoet\Services\AuthorizedEmailsController;
|
use MailPoet\Services\AuthorizedEmailsController;
|
||||||
use MailPoet\Services\Bridge;
|
use MailPoet\Services\Bridge;
|
||||||
use MailPoet\Settings\SettingsController;
|
use MailPoet\Settings\SettingsController;
|
||||||
@ -54,6 +57,15 @@ class Settings extends APIEndpoint {
|
|||||||
/** @var FormMessageController */
|
/** @var FormMessageController */
|
||||||
private $messageController;
|
private $messageController;
|
||||||
|
|
||||||
|
/** @var TransientCache */
|
||||||
|
private $transientCache;
|
||||||
|
|
||||||
|
/** @var SegmentsRepository */
|
||||||
|
private $segmentsRepository;
|
||||||
|
|
||||||
|
/** @var SegmentSubscribersRepository */
|
||||||
|
private $segmentSubscribersRepository;
|
||||||
|
|
||||||
public $permissions = [
|
public $permissions = [
|
||||||
'global' => AccessControl::PERMISSION_MANAGE_SETTINGS,
|
'global' => AccessControl::PERMISSION_MANAGE_SETTINGS,
|
||||||
];
|
];
|
||||||
@ -68,7 +80,10 @@ class Settings extends APIEndpoint {
|
|||||||
StatisticsOpensRepository $statisticsOpensRepository,
|
StatisticsOpensRepository $statisticsOpensRepository,
|
||||||
ScheduledTasksRepository $scheduledTasksRepository,
|
ScheduledTasksRepository $scheduledTasksRepository,
|
||||||
FormMessageController $messageController,
|
FormMessageController $messageController,
|
||||||
ServicesChecker $servicesChecker
|
ServicesChecker $servicesChecker,
|
||||||
|
TransientCache $transientCache,
|
||||||
|
SegmentsRepository $segmentsRepository,
|
||||||
|
SegmentSubscribersRepository $segmentSubscribersRepository
|
||||||
) {
|
) {
|
||||||
$this->settings = $settings;
|
$this->settings = $settings;
|
||||||
$this->bridge = $bridge;
|
$this->bridge = $bridge;
|
||||||
@ -80,6 +95,9 @@ class Settings extends APIEndpoint {
|
|||||||
$this->statisticsOpensRepository = $statisticsOpensRepository;
|
$this->statisticsOpensRepository = $statisticsOpensRepository;
|
||||||
$this->scheduledTasksRepository = $scheduledTasksRepository;
|
$this->scheduledTasksRepository = $scheduledTasksRepository;
|
||||||
$this->messageController = $messageController;
|
$this->messageController = $messageController;
|
||||||
|
$this->transientCache = $transientCache;
|
||||||
|
$this->segmentsRepository = $segmentsRepository;
|
||||||
|
$this->segmentSubscribersRepository = $segmentSubscribersRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function get() {
|
public function get() {
|
||||||
@ -237,4 +255,14 @@ class Settings extends APIEndpoint {
|
|||||||
$task->setStatus(ScheduledTaskEntity::STATUS_SCHEDULED);
|
$task->setStatus(ScheduledTaskEntity::STATUS_SCHEDULED);
|
||||||
return $task;
|
return $task;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function recalculateSubscribersCountsCache() {
|
||||||
|
$this->transientCache->invalidateItems(TransientCache::SUBSCRIBERS_STATISTICS_COUNT_KEY);
|
||||||
|
$segments = $this->segmentsRepository->findAll();
|
||||||
|
foreach ($segments as $segment) {
|
||||||
|
$this->segmentSubscribersRepository->getSubscribersStatisticsCount($segment);
|
||||||
|
}
|
||||||
|
$this->segmentSubscribersRepository->getSubscribersWithoutSegmentStatisticsCount();
|
||||||
|
return $this->successResponse();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user