@fleetbase/storefront-engine
Version:
Headless Commerce & Marketplace Extension for Fleetbase
65 lines (53 loc) • 2.24 kB
JavaScript
import Controller from '@ember/controller';
import { inject as service } from '@ember/service';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
export default class PromotionsPushNotificationsController extends Controller {
fetch;
notifications;
intl;
currentUser;
storefront;
title = '';
body = '';
selectedCustomers = [];
selectAllCustomers = false;
isLoading = false;
async sendPushNotification(event) {
event.preventDefault();
// Validate form
if (!this.title || !this.body) {
this.notifications.warning(this.intl.t('storefront.promotions.push-notifications.validation-title-body-required'));
return;
}
if (!this.selectAllCustomers && (!this.selectedCustomers || this.selectedCustomers.length === 0)) {
this.notifications.warning(this.intl.t('storefront.promotions.push-notifications.validation-customers-required'));
return;
}
this.isLoading = true;
try {
const payload = {
title: this.title,
body: this.body,
store: this.storefront.getActiveStore('public_id'),
select_all: this.selectAllCustomers,
};
// Only include customer IDs if not selecting all
if (!this.selectAllCustomers) {
payload.customers = this.selectedCustomers.map((customer) => customer.id);
}
await this.fetch.post('actions/send-push-notification', payload, { namespace: 'storefront/int/v1' });
this.notifications.success(this.intl.t('storefront.promotions.push-notifications.notification-sent-success'));
// Reset form
this.title = '';
this.body = '';
this.selectedCustomers = [];
this.selectAllCustomers = false;
} catch (error) {
this.notifications.error(this.intl.t('storefront.promotions.push-notifications.notification-sent-error'));
} finally {
this.isLoading = false;
}
}
}