Warranty Desk: the claims queue (React, build/app.js). */
final class AdminPage
{
public const SLUG = 'abcreate-warranty-desk';
private string $hookSuffix = '';
public function __construct(
private readonly string $pluginFile,
private readonly WpdbClaimRepository $claims,
) {
}
public function register(): void
{
add_action('admin_menu', [$this, 'addMenu'], 61);
add_action('admin_enqueue_scripts', [$this, 'enqueue']);
}
public function addMenu(): void
{
$counts = current_user_can(Plugin::CAP_VIEW) ? $this->claims->counts() : ['needs_review' => 0];
$badge = $counts['needs_review'] > 0
? ' ' . (int) $counts['needs_review'] . ''
: '';
$this->hookSuffix = (string) add_submenu_page(
'woocommerce',
__('Warranty Desk', 'abcreate-warranty-desk'),
__('Warranty Desk', 'abcreate-warranty-desk') . $badge,
Plugin::CAP_VIEW,
self::SLUG,
[$this, 'render'],
);
}
public function render(): void
{
echo '
' . esc_html__('Warranty Desk', 'abcreate-warranty-desk') . '
';
echo '
' . esc_html__('Loading…', 'abcreate-warranty-desk') . '
';
}
public function enqueue(string $hook): void
{
if ($hook !== $this->hookSuffix) {
return;
}
$dir = plugin_dir_path($this->pluginFile);
$assetFile = $dir . 'build/app.asset.php';
if (!file_exists($assetFile)) {
add_action('admin_notices', static function (): void {
echo '' . esc_html__('Warranty Desk: admin assets are missing. Run "yarn build".', 'abcreate-warranty-desk') . '
';
});
return;
}
$asset = require $assetFile;
wp_enqueue_script('abcreate-wd-admin', plugins_url('build/app.js', $this->pluginFile), $asset['dependencies'], $asset['version'], true);
// Translations come from translate.wordpress.org language packs.
wp_set_script_translations('abcreate-wd-admin', 'abcreate-warranty-desk');
wp_enqueue_style('wp-components');
if (file_exists($dir . 'build/app.css')) {
wp_enqueue_style('abcreate-wd-admin', plugins_url('build/app.css', $this->pluginFile), ['wp-components'], $asset['version']);
}
wp_add_inline_script('abcreate-wd-admin', 'window.abcreateWd = ' . wp_json_encode([
'namespace' => ClaimsController::NAMESPACE,
'canManage' => current_user_can(Plugin::CAP_MANAGE),
'today' => wp_date('Y-m-d'),
// Dates in the admin's language, not the browser's.
'locale' => str_replace('_', '-', get_user_locale()),
'targetDays' => Options::targetDays(),
'settingsUrl' => admin_url('admin.php?page=wc-settings&tab=products§ion=' . Settings::SECTION),
'exportClaims' => Export::url('claims'),
'exportHistory' => Export::url('history'),
'defaultVersion' => InformativaMailer::DEFAULT_VERSION,
]) . ';', 'before');
}
}