Webiant Logo Webiant Logo
  1. No results found.

    Try your search with a different keyword or use * as a wildcard.

What3wordsViewComponent.cs

using Microsoft.AspNetCore.Mvc;
using Nop.Core;
using Nop.Plugin.Widgets.What3words.Models;
using Nop.Services.Cms;
using Nop.Services.Logging;
using Nop.Web.Framework.Components;
using Nop.Web.Framework.Infrastructure;

namespace Nop.Plugin.Widgets.What3words.Components;

public class What3wordsViewComponent : NopViewComponent
{
    #region Fields

    protected readonly ILogger _logger;
    protected readonly IWidgetPluginManager _widgetPluginManager;
    protected readonly IWorkContext _workContext;
    protected readonly What3wordsSettings _what3WordsSettings;

    #endregion

    #region Ctor

    public What3wordsViewComponent(ILogger logger,
        IWidgetPluginManager widgetPluginManager,
        IWorkContext workContext,
        What3wordsSettings what3WordsSettings)
    {
        _logger = logger;
        _widgetPluginManager = widgetPluginManager;
        _workContext = workContext;
        _what3WordsSettings = what3WordsSettings;
    }

    #endregion

    #region Methods

    /// 
    /// Invoke the widget view component
    /// 
    /// Widget zone
    /// Additional parameters
    /// 
    /// A task that represents the asynchronous operation
    /// The task result contains the view component result
    /// 
    public async Task InvokeAsync(string widgetZone, object additionalData)
    {
        if (!widgetZone.Equals(PublicWidgetZones.AddressBottom))
            return Content(string.Empty);

        //ensure that what3words widget is active and enabled
        var customer = await _workContext.GetCurrentCustomerAsync();
        if (!await _widgetPluginManager.IsPluginActiveAsync(What3wordsDefaults.SystemName, customer))
            return Content(string.Empty);

        if (!_what3WordsSettings.Enabled)
            return Content(string.Empty);

        if (string.IsNullOrEmpty(_what3WordsSettings.ApiKey))
        {
            await _logger.ErrorAsync("what3words error: API key is not set", customer: customer);
            return Content(string.Empty);
        }

        //display this on the checkout pages only
        if (ViewData.TemplateInfo.HtmlFieldPrefix != What3wordsDefaults.BillingAddressPrefix &&
            ViewData.TemplateInfo.HtmlFieldPrefix != What3wordsDefaults.ShippingAddressPrefix)
        {
            return Content(string.Empty);
        }

        var model = new What3wordsAddressModel
        {
            ApiKey = _what3WordsSettings.ApiKey,
            Prefix = ViewData.TemplateInfo.HtmlFieldPrefix
        };

        return View("~/Plugins/Widgets.What3words/Views/PublicInfo.cshtml", model);

    }

    #endregion
}