Wonky.Client/Wonky.Client/Shared/PriceCatalogModal.razor.cs

121 lines
3.9 KiB
C#
Raw Normal View History

2022-07-31 10:12:28 +02:00
// Copyright (C) 2022 FCS Frede's Computer Services.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see [https://www.gnu.org/licenses/agpl-3.0.en.html]
//
using Microsoft.AspNetCore.Components;
using Wonky.Client.HttpInterceptors;
2022-09-06 12:28:44 +02:00
using Wonky.Client.HttpInterfaces;
2022-07-31 10:12:28 +02:00
using Wonky.Client.HttpRepository;
using Wonky.Client.Models;
using Wonky.Client.Services;
using Wonky.Entity.Requests;
using Wonky.Entity.Views;
namespace Wonky.Client.Shared;
2023-01-16 13:53:09 +01:00
public partial class PriceCatalogModal : IDisposable
2022-07-31 10:12:28 +02:00
{
2023-01-16 13:53:09 +01:00
[Parameter] public string CountryCode { get; set; } = "";
[Parameter] public EventCallback<SelectedSku> OnSelected { get; set; }
2023-01-16 16:40:56 +01:00
[Inject] private ICountryCatalogRepository Catalog { get; set; }
[Inject] private HttpInterceptorService Interceptor { get; set; }
[Inject] private UserProfileService ProfileService { get; set; }
2022-07-31 10:12:28 +02:00
private string _modalDisplay = "";
private bool _showBackdrop;
private List<SalesItemView> Items { get; set; } = new();
2023-01-16 13:53:09 +01:00
private MetaData? MetaInfo { get; set; } = new();
private CatalogPager _pager = new();
private UserPref _userPref = new();
2022-07-31 10:12:28 +02:00
2023-01-16 13:53:09 +01:00
protected override async Task OnParametersSetAsync()
2022-07-31 10:12:28 +02:00
{
2023-01-04 14:32:11 +01:00
Interceptor.RegisterEvent();
Interceptor.RegisterBeforeSendEvent();
2022-07-31 10:12:28 +02:00
await GetSalesItems();
}
2023-01-16 13:53:09 +01:00
protected override async Task OnInitializedAsync()
{
_userPref = await ProfileService.GetPreferences();
_pager.OrderBy = _userPref.ItemSort;
_pager.SearchColumn = _userPref.ItemSearch;
_pager.PageSize = Convert.ToInt32(_userPref.PageSize);
}
private async Task GetSalesItems()
{
var pagingResponse = await Catalog.GetSalesItemsPaged(CountryCode, _pager);
Items = pagingResponse.Items!;
MetaInfo = pagingResponse.MetaData;
}
2022-07-31 10:12:28 +02:00
private void SelectItem(string itemId, string quantity, string rate)
{
2022-08-05 13:03:19 +02:00
OnSelected.InvokeAsync(new SelectedSku { Quantity = quantity, Rate = rate, ItemId = itemId });
2022-07-31 10:12:28 +02:00
Hide();
}
private async Task SelectedPage(int page)
{
Items = new List<SalesItemView>();
2023-01-16 13:53:09 +01:00
_pager.PageNumber = page;
await GetSalesItems();
}
2022-08-05 13:03:19 +02:00
private async Task SetSearchPhrase(string searchTerm)
2022-07-31 10:12:28 +02:00
{
Items = new List<SalesItemView>();
2023-01-16 13:53:09 +01:00
_pager.PageNumber = 1;
_pager.SearchTerm = searchTerm;
2022-08-05 13:03:19 +02:00
await GetSalesItems();
2022-07-31 10:12:28 +02:00
}
2022-08-05 13:03:19 +02:00
2022-07-31 10:12:28 +02:00
private async Task SetPageSize(string pageSize)
{
Items = new List<SalesItemView>();
2023-01-16 13:53:09 +01:00
_pager.PageSize = Convert.ToInt32(pageSize);
_pager.PageNumber = 1;
2022-07-31 10:12:28 +02:00
await GetSalesItems();
}
private async Task SetSearchCol(string columnName)
{
Items = new List<SalesItemView>();
2023-01-16 13:53:09 +01:00
_pager.PageNumber = 1;
_pager.SearchColumn = columnName;
2022-07-31 10:12:28 +02:00
await GetSalesItems();
}
private async Task SetSortCol(string orderBy)
{
Items = new List<SalesItemView>();
2023-01-16 13:53:09 +01:00
_pager.OrderBy = orderBy;
2022-07-31 10:12:28 +02:00
await GetSalesItems();
}
2022-08-05 13:03:19 +02:00
public void Show()
{
_modalDisplay = "block;";
_showBackdrop = true;
StateHasChanged();
}
private void Hide()
{
_modalDisplay = "none;";
_showBackdrop = false;
StateHasChanged();
}
public void Dispose() => Interceptor.DisposeEvent();
2022-07-31 10:12:28 +02:00
}