130 lines
6.4 KiB
Plaintext
130 lines
6.4 KiB
Plaintext
@page "/Accounts"
|
|
@using Gremlin_BlazorServer.Services
|
|
@using Gremlin_BlazorServer.Data.EntityClasses
|
|
@using System.Security.Claims
|
|
@using System.Text;
|
|
|
|
@inject GenericController genericController
|
|
@inject GenericImporter genericImporter
|
|
|
|
<AuthorizeView>
|
|
<Authorized Context="Auth">
|
|
<h1>Accounts</h1>
|
|
<Field>
|
|
<FieldLabel>Import Accounts</FieldLabel>
|
|
<FileEdit Filter=".csv" Changed="@OnImportAccounts" />
|
|
</Field>
|
|
@*<Button Color="Color.Primary" Clicked="@OnImportAccounts" Loading="@isImportingAccounts">Import Accounts</Button>*@
|
|
<DataGrid TItem="Account"
|
|
Data="@accounts"
|
|
SelectedRow="@selectedAccount"
|
|
SelectedRowChanged="@OnSelectedAccountChanged"
|
|
CommandMode="DataGridCommandMode.ButtonRow"
|
|
EditMode="DataGridEditMode.Popup"
|
|
UseValidation Narrow Editable ShowPager Bordered Hoverable Sortable Filterable Striped Responsive FixedHeader>
|
|
|
|
<DataGridColumns>
|
|
<DataGridCommandColumn NewCommandAllowed="false" EditCommandAllowed="false" DeleteCommandAllowed="false">
|
|
<SaveCommandTemplate>
|
|
<Button ElementId="btnSave" Type="ButtonType.Submit" PreventDefaultOnSubmit Color="Color.Primary" Clicked="@context.Clicked">@context.LocalizationString</Button>
|
|
</SaveCommandTemplate>
|
|
<CancelCommandTemplate>
|
|
<Button ElementId="btnCancel" Color="Color.Secondary" Clicked="@context.Clicked">@context.LocalizationString</Button>
|
|
</CancelCommandTemplate>
|
|
</DataGridCommandColumn>
|
|
|
|
<DataGridColumn Field="@nameof(Account.AccountId)" Caption="#" Filterable Sortable Editable>
|
|
<EditTemplate>
|
|
<NumericEdit TValue="uint" Value="@(Convert.ToUInt32(context.CellValue))" ValueChanged="@(v => context.CellValue = v)"/>
|
|
</EditTemplate>
|
|
</DataGridColumn>
|
|
<DataGridColumn Field="@nameof(Account.AccountName)" Caption="AccountName" Filterable Sortable Editable/>
|
|
<DataGridColumn Field="@nameof(Account.Street)" Caption="Street" Filterable Sortable Editable/>
|
|
<DataGridColumn Field="@nameof(Account.Zip)" Caption="Zip" Filterable Sortable Editable>
|
|
<EditTemplate>
|
|
<NumericEdit TValue="uint" Value="@(Convert.ToUInt32(context.CellValue))" ValueChanged="@(v => context.CellValue = v)"/>
|
|
</EditTemplate>
|
|
</DataGridColumn>
|
|
<DataGridColumn Field="@nameof(Account.City)" Caption="City" Filterable Sortable Editable/>
|
|
<DataGridColumn Field="@nameof(Account.SapAccountNumber)" Caption="SapAccountNumber" Filterable Sortable Editable>
|
|
<EditTemplate>
|
|
<NumericEdit TValue="uint" Value="@(Convert.ToUInt32(context.CellValue))" ValueChanged="@(v => context.CellValue = v)"/>
|
|
</EditTemplate>
|
|
</DataGridColumn>
|
|
</DataGridColumns>
|
|
|
|
<ButtonRowTemplate>
|
|
<Button Color="Color.Success" Clicked="context.NewCommand.Clicked">New</Button>
|
|
<Button Color="Color.Primary" Disabled="selectedAccount is null" Clicked="context.EditCommand.Clicked">Edit</Button>
|
|
<Button Color="Color.Danger" Disabled="selectedAccount is null" Clicked="context.DeleteCommand.Clicked">Delete</Button>
|
|
<Button Color="Color.Secondary" Clicked="context.ClearFilterCommand.Clicked">Clear Filter</Button>
|
|
</ButtonRowTemplate>
|
|
</DataGrid>
|
|
|
|
@if (selectedAccount != null) {
|
|
<h2>Contacts in @selectedAccount.AccountName</h2>
|
|
<DataGrid TItem="Contact" Data="@selectedAccount.Contacts" UseValidation Narrow FixedHeader Editable ShowPager Bordered Hoverable Sortable Filterable Striped Responsive>
|
|
<DataGridCommandColumn/>
|
|
<DataGridColumn Field="@nameof(Contact.ContactId)" Caption="ContactId" Filterable Sortable Editable/>
|
|
<DataGridColumn Field="@nameof(Contact.LastName)" Caption="LastName" Filterable Sortable Editable/>
|
|
<DataGridColumn Field="@nameof(Contact.FirstName)" Caption="FirstName" Filterable Sortable Editable/>
|
|
<DataGridColumn Field="@nameof(Contact.Gender)" Caption="Gender" Filterable Sortable Editable/>
|
|
<DataGridColumn Field="@nameof(Contact.EMail)" Caption="EMail" Filterable Sortable Editable/>
|
|
</DataGrid>
|
|
}
|
|
</Authorized>
|
|
<NotAuthorized>
|
|
<h3>Authentication Failure!</h3>
|
|
<p>You're not signed in. Please click on the upper right to either register or log in.</p>
|
|
</NotAuthorized>
|
|
</AuthorizeView>
|
|
|
|
@code {
|
|
|
|
[CascadingParameter]
|
|
private Task<AuthenticationState>? authenticationStateTask { get; set; }
|
|
|
|
private IList<Account>? accounts;
|
|
private Account? selectedAccount;
|
|
private bool isImportingAccounts;
|
|
|
|
protected override async Task OnInitializedAsync() {
|
|
if (authenticationStateTask != null) {
|
|
ClaimsPrincipal user = (await authenticationStateTask).User;
|
|
|
|
if (user.Identity is {IsAuthenticated: true }) {
|
|
accounts = genericController.GetAll<Account>();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void OnSelectedAccountChanged(Account sA) {
|
|
selectedAccount = sA;
|
|
selectedAccount.Contacts = genericController.GetAll<Contact>(c => c.AccountId == selectedAccount.AccountId);
|
|
}
|
|
|
|
private async Task OnImportAccounts(FileChangedEventArgs fileChangedEventArgs) {
|
|
isImportingAccounts = true;
|
|
try
|
|
{
|
|
foreach (IFileEntry? file in fileChangedEventArgs.Files)
|
|
{
|
|
using MemoryStream stream = new();
|
|
await file.WriteToStreamAsync(stream);
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
using StreamReader reader = new(stream);
|
|
string fileContent = await reader.ReadToEndAsync();
|
|
bool success = await genericImporter.ImportCsvAsync<Account>(fileContent);
|
|
}
|
|
}
|
|
catch (Exception exception)
|
|
{
|
|
Console.WriteLine(exception.Message);
|
|
}
|
|
Console.WriteLine("Account import successfull");
|
|
isImportingAccounts = false;
|
|
StateHasChanged();
|
|
return;
|
|
}
|
|
|
|
} |