Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/refactor customers page #897

Merged
merged 9 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/adminpanel/Client/Models/CustomerModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class CustomerModel : IEquatable<CustomerModel>
public string? ContactEmail { get; set; }
public string? ContactPhone { get; set; }
public string? OrgNr { get; set; }
public int? ProjectCount { get; set; }
public IList<ProjectModel>? Projects { get; set; }
public bool ShowDetails { get; set; }

Expand Down
197 changes: 53 additions & 144 deletions packages/adminpanel/Client/Pages/Customers/Customers.razor
Original file line number Diff line number Diff line change
Expand Up @@ -22,60 +22,33 @@
@Localizer["CustomersPage.AddCustomer"]
</TextAdjuster>
</MudButton>

<MudTable Items="@AllCustomers"
Hover="true"
Loading="@Loading"
Striped="true"
Dense="true"
Elevation="4"
Filter="new Func<CustomerModel, bool>(FilterCustomer)"
@bind-SelectedItem="_selectedCustomer"
SortLabel="@Localizer["Common.SortBy"]"
OnRowClick="NavigateOnRowClick"
T="CustomerModel"
Class="cursor-pointer">
<ToolBarContent>
<MudTextField @bind-Value="_customerSearchString" Immediate="true" Placeholder="Søk" Adornment="Adornment.Start" AdornmentIcon="@Icons.Material.Filled.Search" IconSize="Size.Medium" Class="mt-0 pl-6"></MudTextField>
</ToolBarContent>
<HeaderContent>
<MudTh>
<MudTableSortLabel SortBy="new Func<CustomerModel, object>(x => x.Name)">Navn</MudTableSortLabel>
</MudTh>
<MudTh>
<MudTableSortLabel SortBy="new Func<CustomerModel, object>(x => x.ContactPerson)">Kontaktperson</MudTableSortLabel>
</MudTh>
<MudTh>
<MudTableSortLabel SortBy="new Func<CustomerModel, object>(x => x.ContactEmail)">Epost</MudTableSortLabel>
</MudTh>
<MudTh>
<MudTableSortLabel SortBy="new Func<CustomerModel, object>(x => x.ContactPhone)">Telefon</MudTableSortLabel>
</MudTh>
<MudTh>
<MudTableSortLabel SortBy="new Func<CustomerModel, object>(x => x.InvoiceAddress)">Fakturaaddresse</MudTableSortLabel>
</MudTh>
<MudTh>
<MudTableSortLabel SortBy="new Func<CustomerModel, object>(x => x.OrgNr)">OrgNr</MudTableSortLabel>
</MudTh>
</HeaderContent>
<RowTemplate>
<MudTd DataLabel="Name">@context.Name</MudTd>
<MudTd DataLabel="ContactPerson">@context.ContactPerson</MudTd>
<MudTd DataLabel="ContactEmail">@context.ContactEmail</MudTd>
<MudTd DataLabel="ContactPhone">@context.ContactPhone</MudTd>
<MudTd DataLabel="InvoiceAddress">@context.InvoiceAddress</MudTd>
<MudTd DataLabel="OrgNr">@context.OrgNr</MudTd>
<MudTd>
@* <MudButton Variant="Variant.Text" Size="Size.Small" OnClick="@(() => ShowCustomerDetails(context.Name))">@(context.ShowDetails ? "\u25b2" : "\u25bc") Prosjekter</MudButton> *@
</MudTd>
</RowTemplate>
<PagerContent>
<MudTablePager
PageSizeOptions="new[] { int.MaxValue, 20, 10 }"
AllItemsText="@Localizer["Common.Pagination.All"]"
RowsPerPageString="@Localizer["CustomerPage.Pagination.PerPage"]"/>
</PagerContent>
</MudTable>
<MudTextField
Value="@_customerSearchString"
Immediate="true"
ValueChanged="@(new Func<string, Task>(OnFilterChanged))"
Placeholder="Søk"
Adornment="Adornment.Start"
AdornmentIcon="@Icons.Material.Filled.Search"
IconSize="Size.Medium"
Class="my-4">
</MudTextField>
<MudPaper
Elevation="0"
Class="d-flex flex-wrap justify-start gap-8">
@if (FilteredCustomers is not null)
{
@foreach (var customer in FilteredCustomers)
{
<CustomerCard
ActionLabel="@Localizer["Common.NavigateTo"]"
OnActionPress="@(() => Navigation.NavigateTo($"/kunder/{customer.Id}"))"
CustomerName="@customer.Name"
ProjectCount="@customer.ProjectCount"
ContactPerson="@customer.ContactPerson">
</CustomerCard>
}
}
</MudPaper>

@code {
[Inject] private HttpClient HttpClient { get; set; }
Expand All @@ -84,9 +57,8 @@
[Inject] NavigationManager Navigation { get; set; }

private string _customerSearchString = "";
private CustomerModel? _selectedCustomer;
private CustomerModel? _customerBeforeEdit;
private HashSet<CustomerModel>? AllCustomers { get; set; }
private HashSet<CustomerModel>? FilteredCustomers { get; set; }

private bool Loading => AllCustomers == null;

Expand All @@ -105,6 +77,7 @@
if (customerResponse.IsSuccessStatusCode)
{
AllCustomers = (await customerResponse.Content.ReadFromJsonAsync<HashSet<CustomerModel>>())?.OrderBy(c => c.Id).ToHashSet();
FilteredCustomers = [..AllCustomers ?? []];
}
}
catch (AccessTokenNotAvailableException exception)
Expand All @@ -113,24 +86,17 @@
}
}

private bool FilterCustomer(CustomerModel customer)
{
if (string.IsNullOrWhiteSpace(_customerSearchString))
return true;
return customer.Name.Contains(_customerSearchString, StringComparison.OrdinalIgnoreCase) || customer.ContactEmail.Contains(_customerSearchString, StringComparison.OrdinalIgnoreCase) || customer.ContactPerson.Contains(_customerSearchString, StringComparison.OrdinalIgnoreCase);
}

private void AddEmptyCustomer()
{
DialogService.ShowAsync<CustomerDialog>(@Localizer["CustomersPage.AddCustomer"], new DialogParameters
{
["OnCustomerSubmit"] = RefreshAfterDialogAction
},
new DialogOptions()
{
FullWidth = true,
MaxWidth = MaxWidth.Small
}
{
["OnCustomerSubmit"] = RefreshAfterDialogAction
},
new DialogOptions()
{
FullWidth = true,
MaxWidth = MaxWidth.Small
}
);
}

Expand All @@ -139,89 +105,32 @@
await RefreshCustomers();
StateHasChanged();
}));

private async Task CustomerChangeCommitted()
private void NavigateToCustomer(int customerId)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Brukes denne metoden lenger?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

planen er å bruke den straks.

{
try
{
if (_selectedCustomer is { Id: 0 })
{
await HttpClient.PostAsJsonAsync(ApiRoutes.CustomersBase, _selectedCustomer.MapToCustomerUpsertRequest());
}
else
{
await HttpClient.PutAsJsonAsync(ApiRoutes.UpdateCustomer(_selectedCustomer!.Id), _selectedCustomer!.MapToCustomerUpsertRequest());
}

await RefreshCustomers();
}
catch (HttpResponseException)
{
ResetCustomerToOriginalValues(_selectedCustomer);
}

StateHasChanged();
Navigation.NavigateTo($"/kunder/{customerId}");
}

private void BackupCustomer(object customer)
private Task OnFilterChanged(string value)
{
_customerBeforeEdit = new CustomerModel
{
Name = ((CustomerModel)customer).Name,
ContactEmail = ((CustomerModel)customer).ContactEmail,
ContactPerson = ((CustomerModel)customer).ContactPerson,
ContactPhone = ((CustomerModel)customer).ContactPhone,
InvoiceAddress = ((CustomerModel)customer).InvoiceAddress,
OrgNr = ((CustomerModel)customer).OrgNr,
};
_customerSearchString = value;
FilterCustomers();
return Task.CompletedTask;
}

private void ResetCustomerToOriginalValues(object? customer)
private void FilterCustomers()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kan den gamle FilterCustomer-metoden fjernes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

godt fanget opp. den skal bort. fikser.

{
if (_customerBeforeEdit == null)
{
return;
}
if (AllCustomers == null) return;

if (customer == null)
{
if (_selectedCustomer == null)
{
return;
}
FilteredCustomers = AllCustomers
.Where(customer =>
string.IsNullOrWhiteSpace(_customerSearchString) ||
customer.Name.Contains(_customerSearchString, StringComparison.OrdinalIgnoreCase) ||
customer.ContactEmail.Contains(_customerSearchString, StringComparison.OrdinalIgnoreCase) ||
customer.ContactPerson.Contains(_customerSearchString, StringComparison.OrdinalIgnoreCase))
.ToHashSet();

customer = _selectedCustomer;
}

if (_selectedCustomer!.Id == 0)
{
UndoNewCustomer();
}

((CustomerModel)customer).Name = _customerBeforeEdit!.Name;
((CustomerModel)customer).ContactEmail = _customerBeforeEdit.ContactEmail;
((CustomerModel)customer).ContactPerson = _customerBeforeEdit.ContactPerson;
((CustomerModel)customer).ContactPhone = _customerBeforeEdit.ContactPhone;
((CustomerModel)customer).InvoiceAddress = _customerBeforeEdit.InvoiceAddress;
((CustomerModel)customer).OrgNr = _customerBeforeEdit.OrgNr;
_customerBeforeEdit = null;
}

private void UndoNewCustomer()
{
AllCustomers!.Remove(_selectedCustomer!);
StateHasChanged();
}

private void ShowCustomerDetails(string name)
{
var customer = AllCustomers!.First(e => e.Name == name);
customer.ShowDetails = !customer.ShowDetails;
}

private void NavigateOnRowClick(TableRowClickEventArgs<CustomerModel> selectedCustomer)
{
Navigation.NavigateTo($"/kunder/{selectedCustomer.Item.Id}");
}

}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@
<value>Kunder</value>
</data>
<data name="CustomersPage.AddCustomer" xml:space="preserve">
<value>Legg til ny kunde</value>
<value>Ny kunde</value>
</data>
<data name="Common.OrgNr" xml:space="preserve">
<value>Org.nummer</value>
Expand Down Expand Up @@ -210,4 +210,7 @@
<data name="CustomerPage.EditHourRate" xml:space="preserve">
<value>Endre timerate</value>
</data>
<data name="Common.NavigateTo" xml:space="preserve">
<value>Gå til</value>
</data>
</root>
Loading
Loading