Skip to content

Commit

Permalink
Feat #438 [User Management] User profiles are now modifyable (#532)
Browse files Browse the repository at this point in the history
* now user profiles are editable
  • Loading branch information
joao4all authored Mar 14, 2024
1 parent e6db00e commit c39cec5
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,46 @@ public void Teardown()
this.messageBus.ClearSubscriptions();
}

[Test]
public async Task VerifyComponent()
{
var renderer = this.context.RenderComponent<UserManagementTable>();

Assert.Multiple(() =>
{
Assert.That(this.viewModel.DataSource.Count, Is.EqualTo(2));
Assert.That(renderer.Markup, Does.Contain(this.person.Name));
Assert.That(renderer.Markup, Does.Contain(this.person1.Name));
});

var grid = renderer.FindComponent<DxGrid>();
var buttons = grid.FindComponents<DxButton>();
var addNewPersonButton = buttons[0];
var editPersonButton = buttons[1];

await grid.InvokeAsync(addNewPersonButton.Instance.Click.InvokeAsync);

Assert.Multiple(() =>
{
Assert.That(this.viewModel.Person.Name?.Trim(), Is.Empty);
Assert.That(renderer.Instance.ShouldCreatePerson, Is.EqualTo(true));
});

await renderer.InvokeAsync(grid.Instance.EditModelSaving.InvokeAsync);
this.sessionService.Verify(x => x.CreateThings(It.IsAny<SiteDirectory>(), It.Is<List<Thing>>(c => c.Contains(this.viewModel.Person))), Times.Once);

await grid.InvokeAsync(editPersonButton.Instance.Click.InvokeAsync);

Assert.Multiple(() =>
{
Assert.That(this.viewModel.Person.Name, Is.EqualTo(this.viewModel.Rows.Items.First().PersonName));
Assert.That(renderer.Instance.ShouldCreatePerson, Is.EqualTo(false));
});

await renderer.InvokeAsync(grid.Instance.EditModelSaving.InvokeAsync);
this.sessionService.Verify(x => x.UpdateThing(It.IsAny<SiteDirectory>(), It.Is<Thing>(c => c == this.viewModel.Person)), Times.Once);
}

[Test]
public async Task VerifyActivatingPerson()
{
Expand Down Expand Up @@ -288,6 +328,8 @@ public async Task VerifyActivatingPerson()
public async Task VerifyAddingPerson()
{
var renderer = this.context.RenderComponent<UserManagementTable>();
this.viewModel.IsDefaultEmail = true;
this.viewModel.IsDefaultTelephoneNumber = true;

Assert.Multiple(() =>
{
Expand All @@ -302,7 +344,9 @@ public async Task VerifyAddingPerson()
Surname = "Test",
ShortName = "TT",
IsActive = true,
IsDeprecated = false
IsDeprecated = false,
EmailAddress = { new EmailAddress() },
TelephoneNumber = { new TelephoneNumber() }
};

await this.viewModel.AddingPerson();
Expand All @@ -311,6 +355,41 @@ public async Task VerifyAddingPerson()
Assert.Multiple(() => { Assert.That(this.viewModel.Rows.Count, Is.EqualTo(2)); });
}

[Test]
public async Task VerifyEditingPerson()
{
var renderer = this.context.RenderComponent<UserManagementTable>();
this.viewModel.IsDefaultEmail = true;
this.viewModel.IsDefaultTelephoneNumber = true;

Assert.Multiple(() =>
{
Assert.That(this.viewModel.DataSource.Count, Is.EqualTo(2));
Assert.That(renderer.Markup, Does.Contain(this.person.Name));
Assert.That(renderer.Markup, Does.Contain(this.person1.Name));
});

this.viewModel.Person = new Person
{
GivenName = "Test",
Surname = "Test",
ShortName = "TT",
IsActive = true,
IsDeprecated = false,
EmailAddress = { new EmailAddress() },
TelephoneNumber = { new TelephoneNumber() }
};

await this.viewModel.EditingPerson();
this.messageBus.SendMessage(new ObjectChangedEvent(this.viewModel.Person, EventKind.Updated));

Assert.Multiple(() =>
{
this.sessionService.Verify(x => x.UpdateThing(It.IsAny<SiteDirectory>(), this.viewModel.Person), Times.Once);
Assert.Multiple(() => { Assert.That(this.viewModel.Rows.Count, Is.EqualTo(2)); });
});
}

[Test]
public async Task VerifyDeprecatingPerson()
{
Expand Down
12 changes: 7 additions & 5 deletions COMETwebapp/Components/UserManagement/UserManagementTable.razor
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@
------------------------------------------------------------------------------->
@using COMETwebapp.ViewModels.Components.UserManagement.Rows
@using CDP4Common.SiteDirectoryData
@using COMETwebapp.ViewModels.Components.UserManagement;
@inherits COMET.Web.Common.Components.Applications.ApplicationBase<COMETwebapp.ViewModels.Components.UserManagement.IUserManagementTableViewModel>;
@inherits COMET.Web.Common.Components.Applications.ApplicationBase<COMETwebapp.ViewModels.Components.UserManagement.IUserManagementTableViewModel>

<LoadingComponent IsVisible="@this.ViewModel.IsLoading">
<DxGrid @ref="Grid"
<DxGrid @ref="this.Grid"
Data="this.ViewModel.Rows.Items"
ShowSearchBox="true"
SearchBoxNullText="Search for a person..."
Expand All @@ -37,7 +36,7 @@
PopupEditFormCssClass="pw-800"
PopupEditFormHeaderText="Create Person"
EditMode="GridEditMode.PopupEditForm"
EditModelSaving="this.ViewModel.AddingPerson"
EditModelSaving="@(this.OnEditModelSaving)"
CustomizeElement="DisableDeprecatedPerson">
<TotalSummary>
<DxGridSummaryItem SummaryType="GridSummaryItemType.Count" FieldName="IsActive" ValueDisplayFormat="{0} Total" />
Expand All @@ -60,11 +59,14 @@
<DxGridDataColumn FieldName="@nameof(PersonRowViewModel.IsDeprecated)" UnboundType="GridUnboundColumnType.Boolean" Visible="false" Caption="Role" MinWidth="80" SearchEnabled="false" />
<DxGridCommandColumn Width="200px" EditButtonVisible="false">
<HeaderTemplate>
<DxButton Id="addPersonButton" Text="Add User" IconCssClass="oi oi-plus" CssClass="my-custom-style" Click="() => Grid.StartEditNewRowAsync()" />
<DxButton Id="addPersonButton" Text="Add User" IconCssClass="oi oi-plus" CssClass="my-custom-style" Click="() => this.Grid.StartEditNewRowAsync()" />
</HeaderTemplate>
<CellDisplayTemplate>
@{
var row = (PersonRowViewModel)context.DataItem;

<DxButton Id="editPersonButton" Text="Edit" Click="@(() => this.Grid.StartEditRowAsync(context.VisibleIndex))" Enabled="row.IsAllowedToWrite" />

if (row.IsDeprecated)
{
<DxButton Id="undeprecateButton" Text="Un-deprecate" Click="() => this.ViewModel.OnDeprecateUnDeprecateButtonClick(row)" Enabled="row.IsAllowedToWrite" />
Expand Down
38 changes: 32 additions & 6 deletions COMETwebapp/Components/UserManagement/UserManagementTable.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ namespace COMETwebapp.Components.UserManagement
{
using CDP4Common.SiteDirectoryData;

using COMETwebapp.ViewModels.Components.UserManagement;
using COMET.Web.Common.Extensions;

using COMETwebapp.ViewModels.Components.UserManagement.Rows;

using DevExpress.Blazor;
Expand All @@ -47,6 +48,11 @@ public partial class UserManagementTable
/// </summary>
private IGrid Grid { get; set; }

/// <summary>
/// Gets the condition to check if a person should be created
/// </summary>
public bool ShouldCreatePerson { get; private set; } = true;

/// <summary>
/// Method invoked when a custom summary calculation is required, allowing you to
/// perform custom calculations based on the data displayed in the grid.
Expand All @@ -73,7 +79,7 @@ public static void CustomSummary(GridCustomSummaryEventArgs e)
}

/// <summary>
/// Method invoked to "Show/Hide Deprecated Items"
/// Method invoked to "Show/Hide Deprecated Items"
/// </summary>
public void HideOrShowDeprecatedItems()
{
Expand All @@ -97,8 +103,9 @@ protected override void OnInitialized()

this.ViewModel.OnInitialized();

this.Disposables.Add(this.ViewModel.Rows.CountChanged.Subscribe(_ => this.InvokeAsync(this.StateHasChanged)));
this.Disposables.Add(this.ViewModel.Rows.Connect().AutoRefresh().Subscribe(_ => this.InvokeAsync(this.StateHasChanged)));
this.Disposables.Add(this.ViewModel.Rows.CountChanged.SubscribeAsync(_ => this.InvokeAsync(this.StateHasChanged)));
this.Disposables.Add(this.ViewModel.WhenAnyValue(x => x.Person).SubscribeAsync(_ => this.InvokeAsync(this.StateHasChanged)));
this.Disposables.Add(this.ViewModel.Rows.Connect().AutoRefresh().SubscribeAsync(_ => this.InvokeAsync(this.StateHasChanged)));
}

/// <summary>
Expand All @@ -107,14 +114,33 @@ protected override void OnInitialized()
/// <param name="e">A <see cref="GridCustomizeEditModelEventArgs" /></param>
private void CustomizeEditPerson(GridCustomizeEditModelEventArgs e)
{
var dataItem = (Person)e.DataItem;
var dataItem = (PersonRowViewModel)e.DataItem;
this.ShouldCreatePerson = e.IsNew;

if (dataItem == null)
{
e.EditModel = new Person();
this.ViewModel.Person = new Person();
return;
}

e.EditModel = dataItem;
this.ViewModel.Person = dataItem.Person.Clone(true);
}

/// <summary>
/// Method that is invoked when the edit/add person model form is being saved
/// </summary>
/// <returns>A <see cref="Task"/></returns>
private async Task OnEditModelSaving()
{
if (!this.ShouldCreatePerson)
{
await this.ViewModel.EditingPerson();
return;
}

this.ViewModel.Person = new Person();
await this.ViewModel.AddingPerson();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,11 @@ public interface IUserManagementTableViewModel : IApplicationBaseViewModel, IHav
/// want the component to refresh when that operation is completed.
/// </summary>
void OnInitialized();

/// <summary>
/// Tries to edit an existing <see cref="Person"/>
/// </summary>
/// <returns>A <see cref="Task" /></returns>
Task EditingPerson();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ namespace COMETwebapp.ViewModels.Components.UserManagement
using AntDesign;

using CDP4Common.CommonData;
using CDP4Common.EngineeringModelData;
using CDP4Common.SiteDirectoryData;

using CDP4Dal;
Expand Down Expand Up @@ -187,6 +186,28 @@ public void OnCancelButtonClick()
this.IsOnDeprecationMode = false;
}

/// <summary>
/// Tries to edit an existing <see cref="Person"/>
/// </summary>
/// <returns>A <see cref="Task" /></returns>
public async Task EditingPerson()
{
if (this.IsDefaultEmail)
{
this.Person.DefaultEmailAddress = this.EmailAddress;
}

if (this.IsDefaultTelephoneNumber)
{
this.Person.DefaultTelephoneNumber = this.TelephoneNumber;
}

this.Person.EmailAddress.Add(this.EmailAddress);
this.Person.TelephoneNumber.Add(this.TelephoneNumber);

await this.sessionService.UpdateThing(this.sessionService.GetSiteDirectory(), this.Person);
}

/// <summary>
/// Tries to create a new <see cref="Person" />
/// </summary>
Expand Down

0 comments on commit c39cec5

Please sign in to comment.