diff --git a/COMETwebapp.Tests/Components/SiteDirectory/OrganizationsTableTestFixture.cs b/COMETwebapp.Tests/Components/SiteDirectory/OrganizationsTableTestFixture.cs index 685c4aec..4e9ee8d6 100644 --- a/COMETwebapp.Tests/Components/SiteDirectory/OrganizationsTableTestFixture.cs +++ b/COMETwebapp.Tests/Components/SiteDirectory/OrganizationsTableTestFixture.cs @@ -42,6 +42,7 @@ namespace COMETwebapp.Tests.Components.SiteDirectory using DynamicData; + using Microsoft.AspNetCore.Components.Forms; using Microsoft.Extensions.DependencyInjection; using Moq; @@ -142,18 +143,27 @@ public async Task VerifyAddingOrEditingOrganization() Assert.Multiple(() => { - Assert.That(renderer.Instance.ShouldCreateThing, Is.EqualTo(true)); + Assert.That(renderer.Instance.IsOnEditMode, Is.EqualTo(false)); Assert.That(this.viewModel.Object.Thing, Is.InstanceOf(typeof(Organization))); }); - - var editORganizationButton = renderer.FindComponents().First(x => x.Instance.Id == "editOrganizationButton"); - await renderer.InvokeAsync(editORganizationButton.Instance.Click.InvokeAsync); + + var organizationsGrid = renderer.FindComponent(); + await renderer.InvokeAsync(() => organizationsGrid.Instance.SelectedDataItemChanged.InvokeAsync(new OrganizationRowViewModel(this.organization1))); + Assert.That(renderer.Instance.IsOnEditMode, Is.EqualTo(true)); + + var organizationsForm = renderer.FindComponents()[1]; + var organizationsEditForm = organizationsForm.FindComponent(); + await organizationsForm.InvokeAsync(organizationsEditForm.Instance.OnValidSubmit.InvokeAsync); Assert.Multiple(() => { - Assert.That(renderer.Instance.ShouldCreateThing, Is.EqualTo(false)); + this.viewModel.Verify(x => x.CreateOrEditOrganization(false), Times.Once); Assert.That(this.viewModel.Object.Thing, Is.InstanceOf(typeof(Organization))); }); + + var form = renderer.FindComponent(); + await renderer.InvokeAsync(form.Instance.EditModelSaving.InvokeAsync); + this.viewModel.Verify(x => x.CreateOrEditOrganization(false), Times.Once); } } } diff --git a/COMETwebapp.Tests/ViewModels/Components/SiteDirectory/OrganizationsTableViewModelTestFixture.cs b/COMETwebapp.Tests/ViewModels/Components/SiteDirectory/OrganizationsTableViewModelTestFixture.cs index 998767fe..eca7f698 100644 --- a/COMETwebapp.Tests/ViewModels/Components/SiteDirectory/OrganizationsTableViewModelTestFixture.cs +++ b/COMETwebapp.Tests/ViewModels/Components/SiteDirectory/OrganizationsTableViewModelTestFixture.cs @@ -34,7 +34,6 @@ namespace COMETwebapp.Tests.ViewModels.Components.SiteDirectory using CDP4Web.Enumerations; - using COMET.Web.Common.Enumerations; using COMET.Web.Common.Services.SessionManagement; using COMETwebapp.Services.ShowHideDeprecatedThingsService; @@ -202,5 +201,17 @@ public async Task VerifyRowOperations() await this.viewModel.OnConfirmPopupButtonClick(); this.sessionService.Verify(x => x.CreateOrUpdateThings(It.IsAny(), It.Is>(c => ((IDeprecatableThing)c.First()).IsDeprecated == false))); } + + [Test] + public async Task VerifyCreateOrEditOrganization() + { + this.viewModel.InitializeViewModel(); + + await this.viewModel.CreateOrEditOrganization(false); + this.sessionService.Verify(x => x.CreateOrUpdateThings(It.IsAny(), It.Is>(c => c.Count == 1)), Times.Once); + + await this.viewModel.CreateOrEditOrganization(true); + this.sessionService.Verify(x => x.CreateOrUpdateThings(It.IsAny(), It.Is>(c => c.Count == 2)), Times.Once); + } } } diff --git a/COMETwebapp/Components/SiteDirectory/OrganizationsForm.razor b/COMETwebapp/Components/SiteDirectory/OrganizationsForm.razor new file mode 100644 index 00000000..95319203 --- /dev/null +++ b/COMETwebapp/Components/SiteDirectory/OrganizationsForm.razor @@ -0,0 +1,46 @@ + +@inherits SelectedDataItemForm + + + + + + + + + + + + + + +
+ +
+ + Save + + + + Cancel + +
+
diff --git a/COMETwebapp/Components/SiteDirectory/OrganizationsForm.razor.cs b/COMETwebapp/Components/SiteDirectory/OrganizationsForm.razor.cs new file mode 100644 index 00000000..82c8de1e --- /dev/null +++ b/COMETwebapp/Components/SiteDirectory/OrganizationsForm.razor.cs @@ -0,0 +1,55 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright (c) 2023-2024 RHEA System S.A. +// +// Authors: Sam Gerené, Alex Vorobiev, Alexander van Delft, Jaime Bernar, Théate Antoine, João Rua +// +// This file is part of CDP4-COMET WEB Community Edition +// The CDP4-COMET WEB Community Edition is the RHEA Web Application implementation of ECSS-E-TM-10-25 Annex A and Annex C. +// +// The CDP4-COMET WEB Community Edition 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. +// +// The CDP4-COMET WEB Community Edition 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 . +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace COMETwebapp.Components.SiteDirectory +{ + using System.ComponentModel.DataAnnotations; + + using COMETwebapp.Components.Common; + using COMETwebapp.ViewModels.Components.SiteDirectory.Organizations; + + using Microsoft.AspNetCore.Components; + + /// + /// Support class for the + /// + public partial class OrganizationsForm : SelectedDataItemForm + { + /// + /// The for this component + /// + [Parameter, Required] + public IOrganizationsTableViewModel ViewModel { get; set; } + + /// + /// Method that is executed when there is a valid submit + /// + /// A + protected override async Task OnValidSubmit() + { + await this.ViewModel.CreateOrEditOrganization(this.ShouldCreate); + await base.OnValidSubmit(); + } + } +} diff --git a/COMETwebapp/Components/SiteDirectory/OrganizationsTable.razor b/COMETwebapp/Components/SiteDirectory/OrganizationsTable.razor index d0d8a7db..428883d8 100644 --- a/COMETwebapp/Components/SiteDirectory/OrganizationsTable.razor +++ b/COMETwebapp/Components/SiteDirectory/OrganizationsTable.razor @@ -14,64 +14,66 @@ Copyright (c) 2023-2024 RHEA System S.A. You should have received a copy of the GNU Affero General Public License along with this program. If not, see http://www.gnu.org/licenses/. -------------------------------------------------------------------------------> -@using COMETwebapp.ViewModels.Components.SiteDirectory.Rows @inherits COMETwebapp.Components.Common.SelectedDeprecatableDataItemBase - - - - - - - - - - - @{ - var row = (OrganizationRowViewModel)context.DataItem; +
+ + + + + + + + + + + @{ + var row = (OrganizationRowViewModel)context.DataItem; - + + } + + + - - } - - - + + + - - - - - - - - - - - - - - - + + + + +
diff --git a/COMETwebapp/Components/SiteDirectory/OrganizationsTable.razor.cs b/COMETwebapp/Components/SiteDirectory/OrganizationsTable.razor.cs index b157db38..0486aada 100644 --- a/COMETwebapp/Components/SiteDirectory/OrganizationsTable.razor.cs +++ b/COMETwebapp/Components/SiteDirectory/OrganizationsTable.razor.cs @@ -65,16 +65,18 @@ protected override void CustomizeEditThing(GridCustomizeEditModelEventArgs e) var dataItem = (OrganizationRowViewModel)e.DataItem; this.ShouldCreateThing = e.IsNew; + this.ViewModel.Thing = dataItem == null ? new Organization() : dataItem.Thing.Clone(true); + e.EditModel = this.ViewModel.Thing; + } - if (dataItem == null) - { - this.ViewModel.Thing = new Organization(); - e.EditModel = this.ViewModel.Thing; - return; - } - - e.EditModel = dataItem; - this.ViewModel.Thing = dataItem.Thing.Clone(true); + /// + /// Method invoked every time a row is selected + /// + /// The selected row + private void OnSelectedDataItemChanged(OrganizationRowViewModel row) + { + this.ViewModel.Thing = row.Thing.Clone(true); + this.IsOnEditMode = true; } } } diff --git a/COMETwebapp/ViewModels/Components/SiteDirectory/Organizations/IOrganizationsTableViewModel.cs b/COMETwebapp/ViewModels/Components/SiteDirectory/Organizations/IOrganizationsTableViewModel.cs index 9d872807..27c6c995 100644 --- a/COMETwebapp/ViewModels/Components/SiteDirectory/Organizations/IOrganizationsTableViewModel.cs +++ b/COMETwebapp/ViewModels/Components/SiteDirectory/Organizations/IOrganizationsTableViewModel.cs @@ -34,5 +34,11 @@ namespace COMETwebapp.ViewModels.Components.SiteDirectory.Organizations /// public interface IOrganizationsTableViewModel : IDeprecatableDataItemTableViewModel { + /// + /// Creates or edits an + /// + /// The value to check if a new should be created + /// A + Task CreateOrEditOrganization(bool shouldCreate); } } diff --git a/COMETwebapp/ViewModels/Components/SiteDirectory/Organizations/OrganizationsTableViewModel.cs b/COMETwebapp/ViewModels/Components/SiteDirectory/Organizations/OrganizationsTableViewModel.cs index 21232bba..f88655d4 100644 --- a/COMETwebapp/ViewModels/Components/SiteDirectory/Organizations/OrganizationsTableViewModel.cs +++ b/COMETwebapp/ViewModels/Components/SiteDirectory/Organizations/OrganizationsTableViewModel.cs @@ -24,6 +24,7 @@ namespace COMETwebapp.ViewModels.Components.SiteDirectory.Organizations { + using CDP4Common.CommonData; using CDP4Common.SiteDirectoryData; using CDP4Dal; @@ -32,16 +33,15 @@ namespace COMETwebapp.ViewModels.Components.SiteDirectory.Organizations using COMETwebapp.Services.ShowHideDeprecatedThingsService; using COMETwebapp.ViewModels.Components.Common.DeprecatableDataItemTable; - using COMETwebapp.ViewModels.Components.ReferenceData.ParameterTypes; using COMETwebapp.ViewModels.Components.SiteDirectory.Rows; /// - /// View model used to manage + /// View model used to manage /// public class OrganizationsTableViewModel : DeprecatableDataItemTableViewModel, IOrganizationsTableViewModel { /// - /// Initializes a new instance of the class. + /// Initializes a new instance of the class. /// /// The /// The @@ -50,6 +50,32 @@ public class OrganizationsTableViewModel : DeprecatableDataItemTableViewModel logger) : base(sessionService, messageBus, showHideDeprecatedThingsService, logger) { + this.Thing = new Organization(); + } + + /// + /// Creates or edits an + /// + /// The value to check if a new should be created + /// A + public async Task CreateOrEditOrganization(bool shouldCreate) + { + this.IsLoading = true; + + var siteDirectoryClone = this.SessionService.GetSiteDirectory().Clone(false); + var thingsToCreate = new List(); + + if (shouldCreate) + { + siteDirectoryClone.Organization.Add(this.Thing); + thingsToCreate.Add(siteDirectoryClone); + } + + thingsToCreate.Add(this.Thing); + await this.SessionService.CreateOrUpdateThings(siteDirectoryClone, thingsToCreate); + await this.SessionService.RefreshSession(); + + this.IsLoading = false; } } }