Skip to content

Commit

Permalink
Disable the edition of existing tags names #1002
Browse files Browse the repository at this point in the history
  • Loading branch information
hocinehacherouf committed Aug 2, 2022
1 parent efb46db commit 6b86fc2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
25 changes: 25 additions & 0 deletions src/AzureIoTHub.Portal/Client/Models/DeviceTagModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) CGI France. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

namespace AzureIoTHub.Portal.Client.Models
{
using Portal.Models.v10;

public class DeviceTagModel : DeviceTag
{
public DeviceTagModel()
{
IsNewTag = true;
}

public DeviceTagModel(DeviceTag deviceTag)
{
Name = deviceTag.Name;
Label = deviceTag.Label;
Required = deviceTag.Required;
Searchable = deviceTag.Searchable;
}

public bool IsNewTag { get; set; }
}
}
27 changes: 16 additions & 11 deletions src/AzureIoTHub.Portal/Client/Pages/Settings/DeviceTagsPage.razor
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
@page "/settings/device-tag"
@using AzureIoTHub.Portal.Models.v10

@attribute [Authorize]
@inject ISnackbar Snackbar
@inject IDeviceTagSettingsClientService DeviceTagSettingsClientService

<MudGrid>
<MudItem xs="12">
<MudTable Items=@Tags Loading="isProcessing" Dense=true Hover=true Bordered=true Striped=true>
<MudTable Items="@Tags" Loading="isProcessing" Dense=true Hover=true Bordered=true Striped=true>

<ToolBarContent>
<MudText Typo="Typo.h6">Tags</MudText>
Expand Down Expand Up @@ -37,6 +36,7 @@
<MudTd DataLabel="Name" Style="word-break: break-all;">
<MudForm @ref="FormName" Model="TagContexte">
<MudTextField @bind-Value="@TagContexte.Name" Label="Name"
Disabled="@(!TagContexte.IsNewTag)"
HelperText="Name that will be registered in the device twin" HelperTextOnFocus="true"
For="@(() => TagContexte.Name)" Margin="Margin.Dense" Variant="Variant.Outlined" Required="true"></MudTextField>
</MudForm>
Expand Down Expand Up @@ -66,7 +66,7 @@
</MudTd>
</RowTemplate>
<FooterContent>
<MudButton StartIcon="@Icons.Material.Filled.Add" Size="Size.Medium" Color="Color.Success" OnClick="AddTag" id="addTagButton">Add a new Tag</MudButton>
<MudButton StartIcon="@Icons.Material.Filled.Add" Size="Size.Medium" Color="Color.Success" OnClick="@AddTag" id="addTagButton">Add a new Tag</MudButton>
</FooterContent>

<NoRecordsContent>
Expand All @@ -83,7 +83,7 @@
[CascadingParameter]
public Error Error { get; set; }

List<DeviceTag> Tags { get; set; } = new();
List<DeviceTagModel> Tags { get; } = new();
private MudForm FormName { get; set; }
private MudForm FormLabel { get; set; }

Expand All @@ -100,7 +100,10 @@
{
isProcessing = true;
Tags.Clear();
Tags.AddRange(await DeviceTagSettingsClientService.GetDeviceTags());

var result = await DeviceTagSettingsClientService.GetDeviceTags();

Tags.AddRange(result.Select(tag => new DeviceTagModel(tag)).ToList());
}
catch (ProblemDetailsException exception)
{
Expand All @@ -118,24 +121,24 @@

if (Tags.Count == 0 || (last.Name is not null && last.Label is not null))
{
Tags.Add(new DeviceTag());
Tags.Add(new DeviceTagModel());
}
}

private void DeleteTag(DeviceTag item)
private void DeleteTag(DeviceTagModel deviceTagModel)
{
try
{
DeviceTagSettingsClientService.DeleteDeviceTagByName(item.Name);
Tags.Remove(item);
DeviceTagSettingsClientService.DeleteDeviceTagByName(deviceTagModel.Name);
Tags.Remove(deviceTagModel);
}
catch (ProblemDetailsException exception)
{
Error?.ProcessProblemDetails(exception);
}
}

private async Task Save(DeviceTag deviceTag)
private async Task Save(DeviceTagModel deviceTagModel)
{
try
{
Expand Down Expand Up @@ -166,7 +169,9 @@
}
else
{
await DeviceTagSettingsClientService.CreateOrUpdateDeviceTag(deviceTag);
await DeviceTagSettingsClientService.CreateOrUpdateDeviceTag(deviceTagModel);

deviceTagModel.IsNewTag = false;
}

Snackbar.Add($"Settings have been successfully updated!", Severity.Success);
Expand Down

0 comments on commit 6b86fc2

Please sign in to comment.