Added Base component with ID used for state management. All other components must inherit from it To get Id property

features/rewrite/statemanagement
markosteger 2 years ago
parent a9282b9b0a
commit 3331105811

@ -14,6 +14,7 @@ internal class Program
builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });
builder.Services.AddModalDialogService(); builder.Services.AddModalDialogService();
builder.Services.AddStateManagementService();
await builder.Build().RunAsync(); await builder.Build().RunAsync();
} }

@ -1,8 +1,9 @@
using Connected.Utilities; using Connected.Components;
using Connected.Utilities;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
namespace Connected.Models; namespace Connected.Models;
public class InputBase : ComponentBase public class InputBase : Base
{ {
[Parameter] [Parameter]
public string Class { get; set; } = string.Empty; public string Class { get; set; } = string.Empty;
@ -72,7 +73,7 @@ public class InputBase : ComponentBase
/// Fired when the text value changes. /// Fired when the text value changes.
/// </summary> /// </summary>
[Parameter] public EventCallback<string> TextChanged { get; set; } [Parameter] public EventCallback<string> TextChanged { get; set; }
public string Text { get; set; } public string Text { get; set; } = string.Empty;
protected virtual async Task SetTextAsync(string text) protected virtual async Task SetTextAsync(string text)
{ {

@ -0,0 +1,18 @@
using Microsoft.AspNetCore.Components;
namespace Connected.Classes
{
public class State
{
public Type? ComponentType { get; set; }
public string Id { get; set; }
public Dictionary<string, object>? Values { get; set; }
public State()
{
ComponentType = null;
Id=string.Empty;
Values = new();
}
}
}

@ -0,0 +1,5 @@
@inherits ComponentBase
@*Base class for all components
It contains unique ID for the component used in
saving state of the component*@

@ -0,0 +1,20 @@
using Microsoft.AspNetCore.Components;
namespace Connected.Components
{
public partial class Base : ComponentBase
{
/// <summary>
/// Unique ID for the component used in state management
/// Every component should inherit from Base class
/// </summary>
[Parameter, EditorRequired]
public string Id { get; set; } = Guid.NewGuid().ToString();
protected override void OnInitialized()
{
if (string.IsNullOrEmpty(Id))
Id = Guid.NewGuid().ToString();
}
}
}

@ -1,4 +1,6 @@
<button type="button" @inherits Base
<button type="button"
@onclick="@OnClick" @onclick="@OnClick"
disabled=@Disabled disabled=@Disabled
style="@StyleList" style="@StyleList"

@ -3,7 +3,7 @@ using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Components.Web;
namespace Connected.Components; namespace Connected.Components;
public partial class Button public partial class Button : Base
{ {
#region Parameters #region Parameters

@ -3,7 +3,7 @@
@inherits InputBase; @inherits InputBase;
<label class="checkbox-group" <label class="checkbox-group"
for="@Id"> for="@base.Id">
<input class="@ClassList" <input class="@ClassList"
style="@StyleList" style="@StyleList"
id="@Id" id="@Id"

@ -13,13 +13,6 @@ public partial class CheckBox : InputBase
[Parameter] [Parameter]
public bool Checked { get; set; } = false; public bool Checked { get; set; } = false;
/// <summary>
/// ID for the CheckBox
/// </summary>
[Parameter, EditorRequired]
public string? Id { get; set; }
/// <summary> /// <summary>
/// Event when the checked is changed /// Event when the checked is changed
/// </summary> /// </summary>

@ -1,4 +1,6 @@
<h3>Chip</h3> @inherits Base
<h3>Chip</h3>
@code { @code {

@ -1,7 +1,5 @@
using Microsoft.AspNetCore.Components; namespace Connected.Components;
namespace Connected.Components; public partial class Chip: Base
public partial class Chip
{ {
} }

@ -1,5 +1,7 @@
@using System.Globalization; @using System.Globalization;
@inherits Base
@if (loaded) @if (loaded)
{ {
<div id="picker"> <div id="picker">

@ -1,7 +1,7 @@
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
namespace Connected.Components; namespace Connected.Components;
public partial class DatePicker public partial class DatePicker: Base
{ {
private bool loaded = false; private bool loaded = false;

@ -1,5 +1,7 @@
@typeparam T; @using Connected.Models
@inherits Connected.Models.InputBase;
@typeparam T;
@inherits InputBase;
<div class="@InputFieldClassList"> <div class="@InputFieldClassList">
<select type="textarea" @onfocus="(()=> Open())" @onblur="(()=> Close())"> <select type="textarea" @onfocus="(()=> Open())" @onblur="(()=> Close())">

@ -1,7 +1,6 @@
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using Connected.Models; using Connected.Models;
using System.Collections.Immutable;
namespace Connected.Components; namespace Connected.Components;

@ -1,4 +1,6 @@
@if (loaded) @inherits Base
@if (loaded)
{ {
<CascadingValue Value="this"> <CascadingValue Value="this">
<section id="@Id" class="@ClassList" style="@StyleList"> <section id="@Id" class="@ClassList" style="@StyleList">
@ -23,5 +25,5 @@
</section> </section>
</CascadingValue> </CascadingValue>
} else { } else {
<p>Loading wizard....</p> <p>@LoadingText</p>
} }

@ -2,11 +2,10 @@
using Connected.Enums; using Connected.Enums;
using Connected.Utilities; using Connected.Utilities;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Options;
using System.Reflection.Metadata; using System.Reflection.Metadata;
namespace Connected.Components; namespace Connected.Components;
public partial class FormWizard public partial class FormWizard: Base
{ {
/// <summary> /// <summary>
@ -23,6 +22,12 @@ public partial class FormWizard
[Parameter] [Parameter]
public List<FormWizardStep> Steps { get; set; } = new(); public List<FormWizardStep> Steps { get; set; } = new();
/// <summary>
/// Steps of type FormWizardStep
/// </summary>
[Parameter]
public string LoadingText { get; set; } = "Loading wizard...";
/// <summary> /// <summary>
/// Steps of type FormWizardStep /// Steps of type FormWizardStep
/// </summary> /// </summary>
@ -41,9 +46,6 @@ public partial class FormWizard
[Parameter] [Parameter]
public EventCallback OnCancelClick { get; set; } public EventCallback OnCancelClick { get; set; }
[Parameter]
public string Id { get; set; } = Guid.NewGuid().ToString();
/// <summary> /// <summary>
/// Text shown inside the button /// Text shown inside the button
/// Options: any string variable /// Options: any string variable

@ -1,3 +1,5 @@
<div class="@ClassList" style="@StyleList"> @inherits Base
<div class="@ClassList" style="@StyleList">
@ChildContent @ChildContent
</div> </div>

@ -2,19 +2,13 @@
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
namespace Connected.Components; namespace Connected.Components;
public partial class FormWizardStep public partial class FormWizardStep: Base
{ {
#region Parameters #region Parameters
[CascadingParameter] [CascadingParameter]
public FormWizard Parent { get; set; } public FormWizard Parent { get; set; }
/// <summary>
/// Unique name of the step (used for properly identifying steps)
/// </summary>
[Parameter]
public string? Id { get; set; } = Guid.NewGuid().ToString();
/// <summary> /// <summary>
/// Text shown inside the button /// Text shown inside the button
/// Options: any string variable /// Options: any string variable
@ -57,7 +51,6 @@ public partial class FormWizardStep
#endregion #endregion
#region Styling #region Styling
public string StyleList public string StyleList

@ -1,3 +1,5 @@
<svg viewBox="0 0 24 24" class="@GlyphClassList" @onclick="@OnClick"> @inherits Base
<svg viewBox="0 0 24 24" class="@GlyphClassList" @onclick="@OnClick">
@((MarkupString)SVG) @((MarkupString)SVG)
</svg> </svg>

@ -4,7 +4,7 @@ using Microsoft.AspNetCore.Components.Web;
namespace Connected.Components; namespace Connected.Components;
public partial class Glyph public partial class Glyph: Base
{ {
/// <summary> /// <summary>
/// SVG markup string for glyph /// SVG markup string for glyph

@ -1,6 +1,8 @@
@attribute [CascadingTypeParameter(nameof(DataType))] @attribute [CascadingTypeParameter(nameof(DataType))]
@typeparam DataType @typeparam DataType
@inherits Base
<CascadingValue Value="this"> <CascadingValue Value="this">
<div class="@GridClassList.ToString()"> <div class="@GridClassList.ToString()">
@foreach (var Item in ItemsToShow) @foreach (var Item in ItemsToShow)

@ -1,11 +1,10 @@
using Connected.Classes.Grid; using Connected.Classes.Grid;
using Connected.Utilities; using Connected.Utilities;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.Extensions.Options;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
namespace Connected.Components; namespace Connected.Components;
public partial class Grid<DataType> : ComponentBase public partial class Grid<DataType> : Base
{ {
[Parameter] [Parameter]

@ -1,5 +1,7 @@
@typeparam DataType @typeparam DataType
@inherits Base
<CascadingValue Value="this"> <CascadingValue Value="this">
<div class="@GridRowClass.ToString()" id="@Guid.NewGuid()"> <div class="@GridRowClass.ToString()" id="@Guid.NewGuid()">
<div class="data-grid-select"> <div class="data-grid-select">

@ -2,7 +2,7 @@
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
namespace Connected.Components; namespace Connected.Components;
public partial class GridRow<DataType> : ComponentBase public partial class GridRow<DataType> : Base
{ {
[CascadingParameter] [CascadingParameter]

@ -1,4 +1,6 @@
<a class="@LinkClassList" @inherits Base
<a class="@LinkClassList"
style="@LinkStyleList" style="@LinkStyleList"
href="@Url" href="@Url"
target="@_target"> target="@_target">

@ -1,10 +1,9 @@
using Connected.Enums; using Connected.Enums;
using Connected.Utilities; using Connected.Utilities;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using static Connected.Colors;
namespace Connected.Components; namespace Connected.Components;
public partial class Link public partial class Link: Base
{ {
/// <summary> /// <summary>
/// URL of the link /// URL of the link

@ -1,4 +1,7 @@
@using Connected.Models.Modal; @using Connected.Models.Modal;
@inherits Base
@if (IsVisible) @if (IsVisible)
{ {
<div class="@ClassList" @onclick="@CloseIfEnabled" @onkeydown="@(e => CheckEscape(e))" tabindex="-1" @ref="@root"> <div class="@ClassList" @onclick="@CloseIfEnabled" @onkeydown="@(e => CheckEscape(e))" tabindex="-1" @ref="@root">

@ -4,10 +4,9 @@ using Connected.Services;
using Connected.Utilities; using Connected.Utilities;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Components.Web;
using System.Reflection;
namespace Connected.Components; namespace Connected.Components;
public partial class ModalDialog : IDisposable public partial class ModalDialog : Base, IDisposable
{ {
[Inject] ModalDialogService? ModalService { get; set; } [Inject] ModalDialogService? ModalService { get; set; }

@ -1,5 +1,7 @@
@typeparam NumberType @typeparam NumberType
@inherits Base
<div class="form-group-alt"> <div class="form-group-alt">
<div class="input-group-content"> <div class="input-group-content">
<div class="input-cta-icon" @onclick="StepDown"> <div class="input-cta-icon" @onclick="StepDown">

@ -4,7 +4,7 @@ using System.Numerics;
namespace Connected.Components namespace Connected.Components
{ {
public partial class NumberStepper<NumberType> where NumberType : INumber<NumberType> public partial class NumberStepper<NumberType> where NumberType : Base, INumber<NumberType>
{ {
[Parameter] [Parameter]

@ -12,9 +12,6 @@ public partial class Radio : InputBase
[Parameter] [Parameter]
public bool Checked { get; set; } = false; public bool Checked { get; set; } = false;
[Parameter, EditorRequired]
public string? Id { get; set; }
#endregion #endregion
#region Events #region Events

@ -1,5 +1,7 @@
@using Connected.Models; @using Connected.Models;
@inherits Base
<CascadingValue Value="this"> <CascadingValue Value="this">
<div> <div>
@if (!string.IsNullOrEmpty(Name)) @if (!string.IsNullOrEmpty(Name))

@ -1,7 +1,7 @@
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
namespace Connected.Components; namespace Connected.Components;
public partial class RadioGroup public partial class RadioGroup: Base
{ {
#region Parameters #region Parameters

@ -13,9 +13,6 @@ public partial class ToggleInput: InputBase
set => _checked= (bool)value; set => _checked= (bool)value;
} }
[Parameter, EditorRequired]
public string Id { get; set; }
[Parameter] [Parameter]
public EventCallback<bool> CheckedChanged { get; set; } public EventCallback<bool> CheckedChanged { get; set; }

@ -6,4 +6,7 @@ public static class ServiceCollectionExtensions
{ {
public static IServiceCollection AddModalDialogService(this IServiceCollection services) public static IServiceCollection AddModalDialogService(this IServiceCollection services)
=> services.AddScoped<ModalDialogService>(); => services.AddScoped<ModalDialogService>();
public static IServiceCollection AddStateManagementService(this IServiceCollection services)
=> services.AddSingleton<StateManagerService>();
} }

@ -0,0 +1,71 @@
using Connected.Classes;
using Connected.Components;
using System.Collections.ObjectModel;
namespace Connected.Services;
public class StateManagerService
{
public void SaveOrUpdateComponentState(Base Component)
{
var current_state = new State();
var properties = Component.GetType().GetProperties();
current_state.Id = Component.Id;
current_state.ComponentType = Component.GetType();
if (current_state.Values is null) current_state.Values = new Dictionary<string, object>();
foreach (var property in properties)
{
var value = property.GetValue(Component);
if (value is not null)
current_state.Values.Add(property.Name, value);
}
if (SavedStates is null) SavedStates = new ObservableCollection<State>();
SavedStates.Add(current_state);
}
public State? LoadComponentState(string ComponentId)
{
if (string.IsNullOrEmpty(ComponentId)) return null;
if (SavedStates is not null)
{
var Component = SavedStates.Where(state => state.Id == ComponentId).FirstOrDefault();
return Component;
}
return null;
}
public bool RemoveComponentState(string ComponentId)
{
bool result = false;
try
{
if (SavedStates is not null)
{
return SavedStates.Remove(SavedStates.FirstOrDefault(state => state.Id == ComponentId));
}
} catch (Exception ex)
{
result = false;
}
return result;
}
public bool ClearComponentStates()
{
bool result = false;
try
{
if (SavedStates is not null)
{
SavedStates.Clear();
result = true;
}
} catch
{
result = false;
}
return result;
}
private ObservableCollection<State>? SavedStates { get; set; } = new();
}
Loading…
Cancel
Save