Skip to content

Commit

Permalink
feat: make rules (conditions) editable
Browse files Browse the repository at this point in the history
  • Loading branch information
wgnf committed Nov 4, 2023
1 parent 6ba491d commit 247664f
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 16 deletions.
10 changes: 10 additions & 0 deletions src/CsvProc9000.Model/Configuration/Rule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,20 @@ namespace CsvProc9000.Model.Configuration
[ExcludeFromCodeCoverage] // DTO
public class Rule
{
[UsedImplicitly]
public string Name { get; set; }

[UsedImplicitly]
public List<Condition> Conditions { get; set; }

[UsedImplicitly]
public List<Change> Changes { get; set; }

public string GetName()
{
return string.IsNullOrWhiteSpace(Name)
? "< Unknown >"
: Name;
}
}
}
5 changes: 5 additions & 0 deletions src/CsvProc9000.UI/MauiProgram.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CsvProc9000.UI.Dialogues;
using CsvProc9000.UI.Platforms.Windows;
using CsvProc9000.UI.Settings;
using CsvProc9000.UI.States;
using Microsoft.Extensions.Logging;

namespace CsvProc9000.UI;
Expand Down Expand Up @@ -29,6 +30,10 @@ public static MauiApp CreateMauiApp()
.Services
.AddSingleton<ISettingsLoader, SettingsLoader>();

builder
.Services
.AddSingleton<IConfigurationState, ConfigurationState>();

return builder.Build();
}
}
70 changes: 55 additions & 15 deletions src/CsvProc9000.UI/Pages/Configure.razor
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
@page "/config"
@using CsvProc9000.Model.Configuration
@using CsvProc9000.UI.Settings

@using CsvProc9000.UI.Components
@inject ISettingsLoader SettingsLoader
@using CsvProc9000.UI.States
@using CsvProc9000.Model.Configuration

@inject IConfigurationState State
@inject NavigationManager NavigationManager

<!-- TODO: GO BACK TO HOME link! -->

Expand All @@ -28,7 +31,7 @@
</div>

@{
if (_settings != null)
if (State.Settings != null)
{
<div class="settings mt-4">
<h2>Settings</h2>
Expand All @@ -44,7 +47,7 @@
Inbox directory:
</div>
<div>
<FolderSelect DialogueTitle="Select the Inbox" @bind-SelectedPath="@_settings.Inbox"></FolderSelect>
<FolderSelect DialogueTitle="Select the Inbox" @bind-SelectedPath="@State.Settings.Inbox"></FolderSelect>
</div>
</div>

Expand All @@ -53,7 +56,7 @@
Inbox delimiter:
</div>
<div>
<InputText type="text" @bind-Value="@_settings.InboxDelimiter"></InputText>
<InputText type="text" @bind-Value="@State.Settings.InboxDelimiter"></InputText>
</div>
</div>

Expand All @@ -62,7 +65,7 @@
Should the file in the Inbox be deleted?
</div>
<div>
<InputCheckbox @bind-Value="@_settings.DeleteInboxFile"></InputCheckbox>
<InputCheckbox @bind-Value="@State.Settings.DeleteInboxFile"></InputCheckbox>
</div>
</div>
</div>
Expand All @@ -75,7 +78,7 @@
Outbox directory:
</div>
<div>
<FolderSelect DialogueTitle="Select the Outbox" @bind-SelectedPath="@_settings.Outbox"></FolderSelect>
<FolderSelect DialogueTitle="Select the Outbox" @bind-SelectedPath="@State.Settings.Outbox"></FolderSelect>
</div>
</div>

Expand All @@ -84,7 +87,7 @@
Outbox delimiter:
</div>
<div>
<InputText type="text" @bind-Value="@_settings.OutboxDelimiter"></InputText>
<InputText type="text" @bind-Value="@State.Settings.OutboxDelimiter"></InputText>
</div>
</div>

Expand All @@ -93,7 +96,7 @@
Outbox charset:
</div>
<div>
<InputText type="text" @bind-Value="@_settings.OutboxFileCharset"></InputText>
<InputText type="text" @bind-Value="@State.Settings.OutboxFileCharset"></InputText>
</div>
</div>

Expand All @@ -102,15 +105,32 @@
Should the values in the Outbox-file be in quotes?
</div>
<div>
<InputCheckbox @bind-Value="@_settings.OutboxValuesInQuotes"></InputCheckbox>
<InputCheckbox @bind-Value="@State.Settings.OutboxValuesInQuotes"></InputCheckbox>
</div>
</div>
</div>

<div class="mt-2">
<h3>Rules</h3>

<p>TODO...</p>
@for (var index = 0; index < State.Settings.Rules.Count; index++)
{
<div class="row mt-1">
@{
var ruleIndex = index;
var rule = State.GetRuleAt(ruleIndex);
}

<div>
<button class="btn btn-secondary" @onclick="() => NavigateToEditRule(ruleIndex)">@rule.GetName()</button>
<button class="btn btn-danger oi oi-minus float-end" @onclick="() => RemoveRule(rule)"></button>
</div>
</div>
}

<div class="mt-2">
<button class="btn btn-success float-start oi oi-plus" @onclick="AddRule"></button>
</div>
</div>
</div>
}
Expand All @@ -120,12 +140,32 @@

private string _serviceInstallationPath = string.Empty;
private bool IsLoadButtonDisabled => string.IsNullOrWhiteSpace(_serviceInstallationPath);
private CsvProcessorOptions _settings;

private void LoadSettings()
{
var settings = SettingsLoader.Load(_serviceInstallationPath);
_settings = settings;
State.ReadSettings(_serviceInstallationPath);
}

private void NavigateToEditRule(int ruleIndex)
{
NavigationManager.NavigateTo($"/rule/{ruleIndex}");
}

private void RemoveRule(Rule rule)
{
State.Settings.Rules.Remove(rule);
}

private void AddRule()
{
var newRule = new Rule
{
Name = "New Rule",
};

State.Settings.Rules.Add(newRule);

NavigateToEditRule(State.Settings.Rules.Count - 1);
}

}
91 changes: 91 additions & 0 deletions src/CsvProc9000.UI/Pages/EditRule.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
@page "/rule/{Index:int}"
@using CsvProc9000.Model.Configuration
@using CsvProc9000.UI.States

@inject IConfigurationState State
@inject NavigationManager NavigationManager

<h1>Edit Rule: @_rule?.GetName()</h1>

@if (_rule == null)
{
<p>There was an error loading the Rule...</p>
return;
}

<div>
<div class="row">
Name:
<InputText type="text" @bind-Value="@_rule.Name"></InputText>
</div>

<div class="mt-2">
<h3>Conditions</h3>

<table class="table">
<thead>
<tr>
<th scope="col">If the Field ...</th>
<th scope="col">... has the Value ...</th>
<th scope="col"></th>
</tr>
</thead>

<tbody>
@foreach (var condition in _rule.Conditions)
{
<tr>
<td>
<InputText @bind-Value="@condition.Field"></InputText>
</td>
<td>
<InputText @bind-Value="@condition.Value"></InputText>
</td>
<td>
<button class="btn btn-danger oi oi-minus" @onclick="() => RemoveCondition(condition)"></button>
</td>
</tr>
}

</tbody>
</table>

<div class="mt-2">
<button class="btn btn-success float-end oi oi-plus" @onclick="AddCondition"></button>
</div>
</div>

<div class="mt-5">
<button class="btn btn-primary float-start" @onclick="Ok">OK</button>
</div>
</div>

@code {

[Parameter]
public int Index { get; set; }

private Rule _rule;

protected override void OnInitialized()
{
_rule = State.GetRuleAt(Index);
_rule.Conditions ??= new List<Condition>();
_rule.Changes ??= new List<Change>();
}

private void RemoveCondition(Condition condition)
{
_rule.Conditions.Remove(condition);
}

private void AddCondition()
{
_rule.Conditions.Add(new Condition());
}

private void Ok()
{
NavigationManager.NavigateTo("/config");
}
}
2 changes: 1 addition & 1 deletion src/CsvProc9000.UI/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@
@code {
private void NavigateToConfigPage()
{
NavigationManager.NavigateTo("config");
NavigationManager.NavigateTo("/config");
}
}
34 changes: 34 additions & 0 deletions src/CsvProc9000.UI/States/ConfigurationState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using CsvProc9000.Model.Configuration;
using CsvProc9000.UI.Settings;

namespace CsvProc9000.UI.States;

internal sealed class ConfigurationState : IConfigurationState
{
private readonly ISettingsLoader _settingsLoader;

/// <inheritdoc />
public CsvProcessorOptions Settings { get; private set; }

/// <summary>
/// Constructor
/// </summary>
public ConfigurationState(ISettingsLoader settingsLoader)
{
_settingsLoader = settingsLoader ?? throw new ArgumentNullException(nameof(settingsLoader));
}

/// <inheritdoc />
public void ReadSettings(string pathToSettings)
{
var settings = _settingsLoader.Load(pathToSettings);
settings.Rules ??= new List<Rule>();
Settings = settings;
}

/// <inheritdoc />
public Rule GetRuleAt(int index)
{
return Settings.Rules[index];
}
}
27 changes: 27 additions & 0 deletions src/CsvProc9000.UI/States/IConfigurationState.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using CsvProc9000.Model.Configuration;

namespace CsvProc9000.UI.States;

/// <summary>
/// State for configuring the settings
/// </summary>
internal interface IConfigurationState
{
/// <summary>
/// Reads in the settings from the given path
/// </summary>
/// <param name="pathToSettings">The path to read in the settings from</param>
void ReadSettings(string pathToSettings);

/// <summary>
/// Gets the rule from the given index
/// </summary>
/// <param name="index">The index to get the rule from</param>
/// <returns>The rule that was gotten from the index</returns>
Rule GetRuleAt(int index);

/// <summary>
/// The read in settings
/// </summary>
CsvProcessorOptions Settings { get; }
}

0 comments on commit 247664f

Please sign in to comment.