Skip to content

Commit 0c8b7b8

Browse files
committed
Refactoring
Refacotring
1 parent a7a0f94 commit 0c8b7b8

9 files changed

+165
-114
lines changed

src/ComponentInstances/DateInputFormComponentInstance.cs

Lines changed: 0 additions & 107 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Newtonsoft.Json.Linq;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Orbyss.Blazor.JsonForms.ComponentInstances
9+
{
10+
public class DateOnlyInputFormComponentInstance(Type componentGenericTypeDefinition) : DateOnlyInputFormComponentInstanceBase<DateOnly?>
11+
{
12+
public override Type ComponentType => componentGenericTypeDefinition.MakeGenericType(typeof(DateOnly?));
13+
14+
protected override sealed Func<DateTime?, DateOnly?>? ConvertFromDateTime => dt => dt.HasValue ? new DateOnly(dt.Value.Year, dt.Value.Month, dt.Value.Day) : null;
15+
16+
protected override sealed Func<DateOnly?, DateTime?>? ConvertToDateTime => dt => dt.HasValue ? dt.Value.ToDateTime(TimeOnly.MinValue) : null;
17+
18+
protected override sealed object? ConvertValue(JToken? token)
19+
{
20+
return DateOnly.TryParse($"{token}", Culture, out var date)
21+
? date
22+
: null;
23+
}
24+
}
25+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
namespace Orbyss.Blazor.JsonForms.ComponentInstances
2+
{
3+
public abstract class DateOnlyInputFormComponentInstanceBase<TDate> : InputFormComponentInstanceBase
4+
{
5+
protected abstract Func<DateTime?, TDate>? ConvertFromDateTime { get; }
6+
7+
protected abstract Func<TDate, DateTime?>? ConvertToDateTime { get; }
8+
9+
public DateTime? MinimumDate { get; set; }
10+
11+
public DateTime? MaximumDate { get; set; }
12+
13+
public bool AllowManualInput { get; set; }
14+
15+
protected override sealed IDictionary<string, object?> GetFormInputParameters()
16+
{
17+
var result = GetDateInputParameter();
18+
19+
result[nameof(ConvertFromDateTime)] = ConvertFromDateTime;
20+
result[nameof(ConvertToDateTime)] = ConvertToDateTime;
21+
result[nameof(MinimumDate)] = MinimumDate;
22+
result[nameof(MaximumDate)] = MaximumDate;
23+
result[nameof(AllowManualInput)] = AllowManualInput;
24+
25+
return result;
26+
}
27+
28+
protected virtual IDictionary<string, object?> GetDateInputParameter()
29+
{
30+
return new Dictionary<string, object?>();
31+
}
32+
}
33+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Newtonsoft.Json.Linq;
2+
using Orbyss.Components.Json.Models;
3+
4+
namespace Orbyss.Blazor.JsonForms.ComponentInstances
5+
{
6+
public class DateOnlyUtcTicksInputFormComponentInstance(Type componentGenericTypeDefinition) : DateOnlyInputFormComponentInstanceBase<DateUtcTicks?>
7+
{
8+
public override Type ComponentType => componentGenericTypeDefinition.MakeGenericType(typeof(DateUtcTicks?));
9+
10+
protected override sealed Func<DateTime?, DateUtcTicks?>? ConvertFromDateTime => dt => dt.HasValue ? new DateUtcTicks(dt.Value.Ticks) : null;
11+
12+
protected override sealed Func<DateUtcTicks?, DateTime?>? ConvertToDateTime => dt => dt.HasValue ? dt.Value.DateOnly.ToDateTime(TimeOnly.MinValue) : null;
13+
14+
protected override sealed object? ConvertValue(JToken? token)
15+
{
16+
if (token is null || string.IsNullOrWhiteSpace($"{token}"))
17+
{
18+
return null;
19+
}
20+
21+
return new DateUtcTicks((long)token);
22+
}
23+
}
24+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Newtonsoft.Json.Linq;
2+
3+
namespace Orbyss.Blazor.JsonForms.ComponentInstances
4+
{
5+
public class DateTimeInputFormComponentInstance(Type componentGenericTypeDefinition) : DateTimeInputFormComponentInstanceBase<DateTime?>
6+
{
7+
public override Type ComponentType => componentGenericTypeDefinition.MakeGenericType(typeof(DateTime?));
8+
9+
protected override sealed Func<DateTime?, DateTime?>? ConvertFromDateTime => (dt) => dt;
10+
11+
protected override sealed Func<DateTime?, DateTime?>? ConvertToDateTime => (dt) => dt;
12+
13+
protected override sealed object? ConvertValue(JToken? token)
14+
{
15+
return DateTime.TryParse($"{token}", Culture, out var dateTime)
16+
? dateTime
17+
: null;
18+
}
19+
}
20+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Orbyss.Blazor.JsonForms.ComponentInstances
8+
{
9+
public abstract class DateTimeInputFormComponentInstanceBase<TDate> : DateOnlyInputFormComponentInstanceBase<TDate>
10+
{
11+
public DateTime? MinimumTime { get; set; }
12+
13+
public DateTime? MaximumTime { get; set; }
14+
15+
public int? TimeStep { get; set; }
16+
17+
protected sealed override IDictionary<string, object?> GetDateInputParameter()
18+
{
19+
var result = GetDateTimeInputParameter();
20+
21+
result[nameof(MinimumTime)] = MinimumTime;
22+
result[nameof(MaximumTime)] = MaximumTime;
23+
result[nameof(TimeStep)] = TimeStep;
24+
25+
return result;
26+
}
27+
28+
protected virtual IDictionary<string, object?> GetDateTimeInputParameter()
29+
{
30+
return new Dictionary<string, object?>();
31+
}
32+
}
33+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Newtonsoft.Json.Linq;
2+
using Orbyss.Components.Json.Models;
3+
4+
namespace Orbyss.Blazor.JsonForms.ComponentInstances
5+
{
6+
7+
public class DateTimeUtcTicksInputFormComponentInstance(Type componentGenericTypeDefinition) : DateTimeInputFormComponentInstanceBase<DateTimeUtcTicks?>
8+
{
9+
public override Type ComponentType => componentGenericTypeDefinition.MakeGenericType(typeof(DateTimeUtcTicks?));
10+
11+
protected override sealed Func<DateTime?, DateTimeUtcTicks?>? ConvertFromDateTime => dt => dt.HasValue ? new DateTimeUtcTicks(dt.Value.Ticks) : null;
12+
13+
protected override sealed Func<DateTimeUtcTicks?, DateTime?>? ConvertToDateTime => dt => dt.HasValue ? dt.Value.DateTime.DateTime : null;
14+
15+
protected override sealed object? ConvertValue(JToken? token)
16+
{
17+
if (token is null || string.IsNullOrWhiteSpace($"{token}"))
18+
{
19+
return null;
20+
}
21+
22+
return new DateTimeUtcTicks((long)token);
23+
}
24+
}
25+
}

src/Interpretation/ControlTypeInterpreter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public ControlType Interpret(JSchema jsonSchema, string absoluteJsonSchemaPath,
1111
{
1212
var jsonSchemaToken = JToken.Parse($"{jsonSchema}");
1313
var schemaToken = jsonSchemaToken?.SelectToken(absoluteJsonSchemaPath, false)
14-
?? throw new InvalidSchemaTypeConfigurationException("Data schema is null");
14+
?? throw new InvalidSchemaTypeConfigurationException($"Data schema section for json schema path '{absoluteJsonSchemaPath}' cannot be found");
1515

1616
var parentSchemaToken = !string.IsNullOrWhiteSpace(absoluteParentSchemaJsonPath)
1717
? jsonSchemaToken?.SelectToken(absoluteParentSchemaJsonPath, false)

src/Orbyss.Blazor.JsonForms.csproj

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,10 @@
3030
<RepositoryType>git</RepositoryType>
3131
<PackageIcon>icon.png</PackageIcon>
3232

33-
<ReleaseNotes>
34-
Fixed some major bugs that existed due to major refactoring and first preview revision release.
35-
1. Fixed a bug where the form would not render correctly if the schema was not valid.
36-
2. Fixed bug of OnValueChanged parameter that was not passed to dropdowns
37-
3. Translation context issues: now takes the default translation if the context is not configured with an active language
38-
</ReleaseNotes>
33+
<PackageReleaseNotes>
34+
- Reorganized the Date/Time form component instances.
35+
- Added clearer exception handling in ControlTypeInterpreter
36+
</PackageReleaseNotes>
3937
</PropertyGroup>
4038

4139
<ItemGroup>

0 commit comments

Comments
 (0)