Skip to content

Commit

Permalink
Small improvements on CrudTable and CrudService. Added DateTimeDiff t…
Browse files Browse the repository at this point in the history
…o string extension. Added FakeCurrentUserProvider.
  • Loading branch information
Felix-CodingClimber committed Apr 7, 2024
1 parent 19812c8 commit f32a504
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,33 @@ public static string ToFriendlyLocalDateTime(this DateTimeOffset? dateTimeOffset
{
return dateTimeOffset?.LocalDateTime.ToFriendlyDateTime() ?? Placeholder;
}

public static string ToFriendlyDateDiff(this DateTimeOffset dateTimeOffset, DateTime now)
{
TimeSpan timeSpan = new DateTimeOffset(now).Subtract(dateTimeOffset);

if (timeSpan.TotalMinutes < 1)
return "just now";
if (timeSpan.TotalMinutes < 2)
return "a minute ago";
if (timeSpan.TotalHours < 1)
return $"{timeSpan.Minutes} minutes ago";
if (timeSpan.TotalHours < 2)
return "an hour ago";
if (timeSpan.TotalDays < 1)
return $"{timeSpan.Hours} hours ago";
if (timeSpan.TotalDays < 2)
return "yesterday";
if (timeSpan.TotalDays < 30)
return $"{timeSpan.Days} days ago";
if (timeSpan.TotalDays < 60)
return "a month ago";
if (timeSpan.TotalDays < 365)
return $"{Math.Round(timeSpan.TotalDays / 30)} months ago";
if (timeSpan.TotalDays < 730)
return "last year";

// Handle dates more than 2 years ago
return string.Format("{0} years ago", Math.Round(timeSpan.TotalDays / 365));
}
}
5 changes: 2 additions & 3 deletions src/DotNetElements.Web.AspNetCore/CurrentUserProviderWeb.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using DotNetElements.Core;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http;

namespace DotNetElements.Web.AspNetCore;

Expand All @@ -15,6 +14,6 @@ public CurrentUserProviderWeb(IHttpContextAccessor contextAccessor)
// todo
public Guid GetCurrentUserId()
{
return new Guid("FF4F759C-0916-4611-9B66-306543A51B2A");
return new Guid("e8d118e0-18c6-4fff-9d86-e91a915d8198");
}
}
16 changes: 16 additions & 0 deletions src/DotNetElements.Web.AspNetCore/FakeCurrentUserProviderWeb.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace DotNetElements.Web.AspNetCore;

public class FakeCurrentUserProviderWeb : ICurrentUserProvider
{
private readonly Guid fakeUserId;

public FakeCurrentUserProviderWeb(Guid fakeUserId)
{
this.fakeUserId = fakeUserId;
}

public Guid GetCurrentUserId()
{
return fakeUserId;
}
}
18 changes: 16 additions & 2 deletions src/DotNetElements.Web.Blazor/CrudService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ public interface ICrudService<TKey, TModel, TDetails, TEditModel> : IReadOnlyCru
where TKey : notnull, IEquatable<TKey>
where TModel : IModel<TKey>
where TDetails : ModelDetails
where TEditModel : IMapFromModel<TEditModel, TModel>, ICreateNew<TEditModel>
{
Task<Result<TModel>> CreateEntryAsync(TEditModel editModel);
Task<Result> DeleteEntryAsync(TModel model);
Expand All @@ -15,7 +14,6 @@ public class CrudService<TKey, TModel, TDetails, TEditModel> : ReadOnlyCrudServi
where TKey : notnull, IEquatable<TKey>
where TModel : IModel<TKey>
where TDetails : ModelDetails
where TEditModel : IMapFromModel<TEditModel, TModel>, ICreateNew<TEditModel>
{
public CrudService(ISnackbar snackbar, HttpClient httpClient, CrudOptions<TModel> options) : base(snackbar, httpClient, options)
{
Expand Down Expand Up @@ -77,4 +75,20 @@ public virtual async Task<Result> DeleteEntryAsync(TModel model)

return result;
}

// todo use everywhere
protected void NotifyUser(Result result, string messageOk, string messageFail)
{
// todo add logging
// todo wrap Snackbar call in bool option NotifyUser
// todo add function OnDeleteSuccess
if (result.IsOk)
{
Snackbar.Add(messageOk, Severity.Success);
}
else
{
Snackbar.Add(messageFail, Severity.Error);
}
}
}
12 changes: 12 additions & 0 deletions src/DotNetElements.Web.Blazor/CrudServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,16 @@ public virtual async Task<Result<List<TModel>>> GetAllEntriesAsync()

return result;
}

// todo use everywhere
protected void NotifyUserIfFailed(Result result, string message= "Failed to fetch entries from server")
{
// todo add logging
// todo wrap Snackbar call in bool option NotifyUser
// todo add function OnDeleteSuccess
if (result.IsFail)
{
Snackbar.Add(message, Severity.Error);
}
}
}
38 changes: 38 additions & 0 deletions src/DotNetElements.Web.Blazor/CrudTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,43 @@ protected override async Task OnEditEntry(ModelWithDetails<TModel, TDetails> con
Snackbar.Add("Failed to save changes", Severity.Error);
}
}

protected async Task<Result<TDialogModel>> ShowCrudEditDialogAsync<TDialog, TDialogModel, TDialogEditModel>(
bool isEditMode,
string title,
string apiEndpoint,
TDialogEditModel editModel,
DialogParameters<TDialog>? additionalParameters = null,
DialogOptions? dialogOptions = null)
where TDialog : CrudEditDialog<TDialogModel, TDialogEditModel>
where TDialogEditModel : notnull
{
DialogParameters<TDialog> parameters = new()
{
{ x => x.IsEditMode, isEditMode },
{ x => x.Model, editModel },
{ x => x.EditContext, new EditContext(editModel) },
{ x => x.ApiEndpoint, apiEndpoint }
};

foreach ((string key, object value) in additionalParameters ?? [])
parameters.Add(key, value);

IDialogReference dialog = await DialogService.ShowAsync<TDialog>(title, parameters, dialogOptions ?? CrudTable.DefaultEditDialogOptions);
DialogResult result = await dialog.Result;

Result<TDialogModel> dialogResult = (Result<TDialogModel>)result.Data;

if (dialogResult.IsOk)
{
Snackbar.Add("Entry saved", Severity.Success);
}
else
{
Snackbar.Add("Failed to save entry", Severity.Error);
}

return dialogResult;
}
}

1 change: 1 addition & 0 deletions src/DotNetElements.Web.Blazor/CrudTableNotRecords.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
No Records found
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Linq.Expressions;

namespace DotNetElements.Web.Blazor;
namespace DotNetElements.Web.Blazor.Extensions;

public static class DialogeServiceExtensions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public static IServiceCollection AddCrudService<TKey, TModel, TDetails, TEditMod
where TKey : notnull, IEquatable<TKey>
where TModel : IModel<TKey>
where TDetails : ModelDetails
where TEditModel : IMapFromModel<TEditModel, TModel>, ICreateNew<TEditModel>
{
// todo consider using the options pattern
// Action<CrudOptions<TModel>> configureOptions as parameter
Expand Down

0 comments on commit f32a504

Please sign in to comment.