From f8851b95b0f27ad4f9878e63c7fbef758610b06e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20Pilgaard=20Gr=C3=B8ndahl?= Date: Sun, 1 Sep 2024 21:39:10 +0200 Subject: [PATCH] more post work --- src/shared/Jordnaer.Shared/Database/Post.cs | 6 +- .../Features/Posts/CreatePostComponent.razor | 130 +++++++++++++----- .../Features/Posts/PostSearchForm.razor | 72 ++++++---- 3 files changed, 141 insertions(+), 67 deletions(-) diff --git a/src/shared/Jordnaer.Shared/Database/Post.cs b/src/shared/Jordnaer.Shared/Database/Post.cs index 0df55993..babe59e1 100644 --- a/src/shared/Jordnaer.Shared/Database/Post.cs +++ b/src/shared/Jordnaer.Shared/Database/Post.cs @@ -11,15 +11,15 @@ public class Post [StringLength(1000, ErrorMessage = "Opslag må højest være 1000 karakterer lang.")] [Required(AllowEmptyStrings = false, ErrorMessage = "Opslag skal have mindst 1 karakter.")] - public required string Text { get; init; } + public required string Text { get; set; } - public DateTimeOffset CreatedUtc { get; init; } = DateTimeOffset.UtcNow; + public DateTimeOffset CreatedUtc { get; set; } = DateTimeOffset.UtcNow; public int? ZipCode { get; set; } public string? City { get; set; } [ForeignKey(nameof(UserProfile))] - public required string UserProfileId { get; init; } = null!; + public required string UserProfileId { get; set; } = null!; public UserProfile UserProfile { get; init; } = null!; diff --git a/src/web/Jordnaer/Features/Posts/CreatePostComponent.razor b/src/web/Jordnaer/Features/Posts/CreatePostComponent.razor index bf84829c..63fb52e7 100644 --- a/src/web/Jordnaer/Features/Posts/CreatePostComponent.razor +++ b/src/web/Jordnaer/Features/Posts/CreatePostComponent.razor @@ -1,41 +1,99 @@ -@inject CurrentUser CurrentUser +@inject IProfileCache ProfileCache +@inject ICategoryCache CategoryCache @inject IPostService PostService -@inject IPostSearchService PostSearchService -@inject NavigationManager Navigation -@inject IJSRuntime JsRuntime @inject ISnackbar Snackbar - - Del et opslag - - Send - +@attribute [StreamRendering] + + + + + + + Du skal være logget ind for at lave et opslag. + Log ind + + + + Del et opslag + + + + + Slå op + + + + + + + @code { - private string? newPostContent; - - private async Task CreatePost() - { - if (!string.IsNullOrWhiteSpace(newPostContent)) - { - var post = new Post - { - Id = Guid.NewGuid(), - Text = newPostContent, - CreatedUtc = DateTime.UtcNow, - UserProfileId = CurrentUser.Id, - // Add other necessary properties here - }; - - var result = await PostService.CreatePostAsync(post); - - result.Switch( - success => - { - Snackbar.Add("Opslaget blev oprettet.", Severity.Success); - newPostContent = string.Empty; - }, - error => Snackbar.Add(error.Value, Severity.Error) - ); - } - } + public bool _includeAreaInPost { get; set; } = true; + + private string? _postText; + + private readonly Post _post = new Post + { + Id = NewId.NextGuid(), + Text = string.Empty, + UserProfileId = string.Empty + }; + + private IEnumerable _categories = []; + private bool _isLoading = true; + private UserProfile? _userProfile; + + protected override async Task OnInitializedAsync() + { + _categories = await CategoryCache.GetOrCreateCategoriesAsync(); + _isLoading = false; + + _userProfile = await ProfileCache.GetProfileAsync(); + } + + private async Task CreatePost() + { + if (string.IsNullOrEmpty(_postText)) + { + return; + } + + if (_userProfile is null) + { + Snackbar.Add("Du skal være logget ind for at lave et opslag.", Severity.Info); + return; + } + + _post.Text = _postText; + _post.CreatedUtc = DateTimeOffset.UtcNow; + _post.UserProfileId = _userProfile.Id; + + if (_includeAreaInPost) + { + _post.ZipCode = _userProfile.ZipCode; + _post.City = _userProfile.City; + } + + var result = await PostService.CreatePostAsync(_post); + + result.Switch( + _ => + { + Snackbar.Add("Opslaget blev oprettet.", Severity.Success); + _postText = string.Empty; + }, + error => Snackbar.Add(error.Value, Severity.Error) + ); + } + + private void SelectedCategoriesChanged(IEnumerable categories) + => _post.Categories = _categories.Where(e => categories.Contains(e.Name)).ToList(); } diff --git a/src/web/Jordnaer/Features/Posts/PostSearchForm.razor b/src/web/Jordnaer/Features/Posts/PostSearchForm.razor index 09dcd47b..d1a62e84 100644 --- a/src/web/Jordnaer/Features/Posts/PostSearchForm.razor +++ b/src/web/Jordnaer/Features/Posts/PostSearchForm.razor @@ -1,7 +1,4 @@ -@inject NavigationManager Navigation -@inject IJSRuntime JsRuntime - - +

@@ -14,26 +11,46 @@ - - + + + + + + + + + + + + + + - - - - - - - - + + + + + + + + + @@ -48,11 +65,9 @@ [Parameter, EditorRequired] public EventCallback FilterChanged { get; set; } - [Parameter, EditorRequired] + [Parameter] public required EventCallback OnValidSubmit { get; set; } - private static readonly PostSearchFilter DefaultFilter = new(); - private bool _recentlyClearedForm = false; private async Task ClearFilter() @@ -60,11 +75,12 @@ Filter = new PostSearchFilter(); await FilterChanged.InvokeAsync(Filter); - var uriWithQuery = new Uri(Navigation.Uri); - var uriWithoutQuery = uriWithQuery.GetLeftPart(UriPartial.Path); - _recentlyClearedForm = true; - - await JsRuntime.NavigateTo(uriWithoutQuery); + } + + private void LocationChanged(string location) + { + Filter.Location = location; + Filter.WithinRadiusKilometers ??= 10; } }