-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bcff743
commit f8851b9
Showing
3 changed files
with
141 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 94 additions & 36 deletions
130
src/web/Jordnaer/Features/Posts/CreatePostComponent.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
<MudPaper Class="pa-4"> | ||
<MudText Typo="Typo.h4">Del et opslag</MudText> | ||
<MudTextField @bind-Value="newPostContent" Placeholder="Hvad har du på tankerne?" Lines="4" FullWidth /> | ||
<MudButton Variant="Variant.Filled" Color="Color.Success" OnClick="CreatePost">Send</MudButton> | ||
</MudPaper> | ||
@attribute [StreamRendering] | ||
|
||
<MudLoading @bind-Loading="_isLoading" Overlap Darken> | ||
<MudPaper Class="pa-4"> | ||
|
||
<AuthorizeView> | ||
<NotAuthorized> | ||
<MudAlert Icon="@Icons.Material.Filled.Lock">Du skal være logget ind for at lave et opslag.</MudAlert> | ||
<MudButton Href="/Account/Login" Variant="Variant.Filled" Color="Color.Success">Log ind</MudButton> | ||
</NotAuthorized> | ||
<Authorized> | ||
|
||
<MudText Typo="Typo.h4">Del et opslag</MudText> | ||
<MudTextField @bind-Value="_postText" Placeholder="Hvad har du på tankerne?" Lines="4" FullWidth /> | ||
|
||
<MudSelectExtended ItemCollection="_categories.Select(e => e.Name).ToList()" | ||
T="string" | ||
Label="Kategorier" | ||
MultiSelection="true" | ||
SelectedValues="_post.Categories.Select(e => e.Name)" | ||
ValuePresenter="ValuePresenter.Chip" | ||
Variant="Variant.Filled" | ||
SelectedValuesChanged="SelectedCategoriesChanged"> | ||
</MudSelectExtended> | ||
<MudButton Variant="Variant.Filled" Color="Color.Success" OnClick="CreatePost">Slå op</MudButton> | ||
<MudCheckBox Label="Vis område" title="Gem postnummer og by som en del af opslaget" @bind-Checked="_includeAreaInPost" /> | ||
Check failure on line 31 in src/web/Jordnaer/Features/Posts/CreatePostComponent.razor GitHub Actions / test
|
||
</Authorized> | ||
</AuthorizeView> | ||
</MudPaper> | ||
|
||
</MudLoading> | ||
|
||
@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<Category> _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<string> categories) | ||
=> _post.Categories = _categories.Where(e => categories.Contains(e.Name)).ToList(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters