Skip to content

Commit

Permalink
more post work
Browse files Browse the repository at this point in the history
  • Loading branch information
NielsPilgaard committed Sep 1, 2024
1 parent bcff743 commit f8851b9
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 67 deletions.
6 changes: 3 additions & 3 deletions src/shared/Jordnaer.Shared/Database/Post.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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!;

Expand Down
130 changes: 94 additions & 36 deletions src/web/Jordnaer/Features/Posts/CreatePostComponent.razor
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

View workflow job for this annotation

GitHub Actions / test

The type of component 'MudCheckBox' cannot be inferred based on the values provided. Consider specifying the type arguments directly using the following attributes: 'T'.
</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();
}
72 changes: 44 additions & 28 deletions src/web/Jordnaer/Features/Posts/PostSearchForm.razor
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
@inject NavigationManager Navigation
@inject IJSRuntime JsRuntime

<MudContainer MaxWidth="MaxWidth.Small">
<MudContainer MaxWidth="MaxWidth.Small">

<MudPaper Elevation="3" Class="pa-10 mt-5">
<p class="font-open-sans-light" style="color: @JordnaerPalette.RedHeader; font-size: 20px;">
Expand All @@ -14,26 +11,46 @@
<DataAnnotationsValidator />

<MudGrid Justify="Justify.SpaceAround" Spacing="6">

<MudItem xs="12">

<MudItem xs="8">
<ZipCodeAutoComplete
For="() => Filter.Location"
Location="@Filter.Location"
LocationChanged="LocationChanged"
DisableSmartCompletion="_recentlyClearedForm"/>
</MudItem>
<MudItem xs="4">
<MudNumericField For="() => Filter.WithinRadiusKilometers"
@bind-Value="Filter.WithinRadiusKilometers"
Label="km"
Placeholder="Radius">
</MudNumericField>
</MudItem>

<MudItem xs="12">
<CategorySelector @bind-Categories="Filter.Categories" />
</MudItem>

<MudItem xs="12">
<MudTextField @bind-Value="Filter.Contents" Placeholder="Søg efter indlæg" Label="Søg på indlæg" Clearable />
</MudItem>

<MudItem xs="12" sm="11" md="10" lg="9" xl="8" Class="mt-8">
<MudButtonGroup OverrideStyles="false" Style="width: 100%;">
<MudButton FullWidth
Variant="Variant.Filled"
Color="Color.Success"
ButtonType="ButtonType.Submit">
<MudIcon Icon="@Icons.Material.Filled.Search" />
</MudButton>
<MudButton OnClick="ClearFilter"
Color="Color.Transparent"
Variant="Variant.Filled"
ButtonType="ButtonType.Reset">
<MudIcon Icon="@Icons.Material.Filled.Clear" />
</MudButton>
</MudButtonGroup>

<MudButtonGroup OverrideStyles="false" Style="width: 100%;">
<MudButton FullWidth
Variant="Variant.Filled"
Color="Color.Success"
ButtonType="ButtonType.Submit">
<MudIcon Icon="@Icons.Material.Filled.Search"/>
</MudButton>
<MudButton OnClick="ClearFilter"
Color="Color.Transparent"
Variant="Variant.Filled"
ButtonType="ButtonType.Reset">
<MudIcon Icon="@Icons.Material.Filled.Clear"/>
</MudButton>
</MudButtonGroup>
</MudItem>
</MudGrid>
</EditForm>
Expand All @@ -48,23 +65,22 @@
[Parameter, EditorRequired]
public EventCallback<PostSearchFilter> 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()
{
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;
}
}

0 comments on commit f8851b9

Please sign in to comment.