Skip to content

Commit

Permalink
Add/remove sample sentence works
Browse files Browse the repository at this point in the history
  • Loading branch information
NicklausBrain committed Dec 1, 2024
1 parent 7af0480 commit 8442e3d
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 23 deletions.
40 changes: 26 additions & 14 deletions My1kWordsEe/Components/Layout/SampleV2.razor
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@
[Parameter]
public required SampleSentence Sample { get; init; }

[Parameter]
public required Func<SampleSentence, Task> AddToFavorites { get; init; }

[Parameter]
public required Func<SampleSentence, Task> RemoveFromFavorites { get; init; }

[Parameter]
public bool IsFavorite { get; init; }

private ConfirmDialog DeleteSampleDialog = default!;

[Inject] protected ToastService ToastService { get; set; } = default!;
Expand Down Expand Up @@ -108,27 +117,30 @@
</Button>
<audio id="@Sample.EeAudioUrl" src="@Sample.EeAudioUrl"></audio>
</li>
@if (IsAdmin)
@if (IsLoggedIn)
{
<li class="list-group-item border-0">
<Button Color="ButtonColor.Danger" Size="ButtonSize.Small" @onclick="ShowDeleteConfirmationAsync">
<i class="bi bi-trash3-fill"></i>
</Button>
@if (IsFavorite)
{
<Button Color="ButtonColor.Primary" Size="ButtonSize.Small" @onclick="() => RemoveFromFavorites(Sample)">
<i role="button" class="bi bi-star-fill"></i>
</Button>
}
else
{
<Button Color="ButtonColor.Primary" Size="ButtonSize.Small" @onclick="() => AddToFavorites(Sample)">
<i role="button" class="bi bi-star"></i>
</Button>
}
</li>
}
@if (IsLoggedIn)
@if (IsAdmin)
{
// todo add phrase to favorites
@* <li class="list-group-item border-0">
<Button Color="ButtonColor.Secondary" Size="ButtonSize.Small">
<i class="bi bi-star"></i>
</Button>
</li>
<li class="list-group-item border-0">
<Button Color="ButtonColor.Secondary" Size="ButtonSize.Small">
<i class="bi bi-star-fill"></i>
<Button Color="ButtonColor.Danger" Size="ButtonSize.Small" @onclick="ShowDeleteConfirmationAsync">
<i class="bi bi-trash3-fill"></i>
</Button>
</li> *@
</li>
}
</ul>
</td>
Expand Down
25 changes: 16 additions & 9 deletions My1kWordsEe/Components/Pages/WordPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -65,20 +65,22 @@
return await commonSample;
}

private async Task AddToFavorites()
private async Task AddToFavorites(dynamic favorite)
{
PreloadService.Show(SpinnerColor.Light, "Saving data...");
var user = await UserAccessor.GetRequiredUserAsync(User);

Check warning on line 71 in My1kWordsEe/Components/Pages/WordPage.razor

View workflow job for this annotation

GitHub Actions / test

Possible null reference argument for parameter 'user' in 'Task<ApplicationUser> IdentityUserAccessor.GetRequiredUserAsync(ClaimsPrincipal user)'.
Favorites = await AddToFavoritesCommand.Invoke(user.Id, Value);
Favorites = await AddToFavoritesCommand.Invoke(user.Id, favorite);
PreloadService.Hide();
StateHasChanged();
}

private async Task RemoveFromFavorites()
private async Task RemoveFromFavorites(dynamic favorite)
{
PreloadService.Show(SpinnerColor.Light, "Saving data...");
var user = await UserAccessor.GetRequiredUserAsync(User);

Check warning on line 80 in My1kWordsEe/Components/Pages/WordPage.razor

View workflow job for this annotation

GitHub Actions / test

Possible null reference argument for parameter 'user' in 'Task<ApplicationUser> IdentityUserAccessor.GetRequiredUserAsync(ClaimsPrincipal user)'.
Favorites = await RemoveFromFavoritesCommand.Invoke(user.Id, Value);
Favorites = await RemoveFromFavoritesCommand.Invoke(user.Id, favorite);
PreloadService.Hide();
StateHasChanged();
}

private bool IsSampleGenerationInProgress = false;
Expand Down Expand Up @@ -108,7 +110,9 @@

private bool IsLoggedIn => User?.Identity?.IsAuthenticated ?? false;

private bool IsFavorite => IsDataLoadedOk && IsLoggedIn && Favorites.HasValue && Favorites.Value.IsSuccess && Favorites.Value.Value.Words.ContainsKey(EeWord.ToLower());
private bool IsFavorite() => IsDataLoadedOk && IsLoggedIn && Favorites.HasValue && Favorites.Value.IsSuccess && Favorites.Value.Value.Words.ContainsKey(EeWord.ToLower());

private bool IsFavorite(SampleSentence sentence) => IsDataLoadedOk && IsLoggedIn && Favorites.HasValue && Favorites.Value.IsSuccess && Favorites.Value.Value.Sentences.ContainsKey(sentence.EeSentence.ToLower());
}

<script>
Expand All @@ -123,13 +127,13 @@
<h1>
@EeWord

@if (IsFavorite)
@if (IsFavorite())
{
<i role="button" class="bi bi-star-fill" style="color: rebeccapurple" @onclick="RemoveFromFavorites"></i>
<i role="button" class="bi bi-star-fill favorite-active" @onclick="() => RemoveFromFavorites(Value)"></i>
}
else
{
<i role="button" class="bi bi-star" @onclick="AddToFavorites"></i>
<i role="button" class="bi bi-star" @onclick="() => AddToFavorites(Value)"></i>
}

@if (IsDataLoadedOk && Value.EeAudioUrl != null)
Expand Down Expand Up @@ -169,7 +173,10 @@
@foreach (var sample in Value.Samples)
{
<div class="col col-12 col-sm-6 p-1">
<SampleV2 Sample="@sample">
<SampleV2 Sample="@sample"
AddToFavorites="AddToFavorites"
RemoveFromFavorites="RemoveFromFavorites"
IsFavorite="IsFavorite(sample)" >
</SampleV2>
</div>
}
Expand Down
10 changes: 10 additions & 0 deletions My1kWordsEe/Services/Cqs/RemoveFromFavoritesCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,15 @@ public async Task<Result<Favorites>> Invoke(string userId, SampleWord sample)
Result.Success(favorites));
});
}

public async Task<Result<Favorites>> Invoke(string userId, SampleSentence sample)
{
return await this.getFavoritesCommand.Invoke(userId).Bind(async favorites =>
{
favorites.Sentences.Remove(sample.EeSentence.ToLower());
return await this.azureBlobService.SaveFavorites(favorites).Bind(_ =>
Result.Success(favorites));
});
}
}
}
4 changes: 4 additions & 0 deletions My1kWordsEe/wwwroot/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ h1:focus {
.card:hover {
/*transform: scale(1.05);*/
box-shadow: 0 10px 20px rgba(0,0,0,.12), 0 4px 8px rgba(0,0,0,.06);
}

.favorite-active {
color: rebeccapurple
}

0 comments on commit 8442e3d

Please sign in to comment.