-
-
Notifications
You must be signed in to change notification settings - Fork 82
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: Bookmarks #403
Feature: Bookmarks #403
Conversation
Add bookmarService Add bookmark page
Use this hook because of the need to use javascript to access localStorage
That is already a good start - of course the simple button is a bit rough, but I get your idea. I showed cases that because adding another icon on the navbar might be just too much. Maybe we can merge the Booksmarks with the Archive into dropdown. I am really open to new ideas here. For the icon. We can do one of those two approaches. Number 1: The simple one:
The further I think of it, probably the more I want to replace everything with approach 1 as it is just way too annoying with approach 2. |
Nah don't worry - just a detail. If you have other options/opinions let me know. I don't want to overload the navigation bar. I already feel there is too much stuff inside. |
I currently have no clue, tbh I feel like the navbar isn't overloaded at all (yet) so personally think it is fine to combine it with archive. |
Maybe I am overreacting :D In any case - we can try without moving it into a category with the archive. Grouping it later on is done in no time |
<button @onclick="OnClickHandler"> | ||
@if (!IsBookmarked) | ||
{ | ||
<svg class="w-6 h-6 text-gray-800 dark:text-white" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="none" viewBox="0 0 24 24"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have removed the button background as well as the border and added a hover effect for the icon
return bookmarks.Contains(postId); | ||
} | ||
|
||
public async Task<IReadOnlyList<BlogPost>> GetBookmarkedPosts() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would refactor that method or the general approach a bit better.
The Bookmark service should only take care of the LocalStorage.
The Bookmark-Page is responsible to load all the data via the repository. There should be no need of a DbContextFactory
- keep in mind that this blog also works with RavenDb, MongoDb, ...
YOu can have a look on the ArchivePage or SearchPage.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I reimplemented the GetBookmarkedPosts method, apologies did not consider that it can have different databases, im using the Repository in the bookmarks page now
@if (Configuration.Value.IsAboutMeEnabled) | ||
<li><a class="nav-link" href="/"><i class="home"></i> Home</a></li> | ||
<li><a class="nav-link" href="/archive"><i class="books"></i> Archive</a></li> | ||
<li><a class="dropdown-item" href="/bookmarks">Bookmarks</a></li> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can try to add an icon here instead as well. It also needs the "nav-link" class, otherwise it is misaligned.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, i'll add it and whoops overlooked that from copy pasting it
I also added a bit of padding to the Bookmarks page |
|
||
public async Task<bool> IsBookMarked(string postId) | ||
{ | ||
if(string.IsNullOrEmpty(postId)) throw new ArgumentException(nameof(postId)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if(string.IsNullOrEmpty(postId)) throw new ArgumentException(nameof(postId)); | |
ArgumentException.ThrowIfNullOrEmpty(postId); |
public async Task ToggleBookmark(string postId) | ||
{ | ||
await InitializeIfNotExists(); | ||
if(string.IsNullOrEmpty(postId)) throw new ArgumentException(nameof(postId)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same here - plus: The sanity check should be the first line in this code. No need to call InitializeIfNotExists
when postId is not set.
@@ -14,6 +15,7 @@ | |||
@inject IOptions<Introduction> Introduction | |||
@inject IOptions<ApplicationConfiguration> AppConfiguration | |||
@inject NavigationManager NavigationManager | |||
@inject IBookmarkService BookmarkService |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems not needed - can be removed
src/LinkDotNet.Blog.Web/Features/Bookmarks/Components/BookmarkButton.razor
Outdated
Show resolved
Hide resolved
|
||
public interface IBookmarkService | ||
{ | ||
public Task<bool> IsBookMarked(string postId); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would also write IsBookmarked
as you did not use capital M
for the other methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for sticking with me. There are some smaller things I would like to address.
} | ||
</button> | ||
|
||
<style> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We either could move this into BookmarkButton.razor.css
or on the global css file.
src/LinkDotNet.Blog.Web/Features/Bookmarks/Components/BookmarkButton.razor
Outdated
Show resolved
Hide resolved
@inject IBookmarkService BookmarkService | ||
@inject IRepository<BlogPost> BlogPostRepository; | ||
|
||
<section> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we align this a bit better to other pages like the archive?
<div class="container">
<h3 class="pb-3 fw-bold">Bookmarks</h3>
...
} | ||
</section> | ||
<style> | ||
section { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given the idea from above, this could be deleted
protected override async Task OnAfterRenderAsync(bool firstRender) | ||
{ | ||
var ids = await BookmarkService.GetBookmarkedPostIds(); | ||
bookmarkedPosts = await BlogPostRepository.GetAllByProjectionAsync(post => post, post => ids.Contains(post.Id)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: We could make a small sanity check if there are any ids. No need to go to the database if ids.Count == 0
ArgumentException.ThrowIfNullOrEmpty(postId); | ||
await InitializeIfNotExists(); | ||
|
||
if (await IsBookmarked(postId)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The two branches do a lot in common. Only the add/remove is different. Therefore the getItem
and SetItem
are common
<h2></h2> | ||
<div class="header"> | ||
<h1>@BlogPost.Title</h1> | ||
<BookmarkButton IsBookmarked="isBookmarked" OnClick="async () => { await BookmarkService.ToggleBookmark(BlogPost.Id); StateHasChanged(); }"></BookmarkButton> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this does more than one thing, it might be nice to have it inside a function rather than a lambda here.
@@ -129,6 +134,10 @@ else if (BlogPost is not null) | |||
{ | |||
await JsRuntime.InvokeVoidAsync("hljs.highlightAll"); | |||
_ = UserRecordService.StoreUserRecordAsync(); | |||
|
|||
ArgumentNullException.ThrowIfNull(BlogPost); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As this isn't an argument, the ArgumentNullException
doesn't really apply.
Rather an if(BlogPost is not null)
- if the user navigates to a page with a non-existing ID then we get an exception
Np It's fun to work on something else aside from my job :D. I'll adress the last few things when I get the chance |
Some tests are failing (the Blazor Unit testing) mainly because of new dependencies that aren't mocked or provided in the respective test suite |
Yes, I'm not familiar with the bUnit library (yet), I'm gonna try look into it later today and then try fixing those tests, if that is fine |
Don't worry - I can also take over that part if you want. But of course, I don't want to push myself on top of your good work! |
@linkdotnet I have been messing around with bUnit to fix the failing tests but I'm having difficulties. |
No worries - I can take over that part. |
That might be from bUnit itself. Could that I am maintainer of that library as well. Glad to have more work :D |
No clue, tried to read the entire log file that got spewed out and it starts at xUnit in the stacktrace and goes all the way down to the testrunner so not really sure 🤷♂️ |
Looks like an StackOverflow due to |
Caused by: bUnit-dev/bUnit#1682 |
@levinoeninckx the issue with stackoverflow was mainly because |
So I added some more tests, fixed some behavior and did some small restyling. Is there anything open from your side, otherwise that goes to Thanks for the hard work! @levinoeninckx |
Nope all good! @linkdotnet |
Awesome - then merge it is! |
No description provided.