Berichten: badge met aantal nieuwe berichten bij het menu-item (lichte unread-count-endpoint, 60 s verversing en bij focus, direct bijgewerkt bij openen)
Continuous Integration / config-sls (pull_request) Successful in 7s
Continuous Integration / changes (pull_request) Successful in 10s
Continuous Integration / config-asf (pull_request) Successful in 19s
Continuous Integration / backend-build (pull_request) Successful in 1m19s
Continuous Integration / frontend-prepare (pull_request) Successful in 32s
Continuous Integration / compile-sites (pull_request) Successful in 1m1s
Continuous Integration / vulnerability-scan (pull_request) Successful in 1m20s
Continuous Integration / frontend-build (pull_request) Successful in 35s
Continuous Integration / backend-test (pull_request) Successful in 2m20s
Continuous Integration / frontend-lint (pull_request) Successful in 36s
Continuous Integration / frontend-test (pull_request) Successful in 1m16s
Continuous Integration / publish-sls-test (pull_request) Skipped
Continuous Integration / publish-sls-production (pull_request) Skipped
Continuous Integration / deploy-sls-test (pull_request) Skipped
Continuous Integration / deploy-sls-production (pull_request) Skipped
Continuous Integration / publish-asf-test (pull_request) Skipped
Continuous Integration / deploy-asf-test (pull_request) Skipped
Continuous Integration / smoke-sls-test (pull_request) Skipped
Continuous Integration / smoke-asf-test (pull_request) Skipped
Continuous Integration / config-sls (pull_request) Successful in 7s
Continuous Integration / changes (pull_request) Successful in 10s
Continuous Integration / config-asf (pull_request) Successful in 19s
Continuous Integration / backend-build (pull_request) Successful in 1m19s
Continuous Integration / frontend-prepare (pull_request) Successful in 32s
Continuous Integration / compile-sites (pull_request) Successful in 1m1s
Continuous Integration / vulnerability-scan (pull_request) Successful in 1m20s
Continuous Integration / frontend-build (pull_request) Successful in 35s
Continuous Integration / backend-test (pull_request) Successful in 2m20s
Continuous Integration / frontend-lint (pull_request) Successful in 36s
Continuous Integration / frontend-test (pull_request) Successful in 1m16s
Continuous Integration / publish-sls-test (pull_request) Skipped
Continuous Integration / publish-sls-production (pull_request) Skipped
Continuous Integration / deploy-sls-test (pull_request) Skipped
Continuous Integration / deploy-sls-production (pull_request) Skipped
Continuous Integration / publish-asf-test (pull_request) Skipped
Continuous Integration / deploy-asf-test (pull_request) Skipped
Continuous Integration / smoke-sls-test (pull_request) Skipped
Continuous Integration / smoke-asf-test (pull_request) Skipped
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,6 +7,7 @@ import { useAuth } from '@/features/auth/context/auth-context';
|
|||||||
import { useAvailabilityStatus } from '@/features/availability/services/useAvailability';
|
import { useAvailabilityStatus } from '@/features/availability/services/useAvailability';
|
||||||
import { useSystemCapabilities } from '@/features/system/services/useSystemCapabilities';
|
import { useSystemCapabilities } from '@/features/system/services/useSystemCapabilities';
|
||||||
import { useContactSettings } from '@/features/contact/services/useContactSettings';
|
import { useContactSettings } from '@/features/contact/services/useContactSettings';
|
||||||
|
import { useUnreadContactCount } from '@/features/contact/services/useContactMessages';
|
||||||
import { LanguageSwitcher } from '@/i18n/LanguageSwitcher';
|
import { LanguageSwitcher } from '@/i18n/LanguageSwitcher';
|
||||||
import { BrandLogo } from './BrandLogo';
|
import { BrandLogo } from './BrandLogo';
|
||||||
import { ThemeToggle } from './ThemeToggle';
|
import { ThemeToggle } from './ThemeToggle';
|
||||||
@@ -66,6 +67,7 @@ export function Sidebar({ onClose }: SidebarProps = {}) {
|
|||||||
// while the host switch is off its routes answer 404). Only asked for roles that may see the entry.
|
// while the host switch is off its routes answer 404). Only asked for roles that may see the entry.
|
||||||
const mayManageContact = (role === 'Owner' || role === 'Administrator') && !!capabilities?.modules.includes('Contact');
|
const mayManageContact = (role === 'Owner' || role === 'Administrator') && !!capabilities?.modules.includes('Contact');
|
||||||
const { data: contactSettings } = useContactSettings({ enabled: mayManageContact });
|
const { data: contactSettings } = useContactSettings({ enabled: mayManageContact });
|
||||||
|
const { data: unreadMessages } = useUnreadContactCount(mayManageContact && !!contactSettings);
|
||||||
const systemUnavailable = availability?.status === 'NotAvailable';
|
const systemUnavailable = availability?.status === 'NotAvailable';
|
||||||
const visibleItems = NAV_ITEMS.filter(
|
const visibleItems = NAV_ITEMS.filter(
|
||||||
(item) =>
|
(item) =>
|
||||||
@@ -124,6 +126,20 @@ export function Sidebar({ onClose }: SidebarProps = {}) {
|
|||||||
>
|
>
|
||||||
<Icon className="size-4" />
|
<Icon className="size-4" />
|
||||||
{t(item.labelKey)}
|
{t(item.labelKey)}
|
||||||
|
{item.requiredModule === 'Contact' && !!unreadMessages && unreadMessages > 0 && (
|
||||||
|
<>
|
||||||
|
<span
|
||||||
|
className="ml-auto inline-flex min-w-5 items-center justify-center rounded-full bg-primary px-1.5 text-xs font-semibold leading-5 text-primary-foreground"
|
||||||
|
aria-hidden="true"
|
||||||
|
data-testid="nav-messages-badge"
|
||||||
|
>
|
||||||
|
{unreadMessages > 99 ? '99+' : unreadMessages}
|
||||||
|
</span>
|
||||||
|
<span className="sr-only" data-testid="nav-messages-badge-text">
|
||||||
|
{t('nav.unreadMessages', { count: unreadMessages })}
|
||||||
|
</span>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
</Link>
|
</Link>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
|
|||||||
@@ -140,6 +140,7 @@ export const contactHandlers = [
|
|||||||
unreadCount: messages.filter((m) => m.status === 'new').length,
|
unreadCount: messages.filter((m) => m.status === 'new').length,
|
||||||
});
|
});
|
||||||
}),
|
}),
|
||||||
|
http.get(`${BASE}/unread-count`, () => HttpResponse.json({ unreadCount: messages.filter((m) => m.status === 'new').length })),
|
||||||
http.get(`${BASE}/:id`, ({ params }) => {
|
http.get(`${BASE}/:id`, ({ params }) => {
|
||||||
const m = messages.find((x) => x.id === params.id);
|
const m = messages.find((x) => x.id === params.id);
|
||||||
return m ? HttpResponse.json(m) : new HttpResponse(null, { status: 404 });
|
return m ? HttpResponse.json(m) : new HttpResponse(null, { status: 404 });
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import {
|
|||||||
resetMockContact,
|
resetMockContact,
|
||||||
} from '@/features/contact/mocks/handlers';
|
} from '@/features/contact/mocks/handlers';
|
||||||
import { _resetSetupStatusCache } from '@/router';
|
import { _resetSetupStatusCache } from '@/router';
|
||||||
|
import { UNREAD_REFRESH_MS } from '@/features/contact/services/useContactMessages';
|
||||||
|
|
||||||
const LONG = { timeout: 10000 };
|
const LONG = { timeout: 10000 };
|
||||||
|
|
||||||
@@ -43,6 +44,50 @@ async function openSettings() {
|
|||||||
await screen.findByTestId('contact-panel-settings');
|
await screen.findByTestId('contact-panel-settings');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
describe('Unread badge on the Berichten menu item', () => {
|
||||||
|
it('shows the number of new messages, with an accessible name, and not by colour alone', async () => {
|
||||||
|
capabilities(WITH_CONTACT);
|
||||||
|
renderApp('/dashboard');
|
||||||
|
|
||||||
|
const badge = await screen.findByTestId('nav-messages-badge', {}, LONG);
|
||||||
|
expect(badge).toHaveTextContent('1');
|
||||||
|
expect(badge).toHaveAttribute('aria-hidden', 'true');
|
||||||
|
expect(screen.getByTestId('nav-messages')).toHaveAccessibleName(/Berichten.*1 nieuw bericht|Messages.*1 new message/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('is hidden at zero and shows 99+ above 99', async () => {
|
||||||
|
capabilities(WITH_CONTACT);
|
||||||
|
server.use(http.get(`${API_BASE}/api/v1/contact-messages/unread-count`, () => HttpResponse.json({ unreadCount: 0 })));
|
||||||
|
renderApp('/dashboard');
|
||||||
|
await screen.findByTestId('nav-messages', {}, LONG);
|
||||||
|
await new Promise((r) => setTimeout(r, 300));
|
||||||
|
expect(screen.queryByTestId('nav-messages-badge')).not.toBeInTheDocument();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('shows 99+ for a hundred or more', async () => {
|
||||||
|
capabilities(WITH_CONTACT);
|
||||||
|
server.use(http.get(`${API_BASE}/api/v1/contact-messages/unread-count`, () => HttpResponse.json({ unreadCount: 250 })));
|
||||||
|
renderApp('/dashboard');
|
||||||
|
|
||||||
|
expect(await screen.findByTestId('nav-messages-badge', {}, LONG)).toHaveTextContent('99+');
|
||||||
|
expect(screen.getByTestId('nav-messages-badge-text')).toHaveTextContent(/250/);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('follows at once when a message is opened (and so marked as read)', async () => {
|
||||||
|
capabilities(WITH_CONTACT);
|
||||||
|
renderApp('/messages');
|
||||||
|
expect(await screen.findByTestId('nav-messages-badge', {}, LONG)).toHaveTextContent('1');
|
||||||
|
|
||||||
|
await userEvent.click(await screen.findByTestId(`contact-row-${MSG_NEW}`, {}, LONG));
|
||||||
|
|
||||||
|
await waitFor(() => expect(screen.queryByTestId('nav-messages-badge')).not.toBeInTheDocument(), LONG);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('asks the light endpoint every minute and when the window gets focus', () => {
|
||||||
|
expect(UNREAD_REFRESH_MS).toBe(60_000);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
describe('Berichten navigation', () => {
|
describe('Berichten navigation', () => {
|
||||||
it('shows the sidebar entry when the module is loaded and the API answers', async () => {
|
it('shows the sidebar entry when the module is loaded and the API answers', async () => {
|
||||||
capabilities(WITH_CONTACT);
|
capabilities(WITH_CONTACT);
|
||||||
|
|||||||
@@ -22,13 +22,20 @@ export function useContactMessages(filter: ContactFilter, page: number) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Only the unread counter, for the dashboard tile (one item is enough: the counter is part of every list answer). */
|
/**
|
||||||
|
* Only the unread counter, for the menu badge and the dashboard tile (a light endpoint). It is refreshed every minute and
|
||||||
|
* when the window gets focus again; React Query stops the interval while the tab is hidden. Opening or marking a message
|
||||||
|
* invalidates it (the key lives under ['contact', 'messages']), so the badge follows at once.
|
||||||
|
*/
|
||||||
|
export const UNREAD_REFRESH_MS = 60_000;
|
||||||
|
|
||||||
export function useUnreadContactCount(enabled: boolean) {
|
export function useUnreadContactCount(enabled: boolean) {
|
||||||
return useQuery<number, Error>({
|
return useQuery<number, Error>({
|
||||||
queryKey: ['contact', 'messages', 'unreadCount'],
|
queryKey: ['contact', 'messages', 'unreadCount'],
|
||||||
enabled,
|
enabled,
|
||||||
queryFn: async () =>
|
refetchInterval: UNREAD_REFRESH_MS,
|
||||||
(await api.get<ContactMessageList>(`${BASE}?page=1&pageSize=1`)).unreadCount,
|
refetchOnWindowFocus: true,
|
||||||
|
queryFn: async () => (await api.get<{ unreadCount: number }>(`${BASE}/unread-count`)).unreadCount,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,9 @@
|
|||||||
"profile": "Profile",
|
"profile": "Profile",
|
||||||
"settings": "Settings",
|
"settings": "Settings",
|
||||||
"openMenu": "Open navigation",
|
"openMenu": "Open navigation",
|
||||||
"closeMenu": "Close navigation"
|
"closeMenu": "Close navigation",
|
||||||
|
"unreadMessages_one": "{{count}} new message",
|
||||||
|
"unreadMessages_other": "{{count}} new messages"
|
||||||
},
|
},
|
||||||
"theme": {
|
"theme": {
|
||||||
"switchToLight": "Switch to light theme",
|
"switchToLight": "Switch to light theme",
|
||||||
|
|||||||
@@ -15,7 +15,9 @@
|
|||||||
"profile": "Profiel",
|
"profile": "Profiel",
|
||||||
"settings": "Instellingen",
|
"settings": "Instellingen",
|
||||||
"openMenu": "Navigatie openen",
|
"openMenu": "Navigatie openen",
|
||||||
"closeMenu": "Navigatie sluiten"
|
"closeMenu": "Navigatie sluiten",
|
||||||
|
"unreadMessages_one": "{{count}} nieuw bericht",
|
||||||
|
"unreadMessages_other": "{{count}} nieuwe berichten"
|
||||||
},
|
},
|
||||||
"theme": {
|
"theme": {
|
||||||
"switchToLight": "Overschakelen naar licht thema",
|
"switchToLight": "Overschakelen naar licht thema",
|
||||||
|
|||||||
@@ -393,6 +393,22 @@
|
|||||||
],
|
],
|
||||||
"type": "object"
|
"type": "object"
|
||||||
},
|
},
|
||||||
|
"ContactUnreadCount": {
|
||||||
|
"properties": {
|
||||||
|
"unreadCount": {
|
||||||
|
"format": "int32",
|
||||||
|
"pattern": "^-?(?:0|[1-9]\\d*)$",
|
||||||
|
"type": [
|
||||||
|
"integer",
|
||||||
|
"string"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": [
|
||||||
|
"unreadCount"
|
||||||
|
],
|
||||||
|
"type": "object"
|
||||||
|
},
|
||||||
"CreateCmsInstanceRequest": {
|
"CreateCmsInstanceRequest": {
|
||||||
"properties": {
|
"properties": {
|
||||||
"apiKey": {
|
"apiKey": {
|
||||||
@@ -1956,6 +1972,25 @@
|
|||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"/api/v1/contact-messages/unread-count": {
|
||||||
|
"get": {
|
||||||
|
"responses": {
|
||||||
|
"200": {
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/ContactUnreadCount"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"description": "OK"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"tags": [
|
||||||
|
"ContactMessagesAdmin"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
"/api/v1/contact-messages/{id}": {
|
"/api/v1/contact-messages/{id}": {
|
||||||
"delete": {
|
"delete": {
|
||||||
"parameters": [
|
"parameters": [
|
||||||
|
|||||||
@@ -30,6 +30,17 @@ public class ContactAdminControllerTests
|
|||||||
ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() },
|
ControllerContext = new ControllerContext { HttpContext = new DefaultHttpContext() },
|
||||||
};
|
};
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task UnreadCount_ReturnsOnlyTheNumber_AndIsAdminOnlyLikeTheRestOfTheController()
|
||||||
|
{
|
||||||
|
_service.CountUnreadAsync(Arg.Any<CancellationToken>()).Returns(3);
|
||||||
|
|
||||||
|
var result = await Messages().UnreadCount(CancellationToken.None);
|
||||||
|
|
||||||
|
result.Should().BeOfType<OkObjectResult>().Which.Value.Should().Be(new ContactUnreadCount(3));
|
||||||
|
typeof(ContactMessagesAdminController).GetCustomAttribute<AuthorizeAttribute>()!.Policy.Should().Be("AdminOnly");
|
||||||
|
}
|
||||||
|
|
||||||
private ContactSettingsController Settings(Guid? userId = null)
|
private ContactSettingsController Settings(Guid? userId = null)
|
||||||
{
|
{
|
||||||
var http = new DefaultHttpContext();
|
var http = new DefaultHttpContext();
|
||||||
@@ -242,6 +253,7 @@ public class ContactAdminControllerTests
|
|||||||
.Should().BeEquivalentTo(
|
.Should().BeEquivalentTo(
|
||||||
[
|
[
|
||||||
"GET contact-messages",
|
"GET contact-messages",
|
||||||
|
"GET contact-messages/unread-count",
|
||||||
"GET contact-messages/{id:guid}",
|
"GET contact-messages/{id:guid}",
|
||||||
"PATCH contact-messages/{id:guid}",
|
"PATCH contact-messages/{id:guid}",
|
||||||
"DELETE contact-messages/{id:guid}",
|
"DELETE contact-messages/{id:guid}",
|
||||||
|
|||||||
@@ -161,6 +161,18 @@ public class ContactAdminServiceTests
|
|||||||
(await h.Sut.ListAsync("read", 1, 25, CancellationToken.None))!.UnreadCount.Should().Be(2);
|
(await h.Sut.ListAsync("read", 1, 25, CancellationToken.None))!.UnreadCount.Should().Be(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task TheLightUnreadCount_IsTheSameNumber_ForThisSiteOnly()
|
||||||
|
{
|
||||||
|
using var h = new AdminHarness();
|
||||||
|
h.Add(ContactMessageStatus.New);
|
||||||
|
h.Add(ContactMessageStatus.Read);
|
||||||
|
h.Add(ContactMessageStatus.Spam);
|
||||||
|
h.Add(ContactMessageStatus.New, site: "another-site");
|
||||||
|
|
||||||
|
(await h.Sut.CountUnreadAsync(CancellationToken.None)).Should().Be(1);
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public async Task OtherSitesMessages_AreInvisible()
|
public async Task OtherSitesMessages_AreInvisible()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -74,6 +74,12 @@ public class ContactMessagesAdminController(IContactAdminService service, IOptio
|
|||||||
return result is null ? ContactProblems.Invalid(options.Value.ProblemTypeBase, "query") : Ok(result);
|
return result is null ? ContactProblems.Invalid(options.Value.ProblemTypeBase, "query") : Ok(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>A light count for the badge in the menu; a separate route so the page does not have to fetch a list.</summary>
|
||||||
|
[HttpGet("unread-count")]
|
||||||
|
[ProducesResponseType(typeof(ContactUnreadCount), StatusCodes.Status200OK, "application/json")]
|
||||||
|
public async Task<IActionResult> UnreadCount(CancellationToken cancellationToken)
|
||||||
|
=> Ok(new ContactUnreadCount(await service.CountUnreadAsync(cancellationToken)));
|
||||||
|
|
||||||
[HttpGet("{id:guid}")]
|
[HttpGet("{id:guid}")]
|
||||||
[ProducesResponseType(typeof(ContactMessageDetail), StatusCodes.Status200OK, "application/json")]
|
[ProducesResponseType(typeof(ContactMessageDetail), StatusCodes.Status200OK, "application/json")]
|
||||||
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
||||||
|
|||||||
@@ -17,6 +17,10 @@ public record ContactMessageListItem(
|
|||||||
string Status,
|
string Status,
|
||||||
string MailStatus);
|
string MailStatus);
|
||||||
|
|
||||||
|
/// <summary>Only the number of new (unread) messages: a small answer for the badge in the admin menu.</summary>
|
||||||
|
[ExcludeFromCodeCoverage]
|
||||||
|
public record ContactUnreadCount(int UnreadCount);
|
||||||
|
|
||||||
[ExcludeFromCodeCoverage]
|
[ExcludeFromCodeCoverage]
|
||||||
public record ContactMessageList(
|
public record ContactMessageList(
|
||||||
IReadOnlyList<ContactMessageListItem> Items,
|
IReadOnlyList<ContactMessageListItem> Items,
|
||||||
|
|||||||
@@ -17,6 +17,9 @@ public interface IContactAdminService
|
|||||||
|
|
||||||
Task<ContactMessageDetail?> GetAsync(Guid id, CancellationToken cancellationToken);
|
Task<ContactMessageDetail?> GetAsync(Guid id, CancellationToken cancellationToken);
|
||||||
|
|
||||||
|
/// <summary>The number of new messages of this site (spam and read ones do not count).</summary>
|
||||||
|
Task<int> CountUnreadAsync(CancellationToken cancellationToken);
|
||||||
|
|
||||||
/// <summary>Null when the message does not exist.</summary>
|
/// <summary>Null when the message does not exist.</summary>
|
||||||
Task<ContactMessageDetail?> SetStatusAsync(Guid id, ContactMessageStatus status, CancellationToken cancellationToken);
|
Task<ContactMessageDetail?> SetStatusAsync(Guid id, ContactMessageStatus status, CancellationToken cancellationToken);
|
||||||
|
|
||||||
@@ -75,6 +78,9 @@ public sealed class ContactAdminService(
|
|||||||
page, pageSize, total, unread);
|
page, pageSize, total, unread);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Task<int> CountUnreadAsync(CancellationToken cancellationToken)
|
||||||
|
=> repository.CountUnreadAsync(options.Value.EffectiveSiteId, cancellationToken);
|
||||||
|
|
||||||
public async Task<ContactMessageDetail?> GetAsync(Guid id, CancellationToken cancellationToken)
|
public async Task<ContactMessageDetail?> GetAsync(Guid id, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var message = await repository.GetAsync(options.Value.EffectiveSiteId, id, cancellationToken);
|
var message = await repository.GetAsync(options.Value.EffectiveSiteId, id, cancellationToken);
|
||||||
|
|||||||
Reference in New Issue
Block a user