diff --git a/src/i18n/messages.ts b/src/i18n/messages.ts index fea7f67..ada8f02 100644 --- a/src/i18n/messages.ts +++ b/src/i18n/messages.ts @@ -1973,6 +1973,9 @@ export type MessageKey = | "messages.deleteForeverOnlyTrash" | "messages.readOnlyReceived" | "messages.trashEmptiedToast" + | "messages.emptyTrash" + | "messages.emptyTrashSummary" + | "messages.emptyTrashPageSummary" | "calendar.title" | "calendar.today" | "calendar.noEvents" @@ -4191,6 +4194,9 @@ const en: Dict = { "messages.deleteForeverOnlyTrash": "Permanent delete only applies to messages in the trash.", "messages.readOnlyReceived": "Only received messages can be marked read or unread.", "messages.trashEmptiedToast": "Trash emptied.", + "messages.emptyTrash": "Empty trash", + "messages.emptyTrashSummary": "{count} messages will be deleted permanently. This cannot be undone.", + "messages.emptyTrashPageSummary": "The {count} messages on this page will be deleted permanently ({total} in trash). This cannot be undone.", "nav.academicYears": "Academic years", "instances.title": "Sections", "instances.item": "section", @@ -6321,6 +6327,9 @@ const tr: Dict = { "messages.deleteForeverOnlyTrash": "Kalıcı silme yalnızca çöp kutusundaki mesajlar için geçerli.", "messages.readOnlyReceived": "Yalnızca gelen mesajların okundu durumu değiştirilebilir.", "messages.trashEmptiedToast": "Çöp kutusu boşaltıldı.", + "messages.emptyTrash": "Çöp kutusunu boşalt", + "messages.emptyTrashSummary": "{count} mesaj kalıcı olarak silinecek. Bu işlem geri alınamaz.", + "messages.emptyTrashPageSummary": "Bu sayfadaki {count} mesaj kalıcı olarak silinecek (çöp kutusunda toplam {total}). Bu işlem geri alınamaz.", "nav.academicYears": "Eğitim yılları", "instances.title": "Şubeler", "instances.item": "şube", diff --git a/src/pages/messages-page.test.tsx b/src/pages/messages-page.test.tsx new file mode 100644 index 0000000..2ce5afe --- /dev/null +++ b/src/pages/messages-page.test.tsx @@ -0,0 +1,71 @@ +import { cleanup, fireEvent, render, screen, waitFor, within } from "@solidjs/testing-library"; +import type { JSX } from "solid-js"; +import MessagesPage from "@/pages/messages-page"; +import { PreferencesProvider } from "@/stores/preferences-context"; + +const { getMessages, deleteMessageById } = vi.hoisted(() => ({ + getMessages: vi.fn(), + deleteMessageById: vi.fn(), +})); + +vi.mock("@tanstack/solid-router", () => ({ + Link: (props: { to: string; children: JSX.Element; class?: string }) => {props.children}, + Navigate: () => null, + useNavigate: () => vi.fn(), +})); +vi.mock("@/stores/auth-context", () => ({ + useAuth: () => ({ + user: () => ({ id: "user-1", username: "teacher", role: "teacher" }), + loading: () => false, + error: () => undefined, + }), +})); +vi.mock("@/api/messages", () => ({ + getMessages, + deleteMessageById, + patchMessageById: vi.fn(), + postMessage: vi.fn(), +})); + +const message = (n: number) => ({ + id: `m-${n}`, + sender: { id: "user-2", username: "sender" }, + recipient: { id: "user-1", username: "teacher" }, + subject: `Subject ${n}`, + body: "body", + sent_at: n, + read: true, +}); + +beforeEach(() => vi.spyOn(window, "scrollTo").mockImplementation(() => {})); +afterEach(() => { + cleanup(); + vi.restoreAllMocks(); +}); + +test("emptying trash waits for confirmation and names what it deletes", async () => { + getMessages.mockImplementation(async (folder: string) => + folder === "trash" + ? { items: [message(1), message(2)], total: 5, limit: 30, offset: 0 } + : { items: [], total: 0, limit: 30, offset: 0 }, + ); + deleteMessageById.mockResolvedValue(undefined); + + render(() => ( + + + + )); + + const trashFolder = (await screen.findAllByRole("button", { name: /^(Trash|Çöp kutusu)$/ }))[0]; + fireEvent.click(trashFolder); + fireEvent.click(await screen.findByRole("button", { name: /Empty trash|Çöp kutusunu boşalt/ })); + + const dialog = await screen.findByRole("alertdialog"); + expect(dialog.textContent).toMatch(/2 mesaj|2 messages/); + expect(dialog.textContent).toMatch(/toplam 5|5 in trash/); + expect(deleteMessageById).not.toHaveBeenCalled(); + + fireEvent.click(within(dialog).getByRole("button", { name: /Empty trash|Çöp kutusunu boşalt/ })); + await waitFor(() => expect(deleteMessageById).toHaveBeenCalledTimes(2)); +}); diff --git a/src/pages/messages-page.tsx b/src/pages/messages-page.tsx index 7b1a9d7..0f82457 100644 --- a/src/pages/messages-page.tsx +++ b/src/pages/messages-page.tsx @@ -37,6 +37,7 @@ import { matchesSearch } from "@/lib/search-text"; import { personLabel } from "@/lib/person"; import { createFlash } from "@/lib/flash"; import { Badge } from "@/components/ui/badge"; +import { ConfirmDialog } from "@/components/ui/confirm-dialog"; export default function MessagesPage() { const t = useT(); @@ -149,6 +150,16 @@ export default function MessagesPage() { } }; + const [confirmEmptyTrash, setConfirmEmptyTrash] = createSignal(false); + /** Empty trash deletes the loaded page only; the summary says so when more remain. */ + const emptyTrashSummary = () => { + const count = messages().length; + const total = messagePage()?.total ?? count; + return total > count + ? t("messages.emptyTrashPageSummary", { count, total }) + : t("messages.emptyTrashSummary", { count }); + }; + const handleEmptyTrash = async () => { const trashItems = messages(); if (trashItems.length === 0) return; @@ -343,10 +354,10 @@ export default function MessagesPage() { variant="outline" size="sm" class="h-8 rounded-lg text-[13px] text-destructive hover:bg-destructive/10" - onClick={handleEmptyTrash} + onClick={() => setConfirmEmptyTrash(true)} > - Çöp Kutusunu Boşalt + {t("messages.emptyTrash")} @@ -472,6 +483,15 @@ export default function MessagesPage() { initialBody={replyData()?.body} /> + ); }