forge
Explore
Log in

Don't you dare go Hollow

🗑️ confirm before emptying the message trash

Browse files at this commit

3eb380ab7cbb856bc64e8a921327ed424569c126Burak BoduroğluParent: 15be768
  • fix
    • 🗑️ confirm before emptying the message trash
Show full message
fix: 🗑️ confirm before emptying the message trash
- Empty trash opens a destructive ConfirmDialog instead of deleting at once
- the summary says how many messages go, and that only the loaded page is
  emptied when the trash holds more
- the button label moves from a hardcoded Turkish string into i18n
- test: no delete call until the dialog is confirmed
src/i18n/messages.tsmodified+9 -0
@@ -1973,6 +1973,9 @@ export type MessageKey =
19731973 | "messages.deleteForeverOnlyTrash"
19741974 | "messages.readOnlyReceived"
19751975 | "messages.trashEmptiedToast"
1976+ | "messages.emptyTrash"
1977+ | "messages.emptyTrashSummary"
1978+ | "messages.emptyTrashPageSummary"
19761979 | "calendar.title"
19771980 | "calendar.today"
19781981 | "calendar.noEvents"
@@ -4191,6 +4194,9 @@ const en: Dict = {
41914194 "messages.deleteForeverOnlyTrash": "Permanent delete only applies to messages in the trash.",
41924195 "messages.readOnlyReceived": "Only received messages can be marked read or unread.",
41934196 "messages.trashEmptiedToast": "Trash emptied.",
4197+ "messages.emptyTrash": "Empty trash",
4198+ "messages.emptyTrashSummary": "{count} messages will be deleted permanently. This cannot be undone.",
4199+ "messages.emptyTrashPageSummary": "The {count} messages on this page will be deleted permanently ({total} in trash). This cannot be undone.",
41944200 "nav.academicYears": "Academic years",
41954201 "instances.title": "Sections",
41964202 "instances.item": "section",
@@ -6321,6 +6327,9 @@ const tr: Dict = {
63216327 "messages.deleteForeverOnlyTrash": "Kalıcı silme yalnızca çöp kutusundaki mesajlar için geçerli.",
63226328 "messages.readOnlyReceived": "Yalnızca gelen mesajların okundu durumu değiştirilebilir.",
63236329 "messages.trashEmptiedToast": "Çöp kutusu boşaltıldı.",
6330+ "messages.emptyTrash": "Çöp kutusunu boşalt",
6331+ "messages.emptyTrashSummary": "{count} mesaj kalıcı olarak silinecek. Bu işlem geri alınamaz.",
6332+ "messages.emptyTrashPageSummary": "Bu sayfadaki {count} mesaj kalıcı olarak silinecek (çöp kutusunda toplam {total}). Bu işlem geri alınamaz.",
63246333 "nav.academicYears": "Eğitim yılları",
63256334 "instances.title": "Şubeler",
63266335 "instances.item": "şube",
src/pages/messages-page.test.tsxadded+71 -0
@@ -0,0 +1,71 @@
1+import { cleanup, fireEvent, render, screen, waitFor, within } from "@solidjs/testing-library";
2+import type { JSX } from "solid-js";
3+import MessagesPage from "@/pages/messages-page";
4+import { PreferencesProvider } from "@/stores/preferences-context";
5+
6+const { getMessages, deleteMessageById } = vi.hoisted(() => ({
7+ getMessages: vi.fn(),
8+ deleteMessageById: vi.fn(),
9+}));
10+
11+vi.mock("@tanstack/solid-router", () => ({
12+ Link: (props: { to: string; children: JSX.Element; class?: string }) => <a href={props.to} class={props.class}>{props.children}</a>,
13+ Navigate: () => null,
14+ useNavigate: () => vi.fn(),
15+}));
16+vi.mock("@/stores/auth-context", () => ({
17+ useAuth: () => ({
18+ user: () => ({ id: "user-1", username: "teacher", role: "teacher" }),
19+ loading: () => false,
20+ error: () => undefined,
21+ }),
22+}));
23+vi.mock("@/api/messages", () => ({
24+ getMessages,
25+ deleteMessageById,
26+ patchMessageById: vi.fn(),
27+ postMessage: vi.fn(),
28+}));
29+
30+const message = (n: number) => ({
31+ id: `m-${n}`,
32+ sender: { id: "user-2", username: "sender" },
33+ recipient: { id: "user-1", username: "teacher" },
34+ subject: `Subject ${n}`,
35+ body: "body",
36+ sent_at: n,
37+ read: true,
38+});
39+
40+beforeEach(() => vi.spyOn(window, "scrollTo").mockImplementation(() => {}));
41+afterEach(() => {
42+ cleanup();
43+ vi.restoreAllMocks();
44+});
45+
46+test("emptying trash waits for confirmation and names what it deletes", async () => {
47+ getMessages.mockImplementation(async (folder: string) =>
48+ folder === "trash"
49+ ? { items: [message(1), message(2)], total: 5, limit: 30, offset: 0 }
50+ : { items: [], total: 0, limit: 30, offset: 0 },
51+ );
52+ deleteMessageById.mockResolvedValue(undefined);
53+
54+ render(() => (
55+ <PreferencesProvider>
56+ <MessagesPage />
57+ </PreferencesProvider>
58+ ));
59+
60+ const trashFolder = (await screen.findAllByRole("button", { name: /^(Trash|Çöp kutusu)$/ }))[0];
61+ fireEvent.click(trashFolder);
62+ fireEvent.click(await screen.findByRole("button", { name: /Empty trash|Çöp kutusunu boşalt/ }));
63+
64+ const dialog = await screen.findByRole("alertdialog");
65+ expect(dialog.textContent).toMatch(/2 mesaj|2 messages/);
66+ expect(dialog.textContent).toMatch(/toplam 5|5 in trash/);
67+ expect(deleteMessageById).not.toHaveBeenCalled();
68+
69+ fireEvent.click(within(dialog).getByRole("button", { name: /Empty trash|Çöp kutusunu boşalt/ }));
70+ await waitFor(() => expect(deleteMessageById).toHaveBeenCalledTimes(2));
71+});
src/pages/messages-page.tsxmodified+22 -2
@@ -37,6 +37,7 @@ import { matchesSearch } from "@/lib/search-text";
3737 import { personLabel } from "@/lib/person";
3838 import { createFlash } from "@/lib/flash";
3939 import { Badge } from "@/components/ui/badge";
40+import { ConfirmDialog } from "@/components/ui/confirm-dialog";
4041
4142 export default function MessagesPage() {
4243 const t = useT();
@@ -149,6 +150,16 @@ export default function MessagesPage() {
149150 }
150151 };
151152
153+ const [confirmEmptyTrash, setConfirmEmptyTrash] = createSignal(false);
154+ /** Empty trash deletes the loaded page only; the summary says so when more remain. */
155+ const emptyTrashSummary = () => {
156+ const count = messages().length;
157+ const total = messagePage()?.total ?? count;
158+ return total > count
159+ ? t("messages.emptyTrashPageSummary", { count, total })
160+ : t("messages.emptyTrashSummary", { count });
161+ };
162+
152163 const handleEmptyTrash = async () => {
153164 const trashItems = messages();
154165 if (trashItems.length === 0) return;
@@ -343,10 +354,10 @@ export default function MessagesPage() {
343354 variant="outline"
344355 size="sm"
345356 class="h-8 rounded-lg text-[13px] text-destructive hover:bg-destructive/10"
346- onClick={handleEmptyTrash}
357+ onClick={() => setConfirmEmptyTrash(true)}
347358 >
348359 <IconTrash class="mr-1.5 h-3.5 w-3.5" />
349- Çöp Kutusunu Boşalt
360+ {t("messages.emptyTrash")}
350361 </Button>
351362 </Show>
352363
@@ -472,6 +483,15 @@ export default function MessagesPage() {
472483 initialBody={replyData()?.body}
473484 />
474485 </div>
486+ <ConfirmDialog
487+ open={confirmEmptyTrash()}
488+ onOpenChange={setConfirmEmptyTrash}
489+ title={t("messages.emptyTrash")}
490+ variant="destructive"
491+ summary={emptyTrashSummary()}
492+ confirmLabel={t("messages.emptyTrash")}
493+ onConfirm={handleEmptyTrash}
494+ />
475495 </RouteGuard>
476496 );
477497 }