🗑️ confirm before emptying the message trash
Browse files at this commit3eb380ab7cbb856bc64e8a921327ed424569c126Parent: 15be768- fix
- 🗑️ confirm before emptying the message trash
3 changed files +102 -2
- src/i18n/messages.ts +9 -0
- src/pages/messages-page.test.tsx +71 -0
- src/pages/messages-page.tsx +22 -2
src/i18n/messages.tsmodified+9 -0
| @@ -1973,6 +1973,9 @@ export type MessageKey = | ||
| 1973 | 1973 | | "messages.deleteForeverOnlyTrash" |
| 1974 | 1974 | | "messages.readOnlyReceived" |
| 1975 | 1975 | | "messages.trashEmptiedToast" |
| 1976 | + | "messages.emptyTrash" | |
| 1977 | + | "messages.emptyTrashSummary" | |
| 1978 | + | "messages.emptyTrashPageSummary" | |
| 1976 | 1979 | | "calendar.title" |
| 1977 | 1980 | | "calendar.today" |
| 1978 | 1981 | | "calendar.noEvents" |
| @@ -4191,6 +4194,9 @@ const en: Dict = { | ||
| 4191 | 4194 | "messages.deleteForeverOnlyTrash": "Permanent delete only applies to messages in the trash.", |
| 4192 | 4195 | "messages.readOnlyReceived": "Only received messages can be marked read or unread.", |
| 4193 | 4196 | "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.", | |
| 4194 | 4200 | "nav.academicYears": "Academic years", |
| 4195 | 4201 | "instances.title": "Sections", |
| 4196 | 4202 | "instances.item": "section", |
| @@ -6321,6 +6327,9 @@ const tr: Dict = { | ||
| 6321 | 6327 | "messages.deleteForeverOnlyTrash": "Kalıcı silme yalnızca çöp kutusundaki mesajlar için geçerli.", |
| 6322 | 6328 | "messages.readOnlyReceived": "Yalnızca gelen mesajların okundu durumu değiştirilebilir.", |
| 6323 | 6329 | "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.", | |
| 6324 | 6333 | "nav.academicYears": "Eğitim yılları", |
| 6325 | 6334 | "instances.title": "Şubeler", |
| 6326 | 6335 | "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"; | ||
| 37 | 37 | import { personLabel } from "@/lib/person"; |
| 38 | 38 | import { createFlash } from "@/lib/flash"; |
| 39 | 39 | import { Badge } from "@/components/ui/badge"; |
| 40 | +import { ConfirmDialog } from "@/components/ui/confirm-dialog"; | |
| 40 | 41 | |
| 41 | 42 | export default function MessagesPage() { |
| 42 | 43 | const t = useT(); |
| @@ -149,6 +150,16 @@ export default function MessagesPage() { | ||
| 149 | 150 | } |
| 150 | 151 | }; |
| 151 | 152 | |
| 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 | + | |
| 152 | 163 | const handleEmptyTrash = async () => { |
| 153 | 164 | const trashItems = messages(); |
| 154 | 165 | if (trashItems.length === 0) return; |
| @@ -343,10 +354,10 @@ export default function MessagesPage() { | ||
| 343 | 354 | variant="outline" |
| 344 | 355 | size="sm" |
| 345 | 356 | class="h-8 rounded-lg text-[13px] text-destructive hover:bg-destructive/10" |
| 346 | - onClick={handleEmptyTrash} | |
| 357 | + onClick={() => setConfirmEmptyTrash(true)} | |
| 347 | 358 | > |
| 348 | 359 | <IconTrash class="mr-1.5 h-3.5 w-3.5" /> |
| 349 | - Çöp Kutusunu Boşalt | |
| 360 | + {t("messages.emptyTrash")} | |
| 350 | 361 | </Button> |
| 351 | 362 | </Show> |
| 352 | 363 | |
| @@ -472,6 +483,15 @@ export default function MessagesPage() { | ||
| 472 | 483 | initialBody={replyData()?.body} |
| 473 | 484 | /> |
| 474 | 485 | </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 | + /> | |
| 475 | 495 | </RouteGuard> |
| 476 | 496 | ); |
| 477 | 497 | } |