diff --git a/src/api/__tests__/course-notes/course-notes.test.ts b/src/api/__tests__/course-notes/course-notes.test.ts new file mode 100644 index 0000000..2b287b7 --- /dev/null +++ b/src/api/__tests__/course-notes/course-notes.test.ts @@ -0,0 +1,144 @@ +import { afterEach, describe, expect, it, vi } from "vitest"; +import { getCourseNotes } from "../../course-notes"; +import { getCourseNoteById } from "../../course-notes"; +import { postCourseNote } from "../../course-notes"; +import { patchCourseNoteById } from "../../course-notes"; +import { deleteCourseNoteById } from "../../course-notes"; +import { getCourseNoteFiles } from "../../course-notes"; +import { postCourseNoteFile } from "../../course-notes"; +import { deleteCourseNoteFileById } from "../../course-notes"; +import { getCourseNoteFileUrl } from "../../course-notes"; +import { getCourseNoteFileBlob } from "../../course-notes"; +import { lastFetchCall, mockFetch204, mockFetchBlob, mockFetchSuccess } from "../helpers/mock-fetch"; + +describe("course notes API", () => { + afterEach(() => { + vi.restoreAllMocks(); + }); + + it("getCourseNotes calls /course-notes with the required course filter", async () => { + const mockPage = { items: [{ id: "cn1", course: "c1", title: "Note" }], total: 1 }; + mockFetchSuccess(mockPage); + + const result = await getCourseNotes("c1"); + expect(result).toEqual({ ...mockPage, limit: null, offset: 0 }); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/course-notes?course=c1"); + expect(init?.method).toBe("GET"); + }); + + it("getCourseNotes keeps the course filter alongside pagination", async () => { + mockFetchSuccess({ items: [], total: 0 }); + + await getCourseNotes("c1", { limit: 10, offset: 20 }); + + const [url] = lastFetchCall(); + expect(url).toBe("/api/course-notes?course=c1&limit=10&offset=20"); + }); + + it("postCourseNote calls /course-notes with the course in the body", async () => { + const mockNote = { id: "cn1", course: "c1", title: "Note", content: "Content" }; + mockFetchSuccess(mockNote); + + const data = { course: "c1", title: "Note", content: "Content" }; + const result = await postCourseNote(data); + expect(result).toEqual(mockNote); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/course-notes"); + expect(init?.method).toBe("POST"); + expect(init?.body).toBe(JSON.stringify(data)); + }); + + it("getCourseNoteById calls /course-notes/:id", async () => { + const mockNote = { id: "cn1", course: "c1", title: "Note", content: "Content" }; + mockFetchSuccess(mockNote); + + const result = await getCourseNoteById("cn1"); + expect(result).toEqual(mockNote); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/course-notes/cn1"); + expect(init?.method).toBe("GET"); + }); + + it("patchCourseNoteById calls /course-notes/:id with updates", async () => { + const mockNote = { id: "cn1", course: "c1", title: "Updated", content: "Content" }; + mockFetchSuccess(mockNote); + + const updates = { title: "Updated" }; + const result = await patchCourseNoteById("cn1", updates); + expect(result).toEqual(mockNote); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/course-notes/cn1"); + expect(init?.method).toBe("PATCH"); + expect(init?.body).toBe(JSON.stringify(updates)); + }); + + it("deleteCourseNoteById calls /course-notes/:id", async () => { + mockFetch204(); + + await deleteCourseNoteById("cn1"); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/course-notes/cn1"); + expect(init?.method).toBe("DELETE"); + }); + + it("getCourseNoteFiles calls /course-notes/:id/files with pagination", async () => { + const mockPage = { items: [{ id: "f1", name: "test.pdf" }], total: 1 }; + mockFetchSuccess(mockPage); + + const result = await getCourseNoteFiles("cn1", { limit: 10 }); + expect(result).toEqual({ ...mockPage, limit: null, offset: 0 }); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/course-notes/cn1/files?limit=10"); + expect(init?.method).toBe("GET"); + }); + + it("postCourseNoteFile calls /course-notes/:id/files with FormData", async () => { + const mockFile = { id: "f1", name: "test.pdf", content_type: "application/pdf", size: 100 }; + mockFetchSuccess(mockFile); + + const file = new File(["test"], "test.pdf", { type: "application/pdf" }); + const result = await postCourseNoteFile("cn1", file); + expect(result).toEqual(mockFile); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/course-notes/cn1/files"); + expect(init?.method).toBe("POST"); + expect(init?.body).toBeInstanceOf(FormData); + expect((init?.body as FormData).get("file")).toBe(file); + }); + + it("deleteCourseNoteFileById calls /course-notes/:id/files/:fileId", async () => { + mockFetch204(); + + await deleteCourseNoteFileById("cn1", "f1"); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/course-notes/cn1/files/f1"); + expect(init?.method).toBe("DELETE"); + }); + + it("getCourseNoteFileUrl returns correct URL path", () => { + const url = getCourseNoteFileUrl("cn1", "f1"); + expect(url).toBe("/api/course-notes/cn1/files/f1"); + }); + + it("getCourseNoteFileBlob fetches file blob", async () => { + const blob = new Blob(["test"], { type: "application/pdf" }); + mockFetchBlob(blob); + + const result = await getCourseNoteFileBlob("cn1", "f1"); + expect(result).toBeInstanceOf(Blob); + expect(result.type).toBe("application/pdf"); + + const [url, init] = lastFetchCall(); + expect(url).toBe("/api/course-notes/cn1/files/f1"); + expect(init?.credentials).toBe("same-origin"); + }); +}); diff --git a/src/api/client/types.ts b/src/api/client/types.ts index dd550d2..b8292fc 100644 --- a/src/api/client/types.ts +++ b/src/api/client/types.ts @@ -105,6 +105,14 @@ export type Note = { content: string; }; +/** A note attached to a course; files reuse the personal note's `NoteFile` shape. */ +export type CourseNote = { + id: string; + course: string; + title: string; + content: string; +}; + export type NoteFile = { id: string; name: string; diff --git a/src/api/course-notes/deleteCourseNoteById.ts b/src/api/course-notes/deleteCourseNoteById.ts new file mode 100644 index 0000000..397d314 --- /dev/null +++ b/src/api/course-notes/deleteCourseNoteById.ts @@ -0,0 +1,5 @@ +import { client } from "../client"; + +export function deleteCourseNoteById(id: string): Promise { + return client(`/course-notes/${id}`, { method: "DELETE" }); +} diff --git a/src/api/course-notes/deleteCourseNoteFileById.ts b/src/api/course-notes/deleteCourseNoteFileById.ts new file mode 100644 index 0000000..6292b56 --- /dev/null +++ b/src/api/course-notes/deleteCourseNoteFileById.ts @@ -0,0 +1,5 @@ +import { client } from "../client"; + +export function deleteCourseNoteFileById(noteId: string, fileId: string): Promise { + return client(`/course-notes/${noteId}/files/${fileId}`, { method: "DELETE" }); +} diff --git a/src/api/course-notes/getCourseNoteById.ts b/src/api/course-notes/getCourseNoteById.ts new file mode 100644 index 0000000..dbb7267 --- /dev/null +++ b/src/api/course-notes/getCourseNoteById.ts @@ -0,0 +1,6 @@ +import { client } from "../client"; +import type { CourseNote } from "../client"; + +export function getCourseNoteById(id: string, signal?: AbortSignal): Promise { + return client(`/course-notes/${id}`, { signal }); +} diff --git a/src/api/course-notes/getCourseNoteFileBlob.ts b/src/api/course-notes/getCourseNoteFileBlob.ts new file mode 100644 index 0000000..a93eb05 --- /dev/null +++ b/src/api/course-notes/getCourseNoteFileBlob.ts @@ -0,0 +1,5 @@ +import { blobClient } from "../client"; + +export async function getCourseNoteFileBlob(noteId: string, fileId: string, signal?: AbortSignal): Promise { + return blobClient(`/course-notes/${noteId}/files/${fileId}`, signal); +} diff --git a/src/api/course-notes/getCourseNoteFileUrl.ts b/src/api/course-notes/getCourseNoteFileUrl.ts new file mode 100644 index 0000000..739c2e4 --- /dev/null +++ b/src/api/course-notes/getCourseNoteFileUrl.ts @@ -0,0 +1,3 @@ +export function getCourseNoteFileUrl(noteId: string, fileId: string): string { + return `/api/course-notes/${encodeURIComponent(noteId)}/files/${encodeURIComponent(fileId)}`; +} diff --git a/src/api/course-notes/getCourseNoteFiles.ts b/src/api/course-notes/getCourseNoteFiles.ts new file mode 100644 index 0000000..0cbc9cd --- /dev/null +++ b/src/api/course-notes/getCourseNoteFiles.ts @@ -0,0 +1,8 @@ +import { client } from "../client"; +import { normalizePage, pageQuery, type Page, type PageParams } from "../client"; +import type { NoteFile } from "../client"; + +export async function getCourseNoteFiles(noteId: string, params?: PageParams, signal?: AbortSignal): Promise> { + const data = await client(`/course-notes/${noteId}/files${pageQuery(params)}`, { signal }); + return normalizePage(data); +} diff --git a/src/api/course-notes/getCourseNotes.ts b/src/api/course-notes/getCourseNotes.ts new file mode 100644 index 0000000..df45e47 --- /dev/null +++ b/src/api/course-notes/getCourseNotes.ts @@ -0,0 +1,10 @@ +import { client } from "../client"; +import { appendPageParams, normalizePage, type Page, type PageParams } from "../client"; +import type { CourseNote } from "../client"; + +export async function getCourseNotes(courseId: string, params?: PageParams, signal?: AbortSignal): Promise> { + const query = new URLSearchParams({ course: courseId }); + appendPageParams(query, params); + const data = await client(`/course-notes?${query.toString()}`, { signal }); + return normalizePage(data); +} diff --git a/src/api/course-notes/index.ts b/src/api/course-notes/index.ts new file mode 100644 index 0000000..a417d40 --- /dev/null +++ b/src/api/course-notes/index.ts @@ -0,0 +1,10 @@ +export * from "./getCourseNotes"; +export * from "./getCourseNoteById"; +export * from "./postCourseNote"; +export * from "./patchCourseNoteById"; +export * from "./deleteCourseNoteById"; +export * from "./getCourseNoteFiles"; +export * from "./postCourseNoteFile"; +export * from "./deleteCourseNoteFileById"; +export * from "./getCourseNoteFileUrl"; +export * from "./getCourseNoteFileBlob"; diff --git a/src/api/course-notes/patchCourseNoteById.ts b/src/api/course-notes/patchCourseNoteById.ts new file mode 100644 index 0000000..75bc8bc --- /dev/null +++ b/src/api/course-notes/patchCourseNoteById.ts @@ -0,0 +1,12 @@ +import { client } from "../client"; +import type { CourseNote } from "../client"; + +/** Omitted fields keep their value; null never clears here. */ +export type PatchCourseNoteBody = { + title?: string; + content?: string; +}; + +export function patchCourseNoteById(id: string, body: PatchCourseNoteBody): Promise { + return client(`/course-notes/${id}`, { method: "PATCH", body }); +} diff --git a/src/api/course-notes/postCourseNote.ts b/src/api/course-notes/postCourseNote.ts new file mode 100644 index 0000000..f62ba7b --- /dev/null +++ b/src/api/course-notes/postCourseNote.ts @@ -0,0 +1,12 @@ +import { client } from "../client"; +import type { CourseNote } from "../client"; + +export type PostCourseNoteBody = { + course: string; + title: string; + content?: string | null; +}; + +export function postCourseNote(body: PostCourseNoteBody): Promise { + return client("/course-notes", { method: "POST", body }); +} diff --git a/src/api/course-notes/postCourseNoteFile.ts b/src/api/course-notes/postCourseNoteFile.ts new file mode 100644 index 0000000..94e5754 --- /dev/null +++ b/src/api/course-notes/postCourseNoteFile.ts @@ -0,0 +1,8 @@ +import { formClient } from "../client"; +import type { NoteFile } from "../client"; + +export function postCourseNoteFile(noteId: string, file: File, signal?: AbortSignal): Promise { + const body = new FormData(); + body.append("file", file); + return formClient(`/course-notes/${noteId}/files`, body, signal); +} diff --git a/src/components/courses/course-notes-panel.tsx b/src/components/courses/course-notes-panel.tsx new file mode 100644 index 0000000..d30eb70 --- /dev/null +++ b/src/components/courses/course-notes-panel.tsx @@ -0,0 +1,130 @@ +import { Show, Suspense, createEffect, createMemo, createResource, createSignal } from "solid-js"; +import { deleteCourseNoteById, getCourseNotes, patchCourseNoteById, postCourseNote, postCourseNoteFile } from "@/api/course-notes"; +import { formatApiError } from "@/api/client"; +import { NoteForm } from "@/components/notes/note-form"; +import { NoteList } from "@/components/notes/note-list"; +import { Alert } from "@/components/ui/alert"; +import { PageSpinner } from "@/components/ui/page-spinner"; +import { PaginationControls } from "@/components/ui/pagination-controls"; +import { SidePanel } from "@/components/ui/side-panel"; +import { createFlash } from "@/lib/flash"; +import { loadListPage, totalPages as pagesOf } from "@/lib/list-page"; +import { courseNoteFiles } from "@/lib/note-source"; +import { useT } from "@/stores/preferences-context"; + +const NOTE_PAGE_SIZE = 6; + +export function CourseNotesPanel(props: { + courseId: string; + canManage: boolean; + active: boolean; + createOpen: boolean; + onCreateOpenChange: (open: boolean) => void; + onCountChange: (count: number) => void; +}) { + const t = useT(); + const [error, setError] = createSignal(""); + const [flash, setFlash] = createFlash(); + const [page, setPage] = createSignal(0); + + const [list, { refetch }] = createResource( + () => (props.active ? ({ courseId: props.courseId, page: page() }) : null), + async (key) => + loadListPage({ + page: key.page, + pageSize: NOTE_PAGE_SIZE, + clientMode: false, + fetch: (params) => getCourseNotes(key.courseId, params), + }), + ); + + const total = () => list()?.total ?? 0; + const pageItems = () => list()?.items ?? []; + const totalPages = createMemo(() => pagesOf(total(), NOTE_PAGE_SIZE)); + const safePage = createMemo(() => Math.min(page(), totalPages() - 1)); + + createEffect(() => props.onCountChange(total())); + + const wrap = async (fn: () => Promise, okMessage: string) => { + setError(""); + try { + await fn(); + await refetch(); + setFlash(okMessage); + } catch (err) { + setError(formatApiError(err)); + } + }; + + return ( +
+ + {flash()} + + + {error()} + + + + + props.onCreateOpenChange(false)} + onSubmit={async (values) => { + setError(""); + const note = await postCourseNote({ + course: props.courseId, + title: values.title, + content: values.content || undefined, + }); + let failedUploads = 0; + for (const file of values.files) { + try { + await postCourseNoteFile(note.id, file); + } catch { + failedUploads += 1; + } + } + await refetch(); + props.onCreateOpenChange(false); + if (failedUploads > 0) { + setError(t("notes.fileUploadPartial", { count: failedUploads })); + return; + } + setFlash(t("common.created")); + }} + /> + + + + }> + + {formatApiError(list.error)} + + + + wrap(async () => { + await patchCourseNoteById(id, values); + }, t("common.saved")) + } + onDelete={(id) => + wrap(async () => { + await deleteCourseNoteById(id); + }, t("common.deleted")) + } + /> + NOTE_PAGE_SIZE}> + + + + +
+ ); +} diff --git a/src/components/notes/note-card.tsx b/src/components/notes/note-card.tsx index 62c03cf..b9ae2e9 100644 --- a/src/components/notes/note-card.tsx +++ b/src/components/notes/note-card.tsx @@ -10,6 +10,8 @@ import { useT } from "@/stores/preferences-context"; export function NoteCard(props: { note: Note; + /** Hides the edit/delete menu when false. Defaults to true. */ + canManage?: boolean; onOpen: (note: Note) => void; onUpdate: (id: string, values: { title: string; content: string }) => Promise; onDelete: (id: string) => Promise; @@ -18,6 +20,7 @@ export function NoteCard(props: { const [editing, setEditing] = createSignal(false); const [error, setError] = createSignal(""); const [deleteOpen, setDeleteOpen] = createSignal(false); + const canManage = () => props.canManage !== false; return ( <> @@ -35,24 +38,26 @@ export function NoteCard(props: { >

{props.note.title}

-
event.stopPropagation()}> - , - onSelect: () => setEditing(true), - }, - { - label: t("common.delete"), - icon: , - destructive: true, - onSelect: () => setDeleteOpen(true), - }, - ]} - /> -
+ +
event.stopPropagation()}> + , + onSelect: () => setEditing(true), + }, + { + label: t("common.delete"), + icon: , + destructive: true, + onSelect: () => setDeleteOpen(true), + }, + ]} + /> +
+
void }) { +export function NoteFilePreview(props: { noteId: string; source: NoteFileSource; file: NoteFile | null; onClose: () => void }) { const t = useT(); let currentUrl = ""; const [previewUrl, setPreviewUrl] = createSignal(""); const [previewError, setPreviewError] = createSignal(""); - const downloadUrl = () => (props.file ? getNoteFileUrl(props.noteId, props.file.id) : ""); + const downloadUrl = () => (props.file ? props.source.fileUrl(props.noteId, props.file.id) : ""); const type = () => props.file?.content_type ?? ""; createEffect(() => { @@ -33,7 +32,7 @@ export function NoteFilePreview(props: { noteId: string; file: NoteFile | null; if (!file || !canPreview(file)) return; const controller = new AbortController(); - void getNoteFileBlob(props.noteId, file.id, controller.signal) + void props.source.fileBlob(props.noteId, file.id, controller.signal) .then((blob) => { if (controller.signal.aborted) return; currentUrl = URL.createObjectURL(blob); diff --git a/src/components/notes/note-files-panel.tsx b/src/components/notes/note-files-panel.tsx index 7a6d111..f1e01bd 100644 --- a/src/components/notes/note-files-panel.tsx +++ b/src/components/notes/note-files-panel.tsx @@ -1,13 +1,9 @@ import { For, Show, Suspense, createEffect, createMemo, createResource, createSignal, lazy } from "solid-js"; -import { deleteNoteFileById } from "@/api/notes"; -import { getNoteFileBlob } from "@/api/notes"; -import { getNoteFileUrl } from "@/api/notes"; -import { getNoteFiles } from "@/api/notes"; import { getSettings } from "@/api/settings"; -import { postNoteFile } from "@/api/notes"; import { formatApiError } from "@/api/client"; import type { NoteFile } from "@/api/client"; import type { DrawScene } from "@/lib/draw-stroke"; +import type { NoteFileSource } from "@/lib/note-source"; import { NoteFilePreview } from "@/components/notes/note-file-preview"; import { Alert } from "@/components/ui/alert"; import { Button } from "@/components/ui/button"; @@ -26,14 +22,20 @@ import { useT } from "@/stores/preferences-context"; // Lazy so the drawing pad rides its own chunk, off the notes page's initial load. const DrawCanvas = lazy(() => import("@/components/ui/draw-canvas").then((m) => ({ default: m.DrawCanvas }))); -const MAX_NOTE_FILES = 10; const FILE_PAGE_SIZE = 4; /** Drawings are saved with this suffix so the grid can offer "Edit" without fetching every blob. */ const DRAWING_SUFFIX = ".hzdraw.png"; const isDrawing = (file: NoteFile) => file.name.toLowerCase().endsWith(DRAWING_SUFFIX); -export function NoteFilesPanel(props: { noteId: string; active: boolean }) { +export function NoteFilesPanel(props: { + noteId: string; + active: boolean; + /** Which note family the files hang off — personal notes or a course's. */ + source: NoteFileSource; + /** Read-only when false: no upload, drawing, or delete. Defaults to true. */ + canManage?: boolean; +}) { let input: HTMLInputElement | undefined; const t = useT(); const [error, setError] = createSignal(""); @@ -45,11 +47,12 @@ export function NoteFilesPanel(props: { noteId: string; active: boolean }) { const [editTarget, setEditTarget] = createSignal(null); const [previewFile, setPreviewFile] = createSignal(null); const [deleteTarget, setDeleteTarget] = createSignal(null); + const canManage = () => props.canManage !== false; const [files, { refetch }] = createResource( () => (props.active ? props.noteId : null), async (noteId) => { if (!noteId) return [] as NoteFile[]; - return (await getNoteFiles(noteId, { limit: MAX_NOTE_FILES })).items; + return (await props.source.listFiles(noteId, { limit: props.source.maxFiles })).items; }, ); const [settings] = createResource(async () => { @@ -60,7 +63,7 @@ export function NoteFilesPanel(props: { noteId: string; active: boolean }) { } }); const maxFileBytes = () => maxUploadBytes(settings()); - const atLimit = () => (files() ?? []).length >= MAX_NOTE_FILES; + const atLimit = () => (files() ?? []).length >= props.source.maxFiles; const totalPages = createMemo(() => Math.max(1, Math.ceil((files() ?? []).length / FILE_PAGE_SIZE))); const pageFiles = createMemo(() => { const start = filePage() * FILE_PAGE_SIZE; @@ -80,7 +83,7 @@ export function NoteFilesPanel(props: { noteId: string; active: boolean }) { } setPending(true); try { - await postNoteFile(props.noteId, file); + await props.source.uploadFile(props.noteId, file); await refetch(); if (input) input.value = ""; setFlash(t("common.created")); @@ -95,7 +98,7 @@ export function NoteFilesPanel(props: { noteId: string; active: boolean }) { const download = (file: NoteFile) => { const link = document.createElement("a"); - link.href = getNoteFileUrl(props.noteId, file.id); + link.href = props.source.fileUrl(props.noteId, file.id); link.download = file.name; link.click(); }; @@ -109,7 +112,7 @@ export function NoteFilesPanel(props: { noteId: string; active: boolean }) { const openEditDrawing = async (file: NoteFile) => { setError(""); try { - const blob = await getNoteFileBlob(props.noteId, file.id); + const blob = await props.source.fileBlob(props.noteId, file.id); const { pngBytesToScene } = await import("@/lib/drawing-file"); setEditScene(pngBytesToScene(new Uint8Array(await blob.arrayBuffer()))); setEditTarget(file); @@ -126,7 +129,7 @@ export function NoteFilesPanel(props: { noteId: string; active: boolean }) { if (!(await upload(file))) return; if (target) { try { - await deleteNoteFileById(props.noteId, target.id); + await props.source.deleteFile(props.noteId, target.id); await refetch(); } catch { // New drawing is saved; deleting the old copy failed — a stale duplicate @@ -146,7 +149,9 @@ export function NoteFilesPanel(props: { noteId: string; active: boolean }) {

{t("notes.files")}

-

{t("notes.filesHelp", { size: formatBytes(maxFileBytes()) })}

+ +

{t("notes.filesHelp", { size: formatBytes(maxFileBytes()) })}

+
{ @@ -157,20 +162,22 @@ export function NoteFilesPanel(props: { noteId: string; active: boolean }) { disabled={pending() || atLimit()} onChange={(event) => void upload(event.currentTarget.files?.[0])} /> -
- - -
+ +
+ + +
+
{error() &&

{error()}

} - +

{t("notes.fileLimit")}

@@ -202,7 +209,7 @@ export function NoteFilesPanel(props: { noteId: string; active: boolean }) { , onSelect: () => download(file), }, - { - label: t("common.delete"), - icon: , - destructive: true, - onSelect: () => setDeleteTarget(file), - }, + ...(canManage() + ? [ + { + label: t("common.delete"), + icon: , + destructive: true, + onSelect: () => setDeleteTarget(file), + }, + ] + : []), ]} />
@@ -243,7 +254,7 @@ export function NoteFilesPanel(props: { noteId: string; active: boolean }) { FILE_PAGE_SIZE}> - setPreviewFile(null)} /> + setPreviewFile(null)} /> @@ -281,7 +292,7 @@ export function NoteFilesPanel(props: { noteId: string; active: boolean }) { onConfirm={async () => { const target = deleteTarget(); if (!target) return; - await deleteNoteFileById(props.noteId, target.id); + await props.source.deleteFile(props.noteId, target.id); if (previewFile()?.id === target.id) setPreviewFile(null); setDeleteTarget(null); await refetch(); diff --git a/src/components/notes/note-list.tsx b/src/components/notes/note-list.tsx index 8d8b086..5d4a254 100644 --- a/src/components/notes/note-list.tsx +++ b/src/components/notes/note-list.tsx @@ -1,11 +1,16 @@ import { For, Show, createSignal } from "solid-js"; import type { Note } from "@/api/client"; +import type { NoteFileSource } from "@/lib/note-source"; import { NoteCard } from "@/components/notes/note-card"; import { NoteReaderPanel } from "@/components/notes/note-reader-panel"; import { EmptyState } from "@/components/ui/empty-state"; export function NoteList(props: { notes: Note[]; + /** Which note family these belong to — decides the file routes behind them. */ + source: NoteFileSource; + /** Hides edit/delete and the file controls when false. Defaults to true. */ + canManage?: boolean; emptyTitle: string; emptyDescription?: string; onUpdate: (id: string, values: { title: string; content: string }) => Promise; @@ -30,6 +35,7 @@ export function NoteList(props: {
  • { if (!open) setReadingNote(null); diff --git a/src/components/notes/note-reader-panel.tsx b/src/components/notes/note-reader-panel.tsx index 23f505b..e43109f 100644 --- a/src/components/notes/note-reader-panel.tsx +++ b/src/components/notes/note-reader-panel.tsx @@ -1,11 +1,15 @@ import { Show } from "solid-js"; import type { Note } from "@/api/client"; +import type { NoteFileSource } from "@/lib/note-source"; import { NoteFilesPanel } from "@/components/notes/note-files-panel"; import { SidePanel } from "@/components/ui/side-panel"; import { useT } from "@/stores/preferences-context"; export function NoteReaderPanel(props: { note: Note | null; + source: NoteFileSource; + /** Read-only file section when false. Defaults to true. */ + canManage?: boolean; open: boolean; onOpenChange: (open: boolean) => void; }) { @@ -32,7 +36,7 @@ export function NoteReaderPanel(props: { - + )} diff --git a/src/i18n/messages.ts b/src/i18n/messages.ts index f48ce08..625fb60 100644 --- a/src/i18n/messages.ts +++ b/src/i18n/messages.ts @@ -531,6 +531,12 @@ export type MessageKey = | "dashboard.quickReview.placeholder" | "dashboard.quickReview.practice" | "dashboard.quickReview.startQuiz" + | "courseNotes.title" + | "courseNotes.item" + | "courseNotes.new" + | "courseNotes.add" + | "courseNotes.empty" + | "courseNotes.emptyManageHint" | "notes.title" | "notes.subtitle" | "notes.new" @@ -2056,6 +2062,12 @@ const en: Dict = { "dashboard.quickReview.placeholder": "Choose a topic to review…", "dashboard.quickReview.practice": "Practice", "dashboard.quickReview.startQuiz": "Start quiz", + "courseNotes.title": "Lesson notes", + "courseNotes.item": "lesson note", + "courseNotes.new": "New lesson note", + "courseNotes.add": "Add note", + "courseNotes.empty": "No notes on this course yet.", + "courseNotes.emptyManageHint": "Write a note the whole class can read, and attach files to it.", "notes.title": "Notebook", "notes.subtitle": "Your private notebook for class ideas and reminders.", "notes.new": "New note", @@ -3587,6 +3599,12 @@ const tr: Dict = { "dashboard.quickReview.placeholder": "Tekrar için konu seç…", "dashboard.quickReview.practice": "Alıştır", "dashboard.quickReview.startQuiz": "Teste başla", + "courseNotes.title": "Ders notları", + "courseNotes.item": "ders notu", + "courseNotes.new": "Yeni ders notu", + "courseNotes.add": "Not ekle", + "courseNotes.empty": "Bu derse ait henüz not yok.", + "courseNotes.emptyManageHint": "Tüm sınıfın okuyabileceği bir not yazın, dosya da ekleyebilirsiniz.", "notes.title": "Defter", "notes.subtitle": "Ders fikirleriniz ve hatırlatmalarınız için özel defteriniz.", "notes.new": "Yeni not", diff --git a/src/lib/note-source.ts b/src/lib/note-source.ts new file mode 100644 index 0000000..b964b6f --- /dev/null +++ b/src/lib/note-source.ts @@ -0,0 +1,37 @@ +import type { NoteFile, Page, PageParams } from "@/api/client"; +import { deleteCourseNoteFileById, getCourseNoteFileBlob, getCourseNoteFileUrl, getCourseNoteFiles, postCourseNoteFile } from "@/api/course-notes"; +import { deleteNoteFileById, getNoteFileBlob, getNoteFileUrl, getNoteFiles, postNoteFile } from "@/api/notes"; + +/** + * The file routes behind a note. `/notes` (personal) and `/course-notes` (a + * course's notes) expose the same five operations over the same `NoteFile` + * shape, so the note components take one of these instead of importing a + * single domain's API directly. + */ +export type NoteFileSource = { + /** Backend cap per note: MAX_NOTE_FILES / MAX_COURSE_NOTE_FILES, both 10. */ + maxFiles: number; + listFiles: (noteId: string, params?: PageParams, signal?: AbortSignal) => Promise>; + uploadFile: (noteId: string, file: File, signal?: AbortSignal) => Promise; + deleteFile: (noteId: string, fileId: string) => Promise; + fileUrl: (noteId: string, fileId: string) => string; + fileBlob: (noteId: string, fileId: string, signal?: AbortSignal) => Promise; +}; + +export const personalNoteFiles: NoteFileSource = { + maxFiles: 10, + listFiles: getNoteFiles, + uploadFile: postNoteFile, + deleteFile: deleteNoteFileById, + fileUrl: getNoteFileUrl, + fileBlob: getNoteFileBlob, +}; + +export const courseNoteFiles: NoteFileSource = { + maxFiles: 10, + listFiles: getCourseNoteFiles, + uploadFile: postCourseNoteFile, + deleteFile: deleteCourseNoteFileById, + fileUrl: getCourseNoteFileUrl, + fileBlob: getCourseNoteFileBlob, +}; diff --git a/src/pages/course-detail-page.tsx b/src/pages/course-detail-page.tsx index 4ce5237..4bb01fb 100644 --- a/src/pages/course-detail-page.tsx +++ b/src/pages/course-detail-page.tsx @@ -19,6 +19,7 @@ import { patchExamById } from "@/api/exams"; import { ExamLink } from "@/components/exams/exam-link"; import { ExamForm, type ExamFormValues } from "@/components/exams/exam-form"; import { ExamQuestionsPanel } from "@/components/exams/exam-questions-panel"; +import { CourseNotesPanel } from "@/components/courses/course-notes-panel"; import { CourseSubjectsPanel } from "@/components/courses/course-subjects-panel"; import { CourseTeachersPanel } from "@/components/courses/course-teachers-panel"; import { CourseHomeworkPanel } from "@/components/homework/course-homework-panel"; @@ -31,7 +32,7 @@ import { Button } from "@/components/ui/button"; import { ConfirmDialog } from "@/components/ui/confirm-dialog"; import { DataTable, DataTableSkeleton } from "@/components/ui/data-table"; import { EmptyState } from "@/components/ui/empty-state"; -import { IconBook, IconCalendarDays, IconEdit, IconExam, IconHomework, IconPlus, IconSchool, IconTrash, IconUsers } from "@/components/ui/icons"; +import { IconBook, IconCalendarDays, IconEdit, IconExam, IconHomework, IconNote, IconPlus, IconSchool, IconTrash, IconUsers } from "@/components/ui/icons"; import { createFlash } from "@/lib/flash"; import { cn } from "@/lib/cn"; import { Input } from "@/components/ui/input"; @@ -118,10 +119,12 @@ function CourseDetailContent() { const [showSessionForm, setShowSessionForm] = createSignal(false); const [showHomeworkForm, setShowHomeworkForm] = createSignal(false); const [showTeacherForm, setShowTeacherForm] = createSignal(false); + const [showNoteForm, setShowNoteForm] = createSignal(false); const [showEnrollPanel, setShowEnrollPanel] = createSignal(false); const [subjectCount, setSubjectCount] = createSignal(0); const [sessionCount, setSessionCount] = createSignal(0); const [homeworkCount, setHomeworkCount] = createSignal(0); + const [noteCount, setNoteCount] = createSignal(0); const [enrollUserId, setEnrollUserId] = createSignal(""); const [error, setError] = createSignal(""); const [pending, setPending] = createSignal(false); @@ -599,11 +602,12 @@ function CourseDetailContent() { )} - + {t("subjects.title")}{subjectCount()} {t("courses.exams")}{examCount()} {t("homework.title")}{homeworkCount()} {t("sessions.title")}{sessionCount()} + {t("courseNotes.title")}{noteCount()} {t("courses.teachers")}{assignedTeacherCount()} {t("courses.roster")}{rosterCount()} @@ -647,6 +651,14 @@ function CourseDetailContent() { + +
    + {countDescription(noteCount(), t("courseNotes.item"))} + +
    + +
    +
    {countDescription(assignedTeacherCount(), t("courses.teachers"))} diff --git a/src/pages/notes-page.tsx b/src/pages/notes-page.tsx index c01b16a..20d18a9 100644 --- a/src/pages/notes-page.tsx +++ b/src/pages/notes-page.tsx @@ -19,6 +19,7 @@ import { PaginationControls } from "@/components/ui/pagination-controls"; import { SidePanel } from "@/components/ui/side-panel"; import { createFlash } from "@/lib/flash"; import { loadListPage, totalPages as pagesOf } from "@/lib/list-page"; +import { personalNoteFiles } from "@/lib/note-source"; import { useT } from "@/stores/preferences-context"; const NOTE_PAGE_SIZE = 10; @@ -154,6 +155,7 @@ function NotesContent() {