From ef935ac37bb8789c362f4462df3f1e663189d14b Mon Sep 17 00:00:00 2001 From: Timothy Cardoza Date: Fri, 27 Feb 2026 13:39:32 -0600 Subject: [PATCH] create note api added --- src/components/EmbedAuthWrapper.tsx | 38 ++++++++++++++- test-embed.html | 74 +++++++++++++++++++++++++++-- 2 files changed, 107 insertions(+), 5 deletions(-) diff --git a/src/components/EmbedAuthWrapper.tsx b/src/components/EmbedAuthWrapper.tsx index cf21a75..93390ef 100644 --- a/src/components/EmbedAuthWrapper.tsx +++ b/src/components/EmbedAuthWrapper.tsx @@ -5,18 +5,52 @@ interface EmbedAuthWrapperProps { children: JSX.Element; } +import { NOTES_COLLECTION } from "@/lib/constants"; + export const EmbedAuthWrapper: Component = (props) => { const [isHydrated, setIsHydrated] = createSignal(pb.authStore.isValid); - const handleMessage = (event: MessageEvent) => { + const handleMessage = async (event: MessageEvent) => { const { data } = event; console.log('[DEBUG] EmbedAuthWrapper received message:', data?.type); + if (data?.type === "TASGRID_AUTH" && data.token) { console.log("[DEBUG] Received TasGrid auth payload, hydrating..."); - // Save triggers pb.authStore.onChange, which App.tsx is already listening to and calling initStore() pb.authStore.save(data.token, data.user || null); setIsHydrated(true); } + + if (data?.type === "TASGRID_CREATE_NOTE") { + const currentUserId = pb.authStore.model?.id; + if (!currentUserId) { + console.warn("[DEBUG] Rejecting note creation: Not authenticated"); + return; + } + + try { + const noteData = data.note || {}; + const result = await pb.collection(NOTES_COLLECTION).create({ + title: noteData.title || "New Note", + content: noteData.content || "", + tags: noteData.tags || [], + isPrivate: noteData.isPrivate === true, + user: currentUserId, + }); + + console.log("[DEBUG] Note created via API:", result.id); + + // Respond back to parent + if (window.parent !== window) { + window.parent.postMessage({ + type: "TASGRID_NOTE_CREATED", + noteId: result.id, + title: result.title + }, "*"); + } + } catch (e) { + console.error("Failed to create note via API", e); + } + } }; onMount(() => { diff --git a/test-embed.html b/test-embed.html index 4e3ac05..192f786 100644 --- a/test-embed.html +++ b/test-embed.html @@ -124,6 +124,19 @@
+
+

Note Creation API

+
+ +
+
+ +
+

Or send a token manually:

@@ -152,9 +165,28 @@ iframe.contentWindow.postMessage({ user: { /* optional PB user record */ } }, "*"); -

3. Responsiveness:

-

Both routes are designed to be fluid. Ensure your iframe container has defined dimensions; the - content will expand to fill it.

+

4. Note Creation API (TASGRID_CREATE_NOTE):

+

Trigger note creation from the parent app. TasGrid will respond with + TASGRID_NOTE_CREATED.

+
+// Request
+iframe.contentWindow.postMessage({
+  type: "TASGRID_CREATE_NOTE",
+  note: {
+    title: "Project Alpha",
+    content: "Initial requirements...",
+    tags: ["Project", "2024"],
+    isPrivate: false
+  }
+}, "*");
+
+// Response Listener
+window.addEventListener("message", (event) => {
+  if (event.data.type === "TASGRID_NOTE_CREATED") {
+    console.log("New Note ID:", event.data.noteId);
+  }
+});
@@ -230,6 +262,42 @@ iframe.contentWindow.postMessage({ quickAddIframe.postMessage(payload, "*"); } + async function createNote() { + const apiStatus = document.getElementById('api-status'); + const apiResult = document.getElementById('api-result'); + + apiStatus.textContent = "Sending creation request..."; + apiResult.style.display = 'none'; + + const payload = { + type: "TASGRID_CREATE_NOTE", + note: { + title: "Test Note from Parent " + new Date().toLocaleTimeString(), + content: "This note was created automatically via the postMessage API.", + tags: ["API-TEST", "SISTER-APP"], + isPrivate: false + } + }; + + // We can send to either iframe since EmbedAuthWrapper handles it globally for all embedded views + document.getElementById('notes-iframe').contentWindow.postMessage(payload, "*"); + } + + // Listen for responses + window.addEventListener("message", (event) => { + const { data } = event; + console.log("Parent received message:", data); + + if (data.type === "TASGRID_NOTE_CREATED") { + const apiStatus = document.getElementById('api-status'); + const apiResult = document.getElementById('api-result'); + + apiStatus.innerHTML = `Note Created! ID: ${data.noteId}`; + apiResult.textContent = JSON.stringify(data, null, 2); + apiResult.style.display = 'block'; + } + }); + // Handle case where user refreshed but is still logged in to the parent window.addEventListener('load', () => { if (pb.authStore.isValid && pb.authStore.model) {