create note api added
This commit is contained in:
@@ -5,18 +5,52 @@ interface EmbedAuthWrapperProps {
|
|||||||
children: JSX.Element;
|
children: JSX.Element;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
import { NOTES_COLLECTION } from "@/lib/constants";
|
||||||
|
|
||||||
export const EmbedAuthWrapper: Component<EmbedAuthWrapperProps> = (props) => {
|
export const EmbedAuthWrapper: Component<EmbedAuthWrapperProps> = (props) => {
|
||||||
const [isHydrated, setIsHydrated] = createSignal(pb.authStore.isValid);
|
const [isHydrated, setIsHydrated] = createSignal(pb.authStore.isValid);
|
||||||
|
|
||||||
const handleMessage = (event: MessageEvent) => {
|
const handleMessage = async (event: MessageEvent) => {
|
||||||
const { data } = event;
|
const { data } = event;
|
||||||
console.log('[DEBUG] EmbedAuthWrapper received message:', data?.type);
|
console.log('[DEBUG] EmbedAuthWrapper received message:', data?.type);
|
||||||
|
|
||||||
if (data?.type === "TASGRID_AUTH" && data.token) {
|
if (data?.type === "TASGRID_AUTH" && data.token) {
|
||||||
console.log("[DEBUG] Received TasGrid auth payload, hydrating...");
|
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);
|
pb.authStore.save(data.token, data.user || null);
|
||||||
setIsHydrated(true);
|
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(() => {
|
onMount(() => {
|
||||||
|
|||||||
+71
-3
@@ -124,6 +124,19 @@
|
|||||||
<div id="status" class="status"></div>
|
<div id="status" class="status"></div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div id="api-section" style="margin-top: 30px; border-top: 1px solid #eee; padding-top: 20px;">
|
||||||
|
<h3>Note Creation API</h3>
|
||||||
|
<div style="display: flex; gap: 10px; align-items: center;">
|
||||||
|
<button class="send-manual" onclick="createNote()"
|
||||||
|
style="margin-top: 0; background: #28a745; color: white; border: none; border-radius: 4px; padding: 10px 15px; font-weight: bold; cursor: pointer;">Create
|
||||||
|
Sample Note</button>
|
||||||
|
<div id="api-status" style="font-size: 12px; color: #666;"></div>
|
||||||
|
</div>
|
||||||
|
<div id="api-result"
|
||||||
|
style="margin-top: 10px; font-family: monospace; font-size: 11px; background: #eee; padding: 10px; border-radius: 4px; display: none; word-break: break-all;">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div id="manual-section" style="margin-top: 30px; border-top: 1px solid #eee; padding-top: 20px;">
|
<div id="manual-section" style="margin-top: 30px; border-top: 1px solid #eee; padding-top: 20px;">
|
||||||
<p style="font-size: 13px; color: #666;">Or send a token manually:</p>
|
<p style="font-size: 13px; color: #666;">Or send a token manually:</p>
|
||||||
<input type="text" id="token" class="manual-token" placeholder="Paste PB Token here...">
|
<input type="text" id="token" class="manual-token" placeholder="Paste PB Token here...">
|
||||||
@@ -152,9 +165,28 @@ iframe.contentWindow.postMessage({
|
|||||||
user: { /* optional PB user record */ }
|
user: { /* optional PB user record */ }
|
||||||
}, "*");</pre>
|
}, "*");</pre>
|
||||||
|
|
||||||
<p><strong>3. Responsiveness:</strong></p>
|
<p><strong>4. Note Creation API (TASGRID_CREATE_NOTE):</strong></p>
|
||||||
<p>Both routes are designed to be fluid. Ensure your iframe container has defined dimensions; the
|
<p>Trigger note creation from the parent app. TasGrid will respond with
|
||||||
content will expand to fill it.</p>
|
<code>TASGRID_NOTE_CREATED</code>.</p>
|
||||||
|
<pre
|
||||||
|
style="background: #f8f9fa; padding: 15px; border-radius: 4px; border: 1px solid #e9ecef; overflow-x: auto;">
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
});</pre>
|
||||||
</div>
|
</div>
|
||||||
</details>
|
</details>
|
||||||
</div>
|
</div>
|
||||||
@@ -230,6 +262,42 @@ iframe.contentWindow.postMessage({
|
|||||||
quickAddIframe.postMessage(payload, "*");
|
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 = `<span style="color: green; font-weight: bold;">Note Created! ID: ${data.noteId}</span>`;
|
||||||
|
apiResult.textContent = JSON.stringify(data, null, 2);
|
||||||
|
apiResult.style.display = 'block';
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
// Handle case where user refreshed but is still logged in to the parent
|
// Handle case where user refreshed but is still logged in to the parent
|
||||||
window.addEventListener('load', () => {
|
window.addEventListener('load', () => {
|
||||||
if (pb.authStore.isValid && pb.authStore.model) {
|
if (pb.authStore.isValid && pb.authStore.model) {
|
||||||
|
|||||||
Reference in New Issue
Block a user