From 49f0e57b8737055917dcb7e2765ddd0e5e05c2d3 Mon Sep 17 00:00:00 2001 From: sujalshrestha Date: Wed, 5 Feb 2025 22:08:23 +0545 Subject: [PATCH] bug fix --- src/app/[noteId]/page.tsx | 56 +++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/src/app/[noteId]/page.tsx b/src/app/[noteId]/page.tsx index 088ee39..de85221 100644 --- a/src/app/[noteId]/page.tsx +++ b/src/app/[noteId]/page.tsx @@ -24,26 +24,28 @@ export default function NotePage() { const checkNoteExists = async () => { try { + setIsLoading(true); + setError(''); + const response = await fetch(`/api/notes/${params.noteId}`); const data = await response.json(); - + if (response.status === 401) { - // Note exists but needs password + // Note exists, needs password setIsNewNote(false); setIsPasswordPromptVisible(true); } else if (response.ok && data.exists === false) { - // New note + // URL is available setIsNewNote(true); setText(''); - setIsLoading(false); - } else if (response.ok && data.exists === true) { - // Note exists but wrong password - setIsNewNote(false); - setIsPasswordPromptVisible(true); + } else if (!response.ok) { + throw new Error(data.message || 'Failed to check if note exists'); } } catch (error) { console.error('Failed to check note:', error); - setError('Failed to check if note exists'); + setError(error instanceof Error ? error.message : 'Failed to check if note exists'); + } finally { + setIsLoading(false); } }; @@ -98,11 +100,18 @@ export default function NotePage() { }; const handleSave = async () => { - if (isNewNote && !password) { + // If it's a new note and text is entered but no password set + if (isNewNote && text.trim() && !password) { setIsSetPasswordVisible(true); return; } + // Don't save if there's no text + if (!text.trim()) { + setError('Please enter some text before saving'); + return; + } + setIsSaving(true); setSaveStatus('Saving...'); try { @@ -125,7 +134,8 @@ export default function NotePage() { setIsNewNote(false); setTimeout(() => setSaveStatus(''), 2000); } else { - setSaveStatus('Failed to save'); + const data = await response.json(); + setSaveStatus(data.error || 'Failed to save'); } } catch (error) { console.error('Failed to save note:', error); @@ -137,24 +147,31 @@ export default function NotePage() { if (isLoading && !isPasswordPromptVisible && !isSetPasswordVisible) { return ( -
-
Loading...
+
+
+
Loading...
+ {error && ( +
+ {error} +
+ )} +
); } if (isPasswordPromptVisible) { return ( -
+
-

Enter Password

+

Access Your Note

+

Set Password

@@ -233,7 +250,7 @@ export default function NotePage() {

- {isNewNote ? 'This URL is available!' : params.noteId} + {isNewNote ? 'Create Your Note' : 'Your Note'}

{saveStatus} @@ -264,8 +281,7 @@ export default function NotePage() {

- This URL is available! Start writing your note and click Save to set a - password. + This URL is available! Write your note and click Save to set a password.