diff --git a/src/app/api/pastes/route.ts b/src/app/api/pastes/route.ts index 6ce525b..58973b7 100644 --- a/src/app/api/pastes/route.ts +++ b/src/app/api/pastes/route.ts @@ -34,7 +34,9 @@ export async function POST(request: NextRequest) { const db = client.db('ccbin'); const document = { ...payload, - expiryDate: new Date(payload.expiryDate), // Convert user-provided expiry date to Date object + expiryDate: payload.expiryDate + ? new Date(payload.expiryDate) + : new Date(Date.now() + 1 * (60 * 60 * 1000)), // Convert user-provided expiry date to Date object }; const result = await db.collection('pastes').insertOne(document); return new Response(JSON.stringify(result), { status: 200 }); diff --git a/src/app/components/Editor.tsx b/src/app/components/Editor.tsx index 06d5f91..a35fe3b 100644 --- a/src/app/components/Editor.tsx +++ b/src/app/components/Editor.tsx @@ -56,6 +56,8 @@ import Underline from '@editorjs/underline'; import Warning from '@editorjs/warning'; import { useEffect, useRef, useState } from 'react'; +import { useEditorContext } from '@/app/context/EditorContext'; + interface PropTypes { content?: OutputData | null; onlyReadable?: boolean; @@ -68,6 +70,7 @@ interface EditorJSInstance { save: () => Promise; }; destroy: () => void; + clear: () => void; // Add other methods/properties you plan to use } @@ -75,6 +78,8 @@ export default function Editor({ saveData, content, onlyReadable }: PropTypes) { const ejInstance = useRef(null); const [editorState, saveEditorState] = useState(); + const { resetState } = useEditorContext(); + const initEditor = () => { const editor = new EditorJS({ placeholder: 'Let`s write an awesome story!', @@ -190,6 +195,10 @@ export default function Editor({ saveData, content, onlyReadable }: PropTypes) { }); }; + useEffect(() => { + ejInstance?.current?.clear(); + }, [resetState]); + // This will run only once useEffect(() => { if (ejInstance.current === null) { @@ -215,14 +224,16 @@ export default function Editor({ saveData, content, onlyReadable }: PropTypes) {
-
- -
+ {!onlyReadable && ( +
+ +
+ )} ); } diff --git a/src/app/components/Header.tsx b/src/app/components/Header.tsx index e04fe78..84edd0b 100644 --- a/src/app/components/Header.tsx +++ b/src/app/components/Header.tsx @@ -2,8 +2,20 @@ import { SunIcon } from '@heroicons/react/24/outline'; import Image from 'next/image'; +import { useRouter } from 'next/navigation'; + +import { useEditorContext } from '@/app/context/EditorContext'; export default function Header() { + const { resetEditorState } = useEditorContext(); + + const router = useRouter(); + + const handleOnClick = () => { + router.push('/'); + resetEditorState(); + }; + return (
@@ -18,7 +30,12 @@ export default function Header() {
-
diff --git a/src/app/context/EditorContext.tsx b/src/app/context/EditorContext.tsx new file mode 100644 index 0000000..00593c4 --- /dev/null +++ b/src/app/context/EditorContext.tsx @@ -0,0 +1,39 @@ +'use client'; + +import React, { + createContext, + FC, + ReactNode, + useContext, + useState, +} from 'react'; + +type FooterContextType = { + resetState: boolean; + resetEditorState: () => void; +}; + +const EditorContext = createContext(undefined); + +export const useEditorContext = (): FooterContextType => { + const context = useContext(EditorContext); + if (!context) { + throw new Error('useFooterContext must be used within a FooterProvider'); + } + return context; +}; + +export const EditorProvider: FC<{ children: ReactNode }> = ({ children }) => { + const [resetState, setResetState] = useState(false); + + const resetEditorState = (): void => { + setResetState((prevState) => !prevState); + }; + + // Provide the context value to children components + return ( + + {children} + + ); +}; diff --git a/src/app/layout.tsx b/src/app/layout.tsx index d3b287b..d587c7e 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -5,10 +5,11 @@ import '@/styles/globals.css'; // !STARTERCONF This is for demo purposes, remove @/styles/colors.css import immediately import '@/styles/colors.css'; -// import '@/styles/custom.css' +// import '@/styles/custom.css'; import Footer from '@/app/components/Footer'; import Header from '@/app/components/Header'; import { Toaster } from '@/app/components/ui/sonner'; +import { EditorProvider } from '@/app/context/EditorContext'; import { siteConfig } from '@/constant/config'; // !STARTERCONF Change these default meta @@ -61,12 +62,14 @@ export default function RootLayout({ return ( -
-
-
{children}
- -
-
+ +
+
+
{children}
+ +
+
);