@@ -48,7 +48,7 @@ import {
4848 fetchProjectById ,
4949 updateProject ,
5050} from "@/store/slices/projectSlice" ;
51- import type { EditorSettings , TemplateFile } from "@/types" ;
51+ import type { EditorSettings , SnippetFormState , TemplateFile } from "@/types" ;
5252import Editor from "@monaco-editor/react" ;
5353import { PanelBottomIcon as LuPanelBottom } from "lucide-react" ;
5454import { useEffect , useState } from "react" ;
@@ -67,6 +67,7 @@ import { useNavigate, useSearchParams } from "react-router-dom";
6767import OutputConsole from "../../components/editor/OutputConsole" ;
6868import { createSnippet } from "@/store/slices/snippetSlice" ;
6969import LoadingSnipper from "@/components/LoadingSnipper" ;
70+ import { snippetFormSchema } from "@/schemas" ;
7071
7172const EditorPage = ( ) => {
7273 const navigate = useNavigate ( ) ;
@@ -84,7 +85,7 @@ const EditorPage = () => {
8485 error : projectError ,
8586 } = useSelector ( ( state : RootState ) => state . project ) ;
8687 const [ isSnippetModalOpen , setIsSnippetModalOpen ] = useState ( false ) ;
87- const [ snippetForm , setSnippetForm ] = useState ( {
88+ const [ snippetForm , setSnippetForm ] = useState < SnippetFormState > ( {
8889 title : "" ,
8990 description : "" ,
9091 tags : "" ,
@@ -309,24 +310,18 @@ const EditorPage = () => {
309310 showToast ( "No active file selected" , "error" ) ;
310311 }
311312
312- if ( ! snippetForm . title . trim ( ) ) {
313- showToast ( "Please enter a title for the snippet" , "error" ) ;
314- return ;
315- }
316-
317313 try {
318- const tagsArray = snippetForm . tags
319- . split ( "," )
320- . map ( ( tag ) => tag . trim ( ) . toLowerCase ( ) ) ;
314+ const parsedData = snippetFormSchema . parse ( snippetForm ) ;
315+
321316 const language =
322- snippetForm . language || getFileLanguage ( activeFile ?. name ?? "" ) ;
317+ parsedData . language || getFileLanguage ( activeFile ?. name ?? "" ) ;
323318
324319 const result = await dispatch (
325320 createSnippet ( {
326- title : snippetForm . title ,
327- description : snippetForm . description ,
321+ title : parsedData . title ,
322+ description : parsedData . description ,
328323 code : activeFile ?. content || "" ,
329- tags : tagsArray ,
324+ tags : parsedData . tags ,
330325 language,
331326 author : {
332327 _id : user ?. _id || "" ,
@@ -344,11 +339,17 @@ const EditorPage = () => {
344339 language : "" ,
345340 } ) ;
346341 setIsSnippetModalOpen ( false ) ;
342+ showToast ( "Snippet created successfully" , "success" ) ;
347343 } else {
348344 throw new Error ( result . payload as string ) ;
349345 }
350346 } catch ( error : any ) {
351- console . error ( "Failed to create snippet:" , error . message || error ) ;
347+ if ( error . errors ) {
348+ showToast ( error . errors [ 0 ] . message , "error" ) ;
349+ } else {
350+ showToast ( error . message || "Failed to create snippet" , "error" ) ;
351+ }
352+ console . error ( "Failed to create snippet:" , error ) ;
352353 }
353354 } ;
354355
0 commit comments