1- import { useState } from "react" ;
1+ import { useEffect , useState } from "react" ;
22import CodeMirror from "@uiw/react-codemirror" ;
33import { javascript } from "@codemirror/lang-javascript" ;
44import { createTheme } from "@uiw/codemirror-themes" ;
55import { EditorView } from "@codemirror/view" ;
66
77import Head from "next/head" ;
8- import { ToastContainer } from 'react-toastify' ;
8+ import Toastr from "../components/Toastr" ;
9+ import { ToastContainer } from "react-toastify" ;
910import Button from "../components/Button" ;
1011
1112import { CODE_EDITOR_THEME } from "../constants/theme" ;
@@ -15,14 +16,33 @@ export default function Home() {
1516 const [ outputString , setOutputString ] = useState ( "" ) ;
1617
1718 const handleFormat = ( ) => {
18- setOutputString ( JSON . stringify ( JSON . parse ( inputString ) , null , 2 ) ) ;
19+ try {
20+ setOutputString ( JSON . stringify ( JSON . parse ( inputString ) , null , 2 ) ) ;
21+ } catch ( error ) {
22+ Toastr . error ( "Enter Valid JSON" ) ;
23+ }
1924 } ;
2025
2126 const handleClear = ( ) => {
2227 setInputString ( "" ) ;
2328 setOutputString ( "" ) ;
2429 } ;
2530
31+ const handleDownload = ( ) => {
32+ const element = document . createElement ( "a" ) ;
33+ const file = new Blob ( [ outputString ] , {
34+ type : "text/plain;charset=utf-8" ,
35+ } ) ;
36+ element . href = URL . createObjectURL ( file ) ;
37+ element . download = "json.txt" ;
38+ document . body . appendChild ( element ) ;
39+ element . click ( ) ;
40+ } ;
41+
42+ useEffect ( ( ) => {
43+ inputString === "" && setOutputString ( "" ) ;
44+ } , [ inputString ] ) ;
45+
2646 return (
2747 < div className = "h-screen flex-col justify-between bg-zinc-900 p-5" >
2848 < Head >
@@ -32,11 +52,7 @@ export default function Home() {
3252 </ Head >
3353
3454 < main >
35- < ToastContainer
36- // @Shreya defining this here instead of TOAST_OPTIONS to make implementing dark mode easier
37- // Global state pole vekkam ivide
38- theme = "dark"
39- />
55+ < ToastContainer theme = "dark" />
4056 < p className = "text-2xl text-center" > JSON STYLE</ p >
4157 < div className = "space-y-3 flex-col" >
4258 < div className = "flex space-x-3 h-90" >
@@ -58,9 +74,10 @@ export default function Home() {
5874 className = "w-1/2 mt-7 overflow-auto h-full"
5975 />
6076 </ div >
61- < div className = "flex bg-zinc-800 space-x-3 px-1 py-3 rounded" >
62- < Button className = "ml-1" label = "Format" onClick = { handleFormat } />
63- < Button className = "ml-1" label = "Clear" onClick = { handleClear } />
77+ < div className = "flex bg-zinc-800 space-x-3 px-3 py-3 rounded spacy-x-2" >
78+ < Button className = "" label = "Format" onClick = { handleFormat } />
79+ < Button className = "" label = "Clear" onClick = { handleClear } />
80+ < Button className = "" label = "Download" onClick = { handleDownload } />
6481 </ div >
6582 </ div >
6683 { /* <Footer /> */ }
0 commit comments