Skip to content

Commit 4514710

Browse files
Merge pull request #29 from sreeram-venkitesh/27-download
Download json
2 parents d2cf6ae + b31e67b commit 4514710

File tree

2 files changed

+48
-13
lines changed

2 files changed

+48
-13
lines changed

components/Button.jsx

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,31 @@ const Button = ({
88
onClick = () => {},
99
disabled = false,
1010
label = "",
11+
href = "",
12+
to = "",
13+
type = "button",
1114
...otherProps
1215
}) => {
16+
let Parent, elementSpecificProps;
17+
if (to) {
18+
Parent = Link;
19+
elementSpecificProps = { to };
20+
} else if (href) {
21+
Parent = motion.a;
22+
elementSpecificProps = { href };
23+
} else {
24+
Parent = motion.button;
25+
elementSpecificProps = {
26+
type,
27+
};
28+
}
29+
1330
const handleClick = () => {
1431
if (!disabled) onClick();
1532
};
1633

1734
return (
18-
<motion.button
35+
<Parent
1936
onClick={handleClick}
2037
disabled={disabled}
2138
className={classnames("button", [className], {
@@ -28,9 +45,10 @@ const Button = ({
2845
disabled: disabled,
2946
})}
3047
{...otherProps}
48+
{...elementSpecificProps}
3149
>
3250
{label && <span>{label}</span>}
33-
</motion.button>
51+
</Parent>
3452
);
3553
};
3654

pages/index.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { useState } from "react";
1+
import { useEffect, useState } from "react";
22
import CodeMirror from "@uiw/react-codemirror";
33
import { javascript } from "@codemirror/lang-javascript";
44
import { createTheme } from "@uiw/codemirror-themes";
55
import { EditorView } from "@codemirror/view";
66

77
import Head from "next/head";
8-
import { ToastContainer } from 'react-toastify';
8+
import Toastr from "../components/Toastr";
9+
import { ToastContainer } from "react-toastify";
910
import Button from "../components/Button";
1011

1112
import { 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, 4));
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 label="Format" onClick={handleFormat} />
79+
<Button label="Clear" onClick={handleClear} />
80+
<Button label="Download" onClick={handleDownload} />
6481
</div>
6582
</div>
6683
{/* <Footer /> */}

0 commit comments

Comments
 (0)