Skip to content

Commit

Permalink
add tester to docs site
Browse files Browse the repository at this point in the history
  • Loading branch information
elisherer committed Apr 30, 2024
1 parent e91de28 commit d53b206
Show file tree
Hide file tree
Showing 14 changed files with 1,055 additions and 17 deletions.
31 changes: 17 additions & 14 deletions docs/docusaurus.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,24 @@ const config: Config = {

themeConfig:
{
docs: {
sidebar: {
hideable: true
}
},
navbar: {
title: "JSON Transform",
logo: {
alt: "logo",
src: LOGO,
},
items: [
// {
// type: 'docSidebar',
// sidebarId: 'referenceSidebar',
// position: 'left',
// label: 'Docs',
// },
// {
// type: "doc",
// docId: "reference/index",
// position: "left",
// label: "Reference",
// },
{
to: "playground",
position: "right",
label: "Playground",
className: "button button--primary navbar-button"
},
{
href: "https://github.com/nlighten-oss/json-transform",
label: "GitHub",
Expand All @@ -88,8 +87,12 @@ const config: Config = {
title: 'Docs',
items: [
{
label: 'Tutorial',
to: '/docs/intro',
label: 'Documentation',
to: '/json-transform',
},
{
label: 'Playground',
to: '/json-transform/playground',
},
],
},
Expand Down
65 changes: 65 additions & 0 deletions docs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"private": true,
"scripts": {
"docusaurus": "docusaurus",
"start": "docusaurus start",
"start": "docusaurus start --port 10001",
"build": "docusaurus build",
"swizzle": "docusaurus swizzle",
"deploy": "docusaurus deploy",
Expand All @@ -15,18 +15,22 @@
"typecheck": "tsc"
},
"dependencies": {
"docusaurus-lunr-search": "3.3.2",
"@docusaurus/core": "3.2.1",
"@docusaurus/preset-classic": "3.2.1",
"@mdx-js/react": "3.0.1",
"@monaco-editor/react": "^4.6.0",
"@nlighten/json-transform-core": "^1.0.4",
"clsx": "2.1.0",
"docusaurus-lunr-search": "3.3.2",
"monaco-editor": "^0.48.0",
"react": "18.2.0",
"react-dom": "18.2.0"
},
"devDependencies": {
"@docusaurus/module-type-aliases": "3.2.1",
"@docusaurus/tsconfig": "3.2.1",
"@docusaurus/types": "3.2.1",
"monaco-editor-webpack-plugin": "^7.1.0",
"typescript": "~5.3.3"
},
"browserslist": {
Expand Down
90 changes: 90 additions & 0 deletions docs/src/components/TransformerTester.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import React, {useEffect, useState} from "react";
import MonacoEditor from "../components/monaco/MonacoEditor";
import useJSONString from "../components/hooks/useJSONString";

const API_URL = process.env.NODE_ENV !== 'production' ? "http://localhost:10000/api/v1/transform" :
"https://json-transform-playground-latest.onrender.com/api/v1/transform";
const DEFAULT_INPUT_VALUE = `{
"first_name": "John",
"last_name": "Doe",
"date_of_birth": "1980-01-01"
}`,
DEFAULT_DEFINITION_VALUE = `{
"full_name": {
"$$join": ["$.first_name", " ", "$.last_name"]
},
"age": {
"$$math": [ {
"$$math": [
"$$date(EPOCH):#now",
"-",
"$$date(EPOCH):$.date_of_birth"
]
},
"//",
"$$math(365,*,86400)"
]
}
}`;

const TransformerTester = () => {

if (process.env.NODE_ENV !== 'production') {
useEffect(() => {
window.addEventListener('error', e => {
if (e.message.includes('ResizeObserver loop')) {
const resizeObserverErrDiv = document.getElementById(
'webpack-dev-server-client-overlay-div'
);
const resizeObserverErr = document.getElementById(
'webpack-dev-server-client-overlay'
);
if (resizeObserverErr) {
resizeObserverErr.setAttribute('style', 'display: none');
}
if (resizeObserverErrDiv) {
resizeObserverErrDiv.setAttribute('style', 'display: none');
}
}
});
}, []);
}

const [inputString, setInputString, parsedInput, inputError] = useJSONString(DEFAULT_INPUT_VALUE);
const [transformerString, setTransformerString, parsedTransformer, transformerError] =
useJSONString(DEFAULT_DEFINITION_VALUE);
const [outputJSONString, setOutputJSONString] = useState("");
const [outputError, setOutputError] = useState<string | null>(null);

const handleTransform = () => {
fetch(API_URL, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({input: parsedInput, definition: parsedTransformer}),
})
.then(res => res.json())
.then(data => setOutputJSONString(JSON.stringify(data.result, null, 2)))
.then(() => setOutputError(null))
.catch(e => setOutputError(e.message));
}

return <div className="examples_grid">
<MonacoEditor language="json" value={inputString} onChange={(value) => setInputString(value)} height={350}/>
<MonacoEditor language="json" value={transformerString} onChange={(value) => setTransformerString(value)}
height={350}/>
<MonacoEditor language="json" readOnly value={outputJSONString} height={350}/>
<div style={{color: 'red'}}>{inputError?.message}</div>
<div style={{textAlign: "center"}}>
<div style={{color: 'red'}}>{transformerError?.message}</div>
<button type="button" className="button button--primary shadow--lw"
disabled={!!(inputError || transformerError)}
onClick={handleTransform}>Transform
</button>
</div>
<div style={{color: 'red'}}>{outputError}</div>
</div>
}

export default TransformerTester;
19 changes: 19 additions & 0 deletions docs/src/components/hooks/useJSONString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Dispatch, SetStateAction, useMemo, useState } from "react";

type JSONStringHookReturnType = [string | undefined, Dispatch<SetStateAction<string | undefined>>, any, any];

const useJSONString = (defaultValue: string = "{}"): JSONStringHookReturnType => {
const [codeString, setCodeString] = useState<string | undefined>(defaultValue);

return useMemo(() => {
let parsedObject: any, error: any;
try {
parsedObject = codeString ? JSON.parse(codeString) : undefined;
} catch (e) {
error = e;
}
return [codeString, setCodeString, parsedObject, error];
}, [codeString]);
};

export default useJSONString;
Loading

0 comments on commit d53b206

Please sign in to comment.