Skip to content

Commit

Permalink
feat: keep given json in the editor if swithing pages
Browse files Browse the repository at this point in the history
refs #26
  • Loading branch information
marabesi committed Jul 30, 2022
1 parent c9e4dc6 commit a01ddd1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
29 changes: 29 additions & 0 deletions src/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,35 @@ describe('json utility', () => {

expect(result.nodeValue).toMatchSnapshot(expected);
});

it('should keep content in the editor when navigating away', async () => {
const { container, getByTestId, getByText } = render(<App/>);

const editor = grabCurrentEditor(container);
const json = '{{"random_json":"123"}';

await act(async () => {
await userEvent.type(editor, json);
});

const rawEditor = screen.getByTestId('raw-json');

await waitFor(() => {
expect(rawEditor).toHaveValue('{"random_json":"123"}');
});

fireEvent.click(getByTestId('settings'));

await waitFor(() => {
expect(getByText('Settings')).toBeInTheDocument();
});

fireEvent.click(getByTestId('to-home'));

await waitFor(() => {
expect(screen.getByTestId('raw-json')).toHaveValue('{"random_json":"123"}');
});
});
});

describe('Error handling', () => {
Expand Down
7 changes: 6 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const SettingsContext = createContext(defaultOp);

function App() {
const [options, setOptions] = useState<Option[]>(defaultOp);
const [savedState, setSavedState] = useState<string>('');

const handleChange = (event: Option) => {
setOptions(options.map((option: Option) => {
Expand All @@ -29,10 +30,14 @@ function App() {
}));
};

const saveState = (json: string) => {
setSavedState(json);
};

return (
<Router>
<Routes>
<Route path="/" element={<Editors />} />
<Route path="/" element={<Editors onPersist={saveState} currentJson={savedState} />} />
<Route path="/settings" element={
<SettingsContext.Provider value={options as never}>
<Settings options={options} handleChange={handleChange} />
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default function Header({ ...props }) {
<div className="bg-blue-900 flex justify-between p-5 text-white" {...props}>
<div className="flex">
<h2 className="text-yellow-400 font-bold">
<Link to="/">JSON tool</Link>
<Link to="/" data-testid="to-home">JSON tool</Link>
</h2> |
<a href="https://github.com/marabesi/json-tool" target="_blank" rel="noreferrer">by marabesi</a>
</div>
Expand Down
14 changes: 11 additions & 3 deletions src/pages/Editors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ import DefaultLayout from '../components/ui/layout/Default';

const cleanUp = new CleanUp();

export default function Editors() {
const [originalJson, setOriginalResult] = useState<string>('');
interface Props {
currentJson: string
onPersist: (json: string) => void
}

export default function Editors({ onPersist, currentJson }: Props) {
const [originalJson, setOriginalResult] = useState<string>(currentJson);
const [result, setResult] = useState<string>('');
const [error, setError] = useState<string>('');
const [spacing, setSpacing] = useState<string>('2');
Expand Down Expand Up @@ -43,7 +48,10 @@ export default function Editors() {

useEffect(() => {
onJsonChange(originalJson);
}, [spacing, originalJson, onJsonChange]);
return () => {
onPersist(originalJson);
};
}, [spacing, originalJson, onJsonChange, onPersist]);

const pasteFromClipboard = async () => {
const clipboardItems = await navigator.clipboard.read();
Expand Down

0 comments on commit a01ddd1

Please sign in to comment.