Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding import dialog #83

Merged
merged 5 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import React, { useState } from 'react';

import { Alert, Button, Dialog, DialogActions, DialogContent, DialogTitle, TextField, Typography } from '@mui/material';

import { resetDocument } from '../../../documents/editor/EditorContext';

import validateJsonStringValue from './validateJsonStringValue';

type ImportJsonDialogProps = {
onClose: () => void;
};
export default function ImportJsonDialog({ onClose }: ImportJsonDialogProps) {
const [value, setValue] = useState('');
const [error, setError] = useState<string | null>(null);

const handleChange: React.ChangeEventHandler<HTMLTextAreaElement | HTMLInputElement> = (ev) => {
const v = ev.currentTarget.value;
setValue(v);
const { error } = validateJsonStringValue(v);
setError(error ?? null);
};

let errorAlert = null;
if (error) {
errorAlert = <Alert color="error">{error}</Alert>;
}

return (
<Dialog open onClose={onClose}>
<DialogTitle>Import JSON</DialogTitle>
<form
onSubmit={(ev) => {
ev.preventDefault();
const { error, data } = validateJsonStringValue(value);
setError(error ?? null);
if (!data) {
return;
}
resetDocument(data);
onClose();
}}
>
<DialogContent>
<Typography color="text.secondary" paragraph>
Copy and paste an EmailBuilder.js JSON.
</Typography>
{errorAlert}
<TextField
error={error !== null}
value={value}
onChange={handleChange}
type="text"
helperText="This will override your current template."
variant="outlined"
fullWidth
rows={10}
multiline
/>
</DialogContent>
<DialogActions>
<Button type="button" onClick={onClose}>
Cancel
</Button>
<Button variant="contained" type="submit" disabled={error !== null}>
Import
</Button>
</DialogActions>
</form>
</Dialog>
);
}
26 changes: 26 additions & 0 deletions packages/editor-sample/src/App/TemplatePanel/ImportJson/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import React, { useState } from 'react';

import { UploadFileOutlined } from '@mui/icons-material';
import { IconButton, Tooltip } from '@mui/material';

import ImportJsonDialog from './ImportJsonDialog';

export default function ImportJson() {
const [open, setOpen] = useState(false);

let dialog = null;
if (open) {
dialog = <ImportJsonDialog onClose={() => setOpen(false)} />;
}

return (
<>
<Tooltip title="Import JSON">
<IconButton onClick={() => setOpen(true)}>
<UploadFileOutlined fontSize="small" />
</IconButton>
</Tooltip>
{dialog}
</>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { EditorConfigurationSchema, TEditorConfiguration } from '../../../documents/editor/core';

type TResult = { error: string; data?: undefined } | { data: TEditorConfiguration; error?: undefined };

export default function validateTextAreaValue(value: string): TResult {
let jsonObject = undefined;
try {
jsonObject = JSON.parse(value);
} catch {
return { error: 'Invalid json' };
}

const parseResult = EditorConfigurationSchema.safeParse(jsonObject);
if (!parseResult.success) {
return { error: 'Invalid JSON schema' };
}

if (!parseResult.data.root) {
return { error: 'Missing "root" node' };
}

return { data: parseResult.data };
}
6 changes: 5 additions & 1 deletion packages/editor-sample/src/App/TemplatePanel/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import ToggleInspectorPanelButton from '../InspectorDrawer/ToggleInspectorPanelB
import ToggleSamplesPanelButton from '../SamplesDrawer/ToggleSamplesPanelButton';

import HtmlPanel from './HtmlPanel';
import ImportJson from './ImportJson';
import JsonPanel from './JsonPanel';
import MainTabsGroup from './MainTabsGroup';
import ShareButton from './ShareButton';
Expand Down Expand Up @@ -89,8 +90,11 @@ export default function TemplatePanel() {
>
<ToggleSamplesPanelButton />
<Stack px={2} direction="row" gap={2} width="100%" justifyContent="space-between" alignItems="center">
<MainTabsGroup />
<Stack direction="row" spacing={2}>
<MainTabsGroup />
</Stack>
<Stack direction="row" spacing={2}>
<ImportJson />
<ToggleButtonGroup value={selectedScreenSize} exclusive size="small" onChange={handleScreenSizeChange}>
<ToggleButton value="desktop">
<Tooltip title="Desktop view">
Expand Down
Loading