-
Notifications
You must be signed in to change notification settings - Fork 372
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
packages/editor-sample/src/App/TemplatePanel/ImportJson/ImportJsonDialog.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
packages/editor-sample/src/App/TemplatePanel/ImportJson/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
</> | ||
); | ||
} |
23 changes: 23 additions & 0 deletions
23
packages/editor-sample/src/App/TemplatePanel/ImportJson/validateJsonStringValue.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters