-
Notifications
You must be signed in to change notification settings - Fork 46
MMT-4076: Enable editing of the JSON text at bottom of the metadata form directly. #1507
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
Merged
Changes from 9 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
c2ba904
MMT-4076: Edit JSON text directly
htranho f76300c
MMT-4076: Add json validation
htranho b854998
MMT-4076: npm audit fix
htranho 68ea860
MMT-4076: Add oneOf/anyOf to the list of excluded errors
htranho 1180ddf
MMT-4076: Fix stale data issue when saving the form
htranho c1867f7
Merge branch 'main' into MMT-4076
htranho 214da10
MMT-4076: Make JSON pane a modal pop up
htranho df8efc8
MMT-4076: Re-add import
htranho 82030bf
MMT-4076: Update @edsc/metadata-preview version to 1.5.13
htranho e1fa6d8
MMT-4076: Update process, buttons in dialog, redduce comments
htranho 3461f6d
MMT-4076: Updated
htranho 78946d9
MMT-4076: Fix duplicated key warning
htranho ff55130
MMT-4076: Only 'required' field errors pass through
htranho 3c3232f
MMT-4076: Error message modal width
htranho 9c48950
MMT-4076: Correct some wording in messages
htranho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -1,36 +1,297 @@ | ||
| import React from 'react' | ||
| import React, { useState } from 'react' | ||
| import Accordion from 'react-bootstrap/Accordion' | ||
| import Modal from 'react-bootstrap/Modal' | ||
| import JSONPretty from 'react-json-pretty' | ||
| import { cloneDeep } from 'lodash-es' | ||
| import PropTypes from 'prop-types' | ||
| import validator from '@rjsf/validator-ajv8' | ||
|
|
||
| import useAppContext from '../../hooks/useAppContext' | ||
| import removeEmpty from '../../utils/removeEmpty' | ||
| import Button from '../Button/Button' | ||
|
|
||
| const JsonPreview = () => { | ||
| const JsonPreview = ({ schema }) => { | ||
| const { | ||
| draft = {} | ||
| draft = {}, | ||
| setDraft | ||
| } = useAppContext() | ||
|
|
||
| // Remove || {} in MMT-4070 | ||
| const { ummMetadata = {} } = draft || {} | ||
|
|
||
| const data = cloneDeep(removeEmpty(ummMetadata)) | ||
|
|
||
| const [isEditing, setIsEditing] = useState(false) | ||
| const [jsonText, setJsonText] = useState('') | ||
|
|
||
| // Inline, blocking error -- only ever a JSON.parse failure. There's | ||
| // nothing to save yet, so this can't be resolved with a "save anyway" | ||
| // confirmation the way schema errors can. | ||
| const [parseError, setParseError] = useState(null) | ||
|
|
||
| // Schema/structural errors surfaced on Save. These don't block saving -- | ||
| // they open the confirmation modal below instead, and the user decides | ||
| // whether to save despite them. | ||
| const [pendingErrors, setPendingErrors] = useState([]) | ||
| const [pendingParsed, setPendingParsed] = useState(null) | ||
| const [showConfirm, setShowConfirm] = useState(false) | ||
|
|
||
| const handleEditClick = () => { | ||
| setJsonText(JSON.stringify(data, null, 2)) | ||
| setParseError(null) | ||
| setPendingErrors([]) | ||
| setPendingParsed(null) | ||
| setIsEditing(true) | ||
| } | ||
|
|
||
| const handleCancel = () => { | ||
| setJsonText(JSON.stringify(data, null, 2)) | ||
| setParseError(null) | ||
| setPendingErrors([]) | ||
| setPendingParsed(null) | ||
| setShowConfirm(false) | ||
| setIsEditing(false) | ||
| } | ||
|
|
||
| const handleTextChange = (event) => { | ||
| setJsonText(event.target.value) | ||
| if (parseError) setParseError(null) | ||
| } | ||
|
|
||
| const commitSave = (parsed) => { | ||
| setDraft({ | ||
| ...draft, | ||
| ummMetadata: parsed | ||
| }) | ||
|
|
||
| setParseError(null) | ||
| setPendingErrors([]) | ||
| setPendingParsed(null) | ||
| setShowConfirm(false) | ||
| setIsEditing(false) | ||
| } | ||
|
|
||
| const handleSaveClick = () => { | ||
| let parsed | ||
|
|
||
| try { | ||
| parsed = JSON.parse(jsonText) | ||
| } catch (parseErrorObj) { | ||
| setParseError(`Invalid JSON: ${parseErrorObj.message}`) | ||
|
|
||
| return | ||
| } | ||
|
|
||
| setParseError(null) | ||
|
|
||
| if (schema) { | ||
| const { errors: schemaErrors = [] } = validator.validateFormData(parsed, schema) | ||
|
|
||
| // Only surface structural problems (an unknown/typo'd field name, or a | ||
|
mandyparson marked this conversation as resolved.
Outdated
|
||
| // value of the wrong type). Missing-required-field errors are ignored | ||
| // here so saving through the JSON editor stays as permissive as saving | ||
| // through the form fields, which never blocks on incomplete drafts. | ||
| // A missing required field inside a oneOf/anyOf branch (e.g. a | ||
| // discriminated union) doesn't just produce a 'required' error -- AJV | ||
| // also emits a wrapping 'oneOf'/'anyOf' error at the same instancePath | ||
| // ("must match a schema in oneOf/anyOf"), so that wrapper needs to be | ||
| // ignored too or an otherwise-incomplete-but-valid draft would still | ||
| // trigger a confirmation. | ||
| // | ||
| // However, oneOf/anyOf errors aren't ONLY produced by missing-required | ||
| // noise -- some schemas express a controlled vocabulary (effectively | ||
| // an enum) as `oneOf: [{ const: 'A' }, { const: 'B' }, ...]` instead of | ||
| // a plain `enum`. An invalid value for one of those fields fails with | ||
| // a oneOf/anyOf error too, and a blanket filter would silently let it | ||
| // through. So only drop a oneOf/anyOf error when a 'required' error | ||
| // exists at that same instancePath (i.e. it's the wrapper noise) -- | ||
| // keep it when it's the only error at that path, since that means it's | ||
| // a genuine invalid-value failure. | ||
| const requiredPaths = new Set( | ||
| schemaErrors | ||
| .filter(({ name }) => name === 'required') | ||
| .map(({ instancePath }) => instancePath) | ||
| ) | ||
|
|
||
| const structuralErrors = schemaErrors.filter(({ name, instancePath }) => { | ||
|
mandyparson marked this conversation as resolved.
Outdated
|
||
| if (name === 'required') return false | ||
| if ((name === 'oneOf' || name === 'anyOf') && requiredPaths.has(instancePath)) return false | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| return true | ||
| }) | ||
|
|
||
| if (structuralErrors.length > 0) { | ||
| const messages = structuralErrors.map(({ | ||
| name, | ||
| property, | ||
| message, | ||
| params | ||
| }) => { | ||
| // AJV puts the actual bad key in params.additionalProperty for this | ||
| // error type -- `property` here refers to the parent object, and | ||
| // `message` alone doesn't name the offending field at all. | ||
| if (name === 'additionalProperties' && params?.additionalProperty) { | ||
| const location = property ? `${property} ` : '' | ||
|
|
||
| return `${location}must NOT have additional property '${params.additionalProperty}'` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a space between location and must
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| } | ||
|
|
||
| return property ? `${property} ${message}` : message | ||
| }) | ||
|
|
||
| setPendingErrors(messages) | ||
| setPendingParsed(parsed) | ||
| setShowConfirm(true) | ||
|
|
||
| return | ||
| } | ||
| } | ||
|
|
||
| commitSave(parsed) | ||
| } | ||
|
|
||
| const handleConfirmSaveAnyway = () => { | ||
| commitSave(pendingParsed) | ||
| } | ||
|
|
||
| const handleConfirmBack = () => { | ||
| setShowConfirm(false) | ||
| setPendingErrors([]) | ||
| setPendingParsed(null) | ||
| } | ||
|
|
||
| return ( | ||
| <Accordion | ||
| defaultActiveKey="0" | ||
| className="mt-5" | ||
| > | ||
| <Accordion.Item eventKey="0"> | ||
| <Accordion.Header> | ||
| JSON | ||
| </Accordion.Header> | ||
| <Accordion.Body> | ||
| <JSONPretty data={data} /> | ||
| </Accordion.Body> | ||
| </Accordion.Item> | ||
| </Accordion> | ||
| <> | ||
| <Accordion | ||
| defaultActiveKey="0" | ||
| className="mt-5" | ||
| > | ||
| <Accordion.Item eventKey="0"> | ||
| <Accordion.Header> | ||
| JSON | ||
| </Accordion.Header> | ||
| <Accordion.Body> | ||
| <div className="d-flex justify-content-end mb-2"> | ||
| <Button | ||
| variant="secondary" | ||
| size="sm" | ||
| onClick={handleEditClick} | ||
| > | ||
| Edit JSON | ||
| </Button> | ||
| </div> | ||
|
|
||
| <JSONPretty data={data} /> | ||
| </Accordion.Body> | ||
| </Accordion.Item> | ||
| </Accordion> | ||
|
|
||
| <Modal | ||
|
mandyparson marked this conversation as resolved.
Outdated
|
||
| show={isEditing} | ||
| onHide={handleCancel} | ||
| size="lg" | ||
| animation={false} | ||
| aria-labelledby="json-preview-edit-modal-title" | ||
| > | ||
| <Modal.Header closeButton> | ||
| <Modal.Title id="json-preview-edit-modal-title"> | ||
| Edit JSON | ||
| </Modal.Title> | ||
| </Modal.Header> | ||
|
|
||
| <Modal.Body> | ||
| { | ||
| parseError && ( | ||
| <div className="text-danger small mb-2" role="alert"> | ||
| {parseError} | ||
| </div> | ||
| ) | ||
| } | ||
|
|
||
| <textarea | ||
| className={`form-control font-monospace ${parseError ? 'is-invalid' : ''}`} | ||
| rows={20} | ||
| value={jsonText} | ||
| onChange={handleTextChange} | ||
| spellCheck={false} | ||
| aria-label="Editable JSON metadata" | ||
| /> | ||
| </Modal.Body> | ||
|
|
||
| <Modal.Footer> | ||
| <Button | ||
| variant="secondary" | ||
| size="sm" | ||
| onClick={handleCancel} | ||
| > | ||
| Cancel | ||
| </Button> | ||
| <Button | ||
| variant="primary" | ||
| size="sm" | ||
| onClick={handleSaveClick} | ||
| > | ||
| Save | ||
|
mandyparson marked this conversation as resolved.
Outdated
|
||
| </Button> | ||
| </Modal.Footer> | ||
| </Modal> | ||
|
|
||
| <Modal | ||
| show={showConfirm} | ||
| onHide={handleConfirmBack} | ||
| animation={false} | ||
| aria-labelledby="json-preview-confirm-modal-title" | ||
| > | ||
| <Modal.Header closeButton> | ||
| <Modal.Title id="json-preview-confirm-modal-title"> | ||
| Confirm Save | ||
| </Modal.Title> | ||
| </Modal.Header> | ||
|
|
||
| <Modal.Body> | ||
| <p>Your record has following errors:</p> | ||
|
|
||
| <ul> | ||
| { | ||
| pendingErrors.map((message) => ( | ||
| <li key={message}>{message}</li> | ||
| )) | ||
| } | ||
| </ul> | ||
|
|
||
| <p>Would you like to proceed?</p> | ||
| </Modal.Body> | ||
|
|
||
| <Modal.Footer> | ||
| <Button | ||
| variant="secondary" | ||
| size="sm" | ||
| onClick={handleConfirmBack} | ||
| > | ||
| Back | ||
| </Button> | ||
| <Button | ||
| variant="primary" | ||
| size="sm" | ||
| onClick={handleConfirmSaveAnyway} | ||
| > | ||
| Save & Continue | ||
| </Button> | ||
| </Modal.Footer> | ||
| </Modal> | ||
| </> | ||
| ) | ||
| } | ||
|
|
||
| JsonPreview.defaultProps = { | ||
| schema: null | ||
| } | ||
|
|
||
| JsonPreview.propTypes = { | ||
| // The full UMM schema (not a section-limited schema) to validate the | ||
| // edited JSON against on save. If omitted, only JSON-syntax validation | ||
| // is performed. | ||
| // eslint-disable-next-line react/forbid-prop-types | ||
| schema: PropTypes.object | ||
| } | ||
|
|
||
| export default JsonPreview | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.