import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { Banner, Button, ButtonVariant, EmptyContent, Link, WarningSummary, WorkspaceContainer, css, spacing, } from '@mongodb-js/compass-components'; import ValidationEditor from '../validation-editor'; import SampleDocuments from '../sample-documents'; import { ZeroGraphic } from '../zero-graphic'; import type { ValidationEditorProps } from '../validation-editor/validation-editor'; const validationStatesStyles = css({ padding: spacing[3] }); const contentContainerStyles = css({ height: '100%' }); /** * Warnings for the banner. */ export const READ_ONLY_WARNING = { collectionTimeSeries: 'Schema validation for time-series collections is not supported.', collectionReadOnly: 'Schema validation for readonly views is not supported.', writeStateStoreReadOnly: 'This action is not available on a secondary node.', oldServerReadOnly: 'Compass no longer supports the visual rule builder for server versions below 3.2. To use the visual rule builder, please', }; /** * Link to the schema validation documentation. */ const DOC_SCHEMA_VALIDATION = 'https://docs.mongodb.com/manual/core/schema-validation/'; /** * Link to the upgrading to the latest revision documentation. */ const DOC_UPGRADE_REVISION = 'https://docs.mongodb.com/manual/tutorial/upgrade-revision/'; export interface ValidationStatesProps extends Omit { isZeroState: boolean; isLoaded: boolean; changeZeroState: (value: boolean) => void; editMode: { collectionTimeSeries?: boolean; collectionReadOnly?: boolean; writeStateStoreReadOnly?: boolean; oldServerReadOnly?: boolean; }; readOnly: boolean; } /** * The ValidationStates component. */ class ValidationStates extends Component { static displayName = 'ValidationStates'; static propTypes = { isZeroState: PropTypes.bool.isRequired, isLoaded: PropTypes.bool.isRequired, changeZeroState: PropTypes.func.isRequired, editMode: PropTypes.object.isRequired, serverVersion: PropTypes.string, readOnly: PropTypes.bool, }; renderBanner() { if (this.props.editMode.collectionTimeSeries) { return ( ); } if (this.props.editMode.collectionReadOnly) { return ( ); } if (this.props.editMode.writeStateStoreReadOnly) { return ( ); } if (this.props.editMode.oldServerReadOnly) { return (
{READ_ONLY_WARNING.oldServerReadOnly}  upgrade to MongoDB 3.2.
); } } isEditable() { return ( !this.props.editMode.collectionReadOnly && !this.props.editMode.collectionTimeSeries && !this.props.editMode.writeStateStoreReadOnly && !this.props.editMode.oldServerReadOnly && !this.props.readOnly ); } /** * Renders the schema validation zero state. */ renderZeroState() { if (!this.props.isZeroState) { return; } if (!this.props.isLoaded) { // Don't display the form until we finished fetching validation because that would be misleading. return; } return ( Add Rule } callToActionLink={ Learn more about validations } /> ); } /** * Renders the schema validation content. */ renderContent() { if (this.props.isZeroState) { return; } if (!this.props.isLoaded) { return; } return (
); } /** * Renders the ValidationStates component. */ render() { return (
{this.renderBanner()} {this.renderZeroState()} {this.renderContent()}
); } } export default ValidationStates;