|
| 1 | +import React, { useMemo } from 'react'; |
| 2 | +import { connect } from 'react-redux'; |
| 3 | +import type { |
| 4 | + FieldPath, |
| 5 | + FieldSchema, |
| 6 | + Relationship, |
| 7 | +} from '../../services/data-model-storage'; |
| 8 | +import { TextInput } from '@mongodb-js/compass-components'; |
| 9 | +import toNS from 'mongodb-ns'; |
| 10 | +import { |
| 11 | + createNewRelationship, |
| 12 | + deleteRelationship, |
| 13 | + selectCurrentModelFromState, |
| 14 | + selectRelationship, |
| 15 | +} from '../../store/diagram'; |
| 16 | +import type { DataModelingState } from '../../store/reducer'; |
| 17 | +import { |
| 18 | + DMDrawerSection, |
| 19 | + DMFormFieldContainer, |
| 20 | +} from './drawer-section-components'; |
| 21 | +import { useChangeOnBlur } from './use-change-on-blur'; |
| 22 | +import { RelationshipsSection } from './relationships-section'; |
| 23 | + |
| 24 | +type FieldDrawerContentProps = { |
| 25 | + namespace: string; |
| 26 | + fieldPath: FieldPath; |
| 27 | + fieldPaths: FieldPath[]; |
| 28 | + jsonSchema: FieldSchema; |
| 29 | + relationships: Relationship[]; |
| 30 | + onCreateNewRelationshipClick: ({ |
| 31 | + localNamespace, |
| 32 | + localFields, |
| 33 | + }: { |
| 34 | + localNamespace: string; |
| 35 | + localFields: FieldPath; |
| 36 | + }) => void; |
| 37 | + onEditRelationshipClick: (rId: string) => void; |
| 38 | + onDeleteRelationshipClick: (rId: string) => void; |
| 39 | + onRenameField: ( |
| 40 | + namespace: string, |
| 41 | + fromFieldPath: FieldPath, |
| 42 | + toFieldPath: FieldPath |
| 43 | + ) => void; |
| 44 | + onChangeFieldType: ( |
| 45 | + namespace: string, |
| 46 | + fieldPath: FieldPath, |
| 47 | + fromBsonType: string, |
| 48 | + toBsonType: string |
| 49 | + ) => void; |
| 50 | +}; |
| 51 | + |
| 52 | +export function getIsFieldNameValid( |
| 53 | + currentFieldPath: FieldPath, |
| 54 | + existingFields: FieldPath[], |
| 55 | + newName: string |
| 56 | +): { |
| 57 | + isValid: boolean; |
| 58 | + errorMessage?: string; |
| 59 | +} { |
| 60 | + const trimmedName = newName.trim(); |
| 61 | + if (!trimmedName.length) { |
| 62 | + return { |
| 63 | + isValid: false, |
| 64 | + errorMessage: 'Field name cannot be empty.', |
| 65 | + }; |
| 66 | + } |
| 67 | + |
| 68 | + const fieldsNamesWithoutCurrent = existingFields |
| 69 | + .filter( |
| 70 | + (fieldPath) => |
| 71 | + JSON.stringify(fieldPath) !== JSON.stringify(currentFieldPath) |
| 72 | + ) |
| 73 | + .map((fieldPath) => fieldPath[fieldPath.length - 1]); |
| 74 | + |
| 75 | + const isDuplicate = fieldsNamesWithoutCurrent.some( |
| 76 | + (fieldName) => fieldName === trimmedName |
| 77 | + ); |
| 78 | + |
| 79 | + return { |
| 80 | + isValid: !isDuplicate, |
| 81 | + errorMessage: isDuplicate ? 'Field already exists.' : undefined, |
| 82 | + }; |
| 83 | +} |
| 84 | + |
| 85 | +const FieldDrawerContent: React.FunctionComponent<FieldDrawerContentProps> = ({ |
| 86 | + namespace, |
| 87 | + fieldPath, |
| 88 | + fieldPaths, |
| 89 | + jsonSchema, |
| 90 | + relationships, |
| 91 | + onCreateNewRelationshipClick, |
| 92 | + onEditRelationshipClick, |
| 93 | + onDeleteRelationshipClick, |
| 94 | + onRenameField, |
| 95 | + onChangeFieldType, |
| 96 | +}) => { |
| 97 | + const { value: fieldName, ...nameInputProps } = useChangeOnBlur( |
| 98 | + fieldPath[fieldPath.length - 1], |
| 99 | + (fieldName) => { |
| 100 | + const trimmedName = fieldName.trim(); |
| 101 | + if (trimmedName === fieldName) { |
| 102 | + return; |
| 103 | + } |
| 104 | + if (!isFieldNameValid) { |
| 105 | + return; |
| 106 | + } |
| 107 | + onRenameField(namespace, fieldPath, [ |
| 108 | + ...fieldPath.slice(0, fieldPath.length - 1), |
| 109 | + trimmedName, |
| 110 | + ]); |
| 111 | + } |
| 112 | + ); |
| 113 | + |
| 114 | + const { isValid: isFieldNameValid, errorMessage: fieldNameEditErrorMessage } = |
| 115 | + useMemo( |
| 116 | + () => getIsFieldNameValid(fieldPath, fieldPaths, fieldName), |
| 117 | + [fieldPath, fieldPaths, fieldName] |
| 118 | + ); |
| 119 | + |
| 120 | + return ( |
| 121 | + <> |
| 122 | + <DMDrawerSection label="Field properties"> |
| 123 | + <DMFormFieldContainer> |
| 124 | + <TextInput |
| 125 | + label="Field name" |
| 126 | + data-testid="data-model-collection-drawer-name-input" |
| 127 | + sizeVariant="small" |
| 128 | + value={fieldName} |
| 129 | + {...nameInputProps} |
| 130 | + state={isFieldNameValid ? undefined : 'error'} |
| 131 | + errorMessage={fieldNameEditErrorMessage} |
| 132 | + /> |
| 133 | + </DMFormFieldContainer> |
| 134 | + </DMDrawerSection> |
| 135 | + |
| 136 | + <RelationshipsSection |
| 137 | + relationships={relationships} |
| 138 | + emptyMessage="This field does not have any relationships yet." |
| 139 | + getRelationshipLabel={([local, foreign]) => { |
| 140 | + const labelField = |
| 141 | + local.ns === namespace && |
| 142 | + JSON.stringify(local.fields) === JSON.stringify(fieldPath) |
| 143 | + ? foreign |
| 144 | + : local; |
| 145 | + return [ |
| 146 | + labelField.ns ? toNS(labelField.ns).collection : '', |
| 147 | + labelField.fields?.join('.'), |
| 148 | + ].join('.'); |
| 149 | + }} |
| 150 | + onCreateNewRelationshipClick={() => { |
| 151 | + onCreateNewRelationshipClick({ |
| 152 | + localNamespace: namespace, |
| 153 | + localFields: fieldPath, |
| 154 | + }); |
| 155 | + }} |
| 156 | + onEditRelationshipClick={onEditRelationshipClick} |
| 157 | + onDeleteRelationshipClick={onDeleteRelationshipClick} |
| 158 | + /> |
| 159 | + </> |
| 160 | + ); |
| 161 | +}; |
| 162 | + |
| 163 | +export default connect( |
| 164 | + ( |
| 165 | + state: DataModelingState, |
| 166 | + ownProps: { namespace: string; fieldPath: FieldPath } |
| 167 | + ) => { |
| 168 | + const model = selectCurrentModelFromState(state); |
| 169 | + return { |
| 170 | + jsonSchema: {}, // TODO get field schema |
| 171 | + fieldPaths: [], // TODO get field paths |
| 172 | + relationships: model.relationships.filter((r) => { |
| 173 | + const [local, foreign] = r.relationship; |
| 174 | + return ( |
| 175 | + (local.ns === ownProps.namespace && |
| 176 | + JSON.stringify(local.fields) === |
| 177 | + JSON.stringify(ownProps.fieldPath)) || |
| 178 | + (foreign.ns === ownProps.namespace && |
| 179 | + JSON.stringify(foreign.fields) === |
| 180 | + JSON.stringify(ownProps.fieldPath)) |
| 181 | + ); |
| 182 | + }), |
| 183 | + }; |
| 184 | + }, |
| 185 | + { |
| 186 | + onCreateNewRelationshipClick: createNewRelationship, |
| 187 | + onEditRelationshipClick: selectRelationship, |
| 188 | + onDeleteRelationshipClick: deleteRelationship, |
| 189 | + onRenameField: () => {}, // TODO: renameField, |
| 190 | + onChangeFieldType: () => {}, // TODO: updateFieldSchema, |
| 191 | + } |
| 192 | +)(FieldDrawerContent); |
0 commit comments