|
| 1 | +import React from 'react'; |
| 2 | +import { connect } from 'react-redux'; |
| 3 | +import type { Relationship } from '../../services/data-model-storage'; |
| 4 | +import { |
| 5 | + Badge, |
| 6 | + Button, |
| 7 | + IconButton, |
| 8 | + css, |
| 9 | + FormFieldContainer, |
| 10 | + palette, |
| 11 | + spacing, |
| 12 | + TextInput, |
| 13 | + Icon, |
| 14 | +} from '@mongodb-js/compass-components'; |
| 15 | +import { |
| 16 | + createNewRelationship, |
| 17 | + deleteRelationship, |
| 18 | + getCurrentDiagramFromState, |
| 19 | + selectCurrentModel, |
| 20 | + selectRelationship, |
| 21 | +} from '../../store/diagram'; |
| 22 | +import type { DataModelingState } from '../../store/reducer'; |
| 23 | +import { getRelationshipName } from '../../utils'; |
| 24 | +import DMDrawerSection from './dm-drawer-section'; |
| 25 | + |
| 26 | +type CollectionDrawerContentProps = { |
| 27 | + namespace: string; |
| 28 | + relationships: Relationship[]; |
| 29 | + onCreateNewRelationshipClick: (namespace: string) => void; |
| 30 | + onEditRelationshipClick: (rId: string) => void; |
| 31 | + onDeleteRelationshipClick: (rId: string) => void; |
| 32 | +}; |
| 33 | + |
| 34 | +const formFieldContainerStyles = css({ |
| 35 | + marginBottom: spacing[400], |
| 36 | + marginTop: spacing[400], |
| 37 | +}); |
| 38 | + |
| 39 | +const titleBtnStyles = css({ |
| 40 | + marginLeft: 'auto', |
| 41 | +}); |
| 42 | + |
| 43 | +const emptyRelationshipMessageStyles = css({ |
| 44 | + color: palette.gray.dark1, |
| 45 | +}); |
| 46 | + |
| 47 | +const relationshipsListStyles = css({ |
| 48 | + display: 'flex', |
| 49 | + flexDirection: 'column', |
| 50 | + gap: spacing[200], |
| 51 | +}); |
| 52 | + |
| 53 | +const relationshipItemStyles = css({ |
| 54 | + display: 'flex', |
| 55 | + alignItems: 'center', |
| 56 | +}); |
| 57 | + |
| 58 | +const relationshipNameStyles = css({ |
| 59 | + flexGrow: 1, |
| 60 | + overflow: 'hidden', |
| 61 | + textOverflow: 'ellipsis', |
| 62 | + whiteSpace: 'nowrap', |
| 63 | + minWidth: 0, |
| 64 | + paddingRight: spacing[200], |
| 65 | +}); |
| 66 | + |
| 67 | +const relationshipContentStyles = css({ |
| 68 | + marginTop: spacing[400], |
| 69 | +}); |
| 70 | + |
| 71 | +const CollectionDrawerContent: React.FunctionComponent< |
| 72 | + CollectionDrawerContentProps |
| 73 | +> = ({ |
| 74 | + namespace, |
| 75 | + relationships, |
| 76 | + onCreateNewRelationshipClick, |
| 77 | + onEditRelationshipClick, |
| 78 | + onDeleteRelationshipClick, |
| 79 | +}) => { |
| 80 | + return ( |
| 81 | + <> |
| 82 | + <DMDrawerSection label="COLLECTION"> |
| 83 | + <FormFieldContainer className={formFieldContainerStyles}> |
| 84 | + <TextInput |
| 85 | + label="Name" |
| 86 | + sizeVariant="small" |
| 87 | + value={namespace} |
| 88 | + disabled={true} |
| 89 | + /> |
| 90 | + </FormFieldContainer> |
| 91 | + </DMDrawerSection> |
| 92 | + |
| 93 | + <DMDrawerSection |
| 94 | + label={ |
| 95 | + <> |
| 96 | + RELATIONSHIPS |
| 97 | + <Badge>{relationships.length}</Badge> |
| 98 | + <Button |
| 99 | + className={titleBtnStyles} |
| 100 | + size="xsmall" |
| 101 | + onClick={() => { |
| 102 | + onCreateNewRelationshipClick(namespace); |
| 103 | + }} |
| 104 | + > |
| 105 | + Add relationship |
| 106 | + </Button> |
| 107 | + </> |
| 108 | + } |
| 109 | + > |
| 110 | + <div className={relationshipContentStyles}> |
| 111 | + {!relationships.length ? ( |
| 112 | + <div className={emptyRelationshipMessageStyles}> |
| 113 | + This collection does not have any relationships yet. |
| 114 | + </div> |
| 115 | + ) : ( |
| 116 | + <ul className={relationshipsListStyles}> |
| 117 | + {relationships.map((r) => { |
| 118 | + return ( |
| 119 | + <li |
| 120 | + key={r.id} |
| 121 | + data-relationship-id={r.id} |
| 122 | + className={relationshipItemStyles} |
| 123 | + > |
| 124 | + <span className={relationshipNameStyles}> |
| 125 | + {getRelationshipName(r)} |
| 126 | + </span> |
| 127 | + <IconButton |
| 128 | + aria-label="Edit relationship" |
| 129 | + title="Edit relationship" |
| 130 | + onClick={() => { |
| 131 | + onEditRelationshipClick(r.id); |
| 132 | + }} |
| 133 | + > |
| 134 | + <Icon glyph="Edit" /> |
| 135 | + </IconButton> |
| 136 | + <IconButton |
| 137 | + aria-label="Delete relationship" |
| 138 | + title="Delete relationship" |
| 139 | + onClick={() => { |
| 140 | + onDeleteRelationshipClick(r.id); |
| 141 | + }} |
| 142 | + > |
| 143 | + <Icon glyph="Trash" /> |
| 144 | + </IconButton> |
| 145 | + </li> |
| 146 | + ); |
| 147 | + })} |
| 148 | + </ul> |
| 149 | + )} |
| 150 | + </div> |
| 151 | + </DMDrawerSection> |
| 152 | + </> |
| 153 | + ); |
| 154 | +}; |
| 155 | + |
| 156 | +export default connect( |
| 157 | + (state: DataModelingState, ownProps: { namespace: string }) => { |
| 158 | + return { |
| 159 | + relationships: selectCurrentModel( |
| 160 | + getCurrentDiagramFromState(state).edits |
| 161 | + ).relationships.filter((r) => { |
| 162 | + const [local, foreign] = r.relationship; |
| 163 | + return ( |
| 164 | + local.ns === ownProps.namespace || foreign.ns === ownProps.namespace |
| 165 | + ); |
| 166 | + }), |
| 167 | + }; |
| 168 | + }, |
| 169 | + { |
| 170 | + onCreateNewRelationshipClick: createNewRelationship, |
| 171 | + onEditRelationshipClick: selectRelationship, |
| 172 | + onDeleteRelationshipClick: deleteRelationship, |
| 173 | + } |
| 174 | +)(CollectionDrawerContent); |
0 commit comments