Skip to content

Commit 3dea357

Browse files
committed
disable the create index button when data is not complete
1 parent 1ee91d1 commit 3dea357

File tree

4 files changed

+48
-6
lines changed

4 files changed

+48
-6
lines changed

packages/compass-indexes/src/components/create-index-actions/create-index-actions.tsx

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import React from 'react';
22
import { css, Banner, spacing, Button } from '@mongodb-js/compass-components';
3+
import { connect } from 'react-redux';
4+
import { areAllFieldsFilledIn } from '../../utils/create-index-modal-validation';
5+
import type { Field, Tab } from '../../modules/create-index';
6+
import type { RootState } from '../../modules';
37

48
const containerStyles = css({
59
display: 'flex',
@@ -27,12 +31,37 @@ function CreateIndexActions({
2731
onErrorBannerCloseClick,
2832
onCreateIndexClick,
2933
onCancelCreateIndexClick,
34+
fields,
35+
currentTab,
36+
showIndexesGuidanceVariant,
37+
indexSuggestions,
3038
}: {
3139
error: string | null;
3240
onErrorBannerCloseClick: () => void;
3341
onCreateIndexClick: () => void;
3442
onCancelCreateIndexClick: () => void;
43+
fields: Field[];
44+
currentTab: Tab;
45+
showIndexesGuidanceVariant: boolean;
46+
indexSuggestions: Record<string, number> | null;
3547
}) {
48+
let isCreateIndexButtonDisabled = false;
49+
50+
if (showIndexesGuidanceVariant) {
51+
// Disable create index button if the user is in Query Flow and has no suggestions
52+
if (currentTab === 'QueryFlow') {
53+
if (indexSuggestions === null) {
54+
isCreateIndexButtonDisabled = true;
55+
}
56+
}
57+
// Or if they are in the Index Flow but have not completed the fields
58+
else {
59+
if (!areAllFieldsFilledIn(fields)) {
60+
isCreateIndexButtonDisabled = true;
61+
}
62+
}
63+
}
64+
3665
return (
3766
<div className={containerStyles}>
3867
{error && (
@@ -61,11 +90,21 @@ function CreateIndexActions({
6190
onClick={onCreateIndexClick}
6291
variant="primary"
6392
className={createIndexButtonStyles}
93+
disabled={isCreateIndexButtonDisabled}
6494
>
6595
Create Index
6696
</Button>
6797
</div>
6898
);
6999
}
70100

71-
export default CreateIndexActions;
101+
const mapState = ({ createIndex }: RootState) => {
102+
const { fields, currentTab, indexSuggestions } = createIndex;
103+
return {
104+
fields,
105+
currentTab,
106+
indexSuggestions,
107+
};
108+
};
109+
110+
export default connect(mapState)(CreateIndexActions);

packages/compass-indexes/src/components/create-index-form/index-flow-section.tsx

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
import React, { useState, useCallback } from 'react';
1616
import type { Field } from '../../modules/create-index';
1717
import MDBCodeViewer from './mdb-code-viewer';
18+
import { areAllFieldsFilledIn } from '../../utils/create-index-modal-validation';
1819

1920
const flexContainerStyles = css({
2021
display: 'flex',
@@ -152,16 +153,12 @@ const IndexFlowSection = ({
152153
const [isCodeEquivalentToggleChecked, setIsCodeEquivalentToggleChecked] =
153154
useState(false);
154155

155-
const areAllFieldsFilledIn = fields.every((field) => {
156-
return field.name && field.type;
157-
});
158-
159156
const hasUnsupportedQueryTypes = fields.some((field) => {
160157
return field.type === '2dsphere' || field.type === 'text';
161158
});
162159

163160
const isCoveredQueriesButtonDisabled =
164-
!areAllFieldsFilledIn || hasUnsupportedQueryTypes;
161+
!areAllFieldsFilledIn(fields) || hasUnsupportedQueryTypes;
165162

166163
const indexNameTypeMap = fields.reduce<Record<string, string>>(
167164
(accumulator, currentValue) => {

packages/compass-indexes/src/components/create-index-modal/create-index-modal.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ function CreateIndexModal({
117117
onErrorBannerCloseClick={onErrorBannerCloseClick}
118118
onCreateIndexClick={onCreateIndexClick}
119119
onCancelCreateIndexClick={onCancelCreateIndexClick}
120+
showIndexesGuidanceVariant={showIndexesGuidanceVariant}
120121
/>
121122
</ModalFooter>
122123
</Modal>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import type { Field } from '../modules/create-index';
2+
3+
export const areAllFieldsFilledIn = (fields: Field[]) => {
4+
return fields.every((field) => field.name && field.type);
5+
};

0 commit comments

Comments
 (0)