[App Search] Disabled Save button when nothing selected (#97896)
This commit is contained in:
parent
5c94911543
commit
8bbf9c0e28
6 changed files with 78 additions and 4 deletions
|
@ -86,6 +86,17 @@ describe('ResultSettings', () => {
|
|||
expect(saveButton.prop('disabled')).toBe(true);
|
||||
});
|
||||
|
||||
it('renders the "save" button as disabled if everything is disabled', () => {
|
||||
setMockValues({
|
||||
...values,
|
||||
stagedUpdates: true,
|
||||
resultFieldsEmpty: true,
|
||||
});
|
||||
const buttons = findButtons(subject());
|
||||
const saveButton = shallow(buttons[0]);
|
||||
expect(saveButton.prop('disabled')).toBe(true);
|
||||
});
|
||||
|
||||
it('renders a "restore defaults" button that will reset all values to their defaults', () => {
|
||||
const buttons = findButtons(subject());
|
||||
expect(buttons.length).toBe(3);
|
||||
|
|
|
@ -46,9 +46,13 @@ const UNSAVED_MESSAGE = i18n.translate(
|
|||
);
|
||||
|
||||
export const ResultSettings: React.FC = () => {
|
||||
const { dataLoading, schema, stagedUpdates, resultFieldsAtDefaultSettings } = useValues(
|
||||
ResultSettingsLogic
|
||||
);
|
||||
const {
|
||||
dataLoading,
|
||||
schema,
|
||||
stagedUpdates,
|
||||
resultFieldsAtDefaultSettings,
|
||||
resultFieldsEmpty,
|
||||
} = useValues(ResultSettingsLogic);
|
||||
const {
|
||||
initializeResultSettingsData,
|
||||
saveResultSettings,
|
||||
|
@ -81,7 +85,7 @@ export const ResultSettings: React.FC = () => {
|
|||
color="primary"
|
||||
fill
|
||||
onClick={saveResultSettings}
|
||||
disabled={!stagedUpdates}
|
||||
disabled={resultFieldsEmpty || !stagedUpdates}
|
||||
>
|
||||
{SAVE_BUTTON_LABEL}
|
||||
</EuiButton>,
|
||||
|
|
|
@ -46,6 +46,7 @@ describe('ResultSettingsLogic', () => {
|
|||
const SELECTORS = {
|
||||
serverResultFields: {},
|
||||
reducedServerResultFields: {},
|
||||
resultFieldsEmpty: true,
|
||||
resultFieldsAtDefaultSettings: true,
|
||||
stagedUpdates: false,
|
||||
nonTextResultFields: {},
|
||||
|
@ -333,6 +334,20 @@ describe('ResultSettingsLogic', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('resultFieldsEmpty', () => {
|
||||
it('should return true if no raw or snippet fields are enabled', () => {
|
||||
mount({
|
||||
resultFields: {
|
||||
foo: { raw: false },
|
||||
bar: {},
|
||||
baz: { raw: false, snippet: false },
|
||||
},
|
||||
});
|
||||
|
||||
expect(ResultSettingsLogic.values.resultFieldsEmpty).toEqual(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('stagedUpdates', () => {
|
||||
it('should return true if changes have been made since the last save', () => {
|
||||
mount({
|
||||
|
|
|
@ -24,6 +24,7 @@ import {
|
|||
|
||||
import {
|
||||
areFieldsAtDefaultSettings,
|
||||
areFieldsEmpty,
|
||||
clearAllFields,
|
||||
convertServerResultFieldsToResultFields,
|
||||
convertToServerFieldResultSetting,
|
||||
|
@ -197,6 +198,10 @@ export const ResultSettingsLogic = kea<MakeLogicType<ResultSettingsValues, Resul
|
|||
() => [selectors.resultFields],
|
||||
(resultFields) => areFieldsAtDefaultSettings(resultFields),
|
||||
],
|
||||
resultFieldsEmpty: [
|
||||
() => [selectors.resultFields],
|
||||
(resultFields) => areFieldsEmpty(resultFields),
|
||||
],
|
||||
stagedUpdates: [
|
||||
() => [selectors.lastSavedResultFields, selectors.resultFields],
|
||||
(lastSavedResultFields, resultFields) => !isEqual(lastSavedResultFields, resultFields),
|
||||
|
|
|
@ -14,6 +14,7 @@ import {
|
|||
clearAllFields,
|
||||
resetAllFields,
|
||||
splitResultFields,
|
||||
areFieldsEmpty,
|
||||
} from './utils';
|
||||
|
||||
describe('clearAllFields', () => {
|
||||
|
@ -144,6 +145,37 @@ describe('splitResultFields', () => {
|
|||
});
|
||||
});
|
||||
|
||||
describe('areFieldsEmpty', () => {
|
||||
it('should return true if all fields are empty or have all properties disabled', () => {
|
||||
expect(
|
||||
areFieldsEmpty({
|
||||
foo: {},
|
||||
bar: { raw: false, snippet: false },
|
||||
baz: { raw: false },
|
||||
})
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('should return false otherwise', () => {
|
||||
expect(
|
||||
areFieldsEmpty({
|
||||
foo: {
|
||||
raw: true,
|
||||
rawSize: 5,
|
||||
snippet: false,
|
||||
snippetFallback: false,
|
||||
},
|
||||
bar: {
|
||||
raw: true,
|
||||
rawSize: 5,
|
||||
snippet: false,
|
||||
snippetFallback: false,
|
||||
},
|
||||
})
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('areFieldsAtDefaultSettings', () => {
|
||||
it('will return true if all settings for all fields are at their defaults', () => {
|
||||
expect(
|
||||
|
|
|
@ -112,6 +112,13 @@ export const splitResultFields = (resultFields: FieldResultSettingObject, schema
|
|||
return { textResultFields, nonTextResultFields };
|
||||
};
|
||||
|
||||
export const areFieldsEmpty = (fields: FieldResultSettingObject) => {
|
||||
const anyNonEmptyField = Object.values(fields).find((field) => {
|
||||
return (field as FieldResultSetting).raw || (field as FieldResultSetting).snippet;
|
||||
});
|
||||
return !anyNonEmptyField;
|
||||
};
|
||||
|
||||
export const areFieldsAtDefaultSettings = (fields: FieldResultSettingObject) => {
|
||||
const anyNonDefaultSettingsValue = Object.values(fields).find((resultSettings) => {
|
||||
return !isEqual(resultSettings, DEFAULT_FIELD_SETTINGS);
|
||||
|
|
Loading…
Reference in a new issue