Skip to content

Commit

Permalink
create a seperate use_can_save hook
Browse files Browse the repository at this point in the history
Signed-off-by: abbyhu2000 <[email protected]>
  • Loading branch information
abbyhu2000 committed Aug 25, 2022
1 parent d61c5af commit 202acd0
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
31 changes: 2 additions & 29 deletions src/plugins/wizard/public/application/components/top_nav.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,7 @@ import './top_nav.scss';
import { useIndexPatterns, useSavedWizardVis } from '../utils/use';
import { useTypedSelector, useTypedDispatch } from '../utils/state_management';
import { setEditorState } from '../utils/state_management/metadata_slice';

const useCanSave = (isEmpty, hasChange, hasDraftAgg) => {
let errorMsg = '';

// TODO: Need to finalize the error messages
if (isEmpty) {
errorMsg = 'The canvas is empty. Add some aggregations before saving.';
} else if (!hasChange) {
errorMsg = 'Add some changes before saving.';
} else if (hasDraftAgg) {
errorMsg = 'Has unapplied aggregations changes, update them before saving.';
} else {
errorMsg = '';
}

return {
canSave: !isEmpty && hasChange && !hasDraftAgg,
errorMsg,
};
};
import { useCanSave } from '../utils/use/use_can_save';

export const TopNav = () => {
// id will only be set for the edit route
Expand All @@ -49,15 +30,7 @@ export const TopNav = () => {
const rootState = useTypedSelector((state) => state);
const dispatch = useTypedDispatch();

const isEmpty = useTypedSelector(
(state) => state.visualization.activeVisualization?.aggConfigParams?.length === 0
);
const hasChange = useTypedSelector((state) => state.metadata.editorState.state === 'dirty');
const hasDraftAgg = useTypedSelector(
(state) => !!state.visualization.activeVisualization?.draftAgg
);

const saveState = useCanSave(isEmpty, hasChange, hasDraftAgg);
const saveState = useCanSave();
const savedWizardVis = useSavedWizardVis(visualizationIdFromUrl);

const config = useMemo(() => {
Expand Down
33 changes: 33 additions & 0 deletions src/plugins/wizard/public/application/utils/use/use_can_save.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { useTypedSelector } from '../state_management';

export const useCanSave = () => {
let errorMsg = '';
const isEmpty = useTypedSelector(
(state) => state.visualization.activeVisualization?.aggConfigParams?.length === 0
);
const hasChange = useTypedSelector((state) => state.metadata.editorState.state === 'dirty');
const hasDraftAgg = useTypedSelector(
(state) => !!state.visualization.activeVisualization?.draftAgg
);

// TODO: Need to finalize the error messages
if (isEmpty) {
errorMsg = 'The canvas is empty. Add some aggregations before saving.';
} else if (!hasChange) {
errorMsg = 'Add some changes before saving.';
} else if (hasDraftAgg) {
errorMsg = 'Has unapplied aggregations changes, update them before saving.';
} else {
errorMsg = '';
}

return {
canSave: !isEmpty && hasChange && !hasDraftAgg,
errorMsg,
};
};

0 comments on commit 202acd0

Please sign in to comment.