-
Notifications
You must be signed in to change notification settings - Fork 935
/
Copy pathget_top_nav_config.tsx
204 lines (188 loc) · 6.29 KB
/
get_top_nav_config.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Any modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import React from 'react';
import { i18n } from '@osd/i18n';
import { TopNavMenuData } from '../../../../navigation/public';
import {
OnSaveProps,
SavedObjectSaveModalOrigin,
showSaveModal,
} from '../../../../saved_objects/public';
import { WizardServices } from '../..';
import { WizardVisSavedObject } from '../../types';
import { AppDispatch } from './state_management';
import { EDIT_PATH } from '../../../common';
import { setEditorState } from './state_management/metadata_slice';
export interface TopNavConfigParams {
visualizationIdFromUrl: string;
savedWizardVis: WizardVisSavedObject;
saveDisabledReason?: string;
dispatch: AppDispatch;
}
export const getTopNavConfig = (
{ visualizationIdFromUrl, savedWizardVis, saveDisabledReason, dispatch }: TopNavConfigParams,
services: WizardServices
) => {
const {
i18n: { Context: I18nContext },
embeddable,
scopedHistory,
} = services;
const { originatingApp } =
embeddable
.getStateTransfer(scopedHistory)
.getIncomingEditorState({ keysToRemoveAfterFetch: ['id', 'input'] }) || {};
const stateTransfer = embeddable.getStateTransfer();
const topNavConfig: TopNavMenuData[] = [
{
id: 'save',
iconType: 'save',
emphasize: savedWizardVis && !savedWizardVis.id,
description: i18n.translate('wizard.topNavMenu.saveVisualizationButtonAriaLabel', {
defaultMessage: 'Save Visualization',
}),
className: 'saveButton',
label: i18n.translate('wizard.topNavMenu.saveVisualizationButtonLabel', {
defaultMessage: 'save',
}),
testId: 'wizardSaveButton',
disableButton: !!saveDisabledReason,
tooltip: saveDisabledReason,
run: (_anchorElement) => {
const saveModal = (
<SavedObjectSaveModalOrigin
documentInfo={savedWizardVis}
onSave={getOnSave(
savedWizardVis,
originatingApp,
visualizationIdFromUrl,
dispatch,
services
)}
objectType={'wizard'}
onClose={() => {}}
originatingApp={originatingApp}
getAppNameFromId={stateTransfer.getAppNameFromId}
/>
);
showSaveModal(saveModal, I18nContext);
},
},
];
return topNavConfig;
};
export const getOnSave = (
savedWizardVis,
originatingApp,
visualizationIdFromUrl,
dispatch,
services
) => {
const onSave = async ({
newTitle,
newCopyOnSave,
isTitleDuplicateConfirmed,
onTitleDuplicate,
newDescription,
returnToOrigin,
}: OnSaveProps & { returnToOrigin: boolean }) => {
const { embeddable, toastNotifications, application, history } = services;
const stateTransfer = embeddable.getStateTransfer();
if (!savedWizardVis) {
return;
}
const newlyCreated = !savedWizardVis.id || savedWizardVis.copyOnSave;
const currentTitle = savedWizardVis.title;
savedWizardVis.title = newTitle;
savedWizardVis.description = newDescription;
savedWizardVis.copyOnSave = newCopyOnSave;
try {
const id = await savedWizardVis.save({
confirmOverwrite: false,
isTitleDuplicateConfirmed,
onTitleDuplicate,
returnToOrigin,
});
if (id) {
toastNotifications.addSuccess({
title: i18n.translate('wizard.topNavMenu.saveVisualization.successNotificationText', {
defaultMessage: `Saved '{visTitle}'`,
values: {
visTitle: savedWizardVis.title,
},
}),
'data-test-subj': 'saveVisualizationSuccess',
});
if (originatingApp && returnToOrigin) {
// create or edit wizard directly from another app, such as `dashboard`
if (newlyCreated && stateTransfer) {
// create new embeddable to transfer to originatingApp
stateTransfer.navigateToWithEmbeddablePackage(originatingApp, {
state: { type: 'wizard', input: { savedObjectId: id } },
});
return { id };
} else {
// update an existing wizard from another app
application.navigateToApp(originatingApp);
}
}
// Update URL
if (id !== visualizationIdFromUrl) {
history.push({
...history.location,
pathname: `${EDIT_PATH}/${id}`,
});
}
dispatch(setEditorState({ state: 'clean' }));
} else {
// reset title if save not successful
savedWizardVis.title = currentTitle;
}
// Even if id='', which it will be for a duplicate title warning, we still want to return it, to avoid closing the modal
return { id };
} catch (error: any) {
// eslint-disable-next-line no-console
console.error(error);
toastNotifications.addDanger({
title: i18n.translate('wizard.topNavMenu.saveVisualization.failureNotificationText', {
defaultMessage: `Error on saving '{visTitle}'`,
values: {
visTitle: newTitle,
},
}),
text: error.message,
'data-test-subj': 'saveVisualizationError',
});
// reset title if save not successful
savedWizardVis.title = currentTitle;
return { error };
}
};
return onSave;
};