Skip to content

Commit 2c86460

Browse files
ywzheng1Clarity-89
andauthored
BulkDeleteProvisionedResource: Move progress bar into a second step (#108417)
* Move progress bar into a second step --------- Co-authored-by: Alex Khomenko <[email protected]>
1 parent 0dbede9 commit 2c86460

File tree

4 files changed

+97
-59
lines changed

4 files changed

+97
-59
lines changed

public/app/features/browse-dashboards/components/BulkActionProgress.tsx

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Trans } from '@grafana/i18n';
2-
import { Box, Text } from '@grafana/ui';
2+
import { Box, Icon, Text, Stack } from '@grafana/ui';
33
import ProgressBar from 'app/features/provisioning/Shared/ProgressBar';
44

55
export interface ProgressState {
@@ -13,16 +13,19 @@ export function BulkActionProgress({ progress }: { progress: ProgressState }) {
1313

1414
return (
1515
<Box>
16-
<Text>
17-
<Trans
18-
i18nKey="browse-dashboards.bulk-move-resources-form.progress"
19-
defaults="Progress: {{current}} of {{total}}"
20-
values={{ current: progress.current, total: progress.total }}
21-
/>
22-
</Text>
23-
<ProgressBar progress={progressPercentage} />
16+
<Stack direction="row" alignItems="center">
17+
<Text>
18+
<Trans
19+
i18nKey="browse-dashboards.bulk-move-resources-form.progress"
20+
defaults="Progress: {{current}} of {{total}}"
21+
values={{ current: progress.current, total: progress.total }}
22+
/>
23+
</Text>
24+
<Icon name="spinner" className="fa-spin" size="sm" />
25+
</Stack>
26+
<ProgressBar progress={progressPercentage} topBottomSpacing={1} />
2427
<Text variant="bodySmall" color="secondary">
25-
{progress.item}
28+
<Trans i18nKey="browse-dashboards.bulk-move-resources-form.deleting" defaults="Deleting:" /> {progress.item}
2629
</Text>
2730
</Box>
2831
);

public/app/features/browse-dashboards/components/BulkDeleteProvisionedResource.tsx

Lines changed: 74 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@ import { useState } from 'react';
22
import { FormProvider, useForm } from 'react-hook-form';
33
import { useNavigate } from 'react-router-dom-v5-compat';
44

5-
import { AppEvents } from '@grafana/data';
65
import { Trans, t } from '@grafana/i18n';
7-
import { getAppEvents } from '@grafana/runtime';
8-
import { Box, Button, Stack } from '@grafana/ui';
6+
import { Alert, Box, Button, Stack } from '@grafana/ui';
97
import {
108
DeleteRepositoryFilesWithPathApiArg,
119
DeleteRepositoryFilesWithPathApiResponse,
@@ -56,6 +54,11 @@ type BulkSuccessResponse = Array<{
5654
data: DeleteRepositoryFilesWithPathApiResponse;
5755
}>;
5856

57+
type MoveResultSuccessState = {
58+
allSuccess: boolean;
59+
repoUrl?: string;
60+
};
61+
5962
function FormContent({
6063
initialValues,
6164
selectedItems,
@@ -65,10 +68,17 @@ function FormContent({
6568
isGitHub,
6669
onDismiss,
6770
}: FormProps) {
68-
const [deleteRepoFile, request] = useDeleteRepositoryFilesWithPathMutation();
71+
// States
6972
const [progress, setProgress] = useState<ProgressState | null>(null);
7073
const [failureResults, setFailureResults] = useState<MoveResultFailed[] | undefined>();
74+
const [successState, setSuccessState] = useState<MoveResultSuccessState>({
75+
allSuccess: false,
76+
repoUrl: '',
77+
});
78+
const [hasSubmitted, setHasSubmitted] = useState(false);
7179

80+
// Hooks
81+
const [deleteRepoFile, request] = useDeleteRepositoryFilesWithPathMutation();
7282
const methods = useForm<BulkDeleteFormData>({ defaultValues: initialValues });
7383
const childrenByParentUID = useChildrenByParentUIDState();
7484
const rootItems = useSelector(rootItemsSelector);
@@ -84,21 +94,11 @@ function FormContent({
8494
return isFolder ? `${folderPath}/${item.title}/` : fetchProvisionedDashboardPath(uid);
8595
};
8696

87-
const handleSuccess = (successes: BulkSuccessResponse) => {
88-
getAppEvents().publish({
89-
type: AppEvents.alertSuccess.name,
90-
payload: [
91-
t('browse-dashboards.bulk-delete-resources-form.api-success', `Successfully deleted {{count}} items`, {
92-
count: successes.length,
93-
}),
94-
],
95-
});
96-
97+
const handleSuccess = () => {
9798
if (workflow === 'branch') {
9899
onDismiss?.();
99-
const repoUrl = successes[0].data.urls?.repositoryURL;
100-
if (repoUrl) {
101-
navigate({ search: `?repo_url=${encodeURIComponent(repoUrl)}` });
100+
if (successState.repoUrl) {
101+
navigate({ search: `?repo_url=${encodeURIComponent(successState.repoUrl)}` });
102102
return;
103103
}
104104
window.location.reload();
@@ -110,6 +110,7 @@ function FormContent({
110110

111111
const handleSubmitForm = async (data: BulkDeleteFormData) => {
112112
setFailureResults(undefined);
113+
setHasSubmitted(true);
113114

114115
const targets = collectSelectedItems(selectedItems, childrenByParentUID, rootItems?.items || []);
115116

@@ -175,12 +176,43 @@ function FormContent({
175176
setProgress(null);
176177

177178
if (successes.length > 0 && failures.length === 0) {
178-
handleSuccess(successes);
179+
// handleSuccess(successes);
180+
setSuccessState({
181+
allSuccess: true,
182+
repoUrl: successes[0].data.urls?.repositoryURL,
183+
});
179184
} else if (failures.length > 0) {
180185
setFailureResults(failures);
181186
}
182187
};
183188

189+
const getPostSubmitContent = () => {
190+
if (progress) {
191+
return <BulkActionProgress progress={progress} />;
192+
}
193+
194+
if (successState.allSuccess) {
195+
return (
196+
<>
197+
<Alert severity="success" title={t('browse-dashboards.bulk-delete-resources-form.progress-title', 'Success')}>
198+
<Trans i18nKey="browse-dashboards.bulk-delete-resources-form.success-message">
199+
All resources have been deleted successfully.
200+
</Trans>
201+
</Alert>
202+
<Stack gap={2}>
203+
<Button onClick={() => handleSuccess()}>
204+
<Trans i18nKey="browse-dashboards.bulk-delete-resources-form.button-done">Done</Trans>
205+
</Button>
206+
</Stack>
207+
</>
208+
);
209+
} else if (failureResults) {
210+
return <BulkActionFailureBanner result={failureResults} onDismiss={() => setFailureResults(undefined)} />;
211+
}
212+
213+
return null;
214+
};
215+
184216
return (
185217
<FormProvider {...methods}>
186218
<form onSubmit={handleSubmit(handleSubmitForm)}>
@@ -192,31 +224,31 @@ function FormContent({
192224
<DescendantCount selectedItems={{ ...selectedItems, panel: {}, $all: false }} />
193225
</Box>
194226

195-
{failureResults && (
196-
<BulkActionFailureBanner result={failureResults} onDismiss={() => setFailureResults(undefined)} />
227+
{hasSubmitted ? (
228+
getPostSubmitContent()
229+
) : (
230+
<>
231+
<ResourceEditFormSharedFields
232+
resourceType="folder"
233+
isNew={false}
234+
workflow={workflow}
235+
workflowOptions={workflowOptions}
236+
isGitHub={isGitHub}
237+
hidePath
238+
/>
239+
240+
<Stack gap={2}>
241+
<Button type="submit" disabled={request.isLoading || !!failureResults} variant="destructive">
242+
{request.isLoading
243+
? t('browse-dashboards.bulk-delete-resources-form.button-deleting', 'Deleting...')
244+
: t('browse-dashboards.bulk-delete-resources-form.button-delete', 'Delete')}
245+
</Button>
246+
<Button variant="secondary" fill="outline" onClick={onDismiss} disabled={request.isLoading}>
247+
<Trans i18nKey="browse-dashboards.bulk-delete-resources-form.button-cancel">Cancel</Trans>
248+
</Button>
249+
</Stack>
250+
</>
197251
)}
198-
199-
{progress && <BulkActionProgress progress={progress} />}
200-
201-
<ResourceEditFormSharedFields
202-
resourceType="folder"
203-
isNew={false}
204-
workflow={workflow}
205-
workflowOptions={workflowOptions}
206-
isGitHub={isGitHub}
207-
hidePath
208-
/>
209-
210-
<Stack gap={2}>
211-
<Button type="submit" disabled={request.isLoading || !!failureResults} variant="destructive">
212-
{request.isLoading
213-
? t('browse-dashboards.bulk-delete-resources-form.button-deleting', 'Deleting...')
214-
: t('browse-dashboards.bulk-delete-resources-form.button-delete', 'Delete')}
215-
</Button>
216-
<Button variant="secondary" fill="outline" onClick={onDismiss}>
217-
<Trans i18nKey="browse-dashboards.bulk-delete-resources-form.button-cancel">Cancel</Trans>
218-
</Button>
219-
</Stack>
220252
</Stack>
221253
</form>
222254
</FormProvider>

public/app/features/provisioning/Shared/ProgressBar.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import { useStyles2 } from '@grafana/ui';
55

66
interface ProgressBarProps {
77
progress?: number;
8+
topBottomSpacing?: number;
89
}
9-
const ProgressBar = ({ progress }: ProgressBarProps) => {
10-
const styles = useStyles2(getStyles);
10+
const ProgressBar = ({ progress, topBottomSpacing }: ProgressBarProps) => {
11+
const styles = useStyles2(getStyles, topBottomSpacing);
1112

1213
if (progress === undefined) {
1314
return null;
@@ -20,14 +21,14 @@ const ProgressBar = ({ progress }: ProgressBarProps) => {
2021
);
2122
};
2223

23-
const getStyles = (theme: GrafanaTheme2) => ({
24+
const getStyles = (theme: GrafanaTheme2, topBottomSpacing = 2) => ({
2425
container: css({
2526
height: '10px',
2627
width: '400px',
2728
backgroundColor: theme.colors.background.secondary,
2829
borderRadius: theme.shape.radius.pill,
2930
overflow: 'hidden',
30-
margin: theme.spacing(2, 0),
31+
margin: theme.spacing(topBottomSpacing, 0),
3132
}),
3233
filler: css({
3334
height: '100%',

public/locales/en-US/grafana.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3516,15 +3516,17 @@
35163516
"failed-alert_other": "{{count}} items failed"
35173517
},
35183518
"bulk-delete-resources-form": {
3519-
"api-success_one": "Successfully deleted {{count}} item",
3520-
"api-success_other": "Successfully deleted {{count}} items",
35213519
"button-cancel": "Cancel",
35223520
"button-delete": "Delete",
35233521
"button-deleting": "Deleting...",
3522+
"button-done": "Done",
35243523
"delete-warning": "This will delete selected folders and their descendants. In total, this will affect:",
3525-
"error-path-not-found": "Path not found"
3524+
"error-path-not-found": "Path not found",
3525+
"progress-title": "Success",
3526+
"success-message": "All resources have been deleted successfully."
35263527
},
35273528
"bulk-move-resources-form": {
3529+
"deleting": "Deleting:",
35283530
"progress": "Progress: {{current}} of {{total}}"
35293531
},
35303532
"counts": {

0 commit comments

Comments
 (0)