@@ -2,10 +2,8 @@ import { useState } from 'react';
22import { FormProvider , useForm } from 'react-hook-form' ;
33import { useNavigate } from 'react-router-dom-v5-compat' ;
44
5- import { AppEvents } from '@grafana/data' ;
65import { 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' ;
97import {
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+
5962function 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 >
0 commit comments