2022-06-21 17:19:12 +05:30
< script >
2022-08-27 11:52:29 +05:30
import { GlButton , GlModalDirective , GlModal , GlSprintf } from '@gitlab/ui' ;
import { createAlert } from '~/flash' ;
import { _ _ , s _ _ , n _ _ , sprintf } from '~/locale' ;
2022-06-21 17:19:12 +05:30
import checkedRunnerIdsQuery from '../graphql/list/checked_runner_ids.query.graphql' ;
2022-08-27 11:52:29 +05:30
import BulkRunnerDelete from '../graphql/list/bulk_runner_delete.mutation.graphql' ;
import { RUNNER _TYPENAME } from '../constants' ;
2022-06-21 17:19:12 +05:30
export default {
components : {
GlButton ,
2022-08-27 11:52:29 +05:30
GlModal ,
2022-06-21 17:19:12 +05:30
GlSprintf ,
} ,
directives : {
GlModal : GlModalDirective ,
} ,
inject : [ 'localMutations' ] ,
2022-08-27 11:52:29 +05:30
props : {
runners : {
type : Array ,
default : ( ) => [ ] ,
required : false ,
} ,
} ,
2022-06-21 17:19:12 +05:30
data ( ) {
return {
2022-08-27 11:52:29 +05:30
isDeleting : false ,
2022-06-21 17:19:12 +05:30
checkedRunnerIds : [ ] ,
} ;
} ,
apollo : {
checkedRunnerIds : {
query : checkedRunnerIdsQuery ,
} ,
} ,
computed : {
2022-08-27 11:52:29 +05:30
currentCheckedRunnerIds ( ) {
return this . runners
. map ( ( { id } ) => id )
. filter ( ( id ) => this . checkedRunnerIds . indexOf ( id ) >= 0 ) ;
} ,
2022-06-21 17:19:12 +05:30
checkedCount ( ) {
2022-08-27 11:52:29 +05:30
return this . currentCheckedRunnerIds . length || 0 ;
2022-06-21 17:19:12 +05:30
} ,
bannerMessage ( ) {
return sprintf (
n _ _ (
'Runners|%{strongStart}%{count}%{strongEnd} runner selected' ,
'Runners|%{strongStart}%{count}%{strongEnd} runners selected' ,
this . checkedCount ,
) ,
{
count : this . checkedCount ,
} ,
) ;
} ,
modalTitle ( ) {
return n _ _ ( 'Runners|Delete %d runner' , 'Runners|Delete %d runners' , this . checkedCount ) ;
} ,
2022-08-27 11:52:29 +05:30
modalActionPrimary ( ) {
return {
text : n _ _ (
'Runners|Permanently delete %d runner' ,
'Runners|Permanently delete %d runners' ,
this . checkedCount ,
) ,
attributes : {
loading : this . isDeleting ,
variant : 'danger' ,
} ,
} ;
} ,
modalActionCancel ( ) {
return {
text : _ _ ( 'Cancel' ) ,
attributes : {
loading : this . isDeleting ,
} ,
} ;
} ,
modalMessage ( ) {
2022-06-21 17:19:12 +05:30
return sprintf (
n _ _ (
'Runners|%{strongStart}%{count}%{strongEnd} runner will be permanently deleted and no longer available for projects or groups in the instance. Are you sure you want to continue?' ,
'Runners|%{strongStart}%{count}%{strongEnd} runners will be permanently deleted and no longer available for projects or groups in the instance. Are you sure you want to continue?' ,
this . checkedCount ,
) ,
2022-08-27 11:52:29 +05:30
{ count : this . checkedCount } ,
2022-06-21 17:19:12 +05:30
) ;
} ,
2022-08-27 11:52:29 +05:30
} ,
methods : {
toastConfirmationMessage ( deletedCount ) {
2022-06-21 17:19:12 +05:30
return n _ _ (
2022-08-27 11:52:29 +05:30
'Runners|%d selected runner deleted' ,
'Runners|%d selected runners deleted' ,
deletedCount ,
2022-06-21 17:19:12 +05:30
) ;
} ,
onClearChecked ( ) {
this . localMutations . clearChecked ( ) ;
} ,
2022-08-27 11:52:29 +05:30
async onConfirmDelete ( e ) {
this . isDeleting = true ;
e . preventDefault ( ) ; // don't close modal until deletion is complete
try {
await this . $apollo . mutate ( {
mutation : BulkRunnerDelete ,
variables : {
input : {
ids : this . currentCheckedRunnerIds ,
} ,
} ,
update : ( cache , { data } ) => {
const { errors , deletedIds } = data . bulkRunnerDelete ;
if ( errors ? . length ) {
this . onError ( new Error ( errors . join ( ' ' ) ) ) ;
this . $refs . modal . hide ( ) ;
return ;
}
this . $emit ( 'deleted' , {
message : this . toastConfirmationMessage ( deletedIds . length ) ,
} ) ;
2022-06-21 17:19:12 +05:30
2022-08-27 11:52:29 +05:30
// Clean up
// Remove deleted runners from the cache
deletedIds . forEach ( ( id ) => {
const cacheId = cache . identify ( { _ _typename : RUNNER _TYPENAME , id } ) ;
cache . evict ( { id : cacheId } ) ;
} ) ;
cache . gc ( ) ;
this . $refs . modal . hide ( ) ;
} ,
} ) ;
} catch ( error ) {
this . onError ( error ) ;
} finally {
this . isDeleting = false ;
2022-06-21 17:19:12 +05:30
}
2022-08-27 11:52:29 +05:30
} ,
onError ( error ) {
createAlert ( {
message : s _ _ (
'Runners|Something went wrong while deleting. Please refresh the page to try again.' ,
) ,
captureError : true ,
error ,
} ) ;
} ,
2022-06-21 17:19:12 +05:30
} ,
2022-08-27 11:52:29 +05:30
BULK _DELETE _MODAL _ID : 'bulk-delete-modal' ,
2022-06-21 17:19:12 +05:30
} ;
< / script >
< template >
< div v-if = "checkedCount" class="gl-my-4 gl-p-4 gl-border-1 gl-border-solid gl-border-gray-100" >
< div class = "gl-display-flex gl-align-items-center" >
< div >
< gl-sprintf :message = "bannerMessage" >
< template # strong = "{ content }" >
< strong > { { content } } < / strong >
< / template >
< / gl-sprintf >
< / div >
< div class = "gl-ml-auto" >
2022-08-27 11:52:29 +05:30
< gl-button variant = "default" @click ="onClearChecked" > {{
2022-06-21 17:19:12 +05:30
s _ _ ( 'Runners|Clear selection' )
} } < / gl-button >
2022-08-27 11:52:29 +05:30
< gl-button v -gl -modal = " $ options.BULK_DELETE_MODAL_ID " variant = "danger" > { {
2022-06-21 17:19:12 +05:30
s _ _ ( 'Runners|Delete selected' )
} } < / gl-button >
< / div >
< / div >
2022-08-27 11:52:29 +05:30
< gl-modal
ref = "modal"
size = "sm"
: modal - id = "$options.BULK_DELETE_MODAL_ID"
: title = "modalTitle"
: action - primary = "modalActionPrimary"
: action - cancel = "modalActionCancel"
@ primary = "onConfirmDelete"
>
< gl-sprintf :message = "modalMessage" >
< template # strong = "{ content }" >
< strong > { { content } } < / strong >
< / template >
< / gl-sprintf >
< / gl-modal >
2022-06-21 17:19:12 +05:30
< / div >
< / template >