@@ -12,6 +12,7 @@ import {
1212} from '@patternfly/react-core/deprecated' ;
1313import { translate as __ } from 'foremanReact/common/I18n' ;
1414import { foremanUrl } from 'foremanReact/common/helpers' ;
15+ import { usePermissions } from 'foremanReact/common/hooks/Permissions/permissionHooks' ;
1516import { get } from 'foremanReact/redux/API' ;
1617import {
1718 cancelJob ,
@@ -23,10 +24,7 @@ import {
2324 GET_REPORT_TEMPLATES ,
2425 GET_REPORT_TEMPLATE_INPUTS ,
2526} from './JobInvocationConstants' ;
26- import {
27- selectTaskCancelable ,
28- selectHasPermission ,
29- } from './JobInvocationSelectors' ;
27+ import { selectTaskCancelable } from './JobInvocationSelectors' ;
3028
3129const JobInvocationToolbarButtons = ( { jobId, data } ) => {
3230 const { succeeded, failed, task, recurrence, permissions } = data ;
@@ -38,8 +36,11 @@ const JobInvocationToolbarButtons = ({ jobId, data }) => {
3836 ? permissions . edit_recurring_logics
3937 : false ;
4038 const isTaskCancelable = useSelector ( selectTaskCancelable ) ;
41- const useHasPermission = permissionRequired =>
42- useSelector ( selectHasPermission ( permissionRequired ) ) ;
39+ const canCreateJobInvocations = usePermissions ( [ 'create_job_invocations' ] ) ;
40+ const canCancelJobInvocations = usePermissions ( [ 'cancel_job_invocations' ] ) ;
41+ const canGenerateReportTemplates = usePermissions ( [
42+ 'generate_report_templates' ,
43+ ] ) ;
4344 const [ isActionOpen , setIsActionOpen ] = useState ( false ) ;
4445 const [ reportTemplateJobId , setReportTemplateJobId ] = useState ( undefined ) ;
4546 const [ templateInputId , setTemplateInputId ] = useState ( undefined ) ;
@@ -60,39 +61,55 @@ const JobInvocationToolbarButtons = ({ jobId, data }) => {
6061 } ;
6162
6263 useEffect ( ( ) => {
64+ let isMounted = true ;
6365 dispatch (
6466 get ( {
6567 key : GET_REPORT_TEMPLATES ,
6668 url : '/api/report_templates' ,
6769 handleSuccess : ( { data : { results } } ) => {
68- setReportTemplateJobId (
69- results . find ( result => result . name === 'Job - Invocation Report' )
70- ?. id
71- ) ;
70+ if ( isMounted ) {
71+ setReportTemplateJobId (
72+ results . find ( result => result . name === 'Job - Invocation Report' )
73+ ?. id
74+ ) ;
75+ }
7276 } ,
7377 handleError : ( ) => {
74- setReportTemplateJobId ( undefined ) ;
78+ if ( isMounted ) {
79+ setReportTemplateJobId ( undefined ) ;
80+ }
7581 } ,
7682 } )
7783 ) ;
84+ return ( ) => {
85+ isMounted = false ;
86+ } ;
7887 } , [ dispatch ] ) ;
7988 useEffect ( ( ) => {
89+ let isMounted = true ;
8090 if ( reportTemplateJobId !== undefined ) {
8191 dispatch (
8292 get ( {
8393 key : GET_REPORT_TEMPLATE_INPUTS ,
8494 url : `/api/templates/${ reportTemplateJobId } /template_inputs` ,
8595 handleSuccess : ( { data : { results } } ) => {
86- setTemplateInputId (
87- results . find ( result => result . name === 'job_id' ) ?. id
88- ) ;
96+ if ( isMounted ) {
97+ setTemplateInputId (
98+ results . find ( result => result . name === 'job_id' ) ?. id
99+ ) ;
100+ }
89101 } ,
90102 handleError : ( ) => {
91- setTemplateInputId ( undefined ) ;
103+ if ( isMounted ) {
104+ setTemplateInputId ( undefined ) ;
105+ }
92106 } ,
93107 } )
94108 ) ;
95109 }
110+ return ( ) => {
111+ isMounted = false ;
112+ } ;
96113 } , [ dispatch , reportTemplateJobId ] ) ;
97114
98115 const recurrenceDropdownItems = recurrence
@@ -136,9 +153,7 @@ const JobInvocationToolbarButtons = ({ jobId, data }) => {
136153 ouiaId = "rerun-succeeded-dropdown-item"
137154 href = { foremanUrl ( `/job_invocations/${ jobId } /rerun?succeeded_only=1` ) }
138155 key = "rerun-succeeded"
139- isDisabled = {
140- ! useHasPermission ( 'create_job_invocations' ) || ! ( succeeded > 0 )
141- }
156+ isDisabled = { ! canCreateJobInvocations || ! ( succeeded > 0 ) }
142157 description = "Rerun job on successful hosts"
143158 >
144159 { __ ( 'Rerun successful' ) }
@@ -147,7 +162,7 @@ const JobInvocationToolbarButtons = ({ jobId, data }) => {
147162 ouiaId = "rerun-failed-dropdown-item"
148163 href = { foremanUrl ( `/job_invocations/${ jobId } /rerun?failed_only=1` ) }
149164 key = "rerun-failed"
150- isDisabled = { ! useHasPermission ( 'create_job_invocations' ) || ! ( failed > 0 ) }
165+ isDisabled = { ! canCreateJobInvocations || ! ( failed > 0 ) }
151166 description = "Rerun job on failed hosts"
152167 >
153168 { __ ( 'Rerun failed' ) }
@@ -167,9 +182,7 @@ const JobInvocationToolbarButtons = ({ jobId, data }) => {
167182 onClick = { ( ) => dispatch ( cancelJob ( jobId , false ) ) }
168183 key = "cancel"
169184 component = "button"
170- isDisabled = {
171- ! useHasPermission ( 'cancel_job_invocations' ) || ! isTaskCancelable
172- }
185+ isDisabled = { ! canCancelJobInvocations || ! isTaskCancelable }
173186 description = "Cancel job gracefully"
174187 >
175188 { __ ( 'Cancel' ) }
@@ -179,9 +192,7 @@ const JobInvocationToolbarButtons = ({ jobId, data }) => {
179192 onClick = { ( ) => dispatch ( cancelJob ( jobId , true ) ) }
180193 key = "abort"
181194 component = "button"
182- isDisabled = {
183- ! useHasPermission ( 'cancel_job_invocations' ) || ! isTaskCancelable
184- }
195+ isDisabled = { ! canCancelJobInvocations || ! isTaskCancelable }
185196 description = "Cancel job immediately"
186197 >
187198 { __ ( 'Abort' ) }
@@ -211,7 +222,7 @@ const JobInvocationToolbarButtons = ({ jobId, data }) => {
211222 ) }
212223 variant = "secondary"
213224 isDisabled = {
214- ! useHasPermission ( 'generate_report_templates' ) ||
225+ ! canGenerateReportTemplates ||
215226 task ?. state === STATUS . PENDING ||
216227 templateInputId === undefined
217228 }
@@ -235,7 +246,7 @@ const JobInvocationToolbarButtons = ({ jobId, data }) => {
235246 key = "rerun"
236247 href = { foremanUrl ( `/job_invocations/${ jobId } /rerun` ) }
237248 variant = "control"
238- isDisabled = { ! useHasPermission ( 'create_job_invocations' ) }
249+ isDisabled = { ! canCreateJobInvocations }
239250 >
240251 { __ ( `Rerun all` ) }
241252 </ Button > ,
0 commit comments