1
- import React , { useEffect , useState } from 'react'
2
- import { CButton , CCallout , CCol , CForm , CFormLabel , CRow , CSpinner , CTooltip } from '@coreui/react'
3
- import useQuery from 'src/hooks/useQuery'
4
- import { useSelector } from 'react-redux'
1
+ import React , { useCallback , useEffect , useState } from 'react'
2
+ import { CButton , CCallout , CCol , CForm , CRow , CSpinner , CTooltip } from '@coreui/react'
3
+ import { useDispatch , useSelector } from 'react-redux'
5
4
import { Field , Form , FormSpy } from 'react-final-form'
6
5
import {
7
6
RFFCFormInput ,
@@ -15,20 +14,45 @@ import {
15
14
useLazyGenericPostRequestQuery ,
16
15
} from 'src/store/api/app'
17
16
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
18
- import { faCircleNotch , faEdit , faEye } from '@fortawesome/free-solid-svg-icons'
17
+ import { faCircleNotch , faEdit } from '@fortawesome/free-solid-svg-icons'
19
18
import { CippContentCard , CippPage , CippPageList } from 'src/components/layout'
20
- import { password } from 'src/validators'
21
19
import { cellBadgeFormatter , cellDateFormatter } from 'src/components/tables'
22
20
import { CellTip , cellGenericFormatter } from 'src/components/tables/CellGenericFormat'
23
21
import DatePicker from 'react-datepicker'
24
22
import 'react-datepicker/dist/react-datepicker.css'
25
- import TenantListSelector from 'src/components/utilities/TenantListSelector'
26
23
import { ModalService , TenantSelector } from 'src/components/utilities'
27
24
import CippCodeOffCanvas from 'src/components/utilities/CippCodeOffcanvas'
28
25
import arrayMutators from 'final-form-arrays'
26
+ import { useListTenantsQuery } from 'src/store/api/tenants'
27
+ import { setCurrentTenant } from 'src/store/features/app'
28
+ import { useNavigate , useSearchParams } from 'react-router-dom'
29
+ import { queryString } from 'src/helpers'
29
30
30
31
const Scheduler = ( ) => {
32
+ const [ initialValues , setInitialValues ] = useState ( { } )
33
+ const [ selectedTenant , setSelectedTenant ] = useState ( '' )
31
34
const [ ExecuteGetRequest , getResults ] = useLazyGenericGetRequestQuery ( )
35
+ const { data : tenants , isSuccess : tenantSuccess } = useListTenantsQuery ( {
36
+ showAllTenantSelector : true ,
37
+ } )
38
+ const dispatch = useDispatch ( )
39
+ const navigate = useNavigate ( )
40
+ const [ searchParams , setSearchParams ] = useSearchParams ( )
41
+
42
+ const updateSearchParams = useCallback (
43
+ ( params ) => {
44
+ navigate ( `${ queryString ( params ) } ` , { replace : true } )
45
+ } ,
46
+ [ navigate ] ,
47
+ )
48
+
49
+ const recurrenceOptions = [
50
+ { value : '0' , name : 'Only once' } ,
51
+ { value : '1' , name : 'Every 1 day' } ,
52
+ { value : '7' , name : 'Every 7 days' } ,
53
+ { value : '30' , name : 'Every 30 days' } ,
54
+ { value : '365' , name : 'Every 365 days' } ,
55
+ ]
32
56
33
57
const Offcanvas = ( row , rowIndex , formatExtraData ) => {
34
58
const [ ocVisible , setOCVisible ] = useState ( false )
@@ -59,6 +83,11 @@ const Scheduler = () => {
59
83
< FontAwesomeIcon icon = { 'eye' } href = "" />
60
84
</ CButton >
61
85
</ CTooltip >
86
+ < CTooltip content = "Copy Task" >
87
+ < CButton size = "sm" color = "warning" variant = "ghost" onClick = { ( ) => onCopy ( row ) } >
88
+ < FontAwesomeIcon icon = "copy" href = "" />
89
+ </ CButton >
90
+ </ CTooltip >
62
91
< CTooltip content = "Delete task" >
63
92
< CButton
64
93
onClick = { ( ) =>
@@ -116,6 +145,75 @@ const Scheduler = () => {
116
145
} )
117
146
}
118
147
148
+ const onCopy = ( row ) => {
149
+ // Get post execution options
150
+ var postExecActions = row . PostExecution . split ( ',' )
151
+ // Get recurrence object
152
+ var recurrence = recurrenceOptions . filter ( ( rec ) => rec . value === row . Recurrence ) [ 0 ]
153
+
154
+ // Convert parameters into form object
155
+ var parameters = { }
156
+ Object . keys ( row ?. Parameters ) . forEach ( ( key ) => {
157
+ if ( typeof row ?. Parameters [ key ] === 'object' ) {
158
+ var nestedParamList = [ ]
159
+ Object . keys ( row ?. Parameters [ key ] ) . forEach ( ( nestedKey ) => {
160
+ console . log ( nestedKey )
161
+ nestedParamList . push ( {
162
+ Key : nestedKey ,
163
+ Value : row ?. Parameters [ key ] [ nestedKey ] ,
164
+ } )
165
+ } )
166
+ parameters [ key ] = nestedParamList
167
+ } else {
168
+ parameters [ key ] = row ?. Parameters [ key ]
169
+ }
170
+ } )
171
+
172
+ // Convert additional properties into form object
173
+ var additional = [ ]
174
+ var additionalProps = JSON . parse ( row ?. AdditionalProperties )
175
+ Object . keys ( additionalProps ) . forEach ( ( key ) => {
176
+ console . log ( key )
177
+ additional . push ( {
178
+ Key : key ,
179
+ Value : additionalProps [ key ] ,
180
+ } )
181
+ } )
182
+
183
+ // Set initial values
184
+ var formValues = {
185
+ taskName : row . Name ,
186
+ command : { label : row . Command , value : row . Command } ,
187
+ Recurrence : { label : recurrence . name , value : recurrence . value } ,
188
+ additional : additional ,
189
+ parameters : parameters ,
190
+ webhook : postExecActions . includes ( 'Webhook' ) ,
191
+ email : postExecActions . includes ( 'Email' ) ,
192
+ psa : postExecActions . includes ( 'PSA' ) ,
193
+ }
194
+ setInitialValues ( formValues )
195
+ setSelectedTenant ( row . Tenant )
196
+ }
197
+
198
+ // Update tenant selector on copy
199
+ useEffect ( ( ) => {
200
+ if ( selectedTenant !== '' && tenantSuccess ) {
201
+ const customerId = searchParams . get ( 'customerId' )
202
+ const tableFilter = searchParams . get ( 'tableFilter' )
203
+ var newSearchParams = { }
204
+ if ( tableFilter ) {
205
+ newSearchParams . tableFilter = tableFilter
206
+ }
207
+ const tenant = tenants . filter ( ( t ) => t . defaultDomainName === selectedTenant )
208
+ if ( tenant . length > 0 ) {
209
+ dispatch ( setCurrentTenant ( { tenant : tenant [ 0 ] } ) )
210
+ newSearchParams . customerId = tenant [ 0 ] ?. customerId
211
+ updateSearchParams ( newSearchParams )
212
+ setSelectedTenant ( '' )
213
+ }
214
+ }
215
+ } , [ selectedTenant , tenantSuccess , tenants , dispatch , searchParams , updateSearchParams ] )
216
+
119
217
const columns = [
120
218
{
121
219
name : 'Name' ,
@@ -183,7 +281,7 @@ const Scheduler = () => {
183
281
{
184
282
name : 'Actions' ,
185
283
cell : Offcanvas ,
186
- maxWidth : '80px ' ,
284
+ maxWidth : '100px ' ,
187
285
} ,
188
286
]
189
287
return (
@@ -197,8 +295,7 @@ const Scheduler = () => {
197
295
mutators = { {
198
296
...arrayMutators ,
199
297
} }
200
- initialValues = { { taskName } }
201
- initialValuesEqual = { ( ) => true }
298
+ initialValues = { { ...initialValues } }
202
299
render = { ( { handleSubmit, submitting, values } ) => {
203
300
return (
204
301
< CForm onSubmit = { handleSubmit } >
@@ -235,13 +332,7 @@ const Scheduler = () => {
235
332
< CRow className = "mb-3" >
236
333
< CCol >
237
334
< RFFSelectSearch
238
- values = { [
239
- { value : '0' , name : 'Only once' } ,
240
- { value : '1' , name : 'Every 1 day' } ,
241
- { value : '7' , name : 'Every 7 days' } ,
242
- { value : '30' , name : 'Every 30 days' } ,
243
- { value : '365' , name : 'Every 365 days' } ,
244
- ] }
335
+ values = { recurrenceOptions }
245
336
name = "Recurrence"
246
337
placeholder = "Select a recurrence"
247
338
label = "Recurrence"
0 commit comments