Skip to content

Commit f37310f

Browse files
authored
Merge pull request #2366 from JohnDuprey/dev
Copy Schedule
2 parents 724f76a + 704eab6 commit f37310f

File tree

1 file changed

+108
-17
lines changed

1 file changed

+108
-17
lines changed

src/views/cipp/Scheduler.jsx

Lines changed: 108 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
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'
54
import { Field, Form, FormSpy } from 'react-final-form'
65
import {
76
RFFCFormInput,
@@ -15,20 +14,45 @@ import {
1514
useLazyGenericPostRequestQuery,
1615
} from 'src/store/api/app'
1716
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'
1918
import { CippContentCard, CippPage, CippPageList } from 'src/components/layout'
20-
import { password } from 'src/validators'
2119
import { cellBadgeFormatter, cellDateFormatter } from 'src/components/tables'
2220
import { CellTip, cellGenericFormatter } from 'src/components/tables/CellGenericFormat'
2321
import DatePicker from 'react-datepicker'
2422
import 'react-datepicker/dist/react-datepicker.css'
25-
import TenantListSelector from 'src/components/utilities/TenantListSelector'
2623
import { ModalService, TenantSelector } from 'src/components/utilities'
2724
import CippCodeOffCanvas from 'src/components/utilities/CippCodeOffcanvas'
2825
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'
2930

3031
const Scheduler = () => {
32+
const [initialValues, setInitialValues] = useState({})
33+
const [selectedTenant, setSelectedTenant] = useState('')
3134
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+
]
3256

3357
const Offcanvas = (row, rowIndex, formatExtraData) => {
3458
const [ocVisible, setOCVisible] = useState(false)
@@ -59,6 +83,11 @@ const Scheduler = () => {
5983
<FontAwesomeIcon icon={'eye'} href="" />
6084
</CButton>
6185
</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>
6291
<CTooltip content="Delete task">
6392
<CButton
6493
onClick={() =>
@@ -116,6 +145,75 @@ const Scheduler = () => {
116145
})
117146
}
118147

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+
119217
const columns = [
120218
{
121219
name: 'Name',
@@ -183,7 +281,7 @@ const Scheduler = () => {
183281
{
184282
name: 'Actions',
185283
cell: Offcanvas,
186-
maxWidth: '80px',
284+
maxWidth: '100px',
187285
},
188286
]
189287
return (
@@ -197,8 +295,7 @@ const Scheduler = () => {
197295
mutators={{
198296
...arrayMutators,
199297
}}
200-
initialValues={{ taskName }}
201-
initialValuesEqual={() => true}
298+
initialValues={{ ...initialValues }}
202299
render={({ handleSubmit, submitting, values }) => {
203300
return (
204301
<CForm onSubmit={handleSubmit}>
@@ -235,13 +332,7 @@ const Scheduler = () => {
235332
<CRow className="mb-3">
236333
<CCol>
237334
<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}
245336
name="Recurrence"
246337
placeholder="Select a recurrence"
247338
label="Recurrence"

0 commit comments

Comments
 (0)