Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
232 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
app/javascript/components/copy-dashboard-form/copy-dashboard-form.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { Grid } from 'patternfly-react'; | ||
import MiqFormRenderer from '../../forms/data-driven-form'; | ||
import createSchema from './copy-dashboard-form.schema'; | ||
import { http, API } from '../../http_api'; | ||
import handleError from '../../helpers/handle-failure'; | ||
|
||
const CopyDashboardForm = ({ dashboardId }) => { | ||
const [isLoading, setIsLoading] = useState(true); | ||
const [initialValues, setInitialValues] = useState({}); | ||
const [schema, setSchema] = useState({}); | ||
const [options, setOptions] = useState([]); | ||
|
||
const submitValues = (values) => { | ||
miqSparkleOn(); | ||
const data = { | ||
...values, | ||
dashboard_id: dashboardId, | ||
}; | ||
|
||
return http.post(`/report/db_copy/${dashboardId}?button=save`, data, { skipErrors: [400] }) | ||
.then(({ name }) => | ||
miqAjaxButton( | ||
('/report/dashboard_render'), | ||
{ original_name: name, name: values.name, group: options.find(option => option.value === values.group_id).label }, | ||
)) | ||
.catch((e) => { | ||
handleError(e); | ||
miqSparkleOff(); | ||
}); | ||
}; | ||
|
||
const cancelClicked = () => { | ||
miqSparkleOn(); | ||
miqAjaxButton(`/report/db_copy/${dashboardId}?button=cancel`); | ||
}; | ||
|
||
useEffect(() => { | ||
miqSparkleOn(); | ||
Promise.all([API.get('/api/groups?expand=resources'), | ||
http.get(`/report/dashboard_get/${dashboardId}`)]) | ||
.then(([{ resources }, { name, description, owner_id }]) => { | ||
const options = resources.map(({ id, description }) => ({ value: id, label: description })); | ||
setOptions(options); | ||
setInitialValues({ name, description, group_id: owner_id }); | ||
setSchema(createSchema(options, name, dashboardId)); | ||
setIsLoading(false); | ||
miqSparkleOff(); | ||
}) | ||
.catch((e) => { | ||
handleError(e); | ||
miqSparkleOff(); | ||
}); | ||
}, []); | ||
|
||
if (isLoading) return null; | ||
return ( | ||
<Grid fluid> | ||
<MiqFormRenderer | ||
initialValues={initialValues} | ||
schema={schema} | ||
onSubmit={submitValues} | ||
onCancel={cancelClicked} | ||
onReset={() => add_flash(__('All changes have been reset'), 'warn')} | ||
canReset | ||
buttonsLabels={{ | ||
submitLabel: __('Save'), | ||
}} | ||
/> | ||
</Grid> | ||
); | ||
}; | ||
|
||
CopyDashboardForm.propTypes = { | ||
dashboardId: PropTypes.string.isRequired, | ||
}; | ||
|
||
export default CopyDashboardForm; |
66 changes: 66 additions & 0 deletions
66
app/javascript/components/copy-dashboard-form/copy-dashboard-form.schema.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { componentTypes, validatorTypes } from '@data-driven-forms/react-form-renderer'; | ||
import debouncePromise from '../../helpers/promise-debounce'; | ||
import { http } from '../../http_api'; | ||
|
||
export const asyncValidator = (value, dashboardId, name) => | ||
http.get(`/report/dashboard_get/${dashboardId}?name=${value}`) | ||
.then((json) => { | ||
if (value === name) { | ||
return __('Use different name'); | ||
} | ||
if (json.length > 0) { | ||
return __('Name has already been taken'); | ||
} | ||
if (value === '' || value === undefined) { | ||
return __("Name can't be blank"); | ||
} | ||
return undefined; | ||
}); | ||
|
||
const asyncValidatorDebounced = debouncePromise(asyncValidator); | ||
|
||
export default (miqGroups, name, dashboardId) => { | ||
const fields = [ | ||
{ | ||
component: componentTypes.SUB_FORM, | ||
title: __('Basic Info'), | ||
fields: [ | ||
{ | ||
component: componentTypes.TEXT_FIELD, | ||
name: 'name', | ||
validate: [ | ||
value => asyncValidatorDebounced(value, dashboardId, name), | ||
], | ||
label: __('Name'), | ||
maxLength: 40, | ||
validateOnMount: true, | ||
}, | ||
{ | ||
component: componentTypes.TEXT_FIELD, | ||
name: 'description', | ||
validate: [{ | ||
type: validatorTypes.REQUIRED, | ||
message: __('Required'), | ||
}], | ||
label: __('Description'), | ||
maxLength: 255, | ||
validateOnMount: true, | ||
}, | ||
{ | ||
component: componentTypes.SELECT_COMPONENT, | ||
name: 'group_id', | ||
options: miqGroups, | ||
label: __('Select Group'), | ||
placeholder: __('Nothing selected'), | ||
isSearchable: true, | ||
validate: [{ | ||
type: validatorTypes.REQUIRED, | ||
message: __('Required'), | ||
}], | ||
validateOnMount: true, | ||
}, | ||
], | ||
}, | ||
]; | ||
return { fields }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
= render :partial => "layouts/flash_msg" | ||
.col-md-12 | ||
= react('CopyDashboardForm', { :dashboardId => @edit[:db_id].to_s }) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters