Copy custom forms added

This commit is contained in:
root 2019-07-22 13:01:48 +02:00
parent 75138f9728
commit e9bf4a890c
7 changed files with 67 additions and 6 deletions

View file

@ -380,13 +380,18 @@ export default class CUD extends Component {
}
@withFormErrorHandlers
async submitHandler(submitAndLeave) {
async submitHandler(submitAndLeave, copy) {
const t = this.props.t;
let sendMethod, url;
if (this.props.entity) {
sendMethod = FormSendMethod.PUT;
url = `rest/forms/${this.props.entity.id}`
if(copy){
sendMethod = FormSendMethod.POST;
url = `rest/forms/${this.props.entity.id}`
}else{
sendMethod = FormSendMethod.PUT;
url = `rest/forms/${this.props.entity.id}`
}
} else {
sendMethod = FormSendMethod.POST;
url = 'rest/forms'
@ -404,13 +409,21 @@ export default class CUD extends Component {
} else {
await this.getFormValuesFromURL(`rest/forms/${this.props.entity.id}`);
this.enableForm();
this.setFormStatusMessage('success', t('customFormsUpdated'));
if(copy){
this.navigateToWithFlashMessage(`/lists/forms`, 'success', t('customFormsCopied'));
}else{
this.setFormStatusMessage('success', t('customFormsUpdated'));
}
}
} else {
if (submitAndLeave) {
this.navigateToWithFlashMessage('/lists/forms', 'success', t('customFormsCreated'));
} else {
this.navigateToWithFlashMessage(`/lists/forms/${submitResult}/edit`, 'success', t('customFormsCreated'));
if(copy){
this.navigateToWithFlashMessage(`/lists/forms`, 'success', t('customFormsCopied'));
}else{
this.navigateToWithFlashMessage(`/lists/forms/${submitResult}/edit`, 'success', t('customFormsCreated'));
}
}
}
} else {
@ -552,7 +565,8 @@ export default class CUD extends Component {
<ButtonRow>
<Button type="submit" className="btn-primary" icon="check" label={t('save')}/>
<Button type="submit" className="btn-primary" icon="check" label={t('saveAndLeave')} onClickAsync={async () => await this.submitHandler(true)}/>
<Button type="submit" className="btn-primary" icon="check" label={t('saveAndLeave')} onClickAsync={async () => await this.submitHandler(true, false)}/>
{isEdit && <Button type="submit" className="btn-success" icon="copy" label={t('copy')} onClickAsync={async () => await this.submitHandler(false, true)}/>}
{canDelete && <LinkButton className="btn-danger" icon="trash-alt" label={t('delete')} to={`/lists/forms/${this.props.entity.id}/delete`}/>}
</ButtonRow>
</Form>

View file

@ -100,6 +100,7 @@
"customTemplateEditor": "Custom template editor",
"save": "Save",
"saveAndLeave": "Save and leave",
"copy": "Copy",
"saveAndGoToStatus": "Save and go to status",
"testSend": "Test send",
"createRegularCampaign": "Create Regular Campaign",

View file

@ -100,6 +100,7 @@
"customTemplateEditor": "Custom template editor",
"save": "Save",
"saveAndLeave": "Save and leave",
"copy": "Copy",
"saveAndGoToStatus": "Save and go to status",
"testSend": "Test send",
"createRegularCampaign": "Create Regular Campaign",

View file

@ -105,6 +105,7 @@
"save": "Save",
"saveAndLeave": "Save and leave",
"saveAndLeave - TODO: update line above and then delete this line to mark that the translation has been fixed": "Save and leave",
"copy": "Copiar",
"saveAndGoToStatus": "Save and go to status",
"saveAndGoToStatus - TODO: update line above and then delete this line to mark that the translation has been fixed": "Save and go to status",
"testSend": "Test send",

View file

@ -105,6 +105,7 @@
"save": "Salvar",
"saveAndLeave": "Salvar e sair",
"saveAndLeave - TODO: update line above and then delete this line to mark that the translation has been fixed": "Save and leave",
"copy": "Copy",
"saveAndGoToStatus": "Save and go to status",
"saveAndGoToStatus - TODO: update line above and then delete this line to mark that the translation has been fixed": "Save and go to status",
"testSend": "Testar envio",

View file

@ -175,6 +175,41 @@ async function updateWithConsistencyCheck(context, entity) {
});
}
async function copy(context, entity) {
await knex.transaction(async tx => {
await shares.enforceEntityPermissionTx(tx, context, 'customForm', entity.id, 'edit');
const existing = await _getById(tx, entity.id);
const existingHash = hash(existing);
if (existingHash !== entity.originalHash) {
throw new interoperableErrors.ChangedError();
}
await namespaceHelpers.validateEntity(tx, entity);
await namespaceHelpers.validateMove(context, entity, existing, 'customForm', 'createCustomForm', 'delete');
const form = filterObject(entity, allowedFormKeys);
enforce(!Object.keys(checkForMjmlErrors(form)).length, 'Error(s) in form templates');
entity.name = entity.name + '_COPY';
const ids = await tx('custom_forms').insert(filterObject(entity, formAllowedKeys));
const id = ids[0];
for (const formKey in form) {
await tx('custom_forms_data').insert({
form: id,
data_key: formKey,
data_value: form[formKey]
})
}
await shares.rebuildPermissionsTx(tx, { entityTypeId: 'customForm', entityId: id });
return id;
});
}
async function remove(context, id) {
await knex.transaction(async tx => {
await shares.enforceEntityPermissionTx(tx, context, 'customForm', id, 'delete');
@ -285,6 +320,7 @@ module.exports.hash = hash;
module.exports.getById = getById;
module.exports.create = create;
module.exports.updateWithConsistencyCheck = updateWithConsistencyCheck;
module.exports.copy = copy;
module.exports.remove = remove;
module.exports.getDefaultCustomFormValues = getDefaultCustomFormValues;
module.exports.serverValidate = serverValidate;

View file

@ -26,6 +26,13 @@ router.postAsync('/forms', passport.loggedIn, passport.csrfProtection, async (re
return res.json(await forms.create(req.context, req.body));
});
router.postAsync('/forms/:formId', passport.loggedIn, passport.csrfProtection, async (req, res) => {
const entity = req.body;
entity.id = castToInteger(req.params.formId);
await forms.copy(req.context, entity);
return res.json();
});
router.putAsync('/forms/:formId', passport.loggedIn, passport.csrfProtection, async (req, res) => {
const entity = req.body;
entity.id = castToInteger(req.params.formId);