-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NEW Add save action to element inline edit forms
- Loading branch information
1 parent
ff1c82a
commit b639f68
Showing
8 changed files
with
168 additions
and
14 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,50 @@ | ||
/* global document */ | ||
import React from 'react'; | ||
import AbstractAction from 'components/ElementActions/AbstractAction'; | ||
import backend from 'lib/Backend'; | ||
import i18n from 'i18n'; | ||
import { loadElementSchemaValue } from 'state/editor/loadElementSchemaValue'; | ||
import { getSerializedFormData } from 'state/editor/getSerializedFormData'; | ||
|
||
/** | ||
* Using a REST backend, serialize the current form data and post it to the backend endpoint to save | ||
* the inline edit form's data for the current block. | ||
*/ | ||
const SaveAction = (MenuComponent) => (props) => { | ||
const handleClick = (event) => { | ||
event.stopPropagation(); | ||
|
||
const { id } = this.props; | ||
|
||
const formData = getSerializedFormData(`Form_ElementForm_${id}`); | ||
|
||
const endpointSpec = { | ||
url: loadElementSchemaValue('saveUrl', id), | ||
method: loadElementSchemaValue('saveMethod'), | ||
payloadFormat: loadElementSchemaValue('payloadFormat'), | ||
}; | ||
|
||
// @todo add CSRF SecurityID token | ||
const endpoint = backend.createEndpointFetcher(endpointSpec); | ||
endpoint(formData).then((result) => { | ||
console.log(result); | ||
// @todo update apollo cache? | ||
}); | ||
}; | ||
|
||
const newProps = { | ||
title: i18n._t('SaveAction.SAVE', 'Save'), | ||
extraClass: 'element-editor__actions-save', | ||
onClick: handleClick, | ||
}; | ||
|
||
return ( | ||
<MenuComponent {...props}> | ||
{props.children} | ||
|
||
<AbstractAction {...newProps} /> | ||
</MenuComponent> | ||
); | ||
}; | ||
|
||
export default SaveAction; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* global document */ | ||
/** | ||
* Get all form inputs (etc) inside the given formName div and return as a serialised object | ||
* | ||
* @param {string} formName | ||
* @returns {object} | ||
*/ | ||
export const getSerializedFormData = (formName) => { | ||
const form = document.getElementById(formName); | ||
const fields = form.querySelectorAll('input, select, textarea, checkbox'); | ||
|
||
const output = {}; | ||
for (let i = 0; i <= fields.length; i++) { | ||
if (fields[i]) { | ||
const formField = fields[i]; | ||
const fieldName = formField.name; | ||
|
||
output[fieldName] = formField.value; | ||
} | ||
} | ||
|
||
return output; | ||
}; |
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,19 @@ | ||
import Config from 'lib/Config'; | ||
|
||
/** | ||
* Returns the named component from the elementForm's schema data | ||
* | ||
* @param {string} key | ||
* @param {number|null} elementId If provided, will be concatenated on as value is treated as a URL | ||
* @returns {string} | ||
*/ | ||
export const loadElementSchemaValue = (key, elementId = null) => { | ||
const sectionKey = 'DNADesign\\Elemental\\Controllers\\ElementalAreaController'; | ||
const section = Config.getSection(sectionKey); | ||
const schemaValue = section.form.elementForm[key] || ''; | ||
|
||
if (elementId) { | ||
return `${schemaValue}/${elementId}`; | ||
} | ||
return schemaValue; | ||
}; |
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