Skip to content

Commit

Permalink
NEW LinkFieldController to handle FormSchema
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Oct 26, 2023
1 parent f0a3b25 commit d7aa0de
Show file tree
Hide file tree
Showing 12 changed files with 1,355 additions and 61 deletions.
1 change: 0 additions & 1 deletion _config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@

// Avoid creating global variables
call_user_func(function () {

});
2 changes: 1 addition & 1 deletion _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Name: linkfield

SilverStripe\Admin\LeftAndMain:
extensions:
- SilverStripe\LinkField\Extensions\LeftAndMain
- SilverStripe\LinkField\Extensions\LeftAndMainExtension

SilverStripe\Admin\ModalController:
extensions:
Expand Down
953 changes: 952 additions & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

69 changes: 68 additions & 1 deletion client/dist/styles/bundle.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 16 additions & 9 deletions client/src/components/LinkField/LinkField.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { compose } from 'redux';
import { inject, injectGraphql, loadComponent } from 'lib/Injector';
import fieldHolder from 'components/FieldHolder/FieldHolder';

const LinkField = ({ id, loading, Loading, data, LinkPicker, onChange, types, linkDescription, ...props }) => {
const LinkField = ({ id, loading, Loading, data, LinkPicker, onChange, types, linkDescription, submitForm, ...props }) => {
if (loading) {
return <Loading />;
}
Expand All @@ -15,21 +15,19 @@ const LinkField = ({ id, loading, Loading, data, LinkPicker, onChange, types, li
if (typeof onChange !== 'function') {
return;
}

onChange(event, { id, value: {} });
onChange(event, {});
};

const { typeKey } = data;
const type = types[typeKey];
const modalType = newTypeKey ? types[newTypeKey] : type;

let title = data ? data.Title : '';

if (!title) {
title = data ? data.TitleRelField : '';
}

const linkProps = {
const pickerProps = {
title,
link: type ? { type, title, description: linkDescription } : undefined,
onEdit: () => { setEditing(true); },
Expand All @@ -41,16 +39,24 @@ const LinkField = ({ id, loading, Loading, data, LinkPicker, onChange, types, li
types: Object.values(types)
};

const onModalSubmit = (modalData, action, submitFn) => {
const { SecurityID, action_insert: actionInsert, ...value } = modalData;
const onModalSubmit = async (modalData, action, submitFn) => {
const { SecurityID, action_submit: actionSubmit, ...data } = modalData;

const formSchema = await submitFn();

if (typeof onChange === 'function') {
onChange(event, { id, value });
const match = formSchema.id.match(/\/saveForm\/([0-9]+)$/);
if (match) {
data.ID = parseInt(match[1]);
}
onChange(null, data);
}

setEditing(false);
setNewTypeKey('');

// TODO toast here to say success (or somewhere else?)

return Promise.resolve();
};

Expand All @@ -68,7 +74,7 @@ const LinkField = ({ id, loading, Loading, data, LinkPicker, onChange, types, li
const LinkModal = loadComponent(`LinkModal.${handlerName}`);

return <Fragment>
<LinkPicker {...linkProps} />
<LinkPicker {...pickerProps} />
<LinkModal {...modalProps} />
</Fragment>;
};
Expand All @@ -81,6 +87,7 @@ const stringifyData = (Component) => (({ data, value, ...props }) => {
return <Component dataStr={JSON.stringify(dataValue)} {...props} data={dataValue} />;
});


export default compose(
inject(['LinkPicker', 'Loading']),
injectGraphql('readLinkTypes'),
Expand Down
5 changes: 4 additions & 1 deletion client/src/components/LinkModal/LinkModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ const buildSchemaUrl = (key, data) => {
const parsedQs = qs.parse(parsedURL.query);
parsedQs.key = key;
if (data) {
parsedQs.data = JSON.stringify(data);
for (const prop of ['href', 'path', 'pathname']) {
const id = typeof data.ID !== 'undefined' ? data.ID : '0';
parsedURL[prop] += `/${id}`;
}
}
return url.format({ ...parsedURL, search: qs.stringify(parsedQs)});
}
Expand Down
4 changes: 2 additions & 2 deletions client/src/entwine/JsonField.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ jQuery.entwine('ss', ($) => {
Root.render(<ReactField {...props} noHolder/>);
},

handleChange(event, {id, value}) {
handleChange(event, data) {
const fieldID = $(this).data('field-id');
$('#' + fieldID).val(JSON.stringify(value)).trigger('change');
$('#' + fieldID).val(JSON.stringify(data)).trigger('change');
this.refresh();
},

Expand Down
Loading

0 comments on commit d7aa0de

Please sign in to comment.