Skip to content

Commit

Permalink
ENH Show save record first text on unsaved owners
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Jan 10, 2024
1 parent 02ed5e5 commit c563394
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 7 deletions.
2 changes: 1 addition & 1 deletion client/dist/js/bundle.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion client/dist/styles/bundle.css

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

3 changes: 2 additions & 1 deletion client/lang/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ if (typeof(ss) === 'undefined' || typeof(ss.i18n) === 'undefined') {
"LinkField.DELETE_ERROR": "Failed to delete link",
"LinkField.ADD_LINK": "Add Link",
"LinkField.ARCHIVE": "Archive",
"LinkField.DELETE": "Delete"
"LinkField.DELETE": "Delete",
"LinkField.SAVE_RECORD_FIRST": "Cannot add links until the record has been saved"
});
}
3 changes: 2 additions & 1 deletion client/lang/src/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
"LinkField.LINK_DRAFT_LABEL": "Draft",
"LinkField.LINK_MODIFIED_TITLE": "Link has unpublished changes",
"LinkField.LINK_MODIFIED_LABEL": "Modified",
"LinkField.CANNOT_CREATE_LINK": "Cannot create link"
"LinkField.CANNOT_CREATE_LINK": "Cannot create link",
"LinkField.SAVE_RECORD_FIRST": "Cannot add links until the record has been saved"
}
9 changes: 7 additions & 2 deletions client/src/components/LinkField/LinkField.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,13 @@ const LinkField = ({
return links;
};

const renderPicker = isMulti || Object.keys(data).length === 0;
const renderModal = Boolean(editingID);
const saveRecordFirst = ownerID === 0;
const renderPicker = !saveRecordFirst && (isMulti || Object.keys(data).length === 0);
const renderModal = !saveRecordFirst && Boolean(editingID);
const saveRecordFirstText = i18n._t('LinkField.SAVE_RECORD_FIRST', 'Cannot add links until the record has been saved');

return <LinkFieldContext.Provider value={{ ownerID, ownerClass, ownerRelation }}>
{ saveRecordFirst && <div className="link-field__save-record-first">{saveRecordFirstText}</div>}
{ renderPicker && <LinkPicker
onModalSuccess={onModalSuccess}
onModalClosed={onModalClosed}
Expand Down Expand Up @@ -211,6 +214,8 @@ const mapDispatchToProps = (dispatch) => ({
},
});

export { LinkField as Component };

export default compose(
fieldHolder,
connect(null, mapDispatchToProps)
Expand Down
3 changes: 3 additions & 0 deletions client/src/components/LinkField/LinkField.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.link-field__save-record-first {
padding-top: 7px;
}
54 changes: 54 additions & 0 deletions client/src/components/LinkField/tests/LinkField-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/* global jest, test */
import React from 'react';
import { render } from '@testing-library/react';
import { Component as LinkField } from '../LinkField';

jest.mock('lib/Backend', () => ({
get: () => new Promise(() => {})
}));

window.ss.config = {
sections: [
{
name: 'SilverStripe\\LinkField\\Controllers\\LinkFieldController',
form: {
linkForm: {
dataUrl: 'http://example.com/mock-endpoint'
}
}
}
]
};

function makeProps(obj = {}) {
return {
value: 123,
onChange: () => {},
types: [],
actions: {},
isMulti: false,
canCreate: true,
ownerID: 123,
ownerClass: 'Page',
ownerRelation: 'MyRelation',
...obj
};
}

test('LinkField will render save-record-first div if ownerID is 0', async () => {
const { container } = render(<LinkField {...makeProps({
ownerID: 0
})}
/>);
expect(container.querySelectorAll('.link-field__save-record-first')).toHaveLength(1);
expect(container.querySelectorAll('.link-picker')).toHaveLength(0);
});

test('LinkField will render link-picker if ownerID is not 0', async () => {
const { container } = render(<LinkField {...makeProps({
ownerID: 1
})}
/>);
expect(container.querySelectorAll('.link-field__save-record-first')).toHaveLength(0);
expect(container.querySelectorAll('.link-picker')).toHaveLength(1);
});
2 changes: 1 addition & 1 deletion client/src/styles/bundle.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
@import "~bootstrap/scss/functions";
@import "~bootstrap/scss/variables";


@import "../components/LinkField/LinkField";
@import "../components/LinkPicker/LinkPicker";

0 comments on commit c563394

Please sign in to comment.