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 9, 2024
1 parent 02ed5e5 commit 79fe16f
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 6 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.

10 changes: 7 additions & 3 deletions client/src/components/LinkField/LinkField.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable */
import React, { useState, useEffect, createContext } from 'react';
import React, { useState, useEffect, createContext, Component } from 'react';
import { bindActionCreators, compose } from 'redux';
import { connect } from 'react-redux';
import { injectGraphql } from 'lib/Injector';
Expand Down Expand Up @@ -169,10 +169,12 @@ const LinkField = ({
return links;
};

const renderPicker = isMulti || Object.keys(data).length === 0;
const renderModal = Boolean(editingID);
const saveOwnerFirst = ownerID === 0;
const renderPicker = !saveOwnerFirst && (isMulti || Object.keys(data).length === 0);
const renderModal = !saveOwnerFirst && Boolean(editingID);

return <LinkFieldContext.Provider value={{ ownerID, ownerClass, ownerRelation }}>
{ saveOwnerFirst && <div className="link-field__save-owner-first">Save record first</div>}
{ renderPicker && <LinkPicker
onModalSuccess={onModalSuccess}
onModalClosed={onModalClosed}
Expand Down Expand Up @@ -211,6 +213,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-owner-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-owner-first div if ownerID is 0', async () => {
const { container } = render(<LinkField {...makeProps({
ownerID: 0
})}
/>);
expect(container.querySelectorAll('.link-field__save-owner-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-owner-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 79fe16f

Please sign in to comment.