From e556204e94df792efb9b8ed8d0774db21f4d5c9e Mon Sep 17 00:00:00 2001 From: Rhys Evans Date: Wed, 8 Jan 2020 20:10:27 +0000 Subject: [PATCH] reinstate sorting of relationships in UI --- .../tc-ui/src/primitives/relationship/server.jsx | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/tc-ui/src/primitives/relationship/server.jsx b/packages/tc-ui/src/primitives/relationship/server.jsx index 3f7e14dba..86a157b75 100644 --- a/packages/tc-ui/src/primitives/relationship/server.jsx +++ b/packages/tc-ui/src/primitives/relationship/server.jsx @@ -15,6 +15,14 @@ const { setRelationshipAnnotator, } = require('./lib/view-relationship'); +const maybeSort = (hasMany, props) => { + if (!hasMany) { + return ''; + } + const sortField = props.has('name') ? 'name' : 'code'; + return `(orderBy: ${sortField}_asc)`; +}; + module.exports = { ViewComponent: ViewRelationship, EditComponent: props => ( @@ -40,7 +48,7 @@ module.exports = { hasValue: (value, { hasMany }) => hasMany ? value && value.length : !!value, setRelationshipAnnotator, - graphqlFragment: (propName, { type }) => { + graphqlFragment: (propName, { type, hasMany }) => { const typeDef = getType(type); const props = new Set(['code']); if (typeDef.properties.name) { @@ -54,6 +62,8 @@ module.exports = { .filter(([, { useInSummary }]) => useInSummary) .forEach(([name]) => props.add(name)); - return `${propName} {${[...props].join(' ')}}`; + return `${propName} ${maybeSort(hasMany, props)} {${[...props].join( + ' ', + )}}`; }, };