From b0633898a39e09b24a6e87e20705123cb9bf39b1 Mon Sep 17 00:00:00 2001 From: Stephanie Yang Date: Fri, 30 Jul 2021 16:11:06 -0500 Subject: [PATCH 1/3] add ability to set an aggregate sort on int fields --- .../GroupByBuilder/GroupByBuilder.tsx | 26 +++--- .../QueryBuilder/GroupBySort/GroupBySort.tsx | 83 ++++++++++--------- lattice/src/App/QueryBuilder/utils.ts | 9 +- 3 files changed, 62 insertions(+), 56 deletions(-) diff --git a/lattice/src/App/QueryBuilder/GroupByBuilder/GroupByBuilder.tsx b/lattice/src/App/QueryBuilder/GroupByBuilder/GroupByBuilder.tsx index c0486352f..c2129c755 100644 --- a/lattice/src/App/QueryBuilder/GroupByBuilder/GroupByBuilder.tsx +++ b/lattice/src/App/QueryBuilder/GroupByBuilder/GroupByBuilder.tsx @@ -157,22 +157,18 @@ export const GroupByBuilder: FC = ({ { + onUpdate={(updatedSort) => { let isInvalid = false; - if ( - value.length > 0 && - value[0].sortValue.includes('sum') && - !value[0].field - ) { - isInvalid = true; - } else if ( - value.length > 1 && - value[1].sortValue.includes('sum') && - !value[1].field - ) { - isInvalid = true; - } - onChange({ ...query, sort: value, isInvalid }); + updatedSort.forEach((s) => { + if ( + ['sum', 'aggregate'].includes(s.sortValue) && + !s.sum && + !s.aggregate + ) { + isInvalid = true; + } + }); + onChange({ ...query, sort: updatedSort, isInvalid }); }} fields={table.fields .filter((field) => field.options.type === 'int') diff --git a/lattice/src/App/QueryBuilder/GroupBySort/GroupBySort.tsx b/lattice/src/App/QueryBuilder/GroupBySort/GroupBySort.tsx index c75f9320b..ce66494d6 100644 --- a/lattice/src/App/QueryBuilder/GroupBySort/GroupBySort.tsx +++ b/lattice/src/App/QueryBuilder/GroupBySort/GroupBySort.tsx @@ -4,7 +4,8 @@ import css from './GroupBySort.module.scss'; export type SortOption = { sortValue: string; - field?: string; + sum?: string; + aggregate?: string; }; type GroupBySortProps = { @@ -28,25 +29,23 @@ export const GroupBySort: FC = ({ { label: 'Count (desc)', value: 'count desc' }, { label: 'Count (asc)', value: 'count asc' }, { label: 'Sum (desc)', value: 'sum desc' }, - { label: 'Sum (asc)', value: 'sum asc' } + { label: 'Sum (asc)', value: 'sum asc' }, + { label: 'Aggregate (desc)', value: 'aggregate desc' }, + { label: 'Aggregate (asc)', value: 'aggregate asc' } ]; const onPrimaryChange = (value: string) => { const split = value.split(' '); - const isSum = split[0] === 'sum'; - const fieldValue = hasPrimary ? sort[0].field : undefined; if (!hasSecondary) { - onUpdate([{ sortValue: value, field: isSum ? fieldValue : undefined }]); - } else if (hasSecondary && sort[1].sortValue.includes(split[0])) { - onUpdate([{ sortValue: value, field: isSum ? fieldValue : undefined }]); + onUpdate([{ sortValue: value }]); + } else if (sort[1].sortValue.includes(split[0])) { + onUpdate([{ sortValue: value }]); } else { - onUpdate([ - { sortValue: value, field: isSum ? fieldValue : undefined }, - sort[1] - ]); + onUpdate([{ sortValue: value }, sort[1]]); } }; + const onSecondaryChange = (value: string) => { let sortOp = { sortValue: value }; @@ -56,15 +55,31 @@ export const GroupBySort: FC = ({ onUpdate([sort[0], sortOp]); }; - const onUpdateSumField = (isPrimary: boolean, value: string) => { + const renderFieldSelect = ( + sortFor: 'primary' | 'secondary', + sortType: 'sum' | 'aggregate' + ) => { + const sortIdx = sortFor === 'primary' ? 0 : 1; + + return ( + onUpdateSumField(true, value)} - error={showErrors ? !sort[0].field : false} - /> - ) : null} + {primary.includes('sum') + ? renderFieldSelect('primary', 'sum') + : primary.includes('aggregate') + ? renderFieldSelect('primary', 'aggregate') + : null} {hasPrimary ? ( @@ -103,16 +113,11 @@ export const GroupBySort: FC = ({ allowEmpty={true} /> - {secondary.includes('sum') ? ( -