Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ZoneGridModel more forgiving (handle possibly out of sync state) #3530

Merged
merged 8 commits into from
Nov 21, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 49 additions & 16 deletions cmp/zoneGrid/ZoneGridModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,16 @@ import {
} from '@ag-grid-community/core';
import {Icon} from '@xh/hoist/icon';
import {throwIf, withDefault} from '@xh/hoist/utils/js';
import {castArray, forOwn, isEmpty, isFinite, isPlainObject, isString} from 'lodash';
import {
castArray,
forOwn,
isEmpty,
isFinite,
isPlainObject,
isString,
dropRight,
remove
} from 'lodash';
import {ReactNode} from 'react';
import {ZoneMapperConfig, ZoneMapperModel} from './impl/ZoneMapperModel';
import {ZoneGridPersistenceModel} from './impl/ZoneGridPersistenceModel';
Expand Down Expand Up @@ -616,41 +625,65 @@ export class ZoneGridModel extends HoistModel {
const ret = {} as Record<Zone, ZoneMapping[]>;
forOwn(mappings, (rawMapping, zone) => {
// 1) Standardize mapping into an array of ZoneMappings
const mapping = [];
let mapping = [];
castArray(rawMapping).forEach(it => {
if (!it) return;

const ret = isString(it) ? {field: it} : it,
col = this.findColumnSpec(ret);

throwIf(!col, `Column not found for field ${ret.field}`);
return mapping.push(ret);
if (col) {
mapping.push(ret);
} else {
console.warn(`Column not found for field '${ret.field}'`);
}
});

// 2) Ensure mapping respects configured limits
const limit = this.limits?.[zone];
if (limit) {
throwIf(
isFinite(limit.min) && mapping.length < limit.min,
`Requires minimum ${limit.min} mappings in zone "${zone}"`
);
throwIf(
isFinite(limit.max) && mapping.length > limit.max,
`Exceeds maximum ${limit.max} mappings in zone "${zone}"`
);
if (isFinite(limit.min) && mapping.length < limit.min) {
console.warn(
`Requires minimum ${limit.min} mappings in zone "${zone}. Setting to default."`
);
mapping = this._defaultState.mappings[zone];
}

if (isFinite(limit.max) && mapping.length > limit.max) {
const diff = mapping.length - limit.max;
console.warn(
`Exceeds maximum ${limit.max} mappings in zone "${zone}". Dropping last ${diff} fields`
);
mapping = dropRight(mapping, mapping.length - limit.max);
}

if (!isEmpty(limit.only)) {
const offenders = [];
mapping.forEach(it => {
throwIf(
!limit.only.includes(it.field),
`Field "${it.field}" not allowed in zone "${zone}"`
);
if (!limit.only.includes(it.field)) {
console.warn(
`Field "${it.field}" not allowed in zone "${zone}. Removing"`
);
offenders.push(it.field);
}
});

if (!isEmpty(offenders)) {
remove(mapping, it => offenders.includes(it.field));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set to default again here. Only need to warn for one field and say we're going to the default immediately at that point.

}

if ((zone == 'tl' || zone == 'tr') && isEmpty(mapping)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch! But I think we want to do this outside of the !isEmpty(limit.only) condition.

(I know enforcing only is currently the only place where we remove mappings, but I think the intent is clearer if we always do this as the final check of parseMappings)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great note Tom. I considered the same, but apparently made the wrong choice ;)

console.warn(
`Top mapping '${zone}' requires at least one field. Setting to default.`
);
mapping = this._defaultState.mappings[zone];
}
}
}

ret[zone] = mapping;
});

return ret;
}

Expand Down