Skip to content

Commit

Permalink
feat(): update project schema/metricsUiSchema (#1323)
Browse files Browse the repository at this point in the history
* feat(): update project schema/metricsUiSchema

* test(): update uiSchema metric test

* fix(): try to refactor metricSchema and uiSchema

* test(): update test with new properties

* fix(): get rid of section

* fix(): change scope

* fix(): update scopes

* fix(): update scopes

* test(): update test and add sections

* test(): update test and add sections

* test(): update test and add sections

* fix(): change units to unit

* fix(): add required errors

* fix(): change _metrics to _metric and update testS

* fix(): update logic for editorToMetric and fromEditor

* fix(): fix metric saving logic and tests

* test(): add test for branch coverage

* fix(): change error to required

* fix(): change error to minLength

* feat(): maximum allowed number of metrics

* fix(): change string to be scoped to metrics

* refactor(): remove having to pass in entityInfo directly

* fix(): replace for time being

* fix(): attempt fix for metric errors

* fix(): remove entityInfo from fromEditor
  • Loading branch information
jordantsanz authored Nov 14, 2023
1 parent eb2f3c5 commit ac3a6fa
Show file tree
Hide file tree
Showing 8 changed files with 276 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,95 @@ export const buildUiSchema = (
elements: [
{
type: "Section",
labelKey: `${i18nScope}.details.sectionTitle`,
options: {
section: "block",
},
elements: [
{
labelKey: `${i18nScope}.details.title`,
scope: "/properties/cardTitle",
labelKey: `${i18nScope}.fields.metrics.cardTitle.label`,
scope: "/properties/_metric/properties/cardTitle",
type: "Control",
options: {
messages: [
{
type: "ERROR",
keyword: "minLength",
labelKey: `${i18nScope}.fields.metrics.cardTitle.message.minLength`,
icon: true,
allowShowBeforeInteract: true,
},
],
},
},
{
type: "Section",
labelKey: `${i18nScope}.sections.metrics.source.label`,
options: {
section: "block",
open: true,
},
elements: [
{
labelKey: `${i18nScope}.fields.metrics.value.label`,
scope: "/properties/_metric/properties/value",
type: "Control",
options: {
messages: [
{
type: "ERROR",
keyword: "minLength",
labelKey: `${i18nScope}.fields.metrics.value.message.minLength`,
icon: true,
allowShowBeforeInteract: true,
},
],
},
},
],
},
{
type: "Section",
labelKey: `${i18nScope}.sections.metrics.formatting.label`,
options: {
section: "block",
open: true,
},
elements: [
{
labelKey: `${i18nScope}.fields.metrics.unit.label`,
scope: "/properties/_metric/properties/unit",
type: "Control",
options: {
helperText: {
labelKey: `${i18nScope}.fields.metrics.unit.helperText`,
placement: "bottom",
},
},
},
{
labelKey: `${i18nScope}.fields.metrics.unitPosition.label`,
scope: "/properties/_metric/properties/unitPosition",
type: "Control",
options: {
control: "hub-field-input-select",
enum: {
i18nScope: `${i18nScope}.fields.metrics.unitPosition.enum`,
},
},
},
],
},
{
type: "Section",
labelKey: `${i18nScope}.sections.metrics.appearance.label`,
options: {
section: "block",
open: false,
},
elements: [
{
labelKey: `${i18nScope}.fields.metrics.trailingText.label`,
scope: "/properties/_metric/properties/trailingText",
type: "Control",
},
],
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
IEntityInfo,
IExpression,
IMetricEditorValues,
MetricVisibility,
} from "../../../types/Metrics";
import { ServiceAggregation } from "../../../../core/types/DynamicValues";

Expand Down Expand Up @@ -76,6 +77,7 @@ export function editorToMetric(
const displayConfig: IMetricDisplayConfig = {
...config,
displayType: config.displayType || "stat-card",
visibility: config.visibility || MetricVisibility.hidden,
metricId,
fieldType,
statistic,
Expand Down
3 changes: 3 additions & 0 deletions packages/common/src/core/types/Metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,3 +280,6 @@ export interface IDynamicMetricValues {

[key: string]: any;
}

/** Maxmium number of metrics allowed on any given entity. */
export const MAX_ENTITY_METRICS_ALLOWED = 12;
49 changes: 26 additions & 23 deletions packages/common/src/projects/HubProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,32 @@ export class HubProject
delete editor.view.featuredImage;

// convert back to an entity
const entity = editorToProject(editor, this.context.portal);
let entity = editorToProject(editor, this.context.portal);

// handle metrics
const _metric = editor._metric;

// if we have a current metric and it's beyond the default empty from toEditor
if (_metric && Object.keys(_metric).length) {
let metricId = editorContext?.metricId;

// creating a new metric
if (!metricId) {
metricId = createId(camelize(`${editor._metric.cardTitle}_`));
}

// transform editor values into metric and displayConfig
const { metric, displayConfig } = editorToMetric(
editor._metric,
metricId,
{
metricName: editor._metric.cardTitle,
}
);

// put metric and display config onto entity
entity = setMetricAndDisplay(this.entity, metric, displayConfig);
}

// create it if it does not yet exist...
if (isCreate) {
Expand All @@ -306,28 +331,6 @@ export class HubProject
}
}

// handle metrics
const _metric = editor._metric;

// if we have a current metric and it's beyond the default empty from toEditor
if (_metric && Object.keys(_metric).length) {
let metricId = editorContext?.metricId;

// creating a new metric
if (!metricId) {
metricId = createId(camelize(editor._metric.cardTitle));
}

// transform editor values into metric and displayConfig
const { metric, displayConfig } = editorToMetric(
editor._metric,
metricId
);

// put metric and display config onto entity
this.entity = setMetricAndDisplay(this.entity, metric, displayConfig);
}

/**
* operations that are only relevant to the project create workflow.
* if these ever become part of the edit experience, we can remove
Expand Down
15 changes: 14 additions & 1 deletion packages/common/src/projects/_internal/ProjectSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ export const ProjectSchema: IConfigurationSchema = {
default: HubEntityStatus.notStarted,
enum: Object.keys(HubEntityStatus),
},
_metrics: MetricSchema,
_metric: {
type: "object",
properties: {
...MetricSchema.properties,
cardTitle: {
type: "string",
minLength: 1,
},
value: {
type: "string",
minLength: 1,
},
},
},
},
} as IConfigurationSchema;
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,97 @@ describe("buildUiSchema: metric", () => {
elements: [
{
type: "Section",
labelKey: `some.scope.details.sectionTitle`,
options: {
section: "block",
},
elements: [
{
labelKey: `some.scope.details.title`,
scope: "/properties/cardTitle",
labelKey: `some.scope.fields.metrics.cardTitle.label`,
scope: "/properties/_metric/properties/cardTitle",
type: "Control",
options: {
messages: [
{
type: "ERROR",
keyword: "minLength",
labelKey:
"some.scope.fields.metrics.cardTitle.message.minLength",
icon: true,
allowShowBeforeInteract: true,
},
],
},
},
{
type: "Section",
labelKey: `some.scope.sections.metrics.source.label`,
options: {
section: "block",
open: true,
},
elements: [
{
labelKey: `some.scope.fields.metrics.value.label`,
scope: "/properties/_metric/properties/value",
type: "Control",
options: {
messages: [
{
type: "ERROR",
keyword: "minLength",
labelKey:
"some.scope.fields.metrics.value.message.minLength",
icon: true,
allowShowBeforeInteract: true,
},
],
},
},
],
},
{
type: "Section",
labelKey: `some.scope.sections.metrics.formatting.label`,
options: {
section: "block",
open: true,
},
elements: [
{
labelKey: `some.scope.fields.metrics.unit.label`,
scope: "/properties/_metric/properties/unit",
type: "Control",
options: {
helperText: {
labelKey: `some.scope.fields.metrics.unit.helperText`,
placement: "bottom",
},
},
},
{
labelKey: `some.scope.fields.metrics.unitPosition.label`,
scope: "/properties/_metric/properties/unitPosition",
type: "Control",
options: {
control: "hub-field-input-select",
enum: {
i18nScope: `some.scope.fields.metrics.unitPosition.enum`,
},
},
},
],
},
{
type: "Section",
labelKey: `some.scope.sections.metrics.appearance.label`,
options: {
section: "block",
open: false,
},
elements: [
{
labelKey: `some.scope.fields.metrics.trailingText.label`,
scope: "/properties/_metric/properties/trailingText",
type: "Control",
},
],
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
IMetricEditorValues,
IServiceQueryMetricSource,
IStaticValueMetricSource,
MetricVisibility,
} from "../../../../../src/core/types/Metrics";
import * as EditorToMetric from "../../../../../src/core/schemas/internal/metrics/editorToMetric";

Expand Down Expand Up @@ -394,6 +395,7 @@ describe("editorToMetric", () => {
sourceTitle: "DynamicMaps123",
allowLink: undefined,
fieldType: "esriFieldTypeString",
visibility: MetricVisibility.hidden,
statistic: "",
});
});
Expand Down Expand Up @@ -440,6 +442,7 @@ describe("editorToMetric", () => {
allowLink: undefined,
fieldType: "esriFieldTypeString",
statistic: "",
visibility: MetricVisibility.hidden,
});
});
it("handles static source link correctly when no source", () => {
Expand Down Expand Up @@ -483,6 +486,47 @@ describe("editorToMetric", () => {
allowLink: undefined,
fieldType: "esriFieldTypeString",
statistic: "",
visibility: MetricVisibility.hidden,
});
});
it("handles static source link correctly when no source or entity info", () => {
const values = {
value: "1",
type: "dynamic",
displayType: "stat-card",
allowLink: false,
dynamicMetric: {
layerId: 0,
field: "",
statistic: "",
serviceUrl: "",
fieldType: "esriFieldTypeString" as FieldType,
sourceLink: "/dynamicmaps/123",
sourceTitle: "DynamicMaps123",
allowDynamicLink: true,
allowExpressionSet: true,
expressionSet: [] as IExpression[],
legacyWhere: "location = 'river' OR location = 'sun'",
},
itemId: "",
metricId: "id",
};

const { displayConfig } = EditorToMetric.editorToMetric(
values,
"id",
undefined
);
expect(displayConfig).toEqual({
displayType: "stat-card",
metricId: "id",
type: "dynamic",
sourceLink: "/dynamicmaps/123",
sourceTitle: "DynamicMaps123",
allowLink: undefined,
fieldType: "esriFieldTypeString",
statistic: "",
visibility: MetricVisibility.hidden,
});
});
});
Expand Down
Loading

0 comments on commit ac3a6fa

Please sign in to comment.