Skip to content

Commit

Permalink
feat(): add type for static metric value (#1371)
Browse files Browse the repository at this point in the history
  • Loading branch information
jordantsanz authored Jan 10, 2024
1 parent 2b988b8 commit af404bf
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 53 deletions.
23 changes: 21 additions & 2 deletions packages/common/src/core/schemas/internal/metrics/MetricSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ export const MetricSchema: IConfigurationSchema = {
cardTitle: {
type: "string",
},
value: {
value: {},
valueType: {
type: "string",
default: "string",
enum: ["string", "number", "date"],
},
dynamicMetric: {
type: "object",
Expand Down Expand Up @@ -106,7 +109,10 @@ export const MetricSchema: IConfigurationSchema = {
type: "string",
},
},
allOf: [{ $ref: "#/definitions/if-source-title-then-source-link" }],
allOf: [
{ $ref: "#/definitions/if-source-title-then-source-link" },
{ $ref: "#/definitions/value-type-value-mapping" },
],
definitions: {
// TODO: reimplement popover with layouts release
"if-layout-moreinfo-then-require-popover-title-description": {
Expand All @@ -127,5 +133,18 @@ export const MetricSchema: IConfigurationSchema = {
required: ["sourceLink"],
},
},
"value-type-value-mapping": {
if: {
properties: {
valueType: { enum: ["string", "date"] },
},
},
then: {
properties: { value: { type: "string" } },
},
else: {
properties: { value: { type: "number" } },
},
},
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import {
SHOW_FOR_STATIC_RULE_ENTITY,
SHOW_FOR_DYNAMIC_RULE_ENTITY,
SHOW_FOR_SHARING_RULE_ENTITY,
SHOW_FOR_STATIC_AND_STRING_RULE_ENTITY,
SHOW_FOR_STATIC_AND_NUMBER_RULE_ENTITY,
SHOW_FOR_STATIC_AND_DATE_RULE_ENTITY,
} from "./rules";

/**
Expand All @@ -31,17 +34,6 @@ export const buildUiSchema = (
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,
},
],
},
},
],
},
Expand All @@ -60,21 +52,49 @@ export const buildUiSchema = (
},
},
},
{
scope: "/properties/_metric/properties/valueType",
type: "Control",
labelKey: `${i18nScope}.fields.metrics.valueType.label`,
rule: SHOW_FOR_STATIC_RULE_ENTITY,
options: {
control: "hub-field-input-tile-select",
layout: "horizontal",
helperText: {
labelKey: `${i18nScope}.fields.metrics.valueType.helperText`,
placement: "top",
},
enum: {
i18nScope: `${i18nScope}.fields.metrics.valueType.enum`,
},
},
},
{
labelKey: `${i18nScope}.fields.metrics.value.label`,
scope: "/properties/_metric/properties/value",
type: "Control",
rule: SHOW_FOR_STATIC_RULE_ENTITY,
rule: SHOW_FOR_STATIC_AND_STRING_RULE_ENTITY,
options: {
messages: [
{
type: "ERROR",
keyword: "minLength",
labelKey: `${i18nScope}.fields.metrics.value.message.minLength`,
icon: true,
allowShowBeforeInteract: true,
},
],
control: "hub-field-input-input",
},
},
{
labelKey: `${i18nScope}.fields.metrics.value.label`,
scope: "/properties/_metric/properties/value",
type: "Control",
rule: SHOW_FOR_STATIC_AND_NUMBER_RULE_ENTITY,
options: {
control: "hub-field-input-input",
type: "number",
},
},
{
labelKey: `${i18nScope}.fields.metrics.value.label`,
scope: "/properties/_metric/properties/value",
type: "Control",
rule: SHOW_FOR_STATIC_AND_DATE_RULE_ENTITY,
options: {
control: "hub-field-input-date",
},
},
{
Expand All @@ -93,7 +113,7 @@ export const buildUiSchema = (
options: {
helperText: {
labelKey: `${i18nScope}.fields.metrics.unit.helperText`,
placement: "bottom",
placement: "top",
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function editorToMetric(
metricId: string,
opts?: { metricName?: string; entityInfo?: IEntityInfo }
): { metric: IMetric; displayConfig: IMetricDisplayConfig } {
const { value, dynamicMetric, ...config } = values;
const { value, valueType, dynamicMetric, ...config } = values;
const {
layerId,
itemId,
Expand Down Expand Up @@ -58,6 +58,7 @@ export function editorToMetric(
: {
type: "static-value",
value,
valueType,
};

// create metric
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export function metricToEditor(
editor = {
type: "static",
value: (metric.source as IStaticValueMetricSource).value,
valueType: (metric.source as IStaticValueMetricSource).valueType,
...editor,
};
break;
Expand Down
48 changes: 48 additions & 0 deletions packages/common/src/core/schemas/internal/metrics/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,54 @@ export const SHOW_FOR_STATIC_RULE_ENTITY = {
effect: UiSchemaRuleEffects.SHOW,
};

export const SHOW_FOR_STATIC_AND_STRING_RULE_ENTITY = {
condition: {
schema: {
properties: {
_metric: {
properties: {
type: { const: "static" },
valueType: { const: "string" },
},
},
},
},
},
effect: UiSchemaRuleEffects.SHOW,
};

export const SHOW_FOR_STATIC_AND_NUMBER_RULE_ENTITY = {
condition: {
schema: {
properties: {
_metric: {
properties: {
type: { const: "static" },
valueType: { const: "number" },
},
},
},
},
},
effect: UiSchemaRuleEffects.SHOW,
};

export const SHOW_FOR_STATIC_AND_DATE_RULE_ENTITY = {
condition: {
schema: {
properties: {
_metric: {
properties: {
type: { const: "static" },
valueType: { const: "date" },
},
},
},
},
},
effect: UiSchemaRuleEffects.SHOW,
};

/** Show if the type is set to dynamic */
export const SHOW_FOR_DYNAMIC_RULE_ENTITY = {
condition: {
Expand Down
5 changes: 5 additions & 0 deletions packages/common/src/core/types/Metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ export interface IStaticValueMetricSource {
* Static value to use
*/
value: string | number;

/**
* Type of the static value.
*/
valueType?: "string" | "number" | "date";
}

/**
Expand Down
4 changes: 0 additions & 4 deletions packages/common/src/projects/_internal/ProjectSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@ export const ProjectSchema: IConfigurationSchema = {
type: "string",
minLength: 1,
},
value: {
type: "string",
minLength: 1,
},
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,6 @@ describe("buildUiSchema: metric", () => {
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,
},
],
},
},
],
},
Expand All @@ -49,9 +37,9 @@ describe("buildUiSchema: metric", () => {
},
},
{
labelKey: `some.scope.fields.metrics.value.label`,
scope: "/properties/_metric/properties/value",
scope: "/properties/_metric/properties/valueType",
type: "Control",
labelKey: `some.scope.fields.metrics.valueType.label`,
rule: {
condition: {
scope: "/properties/_metric/properties/type",
Expand All @@ -60,16 +48,85 @@ describe("buildUiSchema: metric", () => {
effect: UiSchemaRuleEffects.SHOW,
},
options: {
messages: [
{
type: "ERROR",
keyword: "minLength",
labelKey:
"some.scope.fields.metrics.value.message.minLength",
icon: true,
allowShowBeforeInteract: true,
control: "hub-field-input-tile-select",
layout: "horizontal",
helperText: {
labelKey: `some.scope.fields.metrics.valueType.helperText`,
placement: "top",
},
enum: {
i18nScope: `some.scope.fields.metrics.valueType.enum`,
},
},
},
{
labelKey: `some.scope.fields.metrics.value.label`,
scope: "/properties/_metric/properties/value",
type: "Control",
rule: {
condition: {
schema: {
properties: {
_metric: {
properties: {
type: { const: "static" },
valueType: { const: "string" },
},
},
},
},
],
},
effect: UiSchemaRuleEffects.SHOW,
},
options: {
control: "hub-field-input-input",
},
},
{
labelKey: `some.scope.fields.metrics.value.label`,
scope: "/properties/_metric/properties/value",
type: "Control",
rule: {
condition: {
schema: {
properties: {
_metric: {
properties: {
type: { const: "static" },
valueType: { const: "number" },
},
},
},
},
},
effect: UiSchemaRuleEffects.SHOW,
},
options: {
control: "hub-field-input-input",
type: "number",
},
},
{
labelKey: `some.scope.fields.metrics.value.label`,
scope: "/properties/_metric/properties/value",
type: "Control",
rule: {
condition: {
schema: {
properties: {
_metric: {
properties: {
type: { const: "static" },
valueType: { const: "date" },
},
},
},
},
},
effect: UiSchemaRuleEffects.SHOW,
},
options: {
control: "hub-field-input-date",
},
},
{
Expand All @@ -94,7 +151,7 @@ describe("buildUiSchema: metric", () => {
options: {
helperText: {
labelKey: `some.scope.fields.metrics.unit.helperText`,
placement: "bottom",
placement: "top",
},
},
},
Expand Down
Loading

0 comments on commit af404bf

Please sign in to comment.