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

draft(widget-field-value-manager): create widget-field-value-manager #5115

Open
wants to merge 2 commits into
base: feature-dashboard-november
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import type { WidgetFieldTypeMap, WidgetFieldValue, WidgetFieldValueMap } from '@/common/modules/widgets/_widget-field-value-manager/type';

type FieldValueValidator<T extends WidgetFieldValue> = (fieldValue: T) => boolean;

export default class WidgetFieldValueManager {
private originData: WidgetFieldValueMap;

private modifiedData: WidgetFieldValueMap;

private fieldValidators: Record<keyof WidgetFieldTypeMap, FieldValueValidator<any>>;

private validationErrors: Record<string, string> = {};

constructor(
originData: WidgetFieldValueMap,
fieldValidators: Record<keyof WidgetFieldTypeMap, FieldValueValidator<any>>,
) {
this.originData = originData;
this.modifiedData = { ...originData };
this.fieldValidators = fieldValidators;
}

setFieldValue<Key extends keyof WidgetFieldTypeMap>(key: Key, value: WidgetFieldTypeMap[Key]): boolean {
const field = this.modifiedData[key] || this.originData[key];
if (!field) {
throw new Error(`Field "${key}" does not exist.`);
}

this.modifiedData[key] = { ...field, value };

const validator = this.fieldValidators[key];
if (validator) {
const isValid = validator(this.modifiedData[key]);
if (!isValid) {
this.validationErrors[key as string] = `Invalid value for field "${key}"`;
return false;
}
}

delete this.validationErrors[key as string];
return true;
}

validateAll(): boolean {
this.validationErrors = {};
let isValid = true;

Object.entries(this.modifiedData).forEach(([key, field]) => {
const validator = this.fieldValidators[key];
if (validator && !validator(field)) {
this.validationErrors[key] = `Invalid value for field "${key}"`;
isValid = false;
}
});

return isValid;
}

getValidationErrors(): Record<string, string> {
return this.validationErrors;
}

getData(): WidgetFieldValueMap {
return this.modifiedData;
}

resetToOrigin(): void {
this.modifiedData = { ...this.originData };
this.validationErrors = {};
}

setOrigin(data: WidgetFieldValueMap): void {
this.originData = { ...data };
this.modifiedData = { ...data };
this.validationErrors = {};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import type { AdvancedFormatRulesValue } from '@/common/modules/widgets/_widget-fields/advanced-format-rules/type';
import type { CategoryByValue } from '@/common/modules/widgets/_widget-fields/category-by/type';
import type { ColorSchemaValue } from '@/common/modules/widgets/_widget-fields/color-schema/type';
import type { ComparisonValue } from '@/common/modules/widgets/_widget-fields/comparison/type';
import type { CustomTableColumnWidthValue } from '@/common/modules/widgets/_widget-fields/custom-table-column-width/type';
import type { DataFieldHeatmapColorValue } from '@/common/modules/widgets/_widget-fields/data-field-heatmap-color/type';
import type { DataFieldValue } from '@/common/modules/widgets/_widget-fields/data-field/type';
import type { DateFormatValue } from '@/common/modules/widgets/_widget-fields/date-format/type';
import type { DateRangeValue } from '@/common/modules/widgets/_widget-fields/date-range/type';
import type { DisplayAnnotationValue } from '@/common/modules/widgets/_widget-fields/display-annotation/type';
import type { DisplaySeriesLabelValue } from '@/common/modules/widgets/_widget-fields/display-series-label/type';
import type { FormatRulesValue } from '@/common/modules/widgets/_widget-fields/format-rules/type';
import type { GranularityValue } from '@/common/modules/widgets/_widget-fields/granularity/type';
import type { GroupByValue } from '@/common/modules/widgets/_widget-fields/group-by/type';
import type { WidgetHeaderValue } from '@/common/modules/widgets/_widget-fields/header/type';
import type { IconValue } from '@/common/modules/widgets/_widget-fields/icon/type';
import type { LegendValue } from '@/common/modules/widgets/_widget-fields/legend/type';
import type { LineByValue } from '@/common/modules/widgets/_widget-fields/line-by/type';
import type { MaxValue } from '@/common/modules/widgets/_widget-fields/max/type';
import type { MinValue } from '@/common/modules/widgets/_widget-fields/min/type';
import type { MissingValueValue } from '@/common/modules/widgets/_widget-fields/missing-value/type';
import type { NumberFormatValue } from '@/common/modules/widgets/_widget-fields/number-format/type';
import type { PieChartTypeValue } from '@/common/modules/widgets/_widget-fields/pie-chart-type/type';
import type { ProgressBarValue } from '@/common/modules/widgets/_widget-fields/progress-bar/type';
import type { StackByValue } from '@/common/modules/widgets/_widget-fields/stack-by/type';
import type { SubTotalValue } from '@/common/modules/widgets/_widget-fields/sub-total/type';
import type { TableColumnWidthValue } from '@/common/modules/widgets/_widget-fields/table-column-width/type';
import type { TableDataFieldValue } from '@/common/modules/widgets/_widget-fields/table-data-field/type';
import type { TextWrapValue } from '@/common/modules/widgets/_widget-fields/text-wrap/type';
import type { TooltipNumberFormatValue } from '@/common/modules/widgets/_widget-fields/tooltip-number-format/type';
import type { TotalValue } from '@/common/modules/widgets/_widget-fields/total/type';
import type { WidgetHeightValue } from '@/common/modules/widgets/_widget-fields/widget-height/type';
import type { XAxisValue } from '@/common/modules/widgets/_widget-fields/x-axis/type';
import type { YAxisValue } from '@/common/modules/widgets/_widget-fields/y-axis/type';

export interface WidgetFieldValueMap {
[key: string]: WidgetFieldValue;
}

export interface WidgetFieldValue {
value: any;
meta?: Record<string, any>;
}

export interface WidgetFieldTypeMap {
xAxis: XAxisValue;
yAxis: YAxisValue;
widgetHeight: WidgetHeightValue;
total: TotalValue;
tooltipNumberFormat: TooltipNumberFormatValue;
textWrap: TextWrapValue;
tableDataField: TableDataFieldValue;
tableColumnWidth: TableColumnWidthValue;
subTotal: SubTotalValue;
stackBy: StackByValue;
progressVar: ProgressBarValue;
pieChartType: PieChartTypeValue;
numberFormat: NumberFormatValue;
missingValue: MissingValueValue;
min: MinValue;
max: MaxValue;
lineBy: LineByValue;
legend: LegendValue;
icon: IconValue;
header: WidgetHeaderValue;
groupBy: GroupByValue;
granularity: GranularityValue;
formatRules: FormatRulesValue;
displaySeriesLabel: DisplaySeriesLabelValue;
displayAnnotation: DisplayAnnotationValue;
dateRange: DateRangeValue;
dateFormat: DateFormatValue;
dataFieldHeatmapColor: DataFieldHeatmapColorValue;
dataField: DataFieldValue;
customTableColumnWidth: CustomTableColumnWidthValue;
comparison: ComparisonValue;
colorSchema: ColorSchemaValue;
categoryBy: CategoryByValue;
advancedFormatRules: AdvancedFormatRulesValue;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@

export interface DataFieldOptions {
multiSelectable?: boolean;
}

export interface DataFieldValue {
data: string|string[];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

export interface GranularityValue {
granularity: 'YEARLY' | 'MONTHLY' | 'DAILY';
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@

export interface MaxOptions {
default?: number;
}

export interface MaxValue {
max: number;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@

export interface MinOptions {
default?: number;
}

export interface MinValue {
min: number;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@

export interface PieChartTypeOptions {
default?: string;
}

export interface PieChartTypeValue {
type: 'pie' | 'donut';
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ export interface SubTotalOptions {
default?: boolean;
toggle?: boolean;
}


export interface SubTotalValue {
toggleValue: boolean;
freeze: boolean;
}
Loading