Skip to content

Commit

Permalink
fix(global): 🐛 fix how data is entitydata is passed in appbuilder fac…
Browse files Browse the repository at this point in the history
…tory

also write comment in code for better understanding

Ref #54
  • Loading branch information
PritamBag committed Jan 6, 2025
1 parent e9fedde commit af5f7fa
Show file tree
Hide file tree
Showing 10 changed files with 305 additions and 92 deletions.
2 changes: 1 addition & 1 deletion app/components/AppBuilder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export default function AppBuilder<T extends AppBuilderData>() {

useEffect(() => {
if(!checkIfBuilderExist()){
const appBuilderData:T = AppBuilderFactory.getAppBuilderData(builderType);
const appBuilderData:T = AppBuilderFactory.getAppBuilderData(builderType, entityData);

dispatch(setBuilderData(appBuilderData, entityData, currentState));
}
Expand Down
154 changes: 120 additions & 34 deletions app/components/data/app-builder/AppBuilder.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@ import { ToolbarData } from "../toolbar/Toolbar.data";
import { ToolBoxData } from "../toolbox/ToolBox.data";
import { ViewerData } from "../viewer/Viewer.data";

/** An abstract base class for managing builder data in application builders. */
/**
* An abstract base class for managing builder data in application builders.
* This class provides core functionalities to set up and manage builder components such as toolboxes, viewers, and toolbars.
*/
export abstract class AppBuilderData {

/** readonly property to store builder id. */
readonly builderId:string;

/** Private property to store builder type. */
private builderType: typeof AppBuilderFactory.BUILDER_TYPES[keyof typeof AppBuilderFactory.BUILDER_TYPES];

/** Private property to store entity data. */
private entityData: DynamicEntity;

/** Private property to store toolbox-related data. */
Expand All @@ -25,107 +31,187 @@ export abstract class AppBuilderData {
/** Private property to store viewer-related data. */
private viewers: ViewerData[] = [];

private toolbars: ToolbarData[];
/** Private property to store toolbar-related data. */
private toolbars: ToolbarData[] = [];

/** Private property to store builder icon. */
private builderIcon: string;
private defaultViewersMeta: any;

/**
* Constructor to initialize the builder with the given data.
*
* @param entityData - The dynamic entity data for this builder.
* @param builderType - The type of builder (e.g., Default, Business, etc.).
* @param builderIcon - The icon for the builder (defaults to STAR_ICON).
* @param defaultToolboxeMeta - Metadata for initializing toolboxes.
* @param defaultViewerMeta - Metadata for initializing viewers.
* @param toolbarMeta - Icons for initializing the toolbar.
*/
constructor(
entityData: DynamicEntity,
builderType: typeof AppBuilderFactory.BUILDER_TYPES[keyof typeof AppBuilderFactory.BUILDER_TYPES],
builderIcon = STAR_ICON,
defaultToolboxesMeta: { [key: string]: any },
defaultViewersMeta: { [key: string]: any},
toolbarIcons: string[]
defaultToolboxeMeta: { [key: string]: any },
defaultViewerMeta: string[],
toolbarMeta: string[]
) {
this.builderId = getWrappidUUID();
this.builderId = getWrappidUUID(); // set builderId a Universally Unique Identifier
this.builderType = builderType;
this.builderIcon = builderIcon;
this.prepareToolBoxes(defaultToolboxesMeta);
this.defaultViewersMeta = defaultViewersMeta;
this.prepareViewers();
this.toolbars = this.prepareToolbarData(toolbarIcons);
this.entityData = entityData;
this.prepareToolBoxes(defaultToolboxeMeta);
this.prepareViewers(entityData, defaultViewerMeta);
this.prepareToolbars(toolbarMeta);
}

/**
* Getter method to retrieve the builder type.
* @returns The builder type.
*/
getBuilderType() {
return this.builderType;
}

/**
* Getter method to retrieve the current entity data.
* @returns The current entity data.
*/
getEntityData() {
return this.entityData;
}

/**
* Getter method to retrieve the current toolbox data.
* @returns {ToolBoxData} The current toolbox data.
* @returns An array of toolbox data.
*/
getToolBoxes(): ToolBoxData[] {
return this.toolboxes;
}

/**
* Getter method to retrieve the current viewer data.
* @returns An array of viewer data.
*/
getViewers(): ViewerData[] {
return this.viewers;
}

/**
* Getter method to retrieve the current toolbar data.
* @returns An array of toolbar data.
*/
getToolbars(): ToolbarData[] {
return this.toolbars;
}

/**
* Getter method to retrieve the builder icon.
* @returns The builder icon.
*/
getBuilderIcon() {
return this.builderIcon;
}

/**
* Setter for entity data.
* @param data - New entity data to set.
*/
setEntityData(data: any) {
this.entityData = data;
this.prepareViewers();
}

// Setter for viewers - this is the setViewers function
/**
* Setter for viewers.
* @param viewers - The new viewers data to set.
*/
setViewers(viewers: ViewerData[]): void {
this.viewers = viewers; // Update the viewers with the new list
}

/**
* Prepare toolboxes - should be called after child class properties are set
* Prepare toolboxes based on the provided metadata.
* This method will process the metadata and initialize the toolboxes.
*
* @param defaultToolboxeMeta - The metadata for initializing toolboxes.
*/
private prepareToolBoxes(defaultToolboxesMeta: { [key: string]: any } = {}): void {
Object.keys(defaultToolboxesMeta).forEach(eachToolBoxVariant => {
private prepareToolBoxes(defaultToolboxeMeta: { [key: string]: any } = {}): void {
// Iterate through the metadata for each toolbox variant and initialize them.
Object.keys(defaultToolboxeMeta).forEach(eachToolBoxVariant => {
const toolboxData = ToolBoxFactory.getToolBoxData(eachToolBoxVariant);

toolboxData.setToolboxContent(defaultToolboxesMeta[eachToolBoxVariant]);
toolboxData.setToolboxContent(defaultToolboxeMeta[eachToolBoxVariant]);
this.addToolBox(toolboxData);

});
}

/**
* Helper method to add a toolbox to the toolboxes array.
*
* @param toolboxData - The toolbox data to add.
*/
private addToolBox(toolboxData: ToolBoxData): void {
this.toolboxes.push(toolboxData);
}

private prepareViewers(): void {
Object.keys(this.defaultViewersMeta).forEach(eachViewerTab => {
const entityData = this.getEntityData();
/**
* Prepare viewers based on the provided metadata and entity data.
* This method will initialize viewers using the factory and the given metadata.
*
* @param entityData - The dynamic entity data.
* @param defaultViewerMeta - The metadata for initializing viewers.
* @throws Error if entityData is invalid or empty.
*/
private prepareViewers(entityData: DynamicEntity, defaultViewerMeta: string[]): void {
// @todo need to update when we handle enpty entityData
if (!entityData || Object.keys(entityData).length === 0) {
throw new Error("Entity data is not set. Unable to prepare viewers.");
}

if (!this.entityData || Object.keys(this.entityData).length === 0) {
// eslint-disable-next-line no-console
console.warn("Entity data is not set. Skipping viewer preparation.");
return;
}
if (!entityData) {
throw new Error("Entity data is not set. Unable to prepare viewers.");
}
// If entityData is undefined, throw an error (redundant but ensures completeness).
if (entityData === undefined) {
throw new Error("Entity data undefined. Unable to prepare viewers.");
}

const viewerData = ViewerFactory.getViewerData(eachViewerTab, entityData);
// Iterate over the viewer metadata and initialize each viewer.
defaultViewerMeta.forEach(viewerVariant => {
const viewerData = ViewerFactory.getViewerData(viewerVariant, entityData);

// eslint-disable-next-line etc/no-commented-out-code
// viewerData.setViewerContent(defaultViewersMeta[eachViewerTab]);
this.addViewer(viewerData);
});
}

/**
* Helper method to add a viewer to the viewers array.
*
* @param viewerData - The viewer data to add.
*/
private addViewer(viewerData: ViewerData): void {
this.viewers.push(viewerData);
}

private prepareToolbarData(toolbarIcons: string[]): ToolbarData[] {
return toolbarIcons.map((icon) => ToolbarFactory.getViewerData<ToolbarData>(icon));
/**
* Prepare toolbars based on the provided icons.
* This method will initialize toolbar data using the provided icons.
*
* @param toolbarMeta - The icons for initializing toolbars.
* @returns An array of toolbar data.
*/
private prepareToolbars(toolbarMeta: string[]): void {
// Map the icons to create toolbar data using the factory.
toolbarMeta.forEach((icon) => {
const toolbarData = ToolbarFactory.getViewerData(icon);

this.addToolbar(toolbarData); // Add each toolbarData individually
});
}

/**
* Helper method to add a viewer to the viewers array.
*
* @param toolbarData - The viewer data to add.
*/
private addToolbar(toolbarData: ToolbarData): void {
this.toolbars.push(toolbarData);
}
}
30 changes: 23 additions & 7 deletions app/components/data/app-builder/BusinessBuilder.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,36 @@ import { AppBuilderData } from "./AppBuilder.data";
import { BUSINESS_ICON } from "../../../constants/AppBuilder.constant";
import AppBuilderFactory from "../../../factory/AppBuilder.factory";
import ToolbarFactory from "../../../factory/Toolbar.factory";
import { DynamicEntity } from "../model/DynamicEntity.interface";

/**
* A concrete implementation of the `AppBuilderData` abstract class.
* This class specializes in managing and preparing toolbox data for a default builder.
* This class specializes in managing and preparing toolbox, viewer, toolbar data for a default builder.
*/
export class BusinessBuilderData extends AppBuilderData {

/** Static property to store meta information related to the toolbox.
* This updated or accessed across instances of this class. */
private static toolboxMeta = {};

private static viewerMeta = {};
/** Static property to store name of viewers. */
private static viewerMeta = [];

private static toolbarIcons = [ToolbarFactory.ICONS.HISTORY];
/** Static property to store icons related to the toolbar. */
private static toolbarMeta = [ToolbarFactory.ICONS.HISTORY];

constructor(builderIcon = BUSINESS_ICON) {
super(AppBuilderFactory.BUILDER_TYPES.BUSINESS, builderIcon, BusinessBuilderData.toolboxMeta, BusinessBuilderData.viewerMeta, BusinessBuilderData.toolbarIcons);
/**
* The constructor initializes an instance of `BusinessBuilderData`
* with the provided entity data and pre-defined constants.
*
* @param entityData The dynamic entity data that is passed to the base class constructor.
*/
constructor(entityData: DynamicEntity) {
super(
entityData,
AppBuilderFactory.BUILDER_TYPES.BUSINESS,
BUSINESS_ICON,
BusinessBuilderData.toolboxMeta,
BusinessBuilderData.viewerMeta,
BusinessBuilderData.toolbarMeta);
}
}
}
29 changes: 23 additions & 6 deletions app/components/data/app-builder/DefaultBuilder.data.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
import { AppBuilderData } from "./AppBuilder.data";
import { DEFAULT_ICON } from "../../../constants/AppBuilder.constant";
import AppBuilderFactory from "../../../factory/AppBuilder.factory";
import { DynamicEntity } from "../model/DynamicEntity.interface";

/**
* A concrete implementation of the `AppBuilderData` abstract class.
* This class specializes in managing and preparing toolbox data for a default builder.
* This class specializes in managing and preparing toolbox, viewer, toolbar data for a default builder.
*/
export class DefaultBuilderData extends AppBuilderData {
/** Static property to store meta information related to the toolbox.
* This updated or accessed across instances of this class. */
private static toolboxMeta = {};

private static viewerMeta = {};
/** Static property to store name of viewers. */
private static viewerMeta = [];

private static toolbarIcons = [];
/** Static property to store icons related to the toolbar. */
private static toolbarMeta = [];

constructor(builderIcon = DEFAULT_ICON) {
super(AppBuilderFactory.BUILDER_TYPES.DEFAULT, builderIcon, DefaultBuilderData.toolboxMeta, DefaultBuilderData.viewerMeta, DefaultBuilderData.toolbarIcons);
/** Constructor that initializes an instance of `DefaultBuilderData`
* with the provided entity data and pre-defined constants.
*
* @param entityData The dynamic entity data that is passed to the base class constructor.
* */
constructor(entityData: DynamicEntity) {
super(
entityData,
AppBuilderFactory.BUILDER_TYPES.DEFAULT,
DEFAULT_ICON,
DefaultBuilderData.toolboxMeta,
DefaultBuilderData.viewerMeta,
DefaultBuilderData.toolbarMeta
);
}
}
}
27 changes: 21 additions & 6 deletions app/components/data/app-builder/FormBuilder.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,36 @@ import { FORM_ICON } from "../../../constants/AppBuilder.constant";
import AppBuilderFactory from "../../../factory/AppBuilder.factory";
import ToolbarFactory from "../../../factory/Toolbar.factory";
import ToolBoxFactory from "../../../factory/ToolBox.factory";
import { DynamicEntity } from "../model/DynamicEntity.interface";

/**
* A concrete implementation of the `AppBuilderData` abstract class.
* This class specializes in managing and preparing toolbox data for a form builder.
* This class specializes in managing and preparing toolbox, viewer, toolbar data for a form builder.
*/
export class FormBuilderData extends AppBuilderData {

/** Static property to store meta information related to the toolbox.
* This updated or accessed across instances of this class. */
private static toolboxMeta = { [ToolBoxFactory.VARIANTS.PROPERTY]: {} };

private static viewerMeta = {};
/** Static property to store name of viewers. */
private static viewerMeta = [];

private static toolbarIcons = [ToolbarFactory.ICONS.HISTORY];
/** Static property to store icons related to the toolbar. */
private static toolbarMeta = [ToolbarFactory.ICONS.HISTORY];

constructor(builderIcon = FORM_ICON) {
super(AppBuilderFactory.BUILDER_TYPES.FORM, builderIcon, FormBuilderData.toolboxMeta, FormBuilderData.viewerMeta, FormBuilderData.toolbarIcons);
/** Constructor that initializes an instance of `FormBuilderData`
* with the provided entity data and pre-defined constants.
*
* @param entityData The dynamic entity data that is passed to the base class constructor.
* */
constructor(entityData: DynamicEntity) {
super(
entityData,
AppBuilderFactory.BUILDER_TYPES.FORM,
FORM_ICON, FormBuilderData.toolboxMeta,
FormBuilderData.viewerMeta,
FormBuilderData.toolbarMeta
);
}

}
Loading

0 comments on commit af5f7fa

Please sign in to comment.