Skip to content

Commit

Permalink
Properly update privateWidgets field
Browse files Browse the repository at this point in the history
  • Loading branch information
ohansFavour committed Jan 19, 2022
1 parent 703b170 commit 1a499b2
Showing 1 changed file with 33 additions and 9 deletions.
42 changes: 33 additions & 9 deletions app/client/src/widgets/ListWidget/widget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
omit,
floor,
isEmpty,
isEqual,
} from "lodash";
import memoizeOne from "memoize-one";
import shallowEqual from "shallowequal";
Expand Down Expand Up @@ -40,6 +41,12 @@ import { escapeSpecialChars } from "../../WidgetUtils";
import { PrivateWidgets } from "entities/DataTree/dataTreeFactory";

const LIST_WIDGEY_PAGINATION_HEIGHT = 36;

/* in the List Widget, "children.0.children.0.children.0.children" is the path to the list of all
widgets present in the List Widget
*/
const PATH_TO_ALL_WIDGETS_IN_LIST_WIDGET =
"children.0.children.0.children.0.children";
class ListWidget extends BaseWidget<ListWidgetProps<WidgetProps>, WidgetState> {
state = {
page: 1,
Expand Down Expand Up @@ -74,6 +81,8 @@ class ListWidget extends BaseWidget<ListWidgetProps<WidgetProps>, WidgetState> {
this.generateChildrenDefaultPropertiesMap(this.props);
this.generateChildrenMetaPropertiesMap(this.props);
this.generateChildrenEntityDefinitions(this.props);

// add privateWidgets to ListWidget
this.addPrivateWidgetsForChildren(this.props);
}

Expand Down Expand Up @@ -115,17 +124,18 @@ class ListWidget extends BaseWidget<ListWidgetProps<WidgetProps>, WidgetState> {
}
}

// updates the "privateWidgets" field of the List Widget
addPrivateWidgetsForChildren(props: ListWidgetProps<WidgetProps>) {
const template = props.template;
const privateWidgets: PrivateWidgets = {};
const listWidgetChildren: WidgetProps[] = get(
props,
PATH_TO_ALL_WIDGETS_IN_LIST_WIDGET,
);
listWidgetChildren.map((child) => {
privateWidgets[child.widgetName] = true;
});

if (template) {
Object.keys(template).map((key: string) => {
privateWidgets[key] = true;
});

super.updateWidgetProperty("privateWidgets", privateWidgets);
}
super.updateWidgetProperty("privateWidgets", privateWidgets);
}

generateChildrenDefaultPropertiesMap = (
Expand Down Expand Up @@ -189,6 +199,16 @@ class ListWidget extends BaseWidget<ListWidgetProps<WidgetProps>, WidgetState> {
};

componentDidUpdate(prevProps: ListWidgetProps<WidgetProps>) {
const currentListWidgetChildren: WidgetProps[] = get(
this.props,
PATH_TO_ALL_WIDGETS_IN_LIST_WIDGET,
);

const previousListWidgetChildren: WidgetProps[] = get(
prevProps,
PATH_TO_ALL_WIDGETS_IN_LIST_WIDGET,
);

if (
xor(
Object.keys(get(prevProps, "template", {})),
Expand All @@ -198,7 +218,6 @@ class ListWidget extends BaseWidget<ListWidgetProps<WidgetProps>, WidgetState> {
this.generateChildrenDefaultPropertiesMap(this.props);
this.generateChildrenMetaPropertiesMap(this.props);
this.generateChildrenEntityDefinitions(this.props);
this.addPrivateWidgetsForChildren(this.props);
}

if (this.props.serverSidePaginationEnabled) {
Expand Down Expand Up @@ -249,6 +268,11 @@ class ListWidget extends BaseWidget<ListWidgetProps<WidgetProps>, WidgetState> {
},
);
}

// Update privateWidget field if there is a change in the List widget children
if (!isEqual(currentListWidgetChildren, previousListWidgetChildren)) {
this.addPrivateWidgetsForChildren(this.props);
}
}

static getDefaultPropertiesMap(): Record<string, string> {
Expand Down

0 comments on commit 1a499b2

Please sign in to comment.