Browse Source

refactor get and set DataResolver to componentWrapper

pages-datasources
mohamedsalem401 1 year ago
parent
commit
436c06406d
  1. 10
      packages/core/src/data_sources/model/ComponentWithCollectionsState.ts
  2. 12
      packages/core/src/data_sources/model/data_collection/ComponentDataCollection.ts
  3. 16
      packages/core/src/dom_components/model/ComponentWrapper.ts

10
packages/core/src/data_sources/model/ComponentWithCollectionsState.ts

@ -11,7 +11,7 @@ import { isArray } from 'underscore';
export type DataVariableMap = Record<string, DataVariableProps>; export type DataVariableMap = Record<string, DataVariableProps>;
export default class ComponentWithCollectionsState extends Component { export default class ComponentWithCollectionsState<DataResolverType> extends Component {
collectionsStateMap: DataCollectionStateMap = {}; collectionsStateMap: DataCollectionStateMap = {};
dataSourceWatcher?: DataResolverListener; dataSourceWatcher?: DataResolverListener;
@ -42,6 +42,14 @@ export default class ComponentWithCollectionsState extends Component {
this.components().forEach((cmp) => cmp.syncComponentsCollectionState?.()); this.components().forEach((cmp) => cmp.syncComponentsCollectionState?.());
} }
setDataResolver(dataResolver: DataResolverType | undefined) {
return this.set('dataResolver', dataResolver);
}
getDataResolver(): DataResolverType | undefined {
return this.get('dataResolver');
}
protected listenToDataSource() { protected listenToDataSource() {
const path = this.dataSourcePath; const path = this.dataSourcePath;
if (!path) return; if (!path) return;

12
packages/core/src/data_sources/model/data_collection/ComponentDataCollection.ts

@ -19,7 +19,7 @@ import ComponentWithCollectionsState, { DataVariableMap } from '../ComponentWith
const AvoidStoreOptions = { avoidStore: true, partial: true }; const AvoidStoreOptions = { avoidStore: true, partial: true };
export default class ComponentDataCollection extends ComponentWithCollectionsState { export default class ComponentDataCollection extends ComponentWithCollectionsState<DataCollectionProps> {
dataSourceWatcher?: DataResolverListener; dataSourceWatcher?: DataResolverListener;
get defaults(): ComponentDefinitionDefined { get defaults(): ComponentDefinitionDefined {
@ -51,10 +51,6 @@ export default class ComponentDataCollection extends ComponentWithCollectionsSta
return cmp; return cmp;
} }
getDataResolver() {
return this.get('dataResolver');
}
getItemsCount() { getItemsCount() {
const items = this.getDataSourceItems(); const items = this.getDataSourceItems();
const itemsCount = getLength(items); const itemsCount = getLength(items);
@ -87,10 +83,6 @@ export default class ComponentDataCollection extends ComponentWithCollectionsSta
return this.firstChild.components(); return this.firstChild.components();
} }
setDataResolver(props: DataCollectionProps) {
return this.set('dataResolver', props);
}
setCollectionId(collectionId: string) { setCollectionId(collectionId: string) {
this.updateCollectionConfig({ collectionId }); this.updateCollectionConfig({ collectionId });
} }
@ -296,7 +288,7 @@ export default class ComponentDataCollection extends ComponentWithCollectionsSta
} }
private get collectionId() { private get collectionId() {
return this.getDataResolver().collectionId as string; return this.getDataResolver()?.collectionId ?? '';
} }
static isComponent(el: HTMLElement) { static isComponent(el: HTMLElement) {

16
packages/core/src/dom_components/model/ComponentWrapper.ts

@ -9,7 +9,7 @@ import { DataVariableProps } from '../../data_sources/model/DataVariable';
import { DataCollectionStateMap } from '../../data_sources/model/data_collection/types'; import { DataCollectionStateMap } from '../../data_sources/model/data_collection/types';
import ComponentWithCollectionsState, { DataVariableMap } from '../../data_sources/model/ComponentWithCollectionsState'; import ComponentWithCollectionsState, { DataVariableMap } from '../../data_sources/model/ComponentWithCollectionsState';
export default class ComponentWrapper extends ComponentWithCollectionsState { export default class ComponentWrapper extends ComponentWithCollectionsState<DataVariableProps> {
dataSourceWatcher?: DataResolverListener; dataSourceWatcher?: DataResolverListener;
get defaults() { get defaults() {
@ -41,13 +41,9 @@ export default class ComponentWrapper extends ComponentWithCollectionsState {
super(props, opt); super(props, opt);
const hasDataResolver = this.getDataResolver(); const hasDataResolver = this.getDataResolver();
console.log('🚀 ~ ComponentWrapper ~ constructor ~ hasDataResolver:', hasDataResolver);
if (hasDataResolver) { if (hasDataResolver) {
this.syncComponentsCollectionState(); this.syncComponentsCollectionState();
console.log(
'🚀 ~ ComponentWrapper ~ constructor ~ this.getCollectionsStateMap():',
this.getCollectionsStateMap(),
);
this.onCollectionsStateMapUpdate(this.getCollectionsStateMap()); this.onCollectionsStateMapUpdate(this.getCollectionsStateMap());
} }
} }
@ -100,14 +96,6 @@ export default class ComponentWrapper extends ComponentWithCollectionsState {
return asDoc ? `${doctype}<html${docElAttrStr}>${headStr}${body}</html>` : body; return asDoc ? `${doctype}<html${docElAttrStr}>${headStr}${body}</html>` : body;
} }
setDataResolver(dataResolver: DataVariableProps) {
return this.set('dataResolver', dataResolver);
}
getDataResolver() {
return this.get('dataResolver');
}
onCollectionsStateMapUpdate(collectionsStateMap: DataCollectionStateMap) { onCollectionsStateMapUpdate(collectionsStateMap: DataCollectionStateMap) {
const { page, head } = this; const { page, head } = this;
super.onCollectionsStateMapUpdate(collectionsStateMap); super.onCollectionsStateMapUpdate(collectionsStateMap);

Loading…
Cancel
Save