Skip to content

Commit

Permalink
Merge pull request #19 from BeverCRM/bug/openLookupForm
Browse files Browse the repository at this point in the history
fixed paging issue
  • Loading branch information
ArsenAghajanyan authored Apr 18, 2023
2 parents c77522d + 79979d5 commit 0a4c9e8
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 39 deletions.
5 changes: 1 addition & 4 deletions FetchToSubgrid/components/FetchToSubgrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ export const FetchToSubgrid: React.FC<IFetchToSubgridProps> = props => {
listInputsHashCode.current = hashCode(`${allocatedWidth}${fetchXml}`);
}, [allocatedWidth]);

React.useMemo(async () => {
totalRecordsCount.current = await dataverseService.getRecordsCount(fetchXml ?? '');
}, [fetchXml, isDialogAccepted]);

React.useEffect(() => setCurrentPage(1), [pageSize, fetchXml]);

React.useEffect(() => {
Expand All @@ -83,6 +79,7 @@ export const FetchToSubgrid: React.FC<IFetchToSubgridProps> = props => {
const fetchItems = async () => {
isButtonActive = false;
setIsLoading(true);
totalRecordsCount.current = await dataverseService.getRecordsCount(fetchXml ?? '');
if (isDialogAccepted) return;

try {
Expand Down
2 changes: 1 addition & 1 deletion FetchToSubgrid/components/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const Footer: React.FC<IFooterProps> = props => {
} = props;

const firstItemIndex = (currentPage - 1) * pageSize + 1;
const lastItemIndex = firstItemIndex + pageSize - 1;
const lastItemIndex = Math.min(firstItemIndex + pageSize - 1, totalRecordsCount);
const nextButtonDisabled = Math.ceil(totalRecordsCount / pageSize) <= currentPage;

function moveToFirst() {
Expand Down
2 changes: 1 addition & 1 deletion FetchToSubgrid/components/LinkableItems.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const LinkableItem: React.FC<ILinkableItemProps> = ({
if (item.isLinkEntity) {
dataverseService.openLinkEntityRecordForm(item.entity, item.fieldName);
}
else if (checkIfAttributeIsEntityReference(item.AttributeType)) {
else if (checkIfAttributeIsEntityReference(item.attributeType)) {
dataverseService.openLookupForm(item.entity, item.fieldName);
}
else {
Expand Down
44 changes: 29 additions & 15 deletions FetchToSubgrid/services/dataverseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class DataverseService implements IDataverseService {
fetchXmlOrJson,
allocatedWidth: this.getAllocatedWidth(),
default: {
fetchXml: this._context.parameters.defaultFetchXmlProperty.raw,
fetchXml: this._context.parameters.defaultFetchXml.raw,
pageSize: defaultPageSize,
newButtonVisibility: this._context.parameters.newButtonVisibility.raw === '1',
deleteButtonVisibility: this._context.parameters.deleteButtonVisibility.raw === '1',
Expand Down Expand Up @@ -105,26 +105,34 @@ export class DataverseService implements IDataverseService {
}

public async getRecordsCount(fetchXml: string): Promise<number> {
let pagingCookie = null;
let numberOfRecords = 0;
let page = 0;
const parser: DOMParser = new DOMParser();
const xmlDoc: Document = parser.parseFromString(fetchXml, 'text/xml');
const fetch: Element = xmlDoc.getElementsByTagName('fetch')?.[0];
let pagingCookie: string | null = null;

const entityName: string = getEntityNameFromFetchXml(fetchXml);
fetch?.removeAttribute('count');
const changedAliasNames: string = changeAliasNames(fetchXml);

do {
const updateFetchXml = (xml: string, page: number): string => {
const parser: DOMParser = new DOMParser();
const xmlDoc: Document = parser.parseFromString(xml, 'text/xml');
const fetch: Element = xmlDoc.getElementsByTagName('fetch')?.[0];

fetch?.removeAttribute('count');
fetch?.removeAttribute('page');
fetch.setAttribute('page', `${++page}`);
// eslint-disable-next-line no-invalid-this

fetch.setAttribute('page', `${page}`);

const serializer = new XMLSerializer();
return serializer.serializeToString(xmlDoc);
};

do {
const updatedFetchXml: string = updateFetchXml(changedAliasNames, ++page);
const data: any = await this._context.webAPI.retrieveMultipleRecords(
entityName, `?fetchXml=${encodeURIComponent(changedAliasNames)}`);
entityName, `?fetchXml=${encodeURIComponent(updatedFetchXml)}`);

numberOfRecords += data.entities.length;
pagingCookie = data.fetchXmlPagingCookie;

} while (pagingCookie);

return numberOfRecords;
Expand Down Expand Up @@ -153,10 +161,16 @@ export class DataverseService implements IDataverseService {
}

public openLookupForm(entity: Entity, fieldName: string): void {
const entityName: string = entity[
`_${fieldName}[email protected]`];

const entityId: string = entity[`_${fieldName}_value`];
let entityName: string;
let entityId: string;
if (fieldName.startsWith('alias')) {
entityName = entity[`${fieldName}@Microsoft.Dynamics.CRM.lookuplogicalname`];
entityId = entity[`${fieldName}`];
}
else {
entityName = entity[`_${fieldName}[email protected]`];
entityId = entity[`_${fieldName}_value`];
}
this.openRecordForm(entityName, entityId);
}

Expand Down
1 change: 0 additions & 1 deletion FetchToSubgrid/styles/comandBarStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export const dataSetStyles = mergeStyleSets({
},
buttons: {
height: '44px',
paddingRight: '20px',
},
detailsList: {
paddingTop: '0px',
Expand Down
6 changes: 3 additions & 3 deletions FetchToSubgrid/utilities/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ interface IJsonProps {
type JsonAllowedProps = Array<keyof IJsonProps>;

export const checkIfAttributeIsEntityReference = (attributeType: AttributeType): boolean => {
const attributetypes: AttributeType[] = [
const attributeTypes: AttributeType[] = [
AttributeType.Lookup,
AttributeType.Owner,
AttributeType.Customer,
];

return attributetypes.includes(attributeType);
return attributeTypes.includes(attributeType);
};

export const checkIfAttributeRequiresFormattedValue = (attributeType: AttributeType) => {
export const checkIfAttributeRequiresFormattedValue = (attributeType: AttributeType): boolean => {
const attributeTypes: AttributeType[] = [
AttributeType.Money,
AttributeType.PickList,
Expand Down
14 changes: 0 additions & 14 deletions Solution/README.md

This file was deleted.

0 comments on commit 0a4c9e8

Please sign in to comment.