Skip to content

Commit

Permalink
fixed bugs related to "error message" and "column width" (#20)
Browse files Browse the repository at this point in the history
* fixed bugs related to "error message" and "column width"
---------

Co-authored-by: Hovhannes Dilanyan <[email protected]>
  • Loading branch information
Artur-Sahakyan and Hovhannes Dilanyan authored Apr 20, 2023
1 parent a16d20a commit 5ed32eb
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
3 changes: 1 addition & 2 deletions FetchToSubgrid/components/AppWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ export const AppWrapper: React.FC<IAppWrapperProps> = props => {
const [error, setError] = React.useState<Error | undefined>(undefined);

const fetchToSubgridProps: IFetchToSubgridProps = parseRawInput(props);
if (fetchToSubgridProps.error) setError(error);

React.useEffect(() => {
setError(undefined);
setError(fetchToSubgridProps.error);
}, [props.fetchXmlOrJson]);

return (
Expand Down
5 changes: 2 additions & 3 deletions FetchToSubgrid/components/FetchToSubgrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,8 @@ export const FetchToSubgrid: React.FC<IFetchToSubgridProps> = props => {

React.useLayoutEffect(() => {
if (allocatedWidth === -1) return;

listInputsHashCode.current = hashCode(`${allocatedWidth}${fetchXml}`);
}, [allocatedWidth]);
}, [allocatedWidth, columns]);

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

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

try {
totalRecordsCount.current = await dataverseService.getRecordsCount(fetchXml ?? '');
const newFetchXml = addOrderToFetch(fetchXml, sortingData);

const data: IItemsData = {
Expand Down
20 changes: 18 additions & 2 deletions FetchToSubgrid/services/dataverseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class DataverseService implements IDataverseService {

public async getEntityDisplayName(entityName: string): Promise<string> {
const entityMetadata: EntityMetadata = await this._context.utils.getEntityMetadata(entityName);
return entityMetadata._displayName;
return entityMetadata?._displayName;
}

public async getTimeZoneDefinitions(): Promise<Object> {
Expand Down Expand Up @@ -105,6 +105,9 @@ export class DataverseService implements IDataverseService {
}

public async getRecordsCount(fetchXml: string): Promise<number> {
const top: number = this.getTopInFetchXml(fetchXml);
if (top) return top;

let numberOfRecords = 0;
let page = 0;
let pagingCookie: string | null = null;
Expand All @@ -117,6 +120,7 @@ export class DataverseService implements IDataverseService {
const xmlDoc: Document = parser.parseFromString(xml, 'text/xml');
const fetch: Element = xmlDoc.getElementsByTagName('fetch')?.[0];

fetch?.removeAttribute('top');
fetch?.removeAttribute('count');
fetch?.removeAttribute('page');

Expand Down Expand Up @@ -188,7 +192,7 @@ export class DataverseService implements IDataverseService {

const confirmOptions = { height: 200, width: 450 };
const confirmStrings = {
text: `Do you want to delete this ${entityMetadata._displayName}?
text: `Do you want to delete this ${entityMetadata?._displayName}?
You can't undo this action.`, title: 'Confirm Deletion',
};

Expand Down Expand Up @@ -219,4 +223,16 @@ export class DataverseService implements IDataverseService {
private getAllocatedWidth(): number {
return this._context.mode.allocatedWidth;
}

private getTopInFetchXml(fetchXml: string): number {
const parser: DOMParser = new DOMParser();
const xmlDoc: Document = parser.parseFromString(fetchXml, 'text/xml');

const top: string | null | undefined = xmlDoc.querySelector('fetch')?.getAttribute('top');

if (top) {
return Number(top);
}
return 0;
}
}
6 changes: 4 additions & 2 deletions FetchToSubgrid/utilities/d365Utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ const createColumnsForEntity = (
const columns: IColumn[] = [];

attributesFieldNames.forEach((name, index) => {
if (!displayNameCollection) return;

let displayName = name === `${entityName}id`
? 'Primary Key'
: displayNameCollection[name].DisplayName;
Expand Down Expand Up @@ -167,7 +169,7 @@ export const getEntityData = (props: IItemProps, dataverseService: IDataverseSer
} = props;

if (attributeType === AttributeType.Number) {
const format: string = entityMetadata.Attributes._collection[fieldName].Format;
const format: string = entityMetadata?.Attributes?._collection[fieldName]?.Format;
const field: string = dataverseService.getWholeNumberFieldName(
format,
entity,
Expand Down Expand Up @@ -324,7 +326,7 @@ export const getColumns = async (
entityName,
attributesFieldNames);

const displayNameCollection: Dictionary<EntityMetadata> = entityMetadata.Attributes._collection;
const displayNameCollection: Dictionary<EntityMetadata> = entityMetadata?.Attributes._collection;

const linkEntityAttFieldNames: Dictionary<EntityAttribute[]> = getLinkEntitiesNamesFromFetchXml(
fetchXml ?? '');
Expand Down

0 comments on commit 5ed32eb

Please sign in to comment.