Skip to content

Commit

Permalink
update NaN check, add $top, change param order
Browse files Browse the repository at this point in the history
  • Loading branch information
Niels Soeth authored and Niels Soeth committed May 29, 2024
1 parent c94de38 commit 2ac28a2
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions src/services/SPService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ export default class SPService implements ISPService {
filterString?: string,
substringSearch: boolean = false,
orderBy?: string,
cacheInterval: number = ICON_GENERIC_16,
top?: number): Promise<any[]> { // eslint-disable-line @typescript-eslint/no-explicit-any
top?: number,
cacheInterval: number = 1): Promise<any[]> { // eslint-disable-line @typescript-eslint/no-explicit-any
const webAbsoluteUrl = !webUrl ? this._webAbsoluteUrl : webUrl;
let apiUrl = '';
let isPost = false;
Expand All @@ -249,7 +249,7 @@ export default class SPService implements ISPService {
const filterStr = substringSearch ? // JJ - 20200613 - find by substring as an option
`${filterText ? `substringof('${encodeURIComponent(filterText.replace("'", "''"))}',${internalColumnName})` : ''}${filterString ? (filterText ? ' and ' : '') + filterString : ''}`
: `${filterText ? `startswith(${internalColumnName},'${encodeURIComponent(filterText.replace("'", "''"))}')` : ''}${filterString ? (filterText ? ' and ' : '') + filterString : ''}`; //string = filterList ? `and ${filterList}` : '';
apiUrl = `${webAbsoluteUrl}/_api/web/lists('${listId}')/items?$select=${keyInternalColumnName || 'Id'},${internalColumnName}&$filter=${filterStr}&$orderby=${orderBy}`;
apiUrl = `${webAbsoluteUrl}/_api/web/lists('${listId}')/items?$select=${keyInternalColumnName || 'Id'},${internalColumnName}&$filter=${filterStr}&$orderby=${orderBy}${top ? `&$top=${top}` : ''}`;
}
else { // we need to get FieldValuesAsText and cache them
const mapKey = `${webAbsoluteUrl}##${listId}##${internalColumnName}##${keyInternalColumnName || 'Id'}`;
Expand All @@ -260,7 +260,7 @@ export default class SPService implements ISPService {
return filteredItems;
}

apiUrl = `${webAbsoluteUrl}/_api/web/lists('${listId}')/items?$select=${keyInternalColumnName || 'Id'},${internalColumnName},FieldValuesAsText/${internalColumnName}&$expand=FieldValuesAsText&$orderby=${orderBy}${filterString ? '&$filter=' + filterString : ''}${top ? `&$top=${top}` : ''};
apiUrl = `${webAbsoluteUrl}/_api/web/lists('${listId}')/items?$select=${keyInternalColumnName || 'Id'},${internalColumnName},FieldValuesAsText/${internalColumnName}&$expand=FieldValuesAsText&$orderby=${orderBy}${filterString ? '&$filter=' + filterString : ''}${top ? `&$top=${top}` : ''}`;
isPost = false;

//eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -536,12 +536,7 @@ export default class SPService implements ISPService {
if (data.ok) {
const result = await data.json();
if (result && result[fieldName]) {
let value = result[fieldName][lookupFieldName || 'Title'];
const dateVal = Date.parse(value);
if (dateVal !== NaN) {
value = new Date(value).toLocaleDateString();
}
return [{ key: result[fieldName].ID, name: value }];
return [{ key: result[fieldName].ID, name: result[fieldName][lookupFieldName || 'Title'] }];
}
}

Expand All @@ -566,9 +561,9 @@ export default class SPService implements ISPService {
//multiselect lookups are arrays
if (isArray) {
result[fieldName].forEach(element => {
let value = element[fieldName][lookupFieldName || 'Title'];
let value = element[lookupFieldName || 'Title'];
const dateVal = Date.parse(value);
if (dateVal !== NaN) {
if (!Number.isNaN(dateVal)) {
value = new Date(value).toLocaleDateString();
}
lookups.push({ key: element.ID, name: value });
Expand All @@ -577,9 +572,9 @@ export default class SPService implements ISPService {
//single select lookups are objects
else {
const singleItem = result[fieldName];
let value = singleItem[fieldName][lookupFieldName || 'Title'];
let value = singleItem[lookupFieldName || 'Title'];
const dateVal = Date.parse(value);
if (dateVal !== NaN) {
if (!Number.isNaN(dateVal)) {
value = new Date(value).toLocaleDateString();
}
lookups.push({ key: singleItem.ID, name: value });
Expand Down

0 comments on commit 2ac28a2

Please sign in to comment.