I am trying to send multiple strings to be processed by a function. The function executes a promise on each string that needs to be evaluated. Once all the promise responses are returned, I need to send that back as a multidimensional array, so that I can do things like sorting on the full results set.
I know this code is bad, but I hope this gives you an idea of what I am trying to do. Does anyone know how to correctly write this?
import {
SPHttpClient,
SPHttpClientResponse
} from '@microsoft/sp-http';
export interface SitePages {
value: Pages[];
}
export interface Pages {
Title: string;
Created: string;
Description: string;
BannerImageUrl: {
Url: string
};
FileRef: string;
}
export async function getListData(sites : string[]) : Promise<SitePages[]> {
let result : Promise<SitePages[]> = await Promise.all (sites.forEach(site => {
this.site.context.spHttpClient.get(site + "/_api/web/lists/GetByTitle('Site Pages')/Items?$filter=PromotedState eq 2&$orderby=Created desc&$expand=Properties&$select=Created,Title,Description,BannerImageUrl,FileRef", SPHttpClient.configurations.v1)
.then((response: SPHttpClientResponse) => {
return response.json();
});
}));
return result;
};
UPDATED CODE
import { Version } from '@microsoft/sp-core-library';
import {
BaseClientSideWebPart,
IPropertyPaneConfiguration,
PropertyPaneTextField
} from '@microsoft/sp-webpart-base';
import { escape } from '@microsoft/sp-lodash-subset';
import styles from './CustomNewsWebpartWebPart.module.scss';
import * as strings from 'CustomNewsWebpartWebPartStrings';
import {
SPHttpClient,
SPHttpClientResponse
} from '@microsoft/sp-http';
export interface SitePages {
value: Pages[];
}
export interface Pages {
Title: string;
Created: string;
Description: string;
BannerImageUrl: {
Url: string
};
FileRef: string;
}
export interface ICustomNewsWebpartWebPartProps {
description: string;
listField: string;
}
export let elems : any[] = [];
export default class CustomNewsWebpartWebPart extends BaseClientSideWebPart<ICustomNewsWebpartWebPartProps> {
public render(): void {
this.getSitesNews();
}
protected get dataVersion(): Version {
return Version.parse('1.0');
}
protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
return {
pages: [
{
header: {
description: strings.PropertyPaneDescription
},
groups: [
{
groupName: strings.BasicGroupName,
groupFields: [
PropertyPaneTextField('description', {
label: strings.DescriptionFieldLabel,
value : strings.DescriptionField
}),
PropertyPaneTextField('listField', {
label: strings.listFieldLabel,
value : strings.listFieldDefault,
multiline: true,
rows:26
})
]
}
]
}
]
};
}
private getSitesNews() : void {
this.domElement.innerHTML = null;
if(null != this.properties.listField){
let splitsites = this.properties.listField.split(/\r?\n|\r|\n/g);
let sites : string[] = [];
splitsites.forEach(site => {
if(site != ''){
sites.push(site.trim());
}
});
console.log(sites);
this._getListData(sites)
.then((response) => {
let pages : any[] = [];
response.forEach(spages => {
spages.value.forEach(spage => {
pages.push(spage);
})
});
pages.sort((a,b) => (a.Created < b.Created) ? 1 : ((b.Created < a.Created) ? -1 : 0));
let output : string = `<h1>${this.properties.description}</h1>`;
let i : number = 0;
for(i; i < pages.length; i++){
let itemPath : string = '';
let itemCreated : string = '';
let itemTitle : string = '';
let itemDescription : string = '';
let itemBannerImageUrl : string = '';
pages[i].Created ? itemCreated = pages[i].Created : itemCreated = '';
pages[i].FileRef ? itemPath = pages[i].FileRef : itemPath = '';
pages[i].Title ? itemTitle = pages[i].Title : itemTitle = '';
pages[i].Description ? itemDescription = pages[i].Description : itemDescription = '';
pages[i].BannerImageUrl.Url ? itemBannerImageUrl = pages[i].BannerImageUrl.Url : itemBannerImageUrl = '';
output += `
<div id="${itemCreated}" class="${styles.newsDiv}">
<table class="${styles.newsTable}">
<tr>
<td><div class="${styles.newsItemBanner}" style="background-image:url('${itemBannerImageUrl}');"></div></td>
</tr>
</tr>
<td>
<div class="${styles.newsItemContent}">
<h2>${itemTitle}</h2>
<p>${itemDescription}</p>
<!-- <p>${itemCreated}</p>-->
<a href="${itemPath}" class=${styles.newsButton}>Read More</a>
</div>
</td>
</tr>
</table>
</div>
`;
}
this.domElement.innerHTML = output;
})
.catch((error) => {
console.log(error);
});
}
}
private async _getListData(spsites : string[]): Promise<SitePages[]> {
return await Promise.all(spsites.map(spsite => {
return this.context.spHttpClient.get(spsite + "/_api/web/lists/GetByTitle('Site Pages')/Items?$filter=PromotedState eq 2&$orderby=Created desc&$expand=Properties&$select=Created,Title,Description,BannerImageUrl,FileRef", SPHttpClient.configurations.v1)
.then((response: SPHttpClientResponse) => {
return response.json();
})
.catch((error) => {
console.log(error);
});
}));
}
}