import { SPHttpClientResponse } from '@microsoft/sp-http' ; import { SPHttpClient } from '@microsoft/sp-http' ; import { Environment , EnvironmentType } from '@microsoft/sp-core-library' ; import { BaseComponentContext } from '@microsoft/sp-component-base' ; import { ISPView , IPropertyFieldViewPickerHostProps , PropertyFieldViewPickerOrderBy } from '../propertyFields/viewPicker' ; import { ISPViewPickerService } from './ISPViewPickerService' ; import { ISPViews } from '../propertyFields/viewPicker' ; /**
 * Service implementation to get list & list items from current SharePoint site
 */ export class SPViewPickerService implements ISPViewPickerService { private context : BaseComponentContext ; private props : IPropertyFieldViewPickerHostProps ; /**
     * Service constructor
     */ constructor ( _props , pageContext ) { this . props = _props ; this . context = pageContext ; } /**
     * Gets the collection of view for a selected list
     */ public async getViews ( ) { if ( Environment . type === EnvironmentType . Local ) { return this . getViewsFromMock ( ) ; } else { if ( this . props . listId === undefined || this . props . listId === "" ) { return this . getEmptyViews ( ) ; } const webAbsoluteUrl = this . props . webAbsoluteUrl ? this . props . webAbsoluteUrl : this . context . pageContext . web . absoluteUrl ; let queryUrl = `${ webAbsoluteUrl }/_api/lists(guid'${ this . props . listId }')/Views?$select=Title,Id` ; if ( this . props . orderBy !== null ) { queryUrl += '&$orderby=' ; switch ( this . props . orderBy ) { case PropertyFieldViewPickerOrderBy . Id : queryUrl += 'Id' ; break ; case PropertyFieldViewPickerOrderBy . Title : queryUrl += 'Title' ; break ; } if ( this . props . filter ) { queryUrl += `&$filter=${ encodeURIComponent ( this . props . filter ) }` ; } let response = await this . context . spHttpClient . get ( queryUrl , SPHttpClient . configurations . v1 ) ; let views = ( await response . json ( ) ) as ISPViews ; if ( this . props . onViewsRetrieved ) { let lr = this . props . onViewsRetrieved ( views . value ) ; let output ; if ( lr instanceof Array ) { output = lr ; } else { output = await lr ; } views . value = output ; } return views ; } } } /**
     * Returns an empty view for when a list isn't selected
     */ private getEmptyViews ( ) { return new Promise < ISPViews > ( ( resolve ) => { const listData = { value : [ ] } ; resolve ( listData ) ; } ) ; } /**
     * Returns 3 fake SharePoint views for the Mock mode
     */ private getViewsFromMock ( ) { return new Promise < ISPViews > ( ( resolve ) => { const listData = { value : [ { Title : 'Mock View One' , Id : '3bacd87b-b7df-439a-bb20-4d4d13523431' } , { Title : 'Mock View Two' , Id : '5e37c820-e2cb-49f7-93f5-14003c07788b' } , { Title : 'Mock View Three' , Id : '5fda7245-c4a7-403b-adc1-8bd8b481b4ee' } ] } ; resolve ( listData ) ; } ) ; } }