forked from pnp/sp-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WeatherInformationWebPart.ts
134 lines (120 loc) · 3.87 KB
/
WeatherInformationWebPart.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import * as React from 'react';
import * as ReactDom from 'react-dom';
import { Version } from '@microsoft/sp-core-library';
import {
BaseClientSideWebPart,
IPropertyPaneConfiguration,
PropertyPaneTextField,
PropertyPaneChoiceGroup
} from '@microsoft/sp-webpart-base';
import * as strings from 'WeatherInformationWebPartStrings';
import WeatherInformation from './components/WeatherInformation';
import { IWeatherInformationProps } from './components/IWeatherInformationProps';
export interface IWeatherInformationWebPartProps {
location: string;
unit: string;
}
export default class WeatherInformationWebPart extends BaseClientSideWebPart<IWeatherInformationWebPartProps> {
public render(): void {
const element: React.ReactElement<IWeatherInformationProps> = React.createElement(
WeatherInformation,
{
needsConfiguration: this._needsConfiguration(),
location: this.properties.location,
unit: this.properties.unit,
httpClient: this.context.httpClient,
configureHandler: this._onConfigure,
errorHandler: this._onError
}
);
ReactDom.render(element, this.domElement);
}
protected get dataVersion(): Version {
return Version.parse('1.0');
}
protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
return {
pages: [
{
header: {
description: strings.PropertyPaneDescription
},
groups: [
{
groupName: strings.DataGroupName,
groupFields: [
PropertyPaneTextField('location', {
label: strings.LocationFieldLabel,
onGetErrorMessage: this._validateLocation.bind(this)
}),
PropertyPaneChoiceGroup('unit', {
label: strings.UnitFieldLabel,
options: [
{
text: strings.UnitFieldCelsius,
key: 'c'
},
{
text: strings.UnitFieldFahrenheit,
key: 'f'
}
]
})
]
}
]
}
]
};
}
/**
* Set the web part property pane to non-reactive, to avoid excessive retrieval
* of data from the third party API when typing the name of the location for
* which to retrieve weather information
*/
protected get disableReactivePropertyChanges(): boolean {
return true;
}
protected onAfterPropertyPaneChangesApplied(): void {
// after one or more web part properties have been changed
// clear any error messages displayed in the web part
this.context.statusRenderer.clearError(this.domElement);
}
/**
* Handles clicking the Configure button in the placeholder
*/
private _onConfigure = (): void => {
// open the property pane to let the user configure the web part
this.context.propertyPane.open();
}
/**
* Handles any error that occurred in the component
*/
private _onError = (errorMessage: string): void => {
// render the message for the error that occurred in the web part
this.context.statusRenderer.renderError(this.domElement, errorMessage);
}
/**
* Verify if the specified location is valid
* @param value Location specified in the web part properties
*/
private _validateLocation(value: string): string {
if (value === null ||
value.trim().length === 0) {
return strings.LocationNotSpecifiedError;
}
if (value.indexOf('"') > -1) {
return strings.LocationDoubleQuoteNotAllowed;
}
return '';
}
/**
* Check if the web part has been configured
*/
private _needsConfiguration(): boolean {
return !this.properties.location ||
this.properties.location.length === 0 ||
!this.properties.unit ||
this.properties.unit.length === 0;
}
}