Skip to content

Commit

Permalink
Merge pull request #417 from waldekmastykarz/lob-personalconfig
Browse files Browse the repository at this point in the history
Updates Leads to store configuration in application's personal folder
  • Loading branch information
VesaJuvonen authored Aug 10, 2020
2 parents 6e66b06 + 73d9964 commit f8a6faa
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 27 deletions.
1 change: 1 addition & 0 deletions solutions/LeadsLOBSolution/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ This solution is compatible with SharePoint Online. Teams capability only works

Version|Date|Comments
-------|----|--------
1.4.2|Jun 15, 2020|Extended with storing user's configuration in application's personal folder
1.4.1|May 25, 2020|Switched to using preview Microsoft To Do APIs for creating lead reminders
1.4.0|May 19, 2020|Extended with the ability to create a reminder in Planner for the specific lead
1.3.0|May 18, 2020|Extended with Teams messaging extension
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
"solution": {
"name": "leads-lob-client-side-solution",
"id": "2dc4f7fe-cd2e-4f2a-8a92-86acc494e909",
"version": "1.4.1.0",
"version": "1.4.2.0",
"includeClientSideAssets": true,
"skipFeatureDeployment": true,
"isDomainIsolated": false,
"webApiPermissionRequests": [
{
"resource": "Microsoft Graph",
"scope": "Tasks.ReadWrite"
},
{
"resource": "Microsoft Graph",
"scope": "Files.ReadWrite"
}
]
},
Expand Down
2 changes: 1 addition & 1 deletion solutions/LeadsLOBSolution/webpart/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "leads-lob",
"main": "lib/index.js",
"version": "1.4.1",
"version": "1.4.2",
"private": true,
"engines": {
"node": ">=0.10.0"
Expand Down
60 changes: 46 additions & 14 deletions solutions/LeadsLOBSolution/webpart/src/LeadsSettings.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SPHttpClient, SPHttpClientResponse } from "@microsoft/sp-http";
import { SPHttpClient, SPHttpClientResponse, MSGraphClient, HttpClient, HttpClientResponse, HttpClientConfiguration } from "@microsoft/sp-http";

export interface ILeadsSettings {
demo: boolean;
Expand All @@ -7,28 +7,60 @@ export interface ILeadsSettings {
}

export class LeadsSettings {
private static settingsStorageKey: string = 'LeadsSettings';
private static settingsFileName: string = 'LeadsSettings.json';
private static settingsFileUrl: string = `/me/drive/special/approot:/${LeadsSettings.settingsFileName}`;
private static graphClient: MSGraphClient;
private static httpClient: HttpClient;

public static initialize(graphHttpClient: MSGraphClient, httpClient: HttpClient): void {
this.graphClient = graphHttpClient;
this.httpClient = httpClient;
}

public static getSettings(): Promise<ILeadsSettings> {
if (!this.graphClient) {
throw 'Initialize LeadsSettings before managing settings';
}

public static getSettings(): ILeadsSettings {
const defaultSettings: ILeadsSettings = {
demo: true,
quarterlyOnly: true
};

try {
const settingsString: string = window.localStorage.getItem(this.settingsStorageKey);
if (settingsString) {
const settings: ILeadsSettings = JSON.parse(settingsString);
return settings;
}
}
catch (e) { }
return this.graphClient
.api(`${LeadsSettings.settingsFileUrl}[email protected]`)
.get()
.then((response: { '@microsoft.graph.downloadUrl': string }): Promise<HttpClientResponse> => {
return this.httpClient
.get(response['@microsoft.graph.downloadUrl'], HttpClient.configurations.v1);
})
.then((response: HttpClientResponse): Promise<string> => {
if (response.ok) {
return response.text();
}

return defaultSettings;
return Promise.reject(response.statusText);
})
.then((settingsString: string): Promise<ILeadsSettings> => {
try {
const settings: ILeadsSettings = JSON.parse(settingsString);
return Promise.resolve(settings);
}
catch (e) {
return Promise.resolve(defaultSettings);
}
}, _ => Promise.resolve(defaultSettings));
}

public static setSettings(settings: ILeadsSettings): void {
window.localStorage.setItem(this.settingsStorageKey, JSON.stringify(settings));
public static setSettings(settings: ILeadsSettings): Promise<void> {
if (!this.graphClient) {
throw 'Initialize LeadsSettings before managing settings';
}

return this.graphClient
.api(`${LeadsSettings.settingsFileUrl}:/content`)
.header('content-type', 'text/plain')
.put(JSON.stringify(settings));
}

public static getLeadsApiUrl(spHttpClient: SPHttpClient, siteUrl: string): Promise<string> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// Components that allow authors to embed arbitrary script code should set this to true.
// https://support.office.com/en-us/article/Turn-scripting-capabilities-on-or-off-1f2c515f-5d7e-448a-9fd7-835da935584f
"requiresCustomScript": false,
"supportedHosts": ["TeamsTab", "SharePointFullPage", "TeamsPersonalApp", "SharePointFullPage"],
"supportedHosts": ["TeamsTab", "SharePointFullPage", "TeamsPersonalApp", "SharePointWebPart"],
"preconfiguredEntries": [{
"groupId": "5c03119e-3074-46fd-976b-c60198311f70", // Other
"group": { "default": "LOB" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,20 @@ export default class LeadsWebPart extends BaseClientSideWebPart<ILeadsWebPartPro
private queryParameters: UrlQueryParameterCollection;
private view?: LeadView;
private msGraphClient: MSGraphClient;
private settings?: ILeadsSettings;

protected onInit(): Promise<void> {
return this.context.msGraphClientFactory
.getClient()
.then((client: MSGraphClient): Promise<void> => {
.then((client: MSGraphClient): Promise<ILeadsSettings> => {
this.msGraphClient = client;

LeadsSettings.initialize(this.msGraphClient, this.context.httpClient);
return LeadsSettings.getSettings();
})
.then((settings: ILeadsSettings): Promise<void> => {
this.settings = settings;

if (this.properties.demo) {
this.needsConfiguration = false;
return Promise.resolve();
Expand Down Expand Up @@ -200,7 +207,7 @@ export default class LeadsWebPart extends BaseClientSideWebPart<ILeadsWebPartPro
this.render();
}
resolve();
}, () => resolve());
}, _ => resolve());
});
}

Expand All @@ -210,12 +217,9 @@ export default class LeadsWebPart extends BaseClientSideWebPart<ILeadsWebPartPro
this.view = this.getLeadView();

if (this.context.microsoftTeams && typeof this.view !== 'undefined') {
const settings: ILeadsSettings = LeadsSettings.getSettings();
if (settings) {
props.demo = settings.demo;
props.quarterlyOnly = settings.quarterlyOnly;
props.region = settings.region;
}
props.demo = this.settings.demo;
props.quarterlyOnly = this.settings.quarterlyOnly;
props.region = this.settings.region;
}

return props;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,23 @@ import {
} from '@microsoft/sp-property-pane';
import { BaseClientSideWebPart } from '@microsoft/sp-webpart-base';
import { LeadsSettings, ILeadsSettingsProps } from './components/LeadsSettings';
import { LeadsSettings as SettingsManager } from '../../LeadsSettings';

export interface ILeadsSettingsWebPartProps {
description: string;
}

export default class LeadsSettingsWebPart extends BaseClientSideWebPart<ILeadsSettingsWebPartProps> {
public onInit(): Promise<void> {
return new Promise<void>((resolve: () => void, reject: (err) => void): void => {
this.context.msGraphClientFactory
.getClient()
.then((client): void => {
SettingsManager.initialize(client, this.context.httpClient);
resolve();
}, err => reject(err));
});
}

public render(): void {
const element: React.ReactElement<ILeadsSettingsProps> = React.createElement(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,14 @@ export class LeadsSettings extends React.Component<ILeadsSettingsProps, ILeadsSe
}

public componentWillMount(): void {
this.settings = SettingsManager.getSettings();
let leadsApiUrl: string;
SettingsManager
.getLeadsApiUrl(this.props.spHttpClient, this.props.webUrl)
.getSettings()
.then((settings: ILeadsSettings) => {
this.settings = settings;

return SettingsManager.getLeadsApiUrl(this.props.spHttpClient, this.props.webUrl);
})
.then((_leadsApiUrl: string): Promise<void> => {
leadsApiUrl = _leadsApiUrl;
return Promise.resolve();
Expand Down

0 comments on commit f8a6faa

Please sign in to comment.