-
Notifications
You must be signed in to change notification settings - Fork 441
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #417 from waldekmastykarz/lob-personalconfig
Updates Leads to store configuration in application's personal folder
- Loading branch information
Showing
8 changed files
with
83 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -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> { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters