diff --git a/samples/react-word-game/src/webparts/wordGame/WordGameWebPart.ts b/samples/react-word-game/src/webparts/wordGame/WordGameWebPart.ts index 4b96b3ca4..866e9b157 100644 --- a/samples/react-word-game/src/webparts/wordGame/WordGameWebPart.ts +++ b/samples/react-word-game/src/webparts/wordGame/WordGameWebPart.ts @@ -8,28 +8,24 @@ import { PropertyPaneCheckbox } from '@microsoft/sp-webpart-base'; -import * as strings from 'WordGameWebPartStrings'; import WordGame from './components/WordGame'; import { IWordGameProps } from './components/IWordGameProps'; -import { WordService } from './components/WordService'; export interface IWordGameWebPartProps { gameTitle: string; - enableHighScores: boolean + enableHighScores: boolean; } export default class WordGameWebPart extends BaseClientSideWebPart { - wordGame: WordGame; - constructor() { - super(); - } - public render(): void { - if (!this.properties.gameTitle) - this.properties.gameTitle='Word Game'; - if (this.properties.enableHighScores==null) - this.properties.enableHighScores=true; + if (!this.properties.gameTitle) { + this.properties.gameTitle = 'Word Game'; + } + + if (this.properties.enableHighScores === undefined) { + this.properties.enableHighScores = true; + } const element: React.ReactElement = React.createElement( WordGame, @@ -40,9 +36,7 @@ export default class WordGameWebPart extends BaseClientSideWebPart { - - wordService: WordService = new WordService(); - currentGame: Game; - timerInterval: number = -1; - numTimer: number; - - state: IWordGameState = + public state: IWordGameState = { gamestate: GameState.GameLoading, currentWord: '', @@ -37,6 +31,12 @@ export default class WordGame extends React.Component { this.wordService.SetContext(this.props.context); await this.wordService.loadWords(); - if (window.innerWidth<600) - this.setState({mobileMode:true}) - + if (window.innerWidth < 600) { + this.setState({ mobileMode: true }); + } this.setState({ - gamestate: GameState.Title, + gamestate: GameState.Title } as IWordGameState); this.getHighScores(); - } - public async redraw() { - this.forceUpdate(); + public render(): React.ReactElement { + // tslint:disable-next-line: no-string-literal + window['wordgame'] = this; + + let body: JSX.Element; + switch (this.state.gamestate) { + case GameState.Title: + body = +
+

{this.props.description}

+ {/*

Word Game

*/} + {/*

Count: {this.wordService.GetWordCount()}

*/} +

Unscramble as many words as you can before the time runs out

+ + { + this.props.enableHighScores ? + : '' + } +
; + break; + case GameState.GameLoading: + body = +
+ Loading... + + Loading... + +
; + break; + case GameState.GamePlaying: + body = +
+
+ {this.state.currentWord} + {/* MOBILE TOAST */} + { + this.state.lblMessage !== '' && this.state.mobileMode ? +
+ { + this.state.lblMessage + } +
: '' + } +
+ + +
{this.state.possibleAnswersText}
+ + + + + + + +
+ + + +
+
{this.state.lblTimer}
+ {/* DESKTOP TOAST */} + { + this.state.lblMessage !== '' && !this.state.mobileMode ? +
+
{this.state.lblMessage}
+
: '' + } +
; + break; + case GameState.GameFinished: + + body = +
+

Well done! Score {this.state.score} out of 6

+

See answers below to the current round

+ +
+
    + { + this.currentGame.rounds.map(round => ( +
  • Word {round.word} + { + round.answers.map(answer => ( +
      + Answer {answer} + { + round.correctAnswer === answer ? + + + : '' + } +
    + )) + } +
  • + )) + } +
+
+ { + this.props.enableHighScores ? + : '' + } +
; + break; + + } + + const maindiv: JSX.Element =
{body}
; + + return ( +
+ {maindiv} +
+ ); } - answerChanged(event) { + // private async redraw(): Promise { + // this.forceUpdate(); + // } + + private answerChanged(event: React.ChangeEvent): void { this.setState({ txtAnswer: event.target.value } as IWordGameState); } - messageCounter = 0; - - showMessage(message: string) { + private showMessage(message: string): void { this.setState({ lblMessage: message } as IWordGameState); this.messageCounter = 3; } - async btnAnswer() { + private async btnAnswer(): Promise { let answerFound: boolean = false; this.currentGame.rounds[this.state.currentRound].answers.forEach(answer => { - if (answer.toLowerCase() == this.state.txtAnswer.toLowerCase()) { + if (answer.toLowerCase() === this.state.txtAnswer.toLowerCase()) { this.currentGame.rounds[this.state.currentRound].correctAnswer = this.state.txtAnswer; this.numTimer += 5; this.showMessage('Correct!! +5 Seconds'); this.setState({ - lblTimer: this.numTimer + " seconds", + lblTimer: this.numTimer + ' seconds', score: this.state.score + 1 } as IWordGameState); answerFound = true; } - }) + }); if (!answerFound) { this.showMessage('Incorrect'); this.currentGame.rounds[this.state.currentRound].incorrectAnswers.push(this.state.txtAnswer); this.setState({ txtAnswer: '' } as IWordGameState); this.focusOnTextbox(); - } - else { + } else { this.nextRound(); } } - btnSkip() { + private btnSkip(): void { this.nextRound(); this.focusOnTextbox(); } - async nextRound() { - - //state doesn't modify immediately + private async nextRound(): Promise { + // state doesn't modify immediately await this.setState({ txtAnswer: '', - currentRound: this.state.currentRound + 1, + currentRound: this.state.currentRound + 1 } as IWordGameState); if (this.state.currentRound >= this.currentGame.rounds.length) { this.finishGame(); - } - else { + } else { this.setState({ currentWord: this.currentGame.rounds[this.state.currentRound].word, possibleAnswersText: this.getPossibleAnswersText(this.state.currentRound) @@ -126,59 +278,61 @@ export default class WordGame extends React.Component1) + private getPossibleAnswersText(currentRound: number): string { + const possibleAnswers: number = this.currentGame.rounds[currentRound].answers.length; + let possibleAnswersText: string = possibleAnswers + ' Possible Answer'; + if (possibleAnswers > 1) { possibleAnswersText = possibleAnswers + ' Possible Answers'; - return possibleAnswersText + } + return possibleAnswersText; } - focusOnTextbox() { - let element = document.getElementById('txtWordGameAnswer'); - if (element) + private focusOnTextbox(): void { + const element: HTMLElement = document.getElementById('txtWordGameAnswer'); + if (element) { element.focus(); + } } - finishGame() { + private finishGame(): void { this.sendScore(this.state.score, this.numTimer, JSON.stringify(this.currentGame)); this.stopTimer(); this.setState({ gamestate: GameState.GameFinished } as IWordGameState); - } - async sendScore(score: number, seconds: number, details: string) { - if (!this.props.enableHighScores) + private async sendScore(score: number, seconds: number, details: string): Promise { + if (!this.props.enableHighScores) { return; + } await this.wordService.SubmitScore(score, seconds, details); this.getHighScores(); } - async getHighScores(){ - if (!this.props.enableHighScores) + private async getHighScores(): Promise { + if (!this.props.enableHighScores) { return; + } - let scores = await this.wordService.GetHighScores(); + const scores: WordGameListItem[] = await this.wordService.GetHighScores(); this.setState({ highScores: scores } as IWordGameState); } - keyDownAnswer(event: React.KeyboardEvent) { - if (event.keyCode == 13) + private keyDownAnswer(event: React.KeyboardEvent): void { + if (event.keyCode === 13) { this.btnAnswer(); + } } - - async btnStartGame() { + private async btnStartGame(): Promise { this.setState({ gamestate: GameState.GameLoading } as IWordGameState); this.currentGame = this.wordService.GenerateGame(); - await new Promise(resolve => { setTimeout(resolve, 1000); }); this.setState({ @@ -192,159 +346,30 @@ export default class WordGame extends React.Component25) this.numTimer--; - this.setState({ lblTimer: this.numTimer + " seconds" }); - if (this.numTimer == 0) { + this.setState({ lblTimer: this.numTimer + ' seconds' }); + if (this.numTimer === 0) { this.finishGame(); } - //toast messages + // toast messages if (this.messageCounter > 0) { this.messageCounter--; - if (this.messageCounter == 0) + if (this.messageCounter === 0) { this.setState({ lblMessage: '' } as IWordGameState); + } } } - stopTimer() { + private stopTimer(): void { this.setState({ lblTimer: '' }); clearInterval(this.timerInterval); } - - public render(): React.ReactElement { - window["wordgame"] = this; - - - let body: JSX.Element; - switch (this.state.gamestate) { - case GameState.Title: - body = -
-

{this.props.description}

- {/*

Word Game

*/} - {/*

Count: {this.wordService.GetWordCount()}

*/} -

Unscramble as many words as you can before the time runs out

- - { - this.props.enableHighScores ? - : '' - } -
- break; - case GameState.GameLoading: - body = -
- - - Loading... - -
- break; - case GameState.GamePlaying: - body = -
-
- {this.state.currentWord} - {/* MOBILE TOAST */} - { - this.state.lblMessage != '' && this.state.mobileMode? -
- { - this.state.lblMessage - } -
: '' - } -
- - -
{this.state.possibleAnswersText}
- - - - - - - -
- - - -
-
{this.state.lblTimer}
- {/* DESKTOP TOAST */} - { - this.state.lblMessage != '' && !this.state.mobileMode? -
-
{this.state.lblMessage}
-
: '' - } -
- break; - case GameState.GameFinished: - - body = -
-

Well done! Score {this.state.score} out of 6

-

See answers below to the current round

- -
-
    - { - this.currentGame.rounds.map(round => ( -
  • Word {round.word} - { - round.answers.map(answer => ( -
      - Answer {answer} - { - round.correctAnswer == answer ? - - - : '' - } -
    - )) - } -
  • - )) - } -
-
- { - this.props.enableHighScores ? - : '' - } -
- break; - - } - - let maindiv =
{body}
; - - - return ( -
- {maindiv} -
- ); - } - - } diff --git a/samples/react-word-game/src/webparts/wordGame/components/WordHighScores.tsx b/samples/react-word-game/src/webparts/wordGame/components/WordHighScores.tsx index ef1a90586..764c010ec 100644 --- a/samples/react-word-game/src/webparts/wordGame/components/WordHighScores.tsx +++ b/samples/react-word-game/src/webparts/wordGame/components/WordHighScores.tsx @@ -1,42 +1,40 @@ -import * as React from "react"; -import { WordGameListItem } from "./WordService" +import * as React from 'react'; +import { WordGameListItem } from './WordService'; export interface IWordHighScoresProps { - scores: WordGameListItem[] + scores: WordGameListItem[]; } export default class WordHighScores extends React.Component { - constructor() { - super(); + constructor() { + super(); - } + } - public render(): React.ReactElement { - let rank = 1; - return ( -
- { - this.props.scores.length>0 ? -

High Scores

: '' - - } - - - { - this.props.scores.map(score => ( - - - - - - - )) - } - -
{rank++ + ') '}{score.Name} Score: {score.Score} Seconds: {score.Seconds}
-
- ); - } - -} \ No newline at end of file + public render(): React.ReactElement { + let rank: number = 1; + return ( +
+ { + this.props.scores.length > 0 ? +

High Scores

: '' + } + + + { + this.props.scores.map(score => ( + + + + + + + )) + } + +
{rank++ + ') '}{score.Name} Score: {score.Score} Seconds: {score.Seconds}
+
+ ); + } +} diff --git a/samples/react-word-game/src/webparts/wordGame/components/WordService.ts b/samples/react-word-game/src/webparts/wordGame/components/WordService.ts index f4665e656..07d145ed0 100644 --- a/samples/react-word-game/src/webparts/wordGame/components/WordService.ts +++ b/samples/react-word-game/src/webparts/wordGame/components/WordService.ts @@ -1,78 +1,110 @@ // import * as $ from '../assets/jquery.min'; -import { SPComponentLoader } from '@microsoft/sp-loader'; -import WordGame from './WordGame'; import { WebPartContext } from '@microsoft/sp-webpart-base'; import { SPHttpClient, - SPHttpClientResponse, - HttpClientConfiguration, + SPHttpClientResponse, ISPHttpClientOptions - } from '@microsoft/sp-http'; +} from '@microsoft/sp-http'; + +// tslint:disable-next-line: no-any const wordjpg: any = require('../assets/wordlist.jpg'); + +// tslint:disable-next-line: no-any const $: any = require('../assets/jquery.min'); -// const wordlist: any = require('../assets/wordlist.json'); -export class Game -{ - rounds:Round[] = []; +export class Game { + public rounds: Round[] = []; } -export class Round -{ - word = ""; - answers:string[] = []; - correctAnswer = ""; - incorrectAnswers:string[] = []; +export class Round { + public word: string = ''; + public answers: string[] = []; + public correctAnswer: string = ''; + public incorrectAnswers: string[] = []; } +export class WordGameListItem { + // tslint:disable-next-line: variable-name + public Name: string; + + // tslint:disable-next-line: variable-name + public Score: number; + + // tslint:disable-next-line: variable-name + public Seconds: number; + + // tslint:disable-next-line: variable-name + public Details: string; + + constructor(name: string, score: number, seconds: number, details: string) { + this.Name = name; + this.Score = score; + this.Seconds = seconds; + this.Details = details; + } +} export class WordService { + public allwords: string[] = []; + public words3: string[] = []; + public words4: string[] = []; + public words5: string[] = []; + public words6: string[] = []; + public words7: string[] = []; + public words8: string[] = []; + public context: WebPartContext; - allwords: string[] = []; - words3: string[] = []; - words4: string[] = []; - words5: string[] = []; - words6: string[] = []; - words7: string[] = []; - words8: string[] = []; - context:WebPartContext; + public GenerateGame(): Game { - GenerateGame():Game - { + const game: Game = new Game(); - let game = new Game(); + const round1: Round = new Round(); + round1.word = this.GetRandomScrambledWord(5); + round1.answers = this.FindPossibleWords(round1.word); - let round1 = new Round(); round1.word = this.GetRandomScrambledWord(5); round1.answers = this.FindPossibleWords(round1.word); - let round2 = new Round(); round2.word = this.GetRandomScrambledWord(5); round2.answers = this.FindPossibleWords(round2.word); - let round3 = new Round(); round3.word = this.GetRandomScrambledWord(5); round3.answers = this.FindPossibleWords(round3.word); - let round4 = new Round(); round4.word = this.GetRandomScrambledWord(6); round4.answers = this.FindPossibleWords(round4.word); - let round5 = new Round(); round5.word = this.GetRandomScrambledWord(6); round5.answers = this.FindPossibleWords(round5.word); - let round6 = new Round(); round6.word = this.GetRandomScrambledWord(6); round6.answers = this.FindPossibleWords(round6.word); + const round2: Round = new Round(); + round2.word = this.GetRandomScrambledWord(5); + round2.answers = this.FindPossibleWords(round2.word); - game.rounds.push(round1); - game.rounds.push(round2); - game.rounds.push(round3); - game.rounds.push(round4); - game.rounds.push(round5); - game.rounds.push(round6); + const round3: Round = new Round(); + round3.word = this.GetRandomScrambledWord(5); + round3.answers = this.FindPossibleWords(round3.word); - return game; + const round4: Round = new Round(); + round4.word = this.GetRandomScrambledWord(6); + round4.answers = this.FindPossibleWords(round4.word); + + const round5: Round = new Round(); + round5.word = this.GetRandomScrambledWord(6); + round5.answers = this.FindPossibleWords(round5.word); + + const round6: Round = new Round(); + round6.word = this.GetRandomScrambledWord(6); + round6.answers = this.FindPossibleWords(round6.word); + + game.rounds.push(round1); + game.rounds.push(round2); + game.rounds.push(round3); + game.rounds.push(round4); + game.rounds.push(round5); + game.rounds.push(round6); + + return game; } + public async loadWords(): Promise { - async loadWords() { - - window["wordService"] = this; + // tslint:disable-next-line: no-string-literal + window['wordService'] = this; /* SP Loader Implementation */ // console.log(jquery); // await SPComponentLoader.loadScript('../assets/jquery.min.js', { globalExportsName: "ScriptGlobal" }); // console.log('jquery loaded'); - /* JSON File Implementation If you have a custom word list you would like to use - add it as a JSON file in assets/wordlist.json and + add it as a JSON file in assets/wordlist.json and uncomment the const wordlist at the top of this file. Then comment out the Text File implementation below */ // let wordvalues = (Object as any).values(wordlist) as any; @@ -80,24 +112,25 @@ export class WordService { // for(let i=0;i { + xhr.overrideMimeType('text/plain; charset=x-user-defined'); } }) as string; - this.allwords = responseText.split("\r\n"); + this.allwords = responseText.split('\r\n'); this.allwords.forEach(word => { - if (word.indexOf("-") > -1) + if (word.indexOf('-') > -1) { return; - if (word.indexOf("-") > -1) + } + if (word.indexOf('-') > -1) { return; + } switch (word.length) { case 3: this.words3.push(word); @@ -126,13 +159,13 @@ export class WordService { } - GetWordCount(): number { + public GetWordCount(): number { return this.allwords.length; } - GetRandomScrambledWord(level: number) { - let randomWord = ""; - let randwordnum = 0; + public GetRandomScrambledWord(level: number): string { + let randomWord: string = ''; + let randwordnum: number = 0; switch (level) { case 3: randwordnum = Math.floor(Math.random() * Math.floor(this.words3.length)); @@ -162,128 +195,151 @@ export class WordService { break; } - let scrambledWord = this.ScrambleWord(randomWord); + const scrambledWord: string = this.ScrambleWord(randomWord); return scrambledWord; } - FindPossibleWords(currentWord: string) { - //coati - //taco + public FindPossibleWords(currentWord: string): string[] { + // coati + // taco - //currentWord = "coati"; - let possibleWords: string[] = []; + // currentWord = "coati"; + const possibleWords: string[] = []; this.allwords.forEach(word => { - let tempword = word;//taco - for (let i = 0; i < currentWord.length; i++) { + let tempword: string = word; // taco + for (let i: number = 0; i < currentWord.length; i++) { - let letter = currentWord[i]; + const letter: string = currentWord[i]; if (tempword.indexOf(letter) > -1) { tempword = tempword.slice(0, tempword.indexOf(letter)) + tempword.slice(tempword.indexOf(letter) + 1); - } - else { + } else { tempword = 'n'; break; } } - if (tempword.length == 0) - possibleWords.push(word) + if (tempword.length === 0) { + possibleWords.push(word); + } }); return possibleWords; } - //replace a character in a string - private replaceCharAt(orig:string, index:number, replacement:string): string { - return orig.substr(0, index) + replacement + orig.substr(index + replacement.length); - } - - ScrambleWord(word: string): string { - let notScrambled = true; - let scrambledWord = ""; - let count = 0; - var originalword = word; + public ScrambleWord(word: string): string { + let notScrambled: boolean = true; + let scrambledWord: string = ''; + let count: number = 0; + const originalword: string = word; while (notScrambled) { word = originalword; - let chars = ''; - for (let i = 0; i < word.length; i++) + let chars: string = ''; + for (let i: number = 0; i < word.length; i++) { chars += ' '; + } - let index = 0; - while (word.length > 0) { - let next = Math.floor(Math.random() * Math.floor(word.length)); // Get a random number between 0 and the length of the word. - chars = this.replaceCharAt(chars, index, word[next]); // Take the character from the random position and add to our char array. - word = word.substr(0, next) + word.substr(next + 1); // Remove the character from the word. + let index: number = 0; + while (word.length > 0) { + // Get a random number between 0 and the length of the word. + const next: number = Math.floor(Math.random() * Math.floor(word.length)); + + // Take the character from the random position and add to our char array. + chars = this.replaceCharAt(chars, index, word[next]); + + // Remove the character from the word. + word = word.substr(0, next) + word.substr(next + 1); ++index; } scrambledWord = chars.slice(0); count++; - if (originalword!=scrambledWord) + if (originalword !== scrambledWord) { notScrambled = false; + } - //just in case there is a problem - if (count == 10) + // just in case there is a problem + if (count === 10) { notScrambled = false; - } + } + } return scrambledWord; - } - //SHAREPOINT APIS + // SHAREPOINT APIS - SetContext(context:WebPartContext){ + public SetContext(context: WebPartContext): void { this.context = context; } - public async SubmitScore(score:number,seconds:number,details:string){ - try{ + public async SubmitScore(score: number, seconds: number, details: string): Promise { + try { await this.CreateListIfNotExists(); - await this.CreateListItem(score,seconds,details); - }catch(error){} + await this.CreateListItem(score, seconds, details); + } catch (error) { + // do nothing + } } - async GetHighScores():Promise{ + public async GetHighScores(): Promise { - var scores:WordGameListItem[] = []; - try{ - let result = await this.context.spHttpClient.get(this.context.pageContext.web.absoluteUrl + "/_api/web/lists/GetByTitle('WordGameList')/items", SPHttpClient.configurations.v1); - let json:any = await result.json(); + let scores: WordGameListItem[] = []; + try { + const result: SPHttpClientResponse = await this.context.spHttpClient.get( + this.context.pageContext.web.absoluteUrl + + "/_api/web/lists/GetByTitle('WordGameList')/items", + SPHttpClient.configurations.v1); + // tslint:disable-next-line: no-any + const json: any = await result.json(); console.log(json); - - - json.value.forEach(item => { - scores.push(new WordGameListItem(item.Title,item.Score,item.Seconds,item.Details)); - }); - - scores.sort((a,b)=> {return b.Score-a.Score}); - //top 10 - if (scores.length>10) - scores = scores.slice(0,10); - - console.log('high scores',scores); - }catch(error){ + json.value.forEach(item => { + scores.push(new WordGameListItem(item.Title, + item.Score, + item.Seconds, + item.Details)); + }); + + scores.sort((a, b) => { + return b.Score - a.Score; + }); + + // top 10 + if (scores.length > 10) { + scores = scores.slice(0, 10); + } + + console.log('high scores', scores); + } catch (error) { console.log('could not find list'); } - + return scores; } - async CreateListIfNotExists(){ - let result = await this.context.spHttpClient.get(this.context.pageContext.web.absoluteUrl + '/_api/web/lists', SPHttpClient.configurations.v1); - let json:any = await result.json(); - let exists = false; + // replace a character in a string + private replaceCharAt(orig: string, index: number, replacement: string): string { + return orig.substr(0, index) + replacement + orig.substr(index + replacement.length); + } + + private async CreateListIfNotExists(): Promise { + const result: SPHttpClientResponse = await this.context.spHttpClient.get( + this.context.pageContext.web.absoluteUrl + + '/_api/web/lists', + SPHttpClient.configurations.v1); + + // tslint:disable-next-line: no-any + const json: any = await result.json(); + let exists: boolean = false; json.value.forEach(list => { - if (list.Title=='WordGameList'){ + if (list.Title === 'WordGameList') { console.log('list found'); exists = true; } }); console.log(json); - if (exists==false){ + if (exists === false) { console.log('Attempting to create list'); await this.CreateList(); await this.AddListColumnNumber('Score'); @@ -292,117 +348,111 @@ export class WordService { } } - async CreateListItem(score:number,seconds:number,details:string){ - var listMetadata = { - "__metadata": { - "type": "SP.Data.WordGameListListItem" + private async CreateListItem(score: number, seconds: number, details: string): Promise { + const listMetadata: {} = { + '__metadata': { + 'type': 'SP.Data.WordGameListListItem' }, - "Title": this.context.pageContext.user.displayName, - "Score": score, - "Seconds": seconds, - "Details": details + 'Title': this.context.pageContext.user.displayName, + 'Score': score, + 'Seconds': seconds, + 'Details': details }; - - var options: ISPHttpClientOptions = { + + const options: ISPHttpClientOptions = { headers: { - "Accept": "application/json;odata=verbose", - "Content-Type": "application/json;odata=verbose", - "OData-Version": "" //Really important to specify + 'Accept': 'application/json;odata=verbose', + 'Content-Type': 'application/json;odata=verbose', + 'OData-Version': '' // Really important to specify }, body: JSON.stringify(listMetadata) }; - let result = await this.context.spHttpClient.post( - this.context.pageContext.web.absoluteUrl + "/_api/web/lists/GetByTitle('WordGameList')/items", SPHttpClient.configurations.v1,options); - let json:any = await result.json(); + const result: SPHttpClientResponse = await this.context.spHttpClient.post( + this.context.pageContext.web.absoluteUrl + + "/_api/web/lists/GetByTitle('WordGameList')/items", + SPHttpClient.configurations.v1, options); + // tslint:disable-next-line: no-any + const json: any = await result.json(); console.log(json); } - async CreateList(){ - var listMetadata = { - "__metadata": { - "type": "SP.List" + private async CreateList(): Promise { + const listMetadata: {} = { + '__metadata': { + 'type': 'SP.List' }, - "AllowContentTypes": true, - "BaseTemplate": 100, - "ContentTypesEnabled": true, - "Description": "Holds high scores for the word game", - "Title": "WordGameList" + 'AllowContentTypes': true, + 'BaseTemplate': 100, + 'ContentTypesEnabled': true, + 'Description': 'Holds high scores for the word game', + 'Title': 'WordGameList' }; - - var options: ISPHttpClientOptions = { + + const options: ISPHttpClientOptions = { headers: { - "Accept": "application/json;odata=verbose", - "Content-Type": "application/json;odata=verbose", - "OData-Version": "" //Really important to specify + 'Accept': 'application/json;odata=verbose', + 'Content-Type': 'application/json;odata=verbose', + 'OData-Version': '' // Really important to specify }, body: JSON.stringify(listMetadata) }; - let result = await this.context.spHttpClient.post( - this.context.pageContext.web.absoluteUrl + '/_api/web/lists', SPHttpClient.configurations.v1,options); - let json:any = await result.json(); + const result: SPHttpClientResponse = await this.context.spHttpClient.post( + this.context.pageContext.web.absoluteUrl + '/_api/web/lists', SPHttpClient.configurations.v1, options); + // tslint:disable-next-line: no-any + const json: any = await result.json(); console.log(json); } - async AddListColumnMultiLineText(name:string){ - var listMetadata = { - '__metadata': {'type':'SP.FieldNumber'}, - 'FieldTypeKind': 3, - 'Title': name, + private async AddListColumnMultiLineText(name: string): Promise { + const listMetadata: {} = { + '__metadata': { 'type': 'SP.FieldNumber' }, + 'FieldTypeKind': 3, + 'Title': name }; - var options: ISPHttpClientOptions = { + const options: ISPHttpClientOptions = { headers: { - "Accept": "application/json;odata=verbose", - "Content-Type": "application/json;odata=verbose", - "OData-Version": "" //Really important to specify + 'Accept': 'application/json;odata=verbose', + 'Content-Type': 'application/json;odata=verbose', + 'OData-Version': '' // Really important to specify }, body: JSON.stringify(listMetadata) }; - let result = await this.context.spHttpClient.post( - this.context.pageContext.web.absoluteUrl + "/_api/web/lists/getbytitle('WordGameList')/fields", SPHttpClient.configurations.v1,options); - let json:any = await result.json(); + const result: SPHttpClientResponse = await this.context.spHttpClient.post( + this.context.pageContext.web.absoluteUrl + + "/_api/web/lists/getbytitle('WordGameList')/fields", + SPHttpClient.configurations.v1, options); + // tslint:disable-next-line: no-any + const json: any = await result.json(); console.log(json); } - async AddListColumnNumber(name:string){ - var listMetadata = { - '__metadata': {'type':'SP.FieldNumber'}, - 'FieldTypeKind': 9, - 'Title': name, - 'MinimumValue': 0, - 'MaximumValue': 1000000 + private async AddListColumnNumber(name: string): Promise { + const listMetadata: {} = { + '__metadata': { 'type': 'SP.FieldNumber' }, + 'FieldTypeKind': 9, + 'Title': name, + 'MinimumValue': 0, + 'MaximumValue': 1000000 }; - var options: ISPHttpClientOptions = { + const options: ISPHttpClientOptions = { headers: { - "Accept": "application/json;odata=verbose", - "Content-Type": "application/json;odata=verbose", - "OData-Version": "" //Really important to specify + 'Accept': 'application/json;odata=verbose', + 'Content-Type': 'application/json;odata=verbose', + 'OData-Version': '' // Really important to specify }, body: JSON.stringify(listMetadata) }; - let result = await this.context.spHttpClient.post( - this.context.pageContext.web.absoluteUrl + "/_api/web/lists/getbytitle('WordGameList')/fields", SPHttpClient.configurations.v1,options); - let json:any = await result.json(); + const result: SPHttpClientResponse = await this.context.spHttpClient.post( + this.context.pageContext.web.absoluteUrl + "/_api/web/lists/getbytitle('WordGameList')/fields", + SPHttpClient.configurations.v1, options); + // tslint:disable-next-line: no-any + const json: any = await result.json(); console.log(json); } - - } - -export class WordGameListItem{ - Name:string; - Score:number; - Seconds:number; - Details:string; - constructor(name:string,score:number,seconds:number,details:string){ - this.Name = name; - this.Score = score; - this.Seconds = seconds; - this.Details = details; - } -} \ No newline at end of file