From 0ed79aead2c37061cc53e8358f84875c4ff8a5cc Mon Sep 17 00:00:00 2001 From: Takashi Shinohara Date: Sat, 1 Jan 2022 22:37:03 +0900 Subject: [PATCH] Updated to retrieve values from API --- .../webparts/ratings/components/Ratings.tsx | 9 ++---- .../src/webparts/ratings/extensions/Map.ts | 26 --------------- .../ratings/services/SPHttpClientService.ts | 32 +++++++++++++------ 3 files changed, 25 insertions(+), 42 deletions(-) delete mode 100644 samples/react-star-ratings/src/webparts/ratings/extensions/Map.ts diff --git a/samples/react-star-ratings/src/webparts/ratings/components/Ratings.tsx b/samples/react-star-ratings/src/webparts/ratings/components/Ratings.tsx index 3266c8d23..e684e6543 100644 --- a/samples/react-star-ratings/src/webparts/ratings/components/Ratings.tsx +++ b/samples/react-star-ratings/src/webparts/ratings/components/Ratings.tsx @@ -12,7 +12,6 @@ import styles from './Ratings.module.scss'; import * as strings from 'RatingsWebPartStrings'; import { IRatingsProps } from './IRatingsProps'; import SPHttpClientService from '../services/SPHttpClientService'; -import '../extensions/Map'; interface IRatings { rating: number; @@ -77,14 +76,11 @@ export const Ratings = ({ context, properties }: IRatingsProps) => { const service = new SPHttpClientService(context); await service.ensureFeatureEnabled(); const user = await service.getCurrentUser(); - const ratings = await service.getRatings(); - const rating = ratings.get(user.LoginName); - const count = ratings.size; - const sum = ratings.values().reduce((current, prev) => prev + current, 0); + const [ average, count, rating ] = await service.getRating(user.LoginName); return { rating: rating, count: count, - average: count ? sum / count : 0 + average: average }; }, []); @@ -103,6 +99,7 @@ export const Ratings = ({ context, properties }: IRatingsProps) => { setValue(await getRating()); } catch (error) { setError(error.toString()); + throw error; } })(); }, [context]); diff --git a/samples/react-star-ratings/src/webparts/ratings/extensions/Map.ts b/samples/react-star-ratings/src/webparts/ratings/extensions/Map.ts deleted file mode 100644 index 5b271bb01..000000000 --- a/samples/react-star-ratings/src/webparts/ratings/extensions/Map.ts +++ /dev/null @@ -1,26 +0,0 @@ -interface Map { - keys(): K[]; - values(): V[]; -} - -Object.defineProperty(Map.prototype, 'keys', { - configurable: true, - enumerable: false, - writable: true, - value: function () { - const array = []; - this.forEach((value, key) => array.push(key)); - return array; - } -}); - -Object.defineProperty(Map.prototype, 'values', { - configurable: true, - enumerable: false, - writable: true, - value: function () { - const array = []; - this.forEach((value, key) => array.push(value)); - return array; - } -}); diff --git a/samples/react-star-ratings/src/webparts/ratings/services/SPHttpClientService.ts b/samples/react-star-ratings/src/webparts/ratings/services/SPHttpClientService.ts index 6252f87cb..167bda5ff 100644 --- a/samples/react-star-ratings/src/webparts/ratings/services/SPHttpClientService.ts +++ b/samples/react-star-ratings/src/webparts/ratings/services/SPHttpClientService.ts @@ -13,6 +13,8 @@ interface IRetedBy { } interface IRating { + AverageRating: number; + RatingCount: number; RatedBy: IRetedBy[]; Ratings: string; } @@ -58,10 +60,10 @@ export default class SPSPHttpClientService { return value; } - public async getRatings(): Promise> { + public async getRating(loginName: string): Promise<[number, number, number]> { const response = await this.client.get( `${this.url}/_api/web/lists('${this.listId}')/items(${this.itemId})` + - '?$select=Ratings,RatedBy/Id,RatedBy/Name' + + '?$select=AverageRating,RatingCount,Ratings,RatedBy/Id,RatedBy/Name' + '&$expand=RatedBy', SPHttpClient.configurations.v1); if (!response.ok) { @@ -69,15 +71,25 @@ export default class SPSPHttpClientService { } const json = await response.json(); const value = json as IRating; - if (value.Ratings) { - return new Map(value.Ratings - .slice(0, -1) - .split(',') - .map(item => Number(item)) - .map((item, index) => ([value.RatedBy[index].Name, item]))); - } else { - return new Map(); + const ratings = value.Ratings ? value.Ratings.slice(0, -1).split(',') : []; + const average = value.AverageRating || 0; + const count = value.RatingCount || 0; + let rating = 0; + if (value.RatedBy) { + for (let index = 0; index < value.RatedBy.length; index += 1) { + const ratedBy = value.RatedBy[index]; + if (ratedBy.Name === loginName) { + if (index < ratings.length) { + rating = Number(ratings[index]); + } + } + } } + return [ + average, + count, + rating + ]; } public async setRating(rating: number): Promise {