Updated to retrieve values from API
This commit is contained in:
parent
c6ade94e23
commit
0ed79aead2
|
@ -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]);
|
||||
|
|
|
@ -1,26 +0,0 @@
|
|||
interface Map<K, V> {
|
||||
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;
|
||||
}
|
||||
});
|
|
@ -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<Map<string, number>> {
|
||||
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<void> {
|
||||
|
|
Loading…
Reference in New Issue