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 * as strings from 'RatingsWebPartStrings';
|
||||||
import { IRatingsProps } from './IRatingsProps';
|
import { IRatingsProps } from './IRatingsProps';
|
||||||
import SPHttpClientService from '../services/SPHttpClientService';
|
import SPHttpClientService from '../services/SPHttpClientService';
|
||||||
import '../extensions/Map';
|
|
||||||
|
|
||||||
interface IRatings {
|
interface IRatings {
|
||||||
rating: number;
|
rating: number;
|
||||||
|
@ -77,14 +76,11 @@ export const Ratings = ({ context, properties }: IRatingsProps) => {
|
||||||
const service = new SPHttpClientService(context);
|
const service = new SPHttpClientService(context);
|
||||||
await service.ensureFeatureEnabled();
|
await service.ensureFeatureEnabled();
|
||||||
const user = await service.getCurrentUser();
|
const user = await service.getCurrentUser();
|
||||||
const ratings = await service.getRatings();
|
const [ average, count, rating ] = await service.getRating(user.LoginName);
|
||||||
const rating = ratings.get(user.LoginName);
|
|
||||||
const count = ratings.size;
|
|
||||||
const sum = ratings.values().reduce((current, prev) => prev + current, 0);
|
|
||||||
return {
|
return {
|
||||||
rating: rating,
|
rating: rating,
|
||||||
count: count,
|
count: count,
|
||||||
average: count ? sum / count : 0
|
average: average
|
||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
@ -103,6 +99,7 @@ export const Ratings = ({ context, properties }: IRatingsProps) => {
|
||||||
setValue(await getRating());
|
setValue(await getRating());
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
setError(error.toString());
|
setError(error.toString());
|
||||||
|
throw error;
|
||||||
}
|
}
|
||||||
})();
|
})();
|
||||||
}, [context]);
|
}, [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 {
|
interface IRating {
|
||||||
|
AverageRating: number;
|
||||||
|
RatingCount: number;
|
||||||
RatedBy: IRetedBy[];
|
RatedBy: IRetedBy[];
|
||||||
Ratings: string;
|
Ratings: string;
|
||||||
}
|
}
|
||||||
|
@ -58,10 +60,10 @@ export default class SPSPHttpClientService {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getRatings(): Promise<Map<string, number>> {
|
public async getRating(loginName: string): Promise<[number, number, number]> {
|
||||||
const response = await this.client.get(
|
const response = await this.client.get(
|
||||||
`${this.url}/_api/web/lists('${this.listId}')/items(${this.itemId})` +
|
`${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',
|
'&$expand=RatedBy',
|
||||||
SPHttpClient.configurations.v1);
|
SPHttpClient.configurations.v1);
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
@ -69,16 +71,26 @@ export default class SPSPHttpClientService {
|
||||||
}
|
}
|
||||||
const json = await response.json();
|
const json = await response.json();
|
||||||
const value = json as IRating;
|
const value = json as IRating;
|
||||||
if (value.Ratings) {
|
const ratings = value.Ratings ? value.Ratings.slice(0, -1).split(',') : [];
|
||||||
return new Map(value.Ratings
|
const average = value.AverageRating || 0;
|
||||||
.slice(0, -1)
|
const count = value.RatingCount || 0;
|
||||||
.split(',')
|
let rating = 0;
|
||||||
.map(item => Number(item))
|
if (value.RatedBy) {
|
||||||
.map((item, index) => ([value.RatedBy[index].Name, item])));
|
for (let index = 0; index < value.RatedBy.length; index += 1) {
|
||||||
} else {
|
const ratedBy = value.RatedBy[index];
|
||||||
return new Map();
|
if (ratedBy.Name === loginName) {
|
||||||
|
if (index < ratings.length) {
|
||||||
|
rating = Number(ratings[index]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return [
|
||||||
|
average,
|
||||||
|
count,
|
||||||
|
rating
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
public async setRating(rating: number): Promise<void> {
|
public async setRating(rating: number): Promise<void> {
|
||||||
const response = await this.client.post(
|
const response = await this.client.post(
|
||||||
|
|
Loading…
Reference in New Issue