Merge pull request #2195 from karamem0/main
react-star-ratings - Updated to retrieve values from API
This commit is contained in:
commit
038e48f6b0
|
@ -0,0 +1,204 @@
|
|||
{
|
||||
"bundles": {
|
||||
"list-search-web-part": {
|
||||
"dependencies": [
|
||||
{
|
||||
"componentId": "f9e737b7-f0df-4597-ba8c-3060f82380db",
|
||||
"componentName": "@microsoft/sp-property-pane",
|
||||
"componentVersion": "1.13.1",
|
||||
"isDirectDependency": true
|
||||
},
|
||||
{
|
||||
"componentId": "1c6c9123-7aac-41f3-a376-3caea41ed83f",
|
||||
"componentName": "@microsoft/sp-loader",
|
||||
"componentVersion": "1.13.1",
|
||||
"isDirectDependency": false
|
||||
},
|
||||
{
|
||||
"componentId": "8217e442-8ed3-41fd-957d-b112e841286a",
|
||||
"componentName": "@ms/sp-telemetry",
|
||||
"componentVersion": "0.19.42",
|
||||
"isDirectDependency": false
|
||||
},
|
||||
{
|
||||
"componentId": "e40f8203-b39d-425a-a957-714852e33b79",
|
||||
"componentName": "@microsoft/sp-dynamic-data",
|
||||
"componentVersion": "1.13.1",
|
||||
"isDirectDependency": false
|
||||
},
|
||||
{
|
||||
"componentId": "73e1dc6c-8441-42cc-ad47-4bd3659f8a3a",
|
||||
"componentName": "@microsoft/sp-lodash-subset",
|
||||
"componentVersion": "1.13.1",
|
||||
"isDirectDependency": false
|
||||
},
|
||||
{
|
||||
"componentId": "7263c7d0-1d6a-45ec-8d85-d4d1d234171b",
|
||||
"componentName": "@microsoft/sp-core-library",
|
||||
"componentVersion": "1.13.1",
|
||||
"isDirectDependency": false
|
||||
},
|
||||
{
|
||||
"componentId": "01c4df03-e775-48cb-aa14-171ee5199a15",
|
||||
"componentName": "tslib",
|
||||
"componentVersion": "1.10.0",
|
||||
"isDirectDependency": false
|
||||
},
|
||||
{
|
||||
"componentId": "2e09fb9b-13bb-48f2-859f-97d6fff71176",
|
||||
"componentName": "@ms/odsp-core-bundle",
|
||||
"componentVersion": "1.1.192",
|
||||
"isDirectDependency": false
|
||||
},
|
||||
{
|
||||
"componentId": "78359e4b-07c2-43c6-8d0b-d060b4d577e8",
|
||||
"componentName": "@microsoft/sp-diagnostics",
|
||||
"componentVersion": "1.13.1",
|
||||
"isDirectDependency": false
|
||||
},
|
||||
{
|
||||
"componentId": "1c4541f7-5c31-41aa-9fa8-fbc9dc14c0a8",
|
||||
"componentName": "@microsoft/sp-page-context",
|
||||
"componentVersion": "1.13.1",
|
||||
"isDirectDependency": false
|
||||
},
|
||||
{
|
||||
"componentId": "229b8d08-79f3-438b-8c21-4613fc877abd",
|
||||
"componentName": "@microsoft/load-themed-styles",
|
||||
"componentVersion": "0.1.2",
|
||||
"isDirectDependency": false
|
||||
},
|
||||
{
|
||||
"componentId": "c07208f0-ea3b-4c1a-9965-ac1b825211a6",
|
||||
"componentName": "@microsoft/sp-http",
|
||||
"componentVersion": "1.13.1",
|
||||
"isDirectDependency": false
|
||||
},
|
||||
{
|
||||
"componentId": "0d910c1c-13b9-4e1c-9aa4-b008c5e42d7d",
|
||||
"componentName": "react",
|
||||
"componentVersion": "16.13.1",
|
||||
"isDirectDependency": false
|
||||
},
|
||||
{
|
||||
"componentId": "aa0a46ec-1505-43cd-a44a-93f3a5aa460a",
|
||||
"componentName": "react-dom",
|
||||
"componentVersion": "16.13.1",
|
||||
"isDirectDependency": false
|
||||
},
|
||||
{
|
||||
"componentId": "467dc675-7cc5-4709-8aac-78e3b71bd2f6",
|
||||
"componentName": "@microsoft/sp-component-base",
|
||||
"componentVersion": "1.13.1",
|
||||
"isDirectDependency": true
|
||||
},
|
||||
{
|
||||
"componentId": "974a7777-0990-4136-8fa6-95d80114c2e0",
|
||||
"componentName": "@microsoft/sp-webpart-base",
|
||||
"componentVersion": "1.13.1",
|
||||
"isDirectDependency": true
|
||||
}
|
||||
]
|
||||
},
|
||||
"list-search-consumer-web-part-web-part": {
|
||||
"dependencies": [
|
||||
{
|
||||
"componentId": "f9e737b7-f0df-4597-ba8c-3060f82380db",
|
||||
"componentName": "@microsoft/sp-property-pane",
|
||||
"componentVersion": "1.13.1",
|
||||
"isDirectDependency": true
|
||||
},
|
||||
{
|
||||
"componentId": "1c6c9123-7aac-41f3-a376-3caea41ed83f",
|
||||
"componentName": "@microsoft/sp-loader",
|
||||
"componentVersion": "1.13.1",
|
||||
"isDirectDependency": false
|
||||
},
|
||||
{
|
||||
"componentId": "8217e442-8ed3-41fd-957d-b112e841286a",
|
||||
"componentName": "@ms/sp-telemetry",
|
||||
"componentVersion": "0.19.42",
|
||||
"isDirectDependency": false
|
||||
},
|
||||
{
|
||||
"componentId": "e40f8203-b39d-425a-a957-714852e33b79",
|
||||
"componentName": "@microsoft/sp-dynamic-data",
|
||||
"componentVersion": "1.13.1",
|
||||
"isDirectDependency": false
|
||||
},
|
||||
{
|
||||
"componentId": "73e1dc6c-8441-42cc-ad47-4bd3659f8a3a",
|
||||
"componentName": "@microsoft/sp-lodash-subset",
|
||||
"componentVersion": "1.13.1",
|
||||
"isDirectDependency": false
|
||||
},
|
||||
{
|
||||
"componentId": "7263c7d0-1d6a-45ec-8d85-d4d1d234171b",
|
||||
"componentName": "@microsoft/sp-core-library",
|
||||
"componentVersion": "1.13.1",
|
||||
"isDirectDependency": false
|
||||
},
|
||||
{
|
||||
"componentId": "01c4df03-e775-48cb-aa14-171ee5199a15",
|
||||
"componentName": "tslib",
|
||||
"componentVersion": "1.10.0",
|
||||
"isDirectDependency": false
|
||||
},
|
||||
{
|
||||
"componentId": "2e09fb9b-13bb-48f2-859f-97d6fff71176",
|
||||
"componentName": "@ms/odsp-core-bundle",
|
||||
"componentVersion": "1.1.192",
|
||||
"isDirectDependency": false
|
||||
},
|
||||
{
|
||||
"componentId": "78359e4b-07c2-43c6-8d0b-d060b4d577e8",
|
||||
"componentName": "@microsoft/sp-diagnostics",
|
||||
"componentVersion": "1.13.1",
|
||||
"isDirectDependency": false
|
||||
},
|
||||
{
|
||||
"componentId": "1c4541f7-5c31-41aa-9fa8-fbc9dc14c0a8",
|
||||
"componentName": "@microsoft/sp-page-context",
|
||||
"componentVersion": "1.13.1",
|
||||
"isDirectDependency": false
|
||||
},
|
||||
{
|
||||
"componentId": "229b8d08-79f3-438b-8c21-4613fc877abd",
|
||||
"componentName": "@microsoft/load-themed-styles",
|
||||
"componentVersion": "0.1.2",
|
||||
"isDirectDependency": false
|
||||
},
|
||||
{
|
||||
"componentId": "c07208f0-ea3b-4c1a-9965-ac1b825211a6",
|
||||
"componentName": "@microsoft/sp-http",
|
||||
"componentVersion": "1.13.1",
|
||||
"isDirectDependency": false
|
||||
},
|
||||
{
|
||||
"componentId": "0d910c1c-13b9-4e1c-9aa4-b008c5e42d7d",
|
||||
"componentName": "react",
|
||||
"componentVersion": "16.13.1",
|
||||
"isDirectDependency": false
|
||||
},
|
||||
{
|
||||
"componentId": "aa0a46ec-1505-43cd-a44a-93f3a5aa460a",
|
||||
"componentName": "react-dom",
|
||||
"componentVersion": "16.13.1",
|
||||
"isDirectDependency": false
|
||||
},
|
||||
{
|
||||
"componentId": "974a7777-0990-4136-8fa6-95d80114c2e0",
|
||||
"componentName": "@microsoft/sp-webpart-base",
|
||||
"componentVersion": "1.13.1",
|
||||
"isDirectDependency": true
|
||||
},
|
||||
{
|
||||
"componentId": "467dc675-7cc5-4709-8aac-78e3b71bd2f6",
|
||||
"componentName": "@microsoft/sp-component-base",
|
||||
"componentVersion": "1.13.1",
|
||||
"isDirectDependency": false
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because one or more lines are too long
|
@ -14,7 +14,7 @@ This web part demonstrates *Star Ratings* capablities to SharePoint News. The "R
|
|||
![Does not work with SharePoint 2019](https://img.shields.io/badge/SharePoint%20Server%202019-Incompatible-red.svg "SharePoint Server 2019 requires SPFx 1.4.1 or lower")
|
||||
![Does not work with SharePoint 2016 (Feature Pack 2)](https://img.shields.io/badge/SharePoint%20Server%202016%20(Feature%20Pack%202)-Incompatible-red.svg "SharePoint Server 2016 Feature Pack 2 requires SPFx 1.1")
|
||||
![Local Workbench Incompatible](https://img.shields.io/badge/Local%20Workbench-Incompatible-red.svg "This solution requires the ratings feature from SharePoint Online")
|
||||
![Hosted Workbench Compatible](https://img.shields.io/badge/Hosted%20Workbench-Compatible-green.svg)
|
||||
![Hosted Workbench Not Compatible](https://img.shields.io/badge/Hosted%20Workbench-Not-Compatible-red.svg "This web part needs to run on a page")
|
||||
|
||||
|
||||
## Prerequisites
|
||||
|
@ -38,15 +38,15 @@ Enable-PnPFeature -Identity 915c240e-a6cc-49b8-8b2c-0bff8b553ed3
|
|||
|
||||
Solution|Author(s)
|
||||
--------|---------
|
||||
react-star-ratings | [Takashi Shinohara](https://github.com/karamem0) ([@karamem0](https://twitter.com/karamem0))
|
||||
react-star-ratings|[Takashi Shinohara](https://github.com/karamem0) ([@karamem0](https://twitter.com/karamem0))
|
||||
|
||||
## Version history
|
||||
|
||||
Version|Date|Comments
|
||||
-------|----|--------
|
||||
1.1|January 12, 2022|Updated to retrieve values from API
|
||||
1.0|October 7, 2021|Initial release
|
||||
|
||||
|
||||
## Help
|
||||
|
||||
We do not support samples, but we this community is always willing to help, and we want to improve these samples. We use GitHub to track issues, which makes it easy for community members to volunteer their time and help resolve issues.
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
"This web part demonstrates Star Ratings capablities to SharePoint News. The Ratings site collection feature provides Likes and Star Ratings, but SharePoint News provides only provides Likes. This web part can get or set ratings of the current page."
|
||||
],
|
||||
"creationDateTime": "2021-10-07",
|
||||
"updateDateTime": "2021-10-07",
|
||||
"updateDateTime": "2022-01-06",
|
||||
"products": [
|
||||
"SharePoint"
|
||||
],
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "ratings",
|
||||
"version": "1.0.0",
|
||||
"version": "1.1.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "ratings",
|
||||
"version": "1.0.0",
|
||||
"version": "1.1.0",
|
||||
"private": true,
|
||||
"main": "lib/index.js",
|
||||
"scripts": {
|
||||
|
|
|
@ -34,7 +34,7 @@ export default class RatingsWebPart extends BaseClientSideWebPart<IRatingsWebPar
|
|||
}
|
||||
|
||||
protected get dataVersion(): Version {
|
||||
return Version.parse('1.0');
|
||||
return Version.parse('1.1');
|
||||
}
|
||||
|
||||
protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
|
||||
|
|
|
@ -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,11 +13,13 @@ interface IRetedBy {
|
|||
}
|
||||
|
||||
interface IRating {
|
||||
AverageRating: number;
|
||||
RatingCount: number;
|
||||
RatedBy: IRetedBy[];
|
||||
Ratings: string;
|
||||
}
|
||||
|
||||
export default class SPSPHttpClientService {
|
||||
export default class SPHttpClientService {
|
||||
|
||||
private client: SPHttpClient;
|
||||
private url: 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