Companion sample to js-gitHubBadge sample (#847)

* Initial commit

* Initial version
This commit is contained in:
Hugo Bernier 2019-05-04 11:05:57 -04:00 committed by Vesa Juvonen
parent 500a566abd
commit d001566f54
33 changed files with 18045 additions and 0 deletions

View File

@ -0,0 +1,25 @@
# EditorConfig helps developers define and maintain consistent
# coding styles between different editors and IDEs
# editorconfig.org
root = true
[*]
# change these settings to your own preference
indent_style = space
indent_size = 2
# we recommend you to keep these unchanged
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.md]
trim_trailing_whitespace = false
[{package,bower}.json]
indent_style = space
indent_size = 2

32
samples/react-github-badge/.gitignore vendored Normal file
View File

@ -0,0 +1,32 @@
# Logs
logs
*.log
npm-debug.log*
# Dependency directories
node_modules
# Build generated files
dist
lib
solution
temp
*.sppkg
# Coverage directory used by tools like istanbul
coverage
# OSX
.DS_Store
# Visual Studio files
.ntvs_analysis.dat
.vs
bin
obj
# Resx Generated Code
*.resx.ts
# Styles Generated Code
*.scss.ts

View File

@ -0,0 +1,5 @@
{
"recommendations": [
"msjsdiag.debugger-for-chrome"
]
}

View File

@ -0,0 +1,43 @@
{
/**
* Install Chrome Debugger Extension for Visual Studio Code to debug your components with the
* Chrome browser: https://aka.ms/spfx-debugger-extensions
*/
"version": "0.2.0",
"configurations": [{
"name": "Local workbench",
"type": "chrome",
"request": "launch",
"url": "https://localhost:4321/temp/workbench.html",
"webRoot": "${workspaceRoot}",
"sourceMaps": true,
"sourceMapPathOverrides": {
"webpack:///.././src/*": "${webRoot}/src/*",
"webpack:///../../../src/*": "${webRoot}/src/*",
"webpack:///../../../../src/*": "${webRoot}/src/*",
"webpack:///../../../../../src/*": "${webRoot}/src/*"
},
"runtimeArgs": [
"--remote-debugging-port=9222"
]
},
{
"name": "Hosted workbench",
"type": "chrome",
"request": "launch",
"url": "https://enter-your-SharePoint-site/_layouts/workbench.aspx",
"webRoot": "${workspaceRoot}",
"sourceMaps": true,
"sourceMapPathOverrides": {
"webpack:///.././src/*": "${webRoot}/src/*",
"webpack:///../../../src/*": "${webRoot}/src/*",
"webpack:///../../../../src/*": "${webRoot}/src/*",
"webpack:///../../../../../src/*": "${webRoot}/src/*"
},
"runtimeArgs": [
"--remote-debugging-port=9222",
"-incognito"
]
}
]
}

View File

@ -0,0 +1,13 @@
// Place your settings in this file to overwrite default and user settings.
{
// Configure glob patterns for excluding files and folders in the file explorer.
"files.exclude": {
"**/.git": true,
"**/.DS_Store": true,
"**/bower_components": true,
"**/coverage": true,
"**/lib-amd": true,
"src/**/*.scss.ts": true
},
"typescript.tsdk": ".\\node_modules\\typescript\\lib"
}

View File

@ -0,0 +1,12 @@
{
"@microsoft/generator-sharepoint": {
"isCreatingSolution": true,
"environment": "spo",
"version": "1.8.1",
"libraryName": "react-github-badge",
"libraryId": "566f9f40-96ac-41b6-bf11-a1f4675dc838",
"packageManager": "npm",
"isDomainIsolated": false,
"componentType": "webpart"
}
}

View File

@ -0,0 +1,52 @@
# React GitHub Badge
## Summary
React version of Eric Skaggs [@skaggej](https://www.twitter.com/skaggej) js-GitHubBadge sample.
Displays information from GitHub for a specified user.
![React GitHub Badge in action](./assets/GitHubBadge.png)
## Used SharePoint Framework Version
![SPFx v1.8.1](https://img.shields.io/badge/SPFx-1.8.1-green.svg)
## Applies to
* [SharePoint Framework](https:/dev.office.com/sharepoint)
* [Office 365 tenant](https://dev.office.com/sharepoint/docs/spfx/set-up-your-development-environment)
## Solution
Solution|Author(s)
--------|---------
react-github-badge | Hugo Bernier ([Tahoe Ninjas](http://tahoeninjas.blog), [@bernierh](https://www.twitter.com/bernierh))
Converted from: [js-GitHubBadge](https://github.com/SharePoint/sp-dev-fx-webparts/tree/master/samples/js-gitHubBadge) | Eric Skaggs [@skaggej](https://www.twitter.com/skaggej)
## Version history
Version|Date|Comments
-------|----|--------
1.0 | April 18, 2019 | Initial release
## Disclaimer
**THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.**
---
## Minimal Path to Awesome
- Clone this repository
- in the command line run:
- `npm install`
- `gulp serve`
## Features
This sample illustrates how to convert an existing Javascript-only SPFx web part to a React SPFx web part while keeping the same look and feel. Please refer to the [js-GitHubBadge](https://github.com/SharePoint/sp-dev-fx-webparts/tree/master/samples/js-gitHubBadge) sample for the original Javascript-only web part.
For more information about the conversion process, refer to this [blog article](https://tahoeninjas.blog/2019/04/19/converting-spfx-from-javascript-to-react/).
<img src="https://telemetry.sharepointpnp.com/sp-dev-fx-webparts/samples/react-github-badge" />

Binary file not shown.

After

Width:  |  Height:  |  Size: 620 KiB

View File

@ -0,0 +1,18 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/spfx-build/config.2.0.schema.json",
"version": "2.0",
"bundles": {
"git-hub-badge-web-part": {
"components": [
{
"entrypoint": "./lib/webparts/gitHubBadge/GitHubBadgeWebPart.js",
"manifest": "./src/webparts/gitHubBadge/GitHubBadgeWebPart.manifest.json"
}
]
}
},
"externals": {},
"localizedResources": {
"GitHubBadgeWebPartStrings": "lib/webparts/gitHubBadge/loc/{locale}.js"
}
}

View File

@ -0,0 +1,4 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/spfx-build/copy-assets.schema.json",
"deployCdnPath": "temp/deploy"
}

View File

@ -0,0 +1,7 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/spfx-build/deploy-azure-storage.schema.json",
"workingDir": "./temp/deploy/",
"account": "<!-- STORAGE ACCOUNT NAME -->",
"container": "react-github-badge",
"accessKey": "<!-- ACCESS KEY -->"
}

View File

@ -0,0 +1,13 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/spfx-build/package-solution.schema.json",
"solution": {
"name": "react-github-badge-client-side-solution",
"id": "566f9f40-96ac-41b6-bf11-a1f4675dc838",
"version": "1.0.0.0",
"includeClientSideAssets": true,
"isDomainIsolated": false
},
"paths": {
"zippedPackage": "solution/react-github-badge.sppkg"
}
}

View File

@ -0,0 +1,10 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/core-build/serve.schema.json",
"port": 4321,
"https": true,
"initialPage": "https://localhost:5432/workbench",
"api": {
"port": 5432,
"entryPath": "node_modules/@microsoft/sp-webpart-workbench/lib/api/"
}
}

View File

@ -0,0 +1,4 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/spfx-build/write-manifests.schema.json",
"cdnBasePath": "<!-- PATH TO CDN -->"
}

View File

@ -0,0 +1,7 @@
'use strict';
const gulp = require('gulp');
const build = require('@microsoft/sp-build-web');
build.addSuppression(`Warning - [sass] The local CSS class 'ms-Grid' is not camelCase and will not be type-safe.`);
build.initialize(gulp);

17284
samples/react-github-badge/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,40 @@
{
"name": "react-github-badge",
"version": "0.0.1",
"private": true,
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"build": "gulp bundle",
"clean": "gulp clean",
"test": "gulp test"
},
"dependencies": {
"react": "16.7.0",
"react-dom": "16.7.0",
"@types/react": "16.4.2",
"@types/react-dom": "16.0.5",
"@microsoft/sp-core-library": "1.8.1",
"@microsoft/sp-property-pane": "1.8.1",
"@microsoft/sp-webpart-base": "1.8.1",
"@microsoft/sp-lodash-subset": "1.8.1",
"@microsoft/sp-office-ui-fabric-core": "1.8.1",
"@types/webpack-env": "1.13.1",
"@types/es6-promise": "0.0.33"
},
"resolutions": {
"@types/react": "16.4.2"
},
"devDependencies": {
"@microsoft/sp-build-web": "1.8.1",
"@microsoft/sp-tslint-rules": "1.8.1",
"@microsoft/sp-module-interfaces": "1.8.1",
"@microsoft/sp-webpart-workbench": "1.8.1",
"@microsoft/rush-stack-compiler-2.7": "0.4.0",
"gulp": "~3.9.1",
"@types/chai": "3.4.34",
"@types/mocha": "2.2.38",
"ajv": "~5.2.2"
}
}

View File

@ -0,0 +1 @@
// A file is required to be in the root of the /src directory by the TypeScript compiler

View File

@ -0,0 +1,23 @@
import { IGitHubService, IGitHubUserProfile } from "./GitHubServices.types";
import { HttpClient, HttpClientResponse } from '@microsoft/sp-http';
export class GitHubService implements IGitHubService {
private _httpClient: HttpClient;
constructor(httpClient: HttpClient) {
this._httpClient = httpClient;
}
public getUserProfile(alias: string): Promise<IGitHubUserProfile> {
const gitHubUrl: string = "https://api.github.com/users/"+alias;
// call the GitHub API
return this._httpClient.get(gitHubUrl,
HttpClient.configurations.v1, {}).then((response: HttpClientResponse) => response.json())
.then((profile: IGitHubUserProfile) => {
return profile;
});
}
}

View File

@ -0,0 +1,37 @@
export interface IGitHubUserProfile {
login: string;
id: number;
node_id: string;
avatar_url: string;
gravatar_id: string;
url: string;
html_url: string;
followers_url: string;
following_url: string;
gists_url: string;
starred_url: string;
subscriptions_url: string;
organizations_url: string;
repos_url: string;
events_url: string;
received_events_url: string;
type: string;
site_admin: boolean;
name: string;
company: string;
blog: string;
location: string;
email?: any;
hireable?: any;
bio: string;
public_repos: number;
public_gists: number;
followers: number;
following: number;
created_at: string;
updated_at: string;
}
export interface IGitHubService {
getUserProfile(alias: string): Promise<IGitHubUserProfile>;
}

View File

@ -0,0 +1,47 @@
import { IGitHubService, IGitHubUserProfile } from "./GitHubServices.types";
export class MockGitHubService implements IGitHubService {
public getUserProfile(alias: string): Promise<IGitHubUserProfile> {
return new Promise<IGitHubUserProfile>((resolve) => {
// pretend we're getting the data from the GitHub API by adding a delay
setTimeout(() => {
const fakeProfile: IGitHubUserProfile = {
login: "skaggej",
id: 1846656,
node_id: "MDQ6VXNlcjE4NDY2NTY=",
avatar_url: "https://avatars1.githubusercontent.com/u/1846656?v=4",
gravatar_id: "",
url: "https://api.github.com/users/skaggej",
html_url: "https://github.com/skaggej",
followers_url: "https://api.github.com/users/skaggej/followers",
following_url: "https://api.github.com/users/skaggej/following{/other_user}",
gists_url: "https://api.github.com/users/skaggej/gists{/gist_id}",
starred_url: "https://api.github.com/users/skaggej/starred{/owner}{/repo}",
subscriptions_url: "https://api.github.com/users/skaggej/subscriptions",
organizations_url: "https://api.github.com/users/skaggej/orgs",
repos_url: "https://api.github.com/users/skaggej/repos",
events_url: "https://api.github.com/users/skaggej/events{/privacy}",
received_events_url: "https://api.github.com/users/skaggej/received_events",
type: "User",
site_admin: false,
name: "Eric Skaggs",
company: "http://www.catapultsystems.com",
blog: "http://www.ericskaggs.net",
location: "Phoenix, AZ",
email: null,
hireable: null,
bio: "Fuse Solution Architect at Catapult Systems",
public_repos: 29,
public_gists: 3,
followers: 8,
following: 33,
created_at: "2012-06-13T14:01:52Z",
updated_at: "2019-04-09T00:18:35Z"
};
resolve(fakeProfile);
}, 500);
});
}
}

View File

@ -0,0 +1,3 @@
export * from './GitHubServices.types';
export * from './MockGitHubService';
export * from './GitHubService';

View File

@ -0,0 +1,23 @@
{
"$schema": "https://developer.microsoft.com/json-schemas/spfx/client-side-web-part-manifest.schema.json",
"id": "7fd97fdd-7641-426c-94b9-f20bb473a6c5",
"alias": "GitHubBadgeWebPart",
"componentType": "WebPart",
"version": "*",
"manifestVersion": 2,
"requiresCustomScript": false,
"supportedHosts": ["SharePointWebPart"],
"preconfiguredEntries": [{
"groupId": "5c03119e-3074-46fd-976b-c60198311f70",
"group": { "default": "Other" },
"title": { "default": "GitHub Badge" },
"description": { "default": "Displays information from GitHub for a specified user" },
"officeFabricIconFontName": "Page",
"properties": {
"gitHubUserName": "skaggej"
}
}]
}

View File

@ -0,0 +1,65 @@
import * as React from 'react';
import * as ReactDom from 'react-dom';
import { Version } from '@microsoft/sp-core-library';
import { BaseClientSideWebPart } from '@microsoft/sp-webpart-base';
import {
IPropertyPaneConfiguration,
PropertyPaneTextField
} from '@microsoft/sp-property-pane';
import * as strings from 'GitHubBadgeWebPartStrings';
import GitHubBadge from './components/GitHubBadge';
import { IGitHubBadgeProps } from './components/GitHubBadge.types';
export interface IGitHubBadgeWebPartProps {
gitHubUserName: string;
}
export default class GitHubBadgeWebPart extends BaseClientSideWebPart<IGitHubBadgeWebPartProps> {
public render(): void {
const element: React.ReactElement<IGitHubBadgeProps> = React.createElement(
GitHubBadge,
{
gitHubUserName: this.properties.gitHubUserName,
httpClient: this.context.httpClient
}
);
ReactDom.render(element, this.domElement);
}
protected onDispose(): void {
ReactDom.unmountComponentAtNode(this.domElement);
}
protected get dataVersion(): Version {
return Version.parse('1.0');
}
protected get disableReactivePropertyChanges(): boolean {
return true;
}
protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
return {
pages: [
{
header: {
description: strings.PropertyPaneDescription
},
groups: [
{
groupName: strings.BasicGroupName,
groupFields: [
PropertyPaneTextField('gitHubUserName', {
label: strings.GitHubUserNameFieldLabel
})
]
}
]
}
]
};
}
}

View File

@ -0,0 +1,74 @@
@import '~@microsoft/sp-office-ui-fabric-core/dist/sass/SPFabricCore.scss';
.gitHubBadge {
.container {
max-width: 700px;
margin: 0px auto;
box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.2), 0 25px 50px 0 rgba(0, 0, 0, 0.1);
}
.row {
@include ms-Grid-row;
@include ms-fontColor-white;
background-color: $ms-color-themeDark;
padding: 20px;
}
.column {
@include ms-Grid-col;
@include ms-lg10;
@include ms-xl8;
@include ms-xlPush2;
@include ms-lgPush1;
}
.title {
@include ms-font-xl;
@include ms-fontColor-white;
}
.subTitle {
@include ms-font-l;
@include ms-fontColor-white;
}
.description {
@include ms-font-l;
@include ms-fontColor-white;
}
.button {
// Our button
text-decoration: none;
height: 32px;
// Primary Button
min-width: 80px;
background-color: $ms-color-themePrimary;
border-color: $ms-color-themePrimary;
color: $ms-color-white;
// Basic Button
outline: transparent;
position: relative;
font-family: "Segoe UI WestEuropean","Segoe UI",-apple-system,BlinkMacSystemFont,Roboto,"Helvetica Neue",sans-serif;
-webkit-font-smoothing: antialiased;
font-size: $ms-font-size-m;
font-weight: $ms-font-weight-regular;
border-width: 0;
text-align: center;
cursor: pointer;
display: inline-block;
padding: 0 16px;
.label {
font-weight: $ms-font-weight-semibold;
font-size: $ms-font-size-m;
height: 32px;
line-height: 32px;
margin: 0 4px;
vertical-align: top;
display: inline-block;
}
}
}

View File

@ -0,0 +1,105 @@
import * as React from 'react';
import styles from './GitHubBadge.module.scss';
import { IGitHubBadgeProps, IGitHubBadgeState } from './GitHubBadge.types';
import { escape } from '@microsoft/sp-lodash-subset';
import { IGitHubService, IGitHubUserProfile, MockGitHubService, GitHubService } from '../../../services/GitHubServices';
export default class GitHubBadge extends React.Component<IGitHubBadgeProps, IGitHubBadgeState> {
/**
*
*/
constructor(props:IGitHubBadgeProps) {
super(props);
this.state = {
isLoading: true
};
}
public componentDidMount(): void {
this.getUserProfile();
}
public componentDidUpdate(prevProps: IGitHubBadgeProps, prevState: IGitHubBadgeState): void {
if (prevProps.gitHubUserName !== this.props.gitHubUserName) {
this.getUserProfile();
}
}
public render(): React.ReactElement<IGitHubBadgeProps> {
const { userProfile, isLoading, errorMessage } = this.state;
return (
<div className={ styles.gitHubBadge }>
<div className={ styles.container }>
<div className={ styles.row }>
{ isLoading &&
<div className={ styles.column }>
<div>Loading...</div>
</div>
}
{ !isLoading && userProfile &&
<div className={ styles.column }>
<div><img src={userProfile.avatar_url} alt="GitHub User Profile Picture" /></div>
<div className={ styles.title }>{this.props.gitHubUserName}</div>
<div className={ styles.label }>{userProfile.login}</div>
<div className={ styles.label }>{userProfile.id}</div>
<div className={ styles.label }>{userProfile.node_id}</div>
<div className={ styles.label }>{userProfile.avatar_url}</div>
<div className={ styles.label }>{userProfile.gravatar_id}</div>
<div className={ styles.label }>{userProfile.url}</div>
<div className={ styles.label }>{userProfile.html_url}</div>
<div className={ styles.label }>{userProfile.followers_url}</div>
<div className={ styles.label }>{userProfile.following_url}</div>
<div className={ styles.label }>{userProfile.gists_url}</div>
<div className={ styles.label }>{userProfile.starred_url}</div>
<div className={ styles.label }>{userProfile.subscriptions_url}</div>
<div className={ styles.label }>{userProfile.organizations_url}</div>
<div className={ styles.label }>{userProfile.repos_url}</div>
<div className={ styles.label }>{userProfile.events_url}</div>
<div className={ styles.label }>{userProfile.received_events_url}</div>
<div className={ styles.label }>{userProfile.type}</div>
<div className={ styles.label }>{userProfile.site_admin}</div>
<div className={ styles.label }>{userProfile.name}</div>
<div className={ styles.label }>{userProfile.name}</div>
<div className={ styles.label }>{userProfile.company}</div>
<div className={ styles.label }>{userProfile.location}</div>
<div className={ styles.label }>{userProfile.email}</div>
<div className={ styles.label }>{userProfile.hireable}</div>
<div className={ styles.label }>{userProfile.bio}</div>
<div className={ styles.label }>{userProfile.public_repos}</div>
<div className={ styles.label }>{userProfile.public_gists}</div>
<div className={ styles.label }>{userProfile.followers}</div>
<div className={ styles.label }>{userProfile.following}</div>
<div className={ styles.label }>{userProfile.created_at}</div>
<div className={ styles.label }>{userProfile.updated_at}</div>
</div>
}
{ !isLoading && errorMessage &&
<div className={ styles.column }>
<div className={styles.label}>WARNING - error when calling URL https://api.github.com/users/{this.props.gitHubUserName}. Error = {errorMessage}</div>
</div>
}
</div>
</div>
</div>
);
}
private getUserProfile() {
// Create an instance of the GitHub service
//const service: IGitHubService = new MockGitHubService();
const service: IGitHubService = new GitHubService(this.props.httpClient);
// Call the GitHub service
// In real-life, we would only call it when we're sure that there is a username
service.getUserProfile(this.props.gitHubUserName).then((results: IGitHubUserProfile)=>{
// Set the userProfile with the results we got and isLoading to false, because we're done
// loading. It'll make things redraw magically.
this.setState({
userProfile: results,
isLoading: false
});
});
}
}

View File

@ -0,0 +1,13 @@
import { IGitHubUserProfile } from "../../../services/GitHubServices";
import { HttpClient } from '@microsoft/sp-http';
export interface IGitHubBadgeProps {
gitHubUserName: string;
httpClient: HttpClient;
}
export interface IGitHubBadgeState {
isLoading: boolean;
userProfile?: IGitHubUserProfile;
errorMessage?: string;
}

View File

@ -0,0 +1,7 @@
define([], function() {
return {
"PropertyPaneDescription": "Description",
"BasicGroupName": "Group Name",
"GitHubUserNameFieldLabel": "GitHub user name"
}
});

View File

@ -0,0 +1,10 @@
declare interface IGitHubBadgeWebPartStrings {
PropertyPaneDescription: string;
BasicGroupName: string;
GitHubUserNameFieldLabel: string;
}
declare module 'GitHubBadgeWebPartStrings' {
const strings: IGitHubBadgeWebPartStrings;
export = strings;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

View File

@ -0,0 +1,38 @@
{
"extends": "./node_modules/@microsoft/rush-stack-compiler-2.7/includes/tsconfig-web.json",
"compilerOptions": {
"target": "es5",
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"jsx": "react",
"declaration": true,
"sourceMap": true,
"experimentalDecorators": true,
"skipLibCheck": true,
"outDir": "lib",
"inlineSources": false,
"strictNullChecks": false,
"noUnusedLocals": false,
"typeRoots": [
"./node_modules/@types",
"./node_modules/@microsoft"
],
"types": [
"es6-promise",
"webpack-env"
],
"lib": [
"es5",
"dom",
"es2015.collection"
]
},
"include": [
"src/**/*.ts"
],
"exclude": [
"node_modules",
"lib"
]
}

View File

@ -0,0 +1,30 @@
{
"extends": "@microsoft/sp-tslint-rules/base-tslint.json",
"rules": {
"class-name": false,
"export-name": false,
"forin": false,
"label-position": false,
"member-access": true,
"no-arg": false,
"no-console": false,
"no-construct": false,
"no-duplicate-variable": true,
"no-eval": false,
"no-function-expression": true,
"no-internal-module": true,
"no-shadowed-variable": true,
"no-switch-case-fall-through": true,
"no-unnecessary-semicolons": true,
"no-unused-expression": true,
"no-use-before-declare": true,
"no-with-statement": true,
"semicolon": true,
"trailing-comma": false,
"typedef": false,
"typedef-whitespace": false,
"use-named-parameter": true,
"variable-name": false,
"whitespace": false
}
}