Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

some typescript refactoring

goenning1262038c8d1e
diff --git a/app/identity/model.go b/app/identity/model.go
index 33656f87c..f97b5d21b 100644
--- a/app/identity/model.go
+++ b/app/identity/model.go
@@ -9,14 +9,14 @@ type Tenant struct {
//User represents an user inside our application
type User struct {
- ID int64
- Name string
- Email string
- Providers []*UserProvider
+ ID int64 `json:"id"`
+ Name string `json:"name"`
+ Email string `json:"email"`
+ Providers []*UserProvider `json:"providers"`
}
//UserProvider represents the relashionship between an User and an Authentication provide
type UserProvider struct {
- Name string
- UID string
+ Name string `json:"name"`
+ UID string `json:"uid"`
}
diff --git a/app/renderer.go b/app/renderer.go
index 5b3aa8a33..a78e12a37 100644
--- a/app/renderer.go
+++ b/app/renderer.go
@@ -1,8 +1,6 @@
package app
import (
- "crypto/md5"
- "encoding/hex"
"fmt"
"html/template"
"io"
@@ -66,12 +64,15 @@ func (r *HTMLRenderer) Render(w io.Writer, name string, data interface{}, c echo
//TODO: refactor (and move somewhere else?)
m := data.(echo.Map)
- claims, ok := c.Get("Claims").(*identity.WechyClaims)
-
m["AuthEndpoint"] = os.Getenv("AUTH_ENDPOINT")
+
+ claims, ok := c.Get("Claims").(*identity.WechyClaims)
if ok {
- m["Claims"] = claims
- m["Gravatar"] = toMDF5(claims.UserEmail)
+ m["User"] = &identity.User{
+ ID: claims.UserID,
+ Name: claims.UserName,
+ Email: claims.UserEmail,
+ }
}
files, _ := ioutil.ReadDir("dist/js")
@@ -81,9 +82,3 @@ func (r *HTMLRenderer) Render(w io.Writer, name string, data interface{}, c echo
return tmpl.Execute(w, m)
}
-
-func toMDF5(content string) string {
- hasher := md5.New()
- hasher.Write([]byte(content))
- return fmt.Sprintf("%v", hex.EncodeToString(hasher.Sum(nil)))
-}
diff --git a/package.json b/package.json
index 8b9c7d741..f3667996a 100644
--- a/package.json
+++ b/package.json
@@ -13,6 +13,7 @@
"dependencies": {
"axios": "^0.15.3",
"jquery": "^3.2.0",
+ "md5": "^2.2.1",
"moment": "^2.17.1",
"react": "^15.4.2",
"react-dom": "^15.4.2",
@@ -20,6 +21,7 @@
},
"devDependencies": {
"@types/jquery": "^2.0.41",
+ "@types/md5": "^2.1.31",
"@types/moment": "^2.13.0",
"@types/react": "^15.0.16",
"@types/react-dom": "^0.14.23",
@@ -27,7 +29,7 @@
"css-loader": "^0.27.3",
"file-loader": "^0.10.1",
"rimraf": "^2.6.1",
- "style-loader": "^0.13.2",
+ "style-loader": "^0.14.1",
"ts-loader": "^2.0.1",
"tslint": "^4.5.1",
"typescript": "^2.2.1",
diff --git a/public/components/Header.tsx b/public/components/Header.tsx
index 73fbf17cd..941bcb2a9 100644
--- a/public/components/Header.tsx
+++ b/public/components/Header.tsx
@@ -1,16 +1,19 @@
+import md5 = require("md5");
import * as React from "react";
+import { Tenant } from "../models";
+import { get, getCurrentUser } from "../storage";
import { SocialSignInButton } from "./SocialSignInButton";
-const tenant = (window as any)._tenant;
-const claims = (window as any)._claims;
-const gravatar = (window as any)._gravatar;
-
export class Header extends React.Component<{}, {}> {
public render() {
- const profile = claims ?
+ const user = getCurrentUser();
+ const tenant = get<Tenant>("tenant");
+
+ const profile = user ?
<a className="item right signin">
- <img className="ui avatar image" src={ "https://www.gravatar.com/avatar/" + gravatar } />
- { claims["user/name"] }
+ <img className="ui avatar image"
+ src={ "https://www.gravatar.com/avatar/" + md5(user.email) } />
+ { user.name }
<i className="dropdown icon"></i>
</a> :
<a className="item right signin">
@@ -19,7 +22,7 @@ export class Header extends React.Component<{}, {}> {
<i className="dropdown icon"></i>
</a>;
- const dropdown = claims ?
+ const dropdown = user ?
<div id="user-popup" className="ui popup top left transition hidden">
<div className="ui">
<div className="item">
diff --git a/public/components/IdeaInput.tsx b/public/components/IdeaInput.tsx
index 741b05a8e..260e467c5 100644
--- a/public/components/IdeaInput.tsx
+++ b/public/components/IdeaInput.tsx
@@ -1,11 +1,10 @@
import axios from "axios";
import * as React from "react";
+import { getCurrentUser } from "../storage";
import { SocialSignInButton } from "./SocialSignInButton";
-const claims = (window as any)._claims;
-
interface IdeaInputState {
- idea: string;
+ title: string;
clicked: boolean;
}
@@ -15,7 +14,7 @@ export class IdeaInput extends React.Component<{}, IdeaInputState> {
constructor() {
super();
this.state = {
- idea: "",
+ title: "",
clicked: false
};
}
@@ -24,7 +23,7 @@ export class IdeaInput extends React.Component<{}, IdeaInputState> {
this.setState({ clicked: true });
const response = await axios.post("/api/ideas", {
- title: this.state.idea,
+ title: this.state.title,
description: this.description.value
});
@@ -32,8 +31,9 @@ export class IdeaInput extends React.Component<{}, IdeaInputState> {
}
public render() {
+ const user = getCurrentUser();
const buttonClasses = `ui positive button ${this.state.clicked && "loading disabled"}`;
- const details = claims ?
+ const details = user ?
<div>
<div className="field">
<textarea ref={(ref) => this.description = ref }
@@ -59,12 +59,12 @@ export class IdeaInput extends React.Component<{}, IdeaInputState> {
<div className="ui fluid input">
<input id="new-idea-input"
type="text"
- maxLength={60}
- onKeyUp={(e) => { this.setState({ idea: e.currentTarget.value }); }}
+ maxLength={100}
+ onKeyUp={(e) => { this.setState({ title: e.currentTarget.value }); }}
placeholder="Enter your idea, new feature or suggestion here ..." />
</div>
- { this.state.idea.length > 0 && details }
+ { this.state.title.length > 0 && details }
</div>;
}
}
diff --git a/public/components/Root.tsx b/public/components/Root.tsx
index c6c2a8e45..3d629d6e4 100644
--- a/public/components/Root.tsx
+++ b/public/components/Root.tsx
@@ -1,17 +1,17 @@
import * as moment from "moment";
import * as React from "react";
+import { Idea } from "../models";
+import * as storage from "../storage";
import { Footer } from "./Footer";
import { Header } from "./Header";
import { IdeaInput } from "./IdeaInput";
-const claims = (window as any)._claims;
-const ideas: any[] = (window as any)._ideas || [];
-
export class Root extends React.Component<{}, {}> {
public render() {
+ const ideas = storage.get<Idea[]>("ideas") || [];
- const ideasList = ideas.map((x: any) =>
+ const ideasList = ideas.map((x) =>
<div className="item" key={x.id}>
<div className="content">
<a className="header">{ x.title }</a>
diff --git a/public/components/SocialSignInButton.tsx b/public/components/SocialSignInButton.tsx
index 5e3a1fce3..ee04dcff7 100644
--- a/public/components/SocialSignInButton.tsx
+++ b/public/components/SocialSignInButton.tsx
@@ -1,6 +1,5 @@
import * as React from "react";
-
-const authEndpoint = (window as any)._authEndpoint;
+import { get } from "../storage";
interface SocialSignInButtonProps {
provider: string;
@@ -19,6 +18,7 @@ export class SocialSignInButton extends React.Component<SocialSignInButtonProps,
}
public render() {
+ const authEndpoint = get<string>("authEndpoint");
const providerClassName = this.props.provider === "google" ? "google plus" : "facebook";
const providerDisplayName = this.props.provider === "google" ? "Google" : "Facebook";
const oauthUrl = `${authEndpoint}/oauth/${this.props.provider}?redirect=${location.href}`;
diff --git a/public/index.ts b/public/index.ts
index 94cafd86b..2e6732513 100644
--- a/public/index.ts
+++ b/public/index.ts
@@ -1,9 +1,11 @@
import * as $ from "jquery";
-
import init from "./init";
+import * as storage from "./storage";
import "./css/main.css";
+storage.setup();
+
$(document).ready(() => {
init();
diff --git a/public/init.tsx b/public/init.tsx
index 555244446..4c04a8169 100644
--- a/public/init.tsx
+++ b/public/init.tsx
@@ -9,4 +9,4 @@ export default function init() {
location.pathname === "/" ? <Root /> : <div />,
document.getElementById("root")
);
-}
\ No newline at end of file
+}
diff --git a/public/models.ts b/public/models.ts
new file mode 100644
index 000000000..d2be68510
--- /dev/null
+++ b/public/models.ts
@@ -0,0 +1,18 @@
+export interface Tenant {
+ id: number;
+ name: string;
+ domain: string;
+}
+
+export interface User {
+ id: number;
+ name: string;
+ email: string;
+}
+
+export interface Idea {
+ id: number;
+ title: string;
+ description: string;
+ createdOn: string;
+}
\ No newline at end of file
diff --git a/public/storage.ts b/public/storage.ts
new file mode 100644
index 000000000..d314fd4dc
--- /dev/null
+++ b/public/storage.ts
@@ -0,0 +1,26 @@
+import { User } from "./models";
+
+const w: any = window;
+
+export function setup() {
+ w.setUser = setUser;
+ w.getCurrentUser = getCurrentUser;
+ w.set = set;
+ w.get = get;
+}
+
+export function setUser(user: User) {
+ set<User>("user", user);
+}
+
+export function getCurrentUser(): User {
+ return get<User>("user");
+}
+
+export function set<T>(key: string, value: T) {
+ w[`_${key}`] = value;
+}
+
+export function get<T>(key: string): T {
+ return w[`_${key}`];
+}
diff --git a/views/base.html b/views/base.html
index 7b02a6944..1f969c8bd 100644
--- a/views/base.html
+++ b/views/base.html
@@ -8,22 +8,21 @@
<title>{{template "title" .}}</title>
</head>
<body>
- <script type="text/javascript">
- var _authEndpoint = {{ .AuthEndpoint }};
- var _tenant = {{ .Tenant }};
- var _claims = {{ .Claims }};
- var _gravatar = {{ .Gravatar }}
- {{template "javascript" .}}
- </script>
-
<div id="root"></div>
{{template "content" .}}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.0/jquery.min.js" integrity="sha256-JAW99MJVpJBGcbzEuXk4Az05s/XyDdBomFqNlM3ic+I=" crossorigin="anonymous"></script>
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react.min.js" integrity="sha256-Rtnfi1MVZAj3v+eDeFjh/iAXo8//D0zVKql8fjVLCiM=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.4.2/react-dom.min.js" integrity="sha256-uzKwYuH50DHzDYr3h/Bnpv7C5QJNUjHEpVmT3Xuq3ww=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.9/semantic.min.js" integrity="sha256-mq9Ru97qzOm1x5zeYFfPniWgQP9tEmjo3O4jUEJdjNY=" crossorigin="anonymous"></script>
<script src="/assets/js/{{ .JavaScriptBundle }}"></script>
+ <script type="text/javascript">
+ setUser({{ .User }});
+ set("authEndpoint", {{ .AuthEndpoint }});
+ set("tenant", {{ .Tenant }});
+ {{template "javascript" .}}
+ </script>
</body>
</html>
diff --git a/views/index.html b/views/index.html
index 4dd447ee9..0fe5c19db 100644
--- a/views/index.html
+++ b/views/index.html
@@ -3,7 +3,7 @@
{{end}}
{{define "javascript"}}
- var _ideas = {{ .Ideas }};
+ set("ideas", {{ .Ideas }});
{{end}}
{{define "content"}}{{end}}
\ No newline at end of file