Open platform to collect and prioritize feedback
allow add comments
goenningb175e8ab1b5e
diff --git a/app/feedback/handler.go b/app/feedback/handler.goindex 21c22d7c9..d684b4aa8 100644--- a/app/feedback/handler.go+++ b/app/feedback/handler.go@@ -75,3 +75,27 @@ func (h IndexHandlder) Details() echo.HandlerFunc {})}}++type newCommentInput struct {+ Content string `json:"content"`+}++// PostComment creates a new comment on given idea+func (h IndexHandlder) PostComment() echo.HandlerFunc {+ return func(c echo.Context) error {+ input := new(newCommentInput)+ c.Bind(input)++ ideaID, _ := strconv.Atoi(c.Param("id"))+ claims := c.Get("Claims").(*identity.WechyClaims)++ id, err := h.ideaService.AddComment(claims.UserID, int64(ideaID), input.Content)+ if err != nil {+ return echo.NewHTTPError(http.StatusInternalServerError, err)+ }++ return c.JSON(200, echo.Map{+ "comment_id": id,+ })+ }+}diff --git a/app/feedback/handler_test.go b/app/feedback/handler_test.goindex c8472aa0b..7de1f9c5b 100644--- a/app/feedback/handler_test.go+++ b/app/feedback/handler_test.go@@ -27,6 +27,10 @@ func (svc mockIdeaService) Save(tenantID, userID int64, title, description strinreturn nil, nil}+func (svc mockIdeaService) AddComment(userID, ideaID int64, content string) (int64, error) {+ return 0, nil+}+func TestIndexHandler(t *testing.T) {RegisterTestingT(t)diff --git a/app/feedback/service.go b/app/feedback/service.goindex ebd42e9db..2deda8a54 100644--- a/app/feedback/service.go+++ b/app/feedback/service.go@@ -6,4 +6,5 @@ type IdeaService interface {GetCommentsByIdeaID(tenantID, ideaID int64) ([]*Comment, error)GetAll(tenantID int64) ([]*Idea, error)Save(tenantID, userID int64, title, description string) (*Idea, error)+ AddComment(userID, ideaID int64, content string) (int64, error)}diff --git a/app/identity/middleware.go b/app/identity/middleware.goindex 99de9bf10..8021fcb92 100644--- a/app/identity/middleware.go+++ b/app/identity/middleware.go@@ -56,6 +56,7 @@ func JwtSetter() echo.MiddlewareFunc {Name: "auth",Value: jwt,HttpOnly: true,+ Path: "/",})scheme := "http"diff --git a/app/postgres/feedback.go b/app/postgres/feedback.goindex 3fc9fd6bd..b2e8837e2 100644--- a/app/postgres/feedback.go+++ b/app/postgres/feedback.go@@ -100,3 +100,19 @@ func (svc IdeaService) Save(tenantID, userID int64, title, description string) (return idea, tx.Commit()}++// AddComment places a new comment on an idea+func (svc IdeaService) AddComment(userID, ideaID int64, content string) (int64, error) {+ tx, err := svc.DB.Begin()+ if err != nil {+ return 0, err+ }++ var id int64+ if err = tx.QueryRow("INSERT INTO comments (idea_id, content, user_id, created_on) VALUES ($1, $2, $3, $4) RETURNING id", ideaID, content, userID, time.Now()).Scan(&id); err != nil {+ tx.Rollback()+ return 0, err+ }++ return id, tx.Commit()+}diff --git a/app/router.go b/app/router.goindex 4fcf6dfeb..04aab70e3 100644--- a/app/router.go+++ b/app/router.go@@ -79,10 +79,11 @@ func GetMainEngine(ctx *WechyServices) *echo.Echo {appGroup.GET("/", feedback.Index(ctx.Idea).List())appGroup.GET("/ideas/:id", feedback.Index(ctx.Idea).Details())-appGroup.GET("/logout", identity.OAuth(ctx.OAuth, ctx.User).Logout())- appGroup.POST("/api/ideas", feedback.Index(ctx.Idea).Post())+appGroup.GET("/api/status", Status(ctx.Health, ctx.Settings))+ appGroup.POST("/api/ideas", feedback.Index(ctx.Idea).Post())+ appGroup.POST("/api/ideas/:id/comments", feedback.Index(ctx.Idea).PostComment())}return routerdiff --git a/public/components/CommentInput.tsx b/public/components/CommentInput.tsxnew file mode 100644index 000000000..b86e3e658--- /dev/null+++ b/public/components/CommentInput.tsx@@ -0,0 +1,59 @@+import axios from "axios";+import * as React from "react";+import { Idea } from "../models";+import { getCurrentUser } from "../storage";+import { SocialSignInButton } from "./SocialSignInButton";++interface CommentInputProps {+ idea: Idea;+}++interface CommentInputState {+ content: string;+ clicked: boolean;+}++export class CommentInput extends React.Component<CommentInputProps, CommentInputState> {+ constructor() {+ super();+ this.state = {+ content: "",+ clicked: false+ };+ }++ public async submit() {+ this.setState({ clicked: true });++ const response = await axios.post(`/api/ideas/${this.props.idea.id}/comments`, {+ content: this.state.content+ });++ location.reload();+ }++ public render() {+ const user = getCurrentUser();+ const buttonClasses = `ui blue labeled submit icon button ${this.state.clicked && "loading disabled"}`;++ const addComment = user ? <form className="ui reply form">+ <div className="field">+ <textarea onKeyUp={(e) => { this.setState({ content: e.currentTarget.value }); }}+ placeholder="Leave your comment here..."></textarea>+ </div>+ <div className={ buttonClasses } onClick={async () => await this.submit()}>+ <i className="icon edit"></i> Add Comment+ </div>+ </form> :+ <div className="ui message">+ <div className="header">+ Please log in before leaving a comment+ </div>+ <p>This only takes a second and you'll be good to go!</p>+ <SocialSignInButton provider="facebook" small={true} />+ <SocialSignInButton provider="google" small={true} />+ </div>;++ return addComment;+ }+}diff --git a/public/components/Header.tsx b/public/components/Header.tsxindex 6d460f625..fd518443d 100644--- a/public/components/Header.tsx+++ b/public/components/Header.tsx@@ -45,9 +45,9 @@ export class Header extends React.Component<{}, {}> {return <div><div className="ui menu"><div className="ui container">- <div className="header item">+ <a href="/" className="header item">{ tenant.name }- </div>+ </a>{ profile }</div></div>diff --git a/public/components/ShowIdeaRoot.tsx b/public/components/ShowIdeaRoot.tsxindex 09a2158b8..74d28836f 100644--- a/public/components/ShowIdeaRoot.tsx+++ b/public/components/ShowIdeaRoot.tsx@@ -1,7 +1,9 @@import * as moment from "moment";import * as React from "react";-import { Comment, Idea } from "../models";import * as storage from "../storage";++import { Comment, Idea } from "../models";+import { CommentInput } from "./CommentInput";import { Gravatar, MultiLineText } from "./Common";import { SocialSignInButton } from "./SocialSignInButton";@@ -32,23 +34,6 @@ export class ShowIdeaRoot extends React.Component<{}, {}> {</div>) : <p>No comments yet.</p>;- const addComment = user ? <form className="ui reply form">- <div className="field">- <textarea></textarea>- </div>- <div className="ui blue labeled submit icon button">- <i className="icon edit"></i> Add Comment- </div>- </form> :- <div className="ui message">- <div className="header">- Please log in before leaving a comment- </div>- <p>This only takes a second and you'll be good to go!</p>- <SocialSignInButton provider="facebook" small={true} />- <SocialSignInButton provider="google" small={true} />- </div>;-return <div><Header /><div className="ui container">@@ -65,7 +50,7 @@ export class ShowIdeaRoot extends React.Component<{}, {}> {<h3 className="ui dividing header">Comments</h3>{ commentsList }</div>- { addComment }+ <CommentInput idea={idea} /></div><Footer /></div>;