Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

feature: show list of voters (#658)

Guilherme Oenning68732aa51d76
diff --git a/app/cmd/routes.go b/app/cmd/routes.go
index 843145e89..549f12133 100644
--- a/app/cmd/routes.go
+++ b/app/cmd/routes.go
@@ -175,7 +175,7 @@ func routes(r *web.Engine) *web.Engine {
api.Get("/api/v1/users", apiv1.ListUsers())
api.Put("/api/v1/posts/:number", apiv1.UpdatePost())
- api.Get("/api/v1/posts/:number/voters", apiv1.ListVoters())
+ api.Get("/api/v1/posts/:number/votes", apiv1.ListVotes())
api.Post("/api/v1/invitations/send", apiv1.SendInvites())
api.Post("/api/v1/invitations/sample", apiv1.SendSampleInvite())
api.Put("/api/v1/posts/:number/status", apiv1.SetResponse())
diff --git a/app/handlers/apiv1/post.go b/app/handlers/apiv1/post.go
index 39ebb9b13..b9ff99ca6 100644
--- a/app/handlers/apiv1/post.go
+++ b/app/handlers/apiv1/post.go
@@ -264,8 +264,8 @@ func Unsubscribe() web.HandlerFunc {
}
}
-// ListVoters returns a list of all users that voted on given post
-func ListVoters() web.HandlerFunc {
+// ListVotes returns a list of all votes on given post
+func ListVotes() web.HandlerFunc {
return func(c web.Context) error {
number, err := c.ParamAsInt("number")
if err != nil {
@@ -277,16 +277,12 @@ func ListVoters() web.HandlerFunc {
return c.Failure(err)
}
- users, err := c.Services().Posts.ListVoters(post)
+ votes, err := c.Services().Posts.ListVotes(post, -1)
if err != nil {
return c.Failure(err)
}
- for _, u := range users {
- u.ShowEmail = c.IsAuthenticated() && c.User().IsCollaborator()
- }
-
- return c.Ok(users)
+ return c.Ok(votes)
}
}
diff --git a/app/handlers/apiv1/user.go b/app/handlers/apiv1/user.go
index 47d86381d..2b095e853 100644
--- a/app/handlers/apiv1/user.go
+++ b/app/handlers/apiv1/user.go
@@ -15,11 +15,6 @@ func ListUsers() web.HandlerFunc {
if err != nil {
return c.Failure(err)
}
-
- for _, u := range users {
- u.ShowEmail = c.IsAuthenticated() && c.User().IsCollaborator()
- }
-
return c.Ok(users)
}
}
diff --git a/app/handlers/post.go b/app/handlers/post.go
index 1e3b5161a..f584997a3 100644
--- a/app/handlers/post.go
+++ b/app/handlers/post.go
@@ -76,6 +76,11 @@ func PostDetails() web.HandlerFunc {
return c.Failure(err)
}
+ votes, err := c.Services().Posts.ListVotes(post, 6)
+ if err != nil {
+ return c.Failure(err)
+ }
+
return c.Page(web.Props{
Title: post.Title,
Description: markdown.PlainText(post.Description),
@@ -84,6 +89,7 @@ func PostDetails() web.HandlerFunc {
"subscribed": subscribed,
"post": post,
"tags": tags,
+ "votes": votes,
},
})
}
diff --git a/app/models/feedback.go b/app/models/feedback.go
index 15fafa82c..2ceb76ceb 100644
--- a/app/models/feedback.go
+++ b/app/models/feedback.go
@@ -22,6 +22,16 @@ type Post struct {
Tags []string `json:"tags"`
}
+//Vote represents a vote given by a user on a post
+type Vote struct {
+ User *struct {
+ ID int `json:"id" db:"id"`
+ Name string `json:"name" db:"name"`
+ Email string `json:"email,omitempty" db:"email"`
+ } `json:"user" db:"user"`
+ CreatedAt time.Time `json:"createdAt" db:"created_at"`
+}
+
// CanBeVoted returns true if this post can have its vote changed
func (i *Post) CanBeVoted() bool {
return i.Status != PostCompleted && i.Status != PostDeclined && i.Status != PostDuplicate
diff --git a/app/storage/inmemory/post.go b/app/storage/inmemory/post.go
index 4ffb7b80c..c685bec15 100644
--- a/app/storage/inmemory/post.go
+++ b/app/storage/inmemory/post.go
@@ -234,9 +234,9 @@ func (s *PostStorage) VotedBy() ([]int, error) {
return s.postsVotedBy[s.user.ID], nil
}
-// ListVoters returns a list of all users that voted on given post
-func (s *PostStorage) ListVoters(post *models.Post) ([]*models.User, error) {
- return make([]*models.User, 0), nil
+// ListVotes returns a list of all votes on given post
+func (s *PostStorage) ListVotes(post *models.Post, limit int) ([]*models.Vote, error) {
+ return make([]*models.Vote, 0), nil
}
// AddSubscriber adds user to the post list of subscribers
diff --git a/app/storage/postgres/post.go b/app/storage/postgres/post.go
index 7addb35d5..180e59cae 100644
--- a/app/storage/postgres/post.go
+++ b/app/storage/postgres/post.go
@@ -712,25 +712,30 @@ func (s *PostStorage) VotedBy() ([]int, error) {
return posts, nil
}
-// ListVoters returns a list of all users that voted on given post
-func (s *PostStorage) ListVoters(post *models.Post) ([]*models.User, error) {
- var users []*dbUser
- err := s.trx.Select(&users, `
- SELECT u.id, u.name, u.email, u.tenant_id, u.role, u.status
+// ListVotes returns a list of all votes on given post
+func (s *PostStorage) ListVotes(post *models.Post, limit int) ([]*models.Vote, error) {
+ sqlLimit := "ALL"
+ if limit > 0 {
+ sqlLimit = strconv.Itoa(limit)
+ }
+
+ var votes []*models.Vote
+ err := s.trx.Select(&votes, `
+ SELECT
+ pv.created_at,
+ u.id AS user_id,
+ u.name AS user_name,
+ u.email AS user_email
FROM post_votes pv
INNER JOIN users u
ON u.id = pv.user_id
AND u.tenant_id = pv.tenant_id
WHERE pv.post_id = $1
AND pv.tenant_id = $2
- ORDER BY pv.created_at`, post.ID, s.tenant.ID)
+ ORDER BY pv.created_at
+ LIMIT `+sqlLimit, post.ID, s.tenant.ID)
if err != nil {
- return nil, errors.Wrap(err, "failed to get voters of post")
+ return nil, errors.Wrap(err, "failed to get votes of post")
}
-
- var result = make([]*models.User, len(users))
- for i, user := range users {
- result[i] = user.toModel()
- }
- return result, nil
+ return votes, nil
}
diff --git a/app/storage/postgres/post_test.go b/app/storage/postgres/post_test.go
index c6c65cb38..354b26ba6 100644
--- a/app/storage/postgres/post_test.go
+++ b/app/storage/postgres/post_test.go
@@ -527,7 +527,7 @@ func TestPostStorage_IsReferenced(t *testing.T) {
Expect(err).IsNil()
}
-func TestPostStorage_ListVotersOfPost(t *testing.T) {
+func TestPostStorage_ListVotesOfPost(t *testing.T) {
SetupDatabaseTest(t)
defer TeardownDatabaseTest()
@@ -537,9 +537,15 @@ func TestPostStorage_ListVotersOfPost(t *testing.T) {
posts.AddVote(post1, jonSnow)
posts.AddVote(post1, aryaStark)
- users, err := posts.ListVoters(post1)
+ users, err := posts.ListVotes(post1, -1)
Expect(err).IsNil()
Expect(users).HasLen(2)
- Expect(users[0].Name).Equals("Jon Snow")
- Expect(users[1].Name).Equals("Arya Stark")
+
+ Expect(users[0].CreatedAt).TemporarilySimilar(time.Now(), 5*time.Second)
+ Expect(users[0].User.Name).Equals("Jon Snow")
+ Expect(users[0].User.Email).Equals("[email protected]")
+
+ Expect(users[1].CreatedAt).TemporarilySimilar(time.Now(), 5*time.Second)
+ Expect(users[1].User.Name).Equals("Arya Stark")
+ Expect(users[1].User.Email).Equals("[email protected]")
}
diff --git a/app/storage/storage.go b/app/storage/storage.go
index 72bc1b53c..23165d8b7 100644
--- a/app/storage/storage.go
+++ b/app/storage/storage.go
@@ -37,7 +37,7 @@ type Post interface {
MarkAsDuplicate(post *models.Post, original *models.Post) error
IsReferenced(post *models.Post) (bool, error)
VotedBy() ([]int, error)
- ListVoters(post *models.Post) ([]*models.User, error)
+ ListVotes(post *models.Post, limit int) ([]*models.Vote, error)
}
// User is used for user operations
diff --git a/public/components/common/Gravatar.tsx b/public/components/common/Gravatar.tsx
index 525e11e71..72c13314c 100644
--- a/public/components/common/Gravatar.tsx
+++ b/public/components/common/Gravatar.tsx
@@ -2,10 +2,14 @@ import "./Gravatar.scss";
import React from "react";
import { classSet, Fider } from "@fider/services";
-import { User, isCollaborator } from "@fider/models";
+import { isCollaborator, UserRole } from "@fider/models";
interface GravatarProps {
- user?: User;
+ user?: {
+ id: number;
+ name: string;
+ role?: UserRole;
+ };
size?: "small" | "normal" | "large";
}
@@ -18,7 +22,7 @@ export const Gravatar = (props: GravatarProps) => {
const className = classSet({
"c-avatar": true,
[`m-${size}`]: true,
- "m-staff": props.user && isCollaborator(props.user.role)
+ "m-staff": props.user && props.user.role && isCollaborator(props.user.role)
});
return <img className={className} title={name} src={url} />;
diff --git a/public/components/common/List.scss b/public/components/common/List.scss
index 030508dff..61b03007f 100644
--- a/public/components/common/List.scss
+++ b/public/components/common/List.scss
@@ -21,6 +21,7 @@
.c-list-item {
padding: 15px 0 0 0;
margin: 0 0 15px 0;
+ border-top: 1px solid $gray-lighter;
&:first-child {
border-top: none;
padding-top: 0;
diff --git a/public/components/common/Modal.tsx b/public/components/common/Modal.tsx
index 6780a5c36..55af08b95 100644
--- a/public/components/common/Modal.tsx
+++ b/public/components/common/Modal.tsx
@@ -5,6 +5,7 @@ import ReactDOM from "react-dom";
import { classSet } from "@fider/services";
interface ModalWindowProps {
+ className?: string;
isOpen: boolean;
size?: "small" | "large";
canClose?: boolean;
@@ -91,6 +92,7 @@ class ModalWindow extends React.Component<ModalWindowProps, ModalWindowState> {
const className = classSet({
"c-modal-window": true,
+ [`${this.props.className}`]: !!this.props.className,
"m-center": this.props.center,
[`m-${this.props.size}`]: true
});
diff --git a/public/components/common/UserName.tsx b/public/components/common/UserName.tsx
index 848111f77..2024eac36 100644
--- a/public/components/common/UserName.tsx
+++ b/public/components/common/UserName.tsx
@@ -1,17 +1,21 @@
import "./UserName.scss";
import React from "react";
-import { User, isCollaborator } from "@fider/models";
+import { isCollaborator, UserRole } from "@fider/models";
import { classSet } from "@fider/services";
interface UserNameProps {
- user: User;
+ user: {
+ id: number;
+ name: string;
+ role?: UserRole;
+ };
}
export const UserName = (props: UserNameProps) => {
const className = classSet({
"c-username": true,
- "m-staff": isCollaborator(props.user.role)
+ "m-staff": props.user.role && isCollaborator(props.user.role)
});
return <span className={className}>{props.user.name || "Anonymous"}</span>;
diff --git a/public/models/post.ts b/public/models/post.ts
index 88cfeda03..c1bb87017 100644
--- a/public/models/post.ts
+++ b/public/models/post.ts
@@ -80,3 +80,12 @@ export interface Tag {
color: string;
isPublic: boolean;
}
+
+export interface Vote {
+ createdAt: Date;
+ user: {
+ id: number;
+ name: string;
+ email: string;
+ };
+}
diff --git a/public/pages/ShowPost/ShowPost.page.scss b/public/pages/ShowPost/ShowPost.page.scss
index cc3ac588a..7a59a6b0e 100644
--- a/public/pages/ShowPost/ShowPost.page.scss
+++ b/public/pages/ShowPost/ShowPost.page.scss
@@ -41,6 +41,10 @@
@include media(">=md") {
width: 200px;
}
+
+ .subtitle + .c-list {
+ margin-bottom: 10px;
+ }
}
.c-tag-list {
diff --git a/public/pages/ShowPost/ShowPost.page.tsx b/public/pages/ShowPost/ShowPost.page.tsx
index 47fb4a34b..d17e28b8d 100644
--- a/public/pages/ShowPost/ShowPost.page.tsx
+++ b/public/pages/ShowPost/ShowPost.page.tsx
@@ -2,7 +2,7 @@ import "./ShowPost.page.scss";
import React from "react";
-import { Comment, Post, Tag } from "@fider/models";
+import { Comment, Post, Tag, Vote } from "@fider/models";
import { actions, Failure, Fider } from "@fider/services";
import {
@@ -25,12 +25,14 @@ import { TagsPanel } from "./components/TagsPanel";
import { NotificationsPanel } from "./components/NotificationsPanel";
import { ModerationPanel } from "./components/ModerationPanel";
import { DiscussionPanel } from "./components/DiscussionPanel";
+import { VotesPanel } from "./components/VotesPanel";
interface ShowPostPageProps {
post: Post;
subscribed: boolean;
comments: Comment[];
tags: Tag[];
+ votes: Vote[];
}
interface ShowPostPageState {
@@ -122,6 +124,8 @@ export default class ShowPostPage extends React.Component<ShowPostPageProps, Sho
</div>
<div className="action-col">
+ <VotesPanel post={this.props.post} votes={this.props.votes} />
+
{Fider.session.isAuthenticated &&
Fider.session.user.isCollaborator && [
<span key={0} className="subtitle">
diff --git a/public/pages/ShowPost/components/ModerationPanel.tsx b/public/pages/ShowPost/components/ModerationPanel.tsx
index bb47733b8..a9456e5a5 100644
--- a/public/pages/ShowPost/components/ModerationPanel.tsx
+++ b/public/pages/ShowPost/components/ModerationPanel.tsx
@@ -79,7 +79,7 @@ export class ModerationPanel extends React.Component<ModerationPanelProps, Moder
);
return (
- <div>
+ <>
{modal}
<span className="subtitle">Moderation</span>
<List>
@@ -89,7 +89,7 @@ export class ModerationPanel extends React.Component<ModerationPanelProps, Moder
</Button>
</ListItem>
</List>
- </div>
+ </>
);
}
}
diff --git a/public/pages/ShowPost/components/TagsPanel.tsx b/public/pages/ShowPost/components/TagsPanel.tsx
index 3bf1f2f86..490f2e55f 100644
--- a/public/pages/ShowPost/components/TagsPanel.tsx
+++ b/public/pages/ShowPost/components/TagsPanel.tsx
@@ -87,14 +87,14 @@ export class TagsPanel extends React.Component<TagsPanelProps, TagsPanelState> {
const icon = this.state.canEdit && (this.state.isEditing ? <FaCheckCircle /> : <FaCog />);
return (
- <div>
+ <>
<span className={subtitleClasses} onClick={this.onSubtitleClick}>
Tags {icon}
</span>
{!this.state.isEditing && tagsList}
{this.state.isEditing && editTagsList}
- </div>
+ </>
);
}
}
diff --git a/public/pages/ShowPost/components/VotesModal.scss b/public/pages/ShowPost/components/VotesModal.scss
new file mode 100644
index 000000000..da0291e59
--- /dev/null
+++ b/public/pages/ShowPost/components/VotesModal.scss
@@ -0,0 +1,24 @@
+@import '~@fider/assets/styles/variables.scss';
+
+.c-votes-modal {
+ .c-modal-content {
+ height: 300px;
+ overflow-y: scroll;
+ .c-list-item {
+ margin: 5px 0px;
+ .c-avatar {
+ margin-right: 5px;
+ }
+ .l-user {
+ display: inline-block;
+ vertical-align: middle;
+ span {
+ display: block;
+ }
+ }
+ span.date {
+ float: right;
+ }
+ }
+ }
+}
\ No newline at end of file
diff --git a/public/pages/ShowPost/components/VotesModal.tsx b/public/pages/ShowPost/components/VotesModal.tsx
new file mode 100644
index 000000000..bae696d0b
--- /dev/null
+++ b/public/pages/ShowPost/components/VotesModal.tsx
@@ -0,0 +1,105 @@
+import "./VotesModal.scss";
+
+import React from "react";
+import { Post, Vote } from "@fider/models";
+import { Modal, Button, Loader, List, ListItem, Gravatar, UserName, Moment, Input } from "@fider/components";
+import { actions } from "@fider/services";
+import { FaTimes, FaSearch } from "react-icons/fa";
+
+interface VotesModalProps {
+ isOpen: boolean;
+ post: Post;
+ onClose?: () => void;
+}
+
+interface VotesModalState {
+ searchText: string;
+ allVotes: Vote[];
+ filteredVotes: Vote[];
+ isLoading: boolean;
+ query: string;
+}
+
+export class VotesModal extends React.Component<VotesModalProps, VotesModalState> {
+ constructor(props: VotesModalProps) {
+ super(props);
+ this.state = {
+ searchText: "",
+ query: "",
+ allVotes: [],
+ filteredVotes: [],
+ isLoading: true
+ };
+ }
+
+ public componentDidUpdate(prevProps: VotesModalProps) {
+ if (this.props.isOpen && !prevProps.isOpen) {
+ actions.listVotes(this.props.post.number).then(response => {
+ if (response.ok) {
+ this.setState({
+ allVotes: response.data,
+ filteredVotes: response.data,
+ isLoading: false
+ });
+ }
+ });
+ }
+ }
+
+ private closeModal = async () => {
+ if (this.props.onClose) {
+ this.props.onClose();
+ }
+ };
+
+ private clearSearch = () => {
+ this.handleSearchFilterChanged("");
+ };
+
+ private handleSearchFilterChanged = (query: string) => {
+ const votes = this.state.allVotes.filter(x => x.user.name.toLowerCase().indexOf(query.toLowerCase()) >= 0);
+ this.setState({ query, filteredVotes: votes });
+ };
+
+ public render() {
+ return (
+ <Modal.Window className="c-votes-modal" isOpen={this.props.isOpen} center={false} onClose={this.props.onClose}>
+ <Modal.Content>
+ {this.state.isLoading && <Loader />}
+ {!this.state.isLoading && (
+ <>
+ <Input
+ field="query"
+ icon={this.state.query ? FaTimes : FaSearch}
+ onIconClick={this.state.query ? this.clearSearch : undefined}
+ placeholder="Search for users by name..."
+ value={this.state.query}
+ onChange={this.handleSearchFilterChanged}
+ />
+ <List hover={true}>
+ {this.state.filteredVotes.map(x => (
+ <ListItem key={x.user.id}>
+ <Gravatar user={x.user} />
+ <span className="l-user">
+ <UserName user={x.user} />
+ <span className="info">{x.user.email}</span>
+ </span>
+ <span className="info">
+ <Moment date={x.createdAt} />
+ </span>
+ </ListItem>
+ ))}
+ </List>
+ </>
+ )}
+ </Modal.Content>
+
+ <Modal.Footer>
+ <Button color="cancel" onClick={this.closeModal}>
+ Close
+ </Button>
+ </Modal.Footer>
+ </Modal.Window>
+ );
+ }
+}
diff --git a/public/pages/ShowPost/components/VotesPanel.scss b/public/pages/ShowPost/components/VotesPanel.scss
new file mode 100644
index 000000000..cc8aa0879
--- /dev/null
+++ b/public/pages/ShowPost/components/VotesPanel.scss
@@ -0,0 +1,18 @@
+@import '~@fider/assets/styles/variables.scss';
+
+.l-votes-list {
+ .c-avatar {
+ margin: 0 -8px 0 -2px;
+ padding: 0;
+ border: 2px solid $bg-color;
+ }
+}
+
+.l-votes-more {
+ margin-left: 14px;
+ font-weight: 600;
+ font-size: $font-size-tiny;
+ &.clickable:hover {
+ text-decoration: underline;
+ }
+}
\ No newline at end of file
diff --git a/public/pages/ShowPost/components/VotesPanel.tsx b/public/pages/ShowPost/components/VotesPanel.tsx
new file mode 100644
index 000000000..91fd40c5b
--- /dev/null
+++ b/public/pages/ShowPost/components/VotesPanel.tsx
@@ -0,0 +1,64 @@
+import "./VotesPanel.scss";
+
+import React from "react";
+import { Post, Vote } from "@fider/models";
+import { Gravatar } from "@fider/components";
+import { Fider, classSet } from "@fider/services";
+import { VotesModal } from "./VotesModal";
+
+interface VotesPanelProps {
+ post: Post;
+ votes: Vote[];
+}
+
+interface VotesPanelState {
+ showModal: boolean;
+}
+
+export class VotesPanel extends React.Component<VotesPanelProps, VotesPanelState> {
+ constructor(props: VotesPanelProps) {
+ super(props);
+ this.state = {
+ showModal: false
+ };
+ }
+
+ private showModal = () => {
+ this.setState({ showModal: true });
+ };
+
+ private hideModal = () => {
+ this.setState({ showModal: false });
+ };
+
+ public render() {
+ const extraVotesCount = this.props.post.votesCount - this.props.votes.length;
+ const canShowAll = Fider.session.isAuthenticated && Fider.session.user.isCollaborator;
+ const moreVotesClassName = classSet({
+ "l-votes-more": true,
+ clickable: canShowAll
+ });
+
+ return (
+ <>
+ <VotesModal post={this.props.post} isOpen={this.state.showModal} onClose={this.hideModal} />
+ <span className="subtitle">Voters</span>
+ <div className="l-votes-list">
+ {this.props.votes.map(x => (
+ <Gravatar key={x.user.id} user={x.user} />
+ ))}
+ {extraVotesCount > 0 && (
+ <span onClick={this.showModal} className={moreVotesClassName}>
+ +{extraVotesCount} more
+ </span>
+ )}
+ {extraVotesCount === 0 && canShowAll && (
+ <span onClick={this.showModal} className={moreVotesClassName}>
+ see details
+ </span>
+ )}
+ </div>
+ </>
+ );
+ }
+}
diff --git a/public/services/actions/post.ts b/public/services/actions/post.ts
index d4f16203b..6cb68692b 100644
--- a/public/services/actions/post.ts
+++ b/public/services/actions/post.ts
@@ -1,5 +1,5 @@
import { http, Result, querystring } from "@fider/services";
-import { Post } from "@fider/models";
+import { Post, Vote } from "@fider/models";
export const getAllPosts = async (): Promise<Result<Post[]>> => {
return await http.get<Post[]>("/api/v1/posts");
@@ -47,6 +47,10 @@ export const unsubscribe = async (postNumber: number): Promise<Result> => {
return http.delete(`/api/v1/posts/${postNumber}/subscription`).then(http.event("post", "unsubscribe"));
};
+export const listVotes = async (postNumber: number): Promise<Result<Vote[]>> => {
+ return http.get<Vote[]>(`/api/v1/posts/${postNumber}/votes`);
+};
+
export const createComment = async (postNumber: number, content: string): Promise<Result> => {
return http.post(`/api/v1/posts/${postNumber}/comments`, { content }).then(http.event("comment", "create"));
};