Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

user can support/undo ideas

goenningf952dae6d629
diff --git a/app/handlers/feedback.go b/app/handlers/feedback.go
index 5d2f37e76..f3f87b3c1 100644
--- a/app/handlers/feedback.go
+++ b/app/handlers/feedback.go
@@ -27,8 +27,14 @@ func (h AllHandlers) List() web.HandlerFunc {
return c.Failure(err)
}
+ supported, err := h.ideas.GetSupportedIdeas(c.User().ID)
+ if err != nil {
+ return c.Failure(err)
+ }
+
return c.Page(echo.Map{
- "ideas": ideas,
+ "ideas": ideas,
+ "supported": supported,
})
}
}
diff --git a/app/pkg/dbx/dbx.go b/app/pkg/dbx/dbx.go
index 7bb828f11..cd800ddec 100644
--- a/app/pkg/dbx/dbx.go
+++ b/app/pkg/dbx/dbx.go
@@ -51,6 +51,29 @@ func (db Database) QueryInt(command string, args ...interface{}) (int, error) {
return data, nil
}
+// QueryIntArray executes given SQL command and return first column as int
+func (db Database) QueryIntArray(command string, args ...interface{}) ([]int, error) {
+ var values []int
+ var value int
+
+ rows, err := db.conn.Query(command, args...)
+ if err != nil {
+ return make([]int, 0), err
+ }
+
+ if rows != nil {
+ defer rows.Close()
+ for rows.Next() {
+ err := rows.Scan(&value)
+ if err != nil {
+ return make([]int, 0), err
+ }
+ values = append(values, value)
+ }
+ }
+ return values, nil
+}
+
// QueryString executes given SQL command and return first string of first row
func (db Database) QueryString(command string, args ...interface{}) (string, error) {
var data string
diff --git a/app/storage/feedback.go b/app/storage/feedback.go
index 6510d54a7..05314a7a0 100644
--- a/app/storage/feedback.go
+++ b/app/storage/feedback.go
@@ -12,4 +12,5 @@ type Idea interface {
AddComment(userID, ideaID int, content string) (int, error)
AddSupporter(userID, ideaID int) error
RemoveSupporter(userID, ideaID int) error
+ GetSupportedIdeas(userID int) ([]int, error)
}
diff --git a/app/storage/inmemory/idea.go b/app/storage/inmemory/idea.go
index bc95a5f7d..dddc3ff7b 100644
--- a/app/storage/inmemory/idea.go
+++ b/app/storage/inmemory/idea.go
@@ -78,3 +78,8 @@ func (s *IdeaStorage) RemoveSupporter(userID, ideaID int) error {
}
return nil
}
+
+// GetSupportedIdeas returns all ideas supported by given user
+func (s *IdeaStorage) GetSupportedIdeas(userID int) ([]int, error) {
+ return make([]int, 0), nil
+}
diff --git a/app/storage/postgres/idea.go b/app/storage/postgres/idea.go
index 19d11d1f4..f74e14734 100644
--- a/app/storage/postgres/idea.go
+++ b/app/storage/postgres/idea.go
@@ -172,3 +172,8 @@ func (s *IdeaStorage) RemoveSupporter(userID, ideaID int) error {
return tx.Commit()
}
+
+// GetSupportedIdeas returns all ideas supported by given user
+func (s *IdeaStorage) GetSupportedIdeas(userID int) ([]int, error) {
+ return s.DB.QueryIntArray("SELECT idea_id FROM idea_supporters WHERE user_id = $1", userID)
+}
diff --git a/public/components/SupportCounter.tsx b/public/components/SupportCounter.tsx
index a9561cc87..76fd8d12a 100644
--- a/public/components/SupportCounter.tsx
+++ b/public/components/SupportCounter.tsx
@@ -1,6 +1,7 @@
import axios from "axios";
import * as React from "react";
import { Idea } from "../models";
+import * as storage from "../storage";
interface SupportCounterProps {
idea: Idea;
@@ -14,8 +15,10 @@ interface SupportCounterState {
export class SupportCounter extends React.Component<SupportCounterProps, SupportCounterState> {
constructor(props: SupportCounterProps) {
super(props);
+
+ const supported = storage.get<number[]>("supported") || [];
this.state = {
- supported: props.idea.totalSupporters >= 1 && props.idea.totalSupporters <= 10,
+ supported: supported.indexOf(props.idea.id) >= 0,
total: props.idea.totalSupporters
};
}
@@ -53,6 +56,7 @@ export class SupportCounter extends React.Component<SupportCounterProps, Support
}
public render() {
+
const support = <div className="support-button ui mini violet inverted animated button"
onClick={async () => await this.support()}>
<div className="visible content">Want</div>