Skip to content

Commit

Permalink
Add user dashboard showing starred boards.
Browse files Browse the repository at this point in the history
  • Loading branch information
bilus committed Sep 9, 2020
1 parent 0442683 commit b77168a
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 11 deletions.
1 change: 1 addition & 0 deletions actions/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func App() *buffalo.App {

app.GET("/", Home)
app.GET("/changelog", Changelog)
app.GET("/dashboard", UserDashboard)

postsResource := PostsResource{}
app.GET("/posts/{post_id}/edit", postsResource.Edit)
Expand Down
2 changes: 1 addition & 1 deletion actions/home.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
func Home(c buffalo.Context) error {
boardID, found, err := GetLastBoardID(c)
if err != nil || !found {
return c.Redirect(http.StatusSeeOther, "/boards/")
return c.Redirect(http.StatusSeeOther, "/dashboard/")
}

// TODO: Use route helper.
Expand Down
12 changes: 10 additions & 2 deletions actions/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package actions

import (
"fmt"
"math/rand"
"net/url"
"rally/models"
"time"
Expand Down Expand Up @@ -126,13 +127,20 @@ func init() {
}
return votes
},
"isBoardStarred": func(board *models.Board, help plush.HelperContext) bool {
"isBoardStarred": func(board interface{}, help plush.HelperContext) bool {
b := toBoardPtr(board)
u, err := CurrentUser(help)
if err != nil {
log.Errorf("Unable to retrieve current user to determine if board starred: %v", err)
return false
}
return u.IsBoardStarred(board)
return u.IsBoardStarred(b)
},
"greeting": func() string {
greetings := []string{
"Salut! 👋", "Hola! 👋", "Privet! 👋", "Nǐ hǎo 👋", "Ciao! 👋", "Yā, Yō 👋", "Hallo! 👋", "Hello! 👋", "Cześć! 👋", "Helo! 👋", "Hey! 👋", "Hei! 👋",
}
return greetings[rand.Intn(len(greetings))]
},
},
})
Expand Down
34 changes: 34 additions & 0 deletions actions/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package actions

import (
"context"
"fmt"
"net/http"
"rally/models"

"github.com/gobuffalo/buffalo"
"github.com/gobuffalo/pop/v5"
"github.com/gobuffalo/x/responder"
"github.com/pkg/errors"
)

Expand Down Expand Up @@ -38,6 +41,37 @@ func UsersCreate(c buffalo.Context) error {
return Login(u, c)
}

func UserDashboard(c buffalo.Context) error {
// Get the DB connection from the context
tx, ok := c.Value("tx").(*pop.Connection)
if !ok {
return fmt.Errorf("no transaction found")
}

boards := &models.Boards{}

// Paginate results. Params "page" and "per_page" control pagination.
// Default values are "page=1" and "per_page=20".
q := tx.PaginateFromParams(c.Params())

// Retrieve all Boards from the DB
if err := q.All(boards); err != nil {
return err
}

return responder.Wants("html", func(c buffalo.Context) error {
// Add the paginator to the context so it can be used in the template.
c.Set("pagination", q.Paginator)

c.Set("boards", boards)
return c.Render(http.StatusOK, r.HTML("/users/dashboard.plush.html"))
}).Wants("json", func(c buffalo.Context) error {
return c.Render(200, r.JSON(boards))
}).Wants("xml", func(c buffalo.Context) error {
return c.Render(200, r.XML(boards))
}).Respond(c)
}

func Login(u *models.User, c buffalo.Context) error {
c.Session().Set("current_user_id", u.ID)

Expand Down
2 changes: 1 addition & 1 deletion templates/application.plush.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<body>
<nav class="navbar navbar-expand-lg bg-primary">
<div class="container">
<a href="/boards" class="navbar-brand">
<a href="/dashboard" class="navbar-brand">
<div class="logo-image">
<i class="icon-home"></i>
</div>
Expand Down
11 changes: 5 additions & 6 deletions templates/boards/index.plush.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ <h3 class="d-inline-block">Boards</h3>
</div>
</div>


<table class="table table-hover table-noborders">
<tbody>
<%= for (board) in boards { %>
<tr class="clickable" data-href="<%= boardPath({ board_id: board.ID }) %>">
<td>
<%= board.Name %>
</td>
</tr>
<tr class="clickable" data-href="<%= boardPath({ board_id: board.ID }) %>">
<td>
<%= board.Name %>
</td>
</tr>
<% } %>
</tbody>
</table>
Expand Down
2 changes: 1 addition & 1 deletion templates/boards/show.plush.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<div class="row">
<div class="col align-middle">
<h2 class="d-inline-block w-100">
<%= board.Name %>
<%= partial("boards/star.html") %>
<%= board.Name %>
<div class="float-right">
<%= if (canManageBoard(board)) { %>
<%= remoteLinkTo(boardPostsPath({ board_id: board.ID }), {class: "btn btn-primary", data-method: "POST", title: "Create new post"}) { %>
Expand Down
46 changes: 46 additions & 0 deletions templates/users/dashboard.plush.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<%= if (len(boards) == 0) { %>
<h3 class="d-inline-block">Welcome</h3>
<p class="lead">There are no boards yet...
<%= linkTo(newBoardsPath(), {class: "btn btn-primary btn-small"}) { %>
Create New Board
<% } %>
</p>
<% } else { %>

<div class="py-4 mb-2">
<h3 class="d-inline-block"><%= greeting() %></h3>
<div class="float-right">
<%= linkTo(newBoardsPath(), {class: "btn btn-primary", title: "Create new board"}) { %>
<i class="icon-plus"></i>
<% } %>
</div>
</div>

<h3 class="d-inline-block">
<i class="icon-star text-primary"></i>
Starred boards
</h3>

<table class="table table-hover table-noborders">
<tbody>
<%= for (board) in boards { %>
<%= if (isBoardStarred(board)) { %>
<tr class="clickable" data-href="<%= boardPath({ board_id: board.ID }) %>">
<td>
<%= board.Name %>
</td>
</tr>
<% } %>
<% } %>
</tbody>
</table>
<% } %>


<div class="text-center">
<%= paginator(pagination) %>
</div>

<h4 class="d-inline-block">
<%= linkTo(boardsPath(), { body: "Browse all boards..." }) %>
</h4>

0 comments on commit b77168a

Please sign in to comment.