Skip to content

Commit

Permalink
feat: poll for changed data every 10 seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarow committed Dec 28, 2023
1 parent f189511 commit 089c3c4
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 26 deletions.
10 changes: 10 additions & 0 deletions internal/api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ func (h Handler) GetIndex(c echo.Context) error {
return render(c, http.StatusOK, index)
}

func (h Handler) GetGameList(c echo.Context) error {
games, err := h.service.List()
if err != nil {
c.Logger().Error(err)
return err
}
gameList := component.GameList(games)
return render(c, http.StatusOK, gameList)
}

func (h Handler) GetGameDetails(c echo.Context) error {
gameId := c.Param("id")

Expand Down
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type Form struct {
func registerRoutes(e *echo.Echo, handler api.Handler) {
e.StaticFS("/static", echo.MustSubFS(staticAssets, "static"))
e.GET("/", handler.GetIndex)
e.GET("/games", handler.GetGameList)

e.POST("/games", handler.CreateGame)
e.GET("/games/create", handler.GetCreateGameForm)
Expand Down
23 changes: 18 additions & 5 deletions templates/GameDetails.templ
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package template

import "github.com/tarow/skat-counter/internal/skat"
import "github.com/tarow/skat-counter/templates/components"
import "fmt"

templ GameDetailsNavbar() {
<div class="navbar bg-base-200">
Expand All @@ -18,14 +19,26 @@ templ GameDetailsNavbar() {
templ GameDetails (game skat.Game) {
@Base() {
@GameDetailsNavbar()
<div class="container flex pt-4 mx-auto">
<div class="flex w-full flex-row flex-wrap-reverse gap-8 justify-around items-end">
@component.RoundsList(game)
<div class="container flex pt-4 mx-auto" hx-get={ fmt.Sprintf("/games/%v", game.ID) } hx-trigger="every 10s" hx-swap="none" hx-select-oob="#gamecard:outerHTML,#roundlist:outerHTML">
<div class="flex w-full flex-row flex-wrap-reverse gap-8 justify-around items-end" hx-disinherit="*">
<div id="roundlist">
@component.RoundsList(game)
</div>
<div class="flex flex-col items-center gap-4">
@component.GameCard(game, false, true, true)
@component.AddRoundForm(game)
<div id="gamecard">
@component.GameCard(game, false, true, true)
</div>
<div id="roundform">
@component.AddRoundForm(game)
</div>
</div>
</div>
</div>
<dialog id="edit_round_modal" class="modal">
<div id="edit_round_form"></div>
</dialog>
<dialog id="edit_game_modal" class="modal">
<div id="edit_game_form" class="modal-box"></div>
</dialog>
}
}
19 changes: 16 additions & 3 deletions templates/GameDetails_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions templates/GameOverview.templ
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ templ GameOverviewNavbar() {
Skatcounter
</a>
<button
onclick="create_game_modal.showModal()"
hx-target="#create_game_form"
hx-get="/games/create"
class="btn btn-primary justify-self-center col-span-1"
Expand All @@ -30,6 +29,8 @@ templ GameOverviewNavbar() {
templ GameOverview(games []skat.Game) {
@Base() {
@GameOverviewNavbar()
@component.GameList(games)
<div id="gamelist" hx-get="/" hx-select="#gamelist" hx-swap="outerHTML" hx-trigger="every 10s">
@component.GameList(games)
</div>
}
}
8 changes: 6 additions & 2 deletions templates/GameOverview_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion templates/base.templ
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ templ Base() {
<script src="/static/sortable.js"></script>
<script src="/static/theme-change.js"></script>
</head>
<body>
<body id="content">
{ children... }
@initSortable()
@initThemeChange()
Expand Down
2 changes: 1 addition & 1 deletion templates/base_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions templates/components/game_card.templ
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ templ GameCard(game skat.Game, detailsBtn, editBtn, deleteBtn bool) {
{ fmt.Sprintf("%d", len(game.Rounds)) }
</div>
</div>
<div class="card-actions justify-end ">
<div class="card-actions justify-end" hx-disinherit="*">
if detailsBtn {
<a class="btn" hx-boost="true" href={ templ.URL(fmt.Sprintf("/games/%v", game.ID)) } method="get">
Details
Expand Down Expand Up @@ -86,9 +86,6 @@ templ GameCard(game skat.Game, detailsBtn, editBtn, deleteBtn bool) {
</div>
</div>
</div>
<dialog id="edit_game_modal" class="modal">
<div id="edit_game_form" class="modal-box"></div>
</dialog>
}

templ GameList(games []skat.Game) {
Expand Down
4 changes: 2 additions & 2 deletions templates/components/game_card_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions templates/components/rounds_list.templ
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ templ RoundsList(game skat.Game) {
<li>
<a
hx-delete={ fmt.Sprintf("/games/%v/rounds/%v", game.ID, round.ID) }
hx-swap="outerHTML"
hx-target="closest body"
>
<svg xmlns="http://www.w3.org/2000/svg" class="w-8 h-8 sm:w-6 sm:h-6 md:w-4 md:h-4" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
Expand Down Expand Up @@ -75,9 +74,6 @@ templ RoundsList(game skat.Game) {
</tfoot>
</table>
</div>
<dialog id="edit_round_modal" class="modal">
<div id="edit_round_form"></div>
</dialog>
}

templ AddRoundForm(game skat.Game) {
Expand Down
4 changes: 2 additions & 2 deletions templates/components/rounds_list_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 089c3c4

Please sign in to comment.