Skip to content

Commit

Permalink
Merge pull request #21 from devilcove/tweaks
Browse files Browse the repository at this point in the history
Tweaks
  • Loading branch information
mattkasun authored Dec 15, 2023
2 parents b2727ab + 78d7df2 commit eeccf53
Show file tree
Hide file tree
Showing 20 changed files with 134 additions and 121 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ test.db
timetraced
timetrace
coverage.out
c.out
c.out
experiment/*
3 changes: 2 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,6 @@ func setConfig(c *gin.Context) {
}
models.SetTheme(user, config.Theme)
models.SetFont(user, config.Font)
displayStatus(c)
models.SetRefresh(user, config.Refresh)
displayMain(c)
}
4 changes: 4 additions & 0 deletions database/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package database

import (
"encoding/json"
"errors"

"github.com/devilcove/timetraced/models"
"go.etcd.io/bbolt"
Expand All @@ -22,6 +23,9 @@ func GetProject(name string) (models.Project, error) {
project := models.Project{}
if err := db.View(func(tx *bbolt.Tx) error {
v := tx.Bucket([]byte(PROJECT_TABLE_NAME)).Get([]byte(name))
if v == nil {
return errors.New("no such project")
}
if err := json.Unmarshal(v, &project); err != nil {
return err
}
Expand Down
4 changes: 4 additions & 0 deletions database/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package database

import (
"encoding/json"
"errors"
"time"

"github.com/devilcove/timetraced/models"
Expand All @@ -24,6 +25,9 @@ func GetRecord(id uuid.UUID) (models.Record, error) {
record := models.Record{}
if err := db.View(func(tx *bbolt.Tx) error {
v := tx.Bucket([]byte(RECORDS_TABLE_NAME)).Get([]byte(id.String()))
if v == nil {
return errors.New("no such record")
}
if err := json.Unmarshal(v, &record); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion database/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func GetUser(name string) (models.User, error) {
user := models.User{}
if err := db.View(func(tx *bbolt.Tx) error {
v := tx.Bucket([]byte(USERS_TABLE_NAME)).Get([]byte(name))
if len(v) == 0 {
if v == nil {
return errors.New("no such user")
}
if err := json.Unmarshal(v, &user); err != nil {
Expand Down
78 changes: 50 additions & 28 deletions html/configuration.html
Original file line number Diff line number Diff line change
@@ -1,30 +1,52 @@
{{define "config"}}
<h1>Configuration</h1>
<form hx-post="/setConfig" hx-target="#content">
<p><label>Theme</label>
<select name="theme">
<option>indigo</option>
<option>red</option>
<option>pink</option>
<option>purple</option>
<option>blue</option>
<option>cyan</option>
<option>teal</option>
<option>khaki</option>
<option>blue-grey</option>
<option>grey</option>
<option>black</option>
</select></p>
<p><label>Font&nbsp;&nbsp;</label>
<select name="font">
<option>Roboto</option>
<option>Montserrat</option>
<option>Lato</option>
<option>Ubuntu</option>
<option>Tangerine</option>
</select></p>
<p><button class="w3-button w3-theme-dark w3-padding large" type="button" hx-get="/" hx-target="#main">Cancel</button>
<button class="w3-button w3-theme-dark w3-padding large" type="submit">Submit</button></p>
</form>
<br><br><br><br><br>
<div class="w3-row">
<div class="w3-third"><br></div>
<div class="w3-third">
<h1>Configuration</h1>
<form hx-post="/config" hx-target="#main">
<p><label>Theme</label>
<select name="theme">
<option>indigo</option>
<option>red</option>
<option>pink</option>
<option>purple</option>
<option>blue</option>
<option>cyan</option>
<option>teal</option>
<option>khaki</option>
<option>blue-grey</option>
<option>grey</option>
<option>black</option>
</select>
</p>
<p><label>Font&nbsp;&nbsp;</label>
<select name="font">
<option>Roboto</option>
<option>Montserrat</option>
<option>Lato</option>
<option>Ubuntu</option>
<option>Tangerine</option>
</select>
</p>
<p><label>Refresh Rate (minutes)</label>
<select name="refresh">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option selected>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
</select>
</p>
<p><button class="w3-button w3-theme-dark w3-padding large" type="button" hx-get="/"
hx-target="#main">Cancel</button>
<button class="w3-button w3-theme-dark w3-padding large" type="submit">Submit</button>
</p>
</form>
</div>
</div>
{{end}}
24 changes: 4 additions & 20 deletions html/content.html
Original file line number Diff line number Diff line change
@@ -1,25 +1,9 @@
{{define "content"}}
<!-- [html-validate-disable prefer-tbody]-->
<!-- [html-validate-disable no-dup-id]-->
{{if (eq .DisplayLogin true)}}
<div id="content">
<div class="w3-container w3-center w3-theme-dark">
<h1>Timetrace Login</h1>
<!-- <form action="/login" method="post"> -->
<form hx-post="/login" hx-target="#content" hx-target-error="#error">
<label for="username">Username</label><br>
<input type="text" placeholder="enter username" name="username" required><br>
<label for="pass">Password</label><br>
<input type="password" placeholder="enter password" name="password" required><br>
<button type="submit">Login</button>
</form>
<div class="w3-container w3-padding-32">
<a hx-get="/register" hx-target="#content">Don't have an Account? Register</a>
</div>
</div>
</div>
{{if (eq .NeedsLogin true)}}
{{template "login" .}}
{{ else }}
<div class="w3-container w3-center w3-padding-64" id="content">
<div class="w3-container w3-center w3-padding-64" id="content" hx-get="/projects/status" hx-trigger="every {{.Refresh}}m">
<div class="w3-row">
<div class="w3-third"><br></div>
<div class="w3-third">
Expand All @@ -38,7 +22,7 @@ <h1>Timetrace Login</h1>
</tr>
</table>
{{ if .Tracking }}
<form hx-post="projects/stop" hx-target="#content">
<form hx-post="projects/stop" hx-target="#main">
<button class="w3-button" type="submit"><i class="fa fa-stopwatch"></i> Stop</button>
</form>
{{end}}
Expand Down
2 changes: 1 addition & 1 deletion html/login.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{define "login"}}
<div id="login">
<div id="content">
<div class="w3-container w3-center w3-theme-dark">
<h1>Timetrace Login</h1>
<!-- <form action="/login" method="post"> -->
Expand Down
2 changes: 1 addition & 1 deletion html/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<button type="button" class="w3-bar-item w3-button" hx-get="/users" hx-target="#content">
<i class="fa fa-user w3-xxlarge"></i>
USERS</button>
<button type="button" class="w3-bar-item w3-button" hx-get="/configuration" hx-target="#content">
<button type="button" class="w3-bar-item w3-button" hx-get="/config" hx-target="#content">
<i class="fa fa-cog w3-xxlarge"></i>
SETTINGS</button>
<button type="button" class="w3-bar-item w3-button"
Expand Down
25 changes: 0 additions & 25 deletions html/sidebar.html

This file was deleted.

2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func main() {
checkDefaultUser()
users, err := database.GetAllUsers()
if err != nil {
slog.Error("get users", "error", err)
log.Fatal("get users", err)
}
for _, user := range users {
project := database.GetActiveProject(user.Username)
Expand Down
5 changes: 3 additions & 2 deletions models/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package models

type Config struct {
Theme string `json:"theme" form:"theme"`
Font string `json:"font" form:"font"`
Theme string `json:"theme" form:"theme"`
Font string `json:"font" form:"font"`
Refresh int `json:"refresh" form:"refresh"`
}
27 changes: 19 additions & 8 deletions models/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import (
)

type Page struct {
DisplayLogin bool
Version string
Theme string
Font string
Tracking bool
Projects []string
Status StatusResponse
DefaultDate string
NeedsLogin bool
Version string
Theme string
Font string
Refresh int
Tracking bool
Projects []string
Status StatusResponse
DefaultDate string
}

// var page Page
Expand All @@ -24,6 +25,7 @@ func initialize() Page {
Version: "v0.1.0",
Theme: "indigo",
Font: "Roboto",
Refresh: 5,
DefaultDate: time.Now().Local().Format("2006-01-02"),
}
}
Expand Down Expand Up @@ -64,3 +66,12 @@ func SetFont(user, font string) {
page.Font = font
pages[user] = page
}

func SetRefresh(user string, refresh int) {
page, ok := pages[user]
if !ok {
page = initialize()
}
page.Refresh = refresh
pages[user] = page
}
22 changes: 17 additions & 5 deletions page.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,36 @@ import (
"github.com/gin-gonic/gin"
)

func displayStatus(c *gin.Context) {
func displayMain(c *gin.Context) {
var page models.Page
session := sessions.Default(c)
user := session.Get("user")
loggedIn := session.Get("loggedin")
slog.Info("displaying status for", "user", user, "loggedIn", loggedIn)
slog.Debug("displaying status for", "user", user, "loggedIn", loggedIn)
if user == nil {
page = populatePage("")
} else {
page = populatePage(user.(string))
}
if loggedIn != nil {
page.DisplayLogin = !loggedIn.(bool)
if loggedIn == nil {
page.NeedsLogin = true
}
slog.Info("displaystatus", "page", loggedIn)
slog.Debug("displaystatus", "page", page.NeedsLogin, "refresh", page.Refresh)
c.HTML(http.StatusOK, "layout", page)
}

func displayStatus(c *gin.Context) {
session := sessions.Default(c)
user := session.Get("user")
loggedIn := session.Get("loggedin")
if user == nil || loggedIn == nil {
displayMain(c)
return
}
page := populatePage(user.(string))
c.HTML(http.StatusOK, "content", page)
}

func populatePage(user string) models.Page {
page := models.GetUserPage(user)
page.Tracking = models.IsTrackingActive(user)
Expand Down
9 changes: 3 additions & 6 deletions projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ func addProject(c *gin.Context) {
processError(c, http.StatusBadRequest, "could not decode request into json "+err.Error())
return
}
slog.Info("addproject1", "project", project)
if regexp.MustCompile(`\s+`).MatchString(project.Name) || project.Name == "" {
processError(c, http.StatusBadRequest, "invalid project name")
return
Expand All @@ -50,13 +49,11 @@ func addProject(c *gin.Context) {
project.ID = uuid.New()
project.Active = true
project.Updated = time.Now()
slog.Info("add project", "project", project)
if err := database.SaveProject(&project); err != nil {
processError(c, http.StatusInternalServerError, "error saving project "+err.Error())
return
}
slog.Info("added", "project", project.Name)
displayStatus(c)
displayMain(c)
}

func getProject(c *gin.Context) {
Expand Down Expand Up @@ -100,7 +97,7 @@ func start(c *gin.Context) {
}
models.TrackingActive(user, project)
slog.Info("tracking started", "project", project.Name)
displayStatus(c)
displayMain(c)
}

func stopE(u string) error {
Expand Down Expand Up @@ -128,5 +125,5 @@ func stop(c *gin.Context) {
processError(c, http.StatusInternalServerError, err.Error())
return
}
displayStatus(c)
displayMain(c)
}
Loading

0 comments on commit eeccf53

Please sign in to comment.