Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkasun committed Apr 2, 2022
2 parents b753218 + bb8ed48 commit ba852b3
Show file tree
Hide file tree
Showing 10 changed files with 268 additions and 107 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

timetrace-gui
dist/
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ module github.com/mattkasun/timetrace-gui
go 1.16

require (
github.com/dominikbraun/timetrace v0.14.2
github.com/dominikbraun/timetrace v0.14.3
github.com/gin-contrib/sessions v0.0.4
github.com/gin-gonic/gin v1.7.4
github.com/gin-gonic/gin v1.7.7
github.com/go-playground/validator/v10 v10.5.0 // indirect
github.com/matryer/is v1.4.0
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
)
232 changes: 147 additions & 85 deletions go.sum

Large diffs are not rendered by default.

80 changes: 72 additions & 8 deletions handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,27 @@ func StartStop(c *gin.Context) {
//no need to track time of less than minute and allow creation of new record
if page.CurrentSession == "0h 0min" {
record, err = timetrace.LoadRecord(time.Now())
err = timetrace.DeleteRecord(*record)
} else {
err = timetrace.Stop()
if err != nil {
fmt.Println("load record err ", err)
}
if err := timetrace.DeleteRecord(*record); err != nil {
fmt.Println("delete record errors ", err)
} else if err := timetrace.Stop(); err != nil {
fmt.Println("stop errors ", err)
}
}
}
err = timetrace.Start(project, true, []string{})
if err := timetrace.Start(project, true, []string{}); err != nil {
fmt.Println("err starting", project, err)
}

} else if action == "stop" {
err = timetrace.Stop()
} else {
err = errors.New("invalid request")
}
if err != nil {
fmt.Println("------errors ", err)
fmt.Println("stop command errors ", err)
session.Set("message", err.Error())
} else {
session.Set("message", "")
Expand Down Expand Up @@ -194,10 +201,8 @@ func ValidateUser(visitor Users) (bool, bool, error) {
}
return true, false, nil
}
return false, false, errors.New("Invalid username or password")
}
//shouldn't get here
return false, false, nil
return false, false, errors.New("invalid username or password")
}

func CheckPassword(plain, hash Users) bool {
Expand Down Expand Up @@ -278,3 +283,62 @@ func GenerateReport(c *gin.Context) {
session.Save()
c.HTML(http.StatusOK, "ReportData", reports)
}

func EditRecord(c *gin.Context) {
action := c.PostForm("action")
if action == "update" {
UpdateRecord(c)
return
}
session := sessions.Default(c)
record := c.PostForm("record")
start, err := time.Parse("2006-01-02-15-04", record)
if err != nil {
ProcessError(c, err, http.StatusBadRequest)
}
edit, err := timetrace.LoadRecord(start)
if err != nil {
ProcessError(c, err, http.StatusBadRequest)
}
session.Set("message", "")
session.Save()
c.HTML(http.StatusOK, "EditRecord", edit)
}

func UpdateRecord(c *gin.Context) {
session := sessions.Default(c)
record := c.PostForm("record")
start := c.PostForm("start")
end := c.PostForm("end")
old, err := time.Parse("2006-01-02-15-04", record)
if err != nil {
ProcessError(c, err, http.StatusBadRequest)
}
edit, err := timetrace.LoadRecord(old)
if err != nil {
ProcessError(c, err, http.StatusBadRequest)
}
if err := timetrace.BackupRecord(old); err != nil {
ProcessError(c, err, http.StatusBadRequest)
}
if err := timetrace.DeleteRecord(*edit); err != nil {
ProcessError(c, err, http.StatusBadRequest)
}
edit.Start, err = time.Parse("2006-01-02-15-04-05", start)
if err != nil {
ProcessError(c, err, http.StatusBadRequest)
}
endtime, err := (time.Parse("2006-01-02-15-04-05", end))
if err != nil {
ProcessError(c, err, http.StatusBadRequest)
}
edit.End = &endtime
if err := timetrace.SaveRecord(*edit, true); err != nil {
ProcessError(c, err, http.StatusBadRequest)
}
session.Set("message", "")
session.Save()
location := url.URL{Path: "/"}
c.Redirect(http.StatusFound, location.RequestURI())

}
2 changes: 1 addition & 1 deletion html/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
</ul>
{{ if .Tracking }}
<form action="/" method=Post>
<input type=hidden name=action value=stop>
<button class="w3-button" type=submit><span class="material-icons-two-tone">timer_off</span> Stop</button>
<input type=hidden name=action value=stop>
</form>
{{end}}

Expand Down
2 changes: 1 addition & 1 deletion html/new.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
</style>
<head>
<body>
<H1>Netmaker</H1>
<H1>TimeTrace GUI</H1>
<p>Create a new admin user<p>
<div id="login">
<form action="/newuser" method=POST name="form-login">
Expand Down
37 changes: 34 additions & 3 deletions html/reports.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<form class="w3-container" action="/reports" method=POST>
<label> Reports </label>
<p>Start Date</p>
<input type=date name=start required>
<input type=date name=start value={{.DefaultDate}} required>
<p>End Date</p>
<input type=date name=end required>
<input type=date name=end value="{{.DefaultDate}}" required>
<p><input type=radio name=billable value=billable><label>Billable Only</label></p>
<p><input type=radio name=billable value=nobillable><label>Non-Billable Only</label></p>
<p><input type=radio name=billable value=both checked><Label>All Records</label></p>
Expand Down Expand Up @@ -42,7 +42,11 @@ <h2> TimeTrace Report</h2>
{{ range . }}
<h3>Project: {{ .Project }}</h3>
{{range .Records }}
<h5> Start:{{.Start.Format "Jan 02, 2006 15:04:05 UTC" }} End:{{ .End.Format "Jan 02, 2006 15:04:05 UTC"}} Billable:{{.IsBillable}}</h4>
<form action="/edit" method=post>
<input type=hidden name=action value=edit>
<input type=hidden name=record value={{ .Start.Format "2006-01-02-15-04" }}>
<button class="w3-button w3-block" type=submit>Start:{{.Start.Format "Jan 02, 2006 15:04:05" }} End:{{ .End.Format "Jan 02, 2006 15:04:05 UTC"}} Billable:{{.IsBillable}}</button>
</form>
{{end}}
<h4> Totals: {{ .Sum }}</h4>
{{ end}}
Expand All @@ -53,6 +57,33 @@ <h4> Totals: {{ .Sum }}</h4>
</html>
{{ end}}

{{ define "EditRecord"}}
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/lib/w3.css" type="text/css">
</head>
<body onLoad="document.getElementById('EditRecord').style.display='block';">
{{ template "Header" }}
<div id="EditRecord" class=w3-modal>
<div class="w3-modal-content w3-card w3-padding" style="width:50%">
<h2> Edit Record</h2>
<form action="/edit" method=Post>
<input type=hidden name=action value=update>
<input type=hidden name=record value={{.Start.Format "2006-01-02-15-04"}}>
<label>start:</label>
<input class="w3-input w3-border" name=start type="text" value={{.Start.Format "2006-01-02-15-04-05"}}>
<label>end:</label>
<input class="w3-input w3-border" name=end type="text" value={{.End.Format "2006-01-02-15-04-05"}}>
<button class="w3-button w3-block w3-green w3-section w3-padding" type="submit">Submit</button>
</form>
<button class=w3-button onclick='location.href="/";'> Close</button>
</div>
</div>
</body>
</html>
{{ end }}




Expand Down
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"embed"
"fmt"
"html/template"
"log"
"net/http"
"os"
"time"
Expand All @@ -25,13 +26,16 @@ var icon embed.FS
var f embed.FS

func PrintDuration(d time.Duration) string {
return fmt.Sprintf("%s", d)
return fmt.Sprint(d)
}

func main() {
config := config.Get()
file := fs.New(config)
timetrace = core.New(config, file)
if err := timetrace.EnsureDirectories(); err != nil {
log.Fatal("unable to create dirs", err)
}
router := SetupRouter()
router.Run(":8090")
}
Expand Down Expand Up @@ -66,6 +70,7 @@ func SetupRouter() *gin.Engine {
restricted.POST("/create_project", CreateProject)
restricted.POST("/delete_project", DeleteProject)
restricted.POST("/reports", GenerateReport)
restricted.POST("/edit", EditRecord)
}

return router
Expand Down
Binary file modified timetrace-gui
Binary file not shown.
8 changes: 4 additions & 4 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/gin-gonic/gin"
)

var version = "v0.1"
var version = "v0.3"

type Report struct {
Project string
Expand Down Expand Up @@ -41,6 +41,7 @@ type PageData struct {
Breaks string
Projects []*core.Project
Summary map[string]string
DefaultDate string
}

func (data *PageData) Init(page string, c *gin.Context) {
Expand All @@ -52,6 +53,7 @@ func (data *PageData) Init(page string, c *gin.Context) {
data.CurrentSession = "---"
data.CurrentProjectTime = "---"
data.Tracking = false
data.DefaultDate = time.Now().Local().Format("2006-01-02")
status, err := timetrace.Status()
fmt.Println("error: ", err)
if err == nil {
Expand Down Expand Up @@ -93,9 +95,7 @@ func (data *PageData) Init(page string, c *gin.Context) {
//get all projects
data.Projects, err = timetrace.ListProjects()
if err != nil {
fmt.Println("error retrieving projects", err)
data.Projects = []*core.Project{}
}

fmt.Println(data.Projects)

}

0 comments on commit ba852b3

Please sign in to comment.