diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index 78ffe1c618..626d8e28d6 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -1726,6 +1726,7 @@ issues.remove_time_estimate_at = removed time estimate %s issues.time_estimate_invalid = Time estimate format is invalid issues.start_tracking_history = started working %s issues.tracker_auto_close = Timer will be stopped automatically when this issue gets closed +issues.tracker_already_stopped = The Timer for this issue is already stopped issues.tracking_already_started = `You have already started time tracking on another issue!` issues.stop_tracking = Stop Timer issues.stop_tracking_history = worked for %[1]s %[2]s diff --git a/routers/web/repo/issue_stopwatch.go b/routers/web/repo/issue_stopwatch.go index 5a8d203771..a582af1eae 100644 --- a/routers/web/repo/issue_stopwatch.go +++ b/routers/web/repo/issue_stopwatch.go @@ -10,31 +10,49 @@ import ( "code.gitea.io/gitea/services/context" ) -// IssueStopwatch creates or stops a stopwatch for the given issue. -func IssueStopwatch(c *context.Context) { +// IssueStartStopwatch creates a stopwatch for the given issue. +func IssueStartStopwatch(c *context.Context) { issue := GetActionIssue(c) if c.Written() { return } - var showSuccessMessage bool - - if !issues_model.StopwatchExists(c, c.Doer.ID, issue.ID) { - showSuccessMessage = true - } - if !c.Repo.CanUseTimetracker(c, issue, c.Doer) { c.NotFound(nil) return } - if err := issues_model.CreateOrStopIssueStopwatch(c, c.Doer, issue); err != nil { - c.ServerError("CreateOrStopIssueStopwatch", err) + if err := issues_model.CreateIssueStopwatch(c, c.Doer, issue); err != nil { + c.ServerError("CreateIssueStopwatch", err) return } - if showSuccessMessage { - c.Flash.Success(c.Tr("repo.issues.tracker_auto_close")) + c.Flash.Success(c.Tr("repo.issues.tracker_auto_close")) + + c.JSONRedirect("") +} + +// IssueStopStopwatch stops a stopwatch for the given issue. +func IssueStopStopwatch(c *context.Context) { + issue := GetActionIssue(c) + if c.Written() { + return + } + + if !c.Repo.CanUseTimetracker(c, issue, c.Doer) { + c.NotFound(nil) + return + } + + wasRunning := issues_model.StopwatchExists(c, c.Doer.ID, issue.ID) + + if wasRunning { + if err := issues_model.FinishIssueStopwatch(c, c.Doer, issue); err != nil { + c.ServerError("FinishIssueStopwatch", err) + return + } + } else { + c.Flash.Warning(c.Tr("repo.issues.tracker_already_stopped")) } c.JSONRedirect("") diff --git a/routers/web/web.go b/routers/web/web.go index 4b5d68b260..66a5a9be5e 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -1253,7 +1253,8 @@ func registerWebRoutes(m *web.Router) { m.Post("/add", web.Bind(forms.AddTimeManuallyForm{}), repo.AddTimeManually) m.Post("/{timeid}/delete", repo.DeleteTime) m.Group("/stopwatch", func() { - m.Post("/toggle", repo.IssueStopwatch) + m.Post("/start", repo.IssueStartStopwatch) + m.Post("/stop", repo.IssueStopStopwatch) m.Post("/cancel", repo.CancelStopwatch) }) }) diff --git a/templates/repo/issue/sidebar/stopwatch_timetracker.tmpl b/templates/repo/issue/sidebar/stopwatch_timetracker.tmpl index 0176aede7a..ab8600b068 100644 --- a/templates/repo/issue/sidebar/stopwatch_timetracker.tmpl +++ b/templates/repo/issue/sidebar/stopwatch_timetracker.tmpl @@ -16,14 +16,14 @@
{{if $.IsStopwatchRunning}} - + {{svg "octicon-stopwatch"}} {{ctx.Locale.Tr "repo.issues.timetracker_timer_stop"}} {{svg "octicon-trash"}} {{ctx.Locale.Tr "repo.issues.timetracker_timer_discard"}} {{else}} - + {{svg "octicon-stopwatch"}} {{ctx.Locale.Tr "repo.issues.timetracker_timer_start"}}