Skip to content

Commit

Permalink
feat: allow updating worklog entries
Browse files Browse the repository at this point in the history
  • Loading branch information
dhth committed Apr 8, 2024
1 parent 1c73ba6 commit 458870d
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 18 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ Issue List View
<ctrl+s> Add manual worklog entry
Worklog List View
<ctrl+s> Update worklog entry
<ctrl+d> Delete worklog entry
s Sync all visible entries to JIRA
<ctrl+r> Refresh list
Expand Down
29 changes: 27 additions & 2 deletions ui/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ func insertManualEntry(db *sql.DB, issueKey string, beginTS time.Time, endTS tim
return func() tea.Msg {

stmt, err := db.Prepare(`
INSERT INTO issue_log (issue_key, begin_ts, end_ts, comment, active, synced)
VALUES (?, ?, ?, ?, ?, ?);
INSERT INTO issue_log (issue_key, begin_ts, end_ts, comment, active, synced)
VALUES (?, ?, ?, ?, ?, ?);
`)

if err != nil {
Expand All @@ -74,6 +74,31 @@ func insertManualEntry(db *sql.DB, issueKey string, beginTS time.Time, endTS tim
}
}

func updateManualEntry(db *sql.DB, rowID int, issueKey string, beginTS time.Time, endTS time.Time, comment string) tea.Cmd {
return func() tea.Msg {

stmt, err := db.Prepare(`
UPDATE issue_log
SET begin_ts = ?,
end_ts = ?,
comment = ?
WHERE ID = ?;
`)

if err != nil {
return ManualEntryUpdated{rowID, issueKey, err}
}
defer stmt.Close()

_, err = stmt.Exec(beginTS, endTS, comment, rowID)
if err != nil {
return ManualEntryUpdated{rowID, issueKey, err}
}

return ManualEntryUpdated{rowID, issueKey, nil}
}
}

func fetchActiveStatus(db *sql.DB, interval time.Duration) tea.Cmd {
return tea.Tick(interval, func(time.Time) tea.Msg {
row := db.QueryRow(`
Expand Down
1 change: 1 addition & 0 deletions ui/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Issue List View
<ctrl+s> Add manual worklog entry
Worklog List View
<ctrl+s> Update worklog entry
<ctrl+d> Delete worklog entry
s Sync all visible entries to JIRA
<ctrl+r> Refresh list
Expand Down
10 changes: 9 additions & 1 deletion ui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const (
IssueListView StateView = iota
WorklogView
AskForCommentView
EntryTrackingView
ManualWorklogEntryView
HelpView
)

Expand All @@ -43,6 +43,13 @@ const (
entryComment
)

type worklogSaveType uint

const (
worklogInsert worklogSaveType = iota
worklogUpdate
)

const (
timeFormat = "2006/01/02 15:04"
)
Expand All @@ -62,6 +69,7 @@ type model struct {
lastChange DBChange
changesLocked bool
activeIssue string
worklogSaveType worklogSaveType
message string
errorMessage string
messages []string
Expand Down
6 changes: 6 additions & 0 deletions ui/msgs.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ type ManualEntryInserted struct {
err error
}

type ManualEntryUpdated struct {
rowId int
issueKey string
err error
}

type UpdateEntryMsg struct {
err error
}
Expand Down
71 changes: 57 additions & 14 deletions ui/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.trackingInputs[entryComment].SetValue("")
return m, tea.Batch(cmds...)
}
case EntryTrackingView:
m.activeView = IssueListView

issue, ok := m.issueList.SelectedItem().(Issue)
case ManualWorklogEntryView:
beginTS, err := time.ParseInLocation(string(timeFormat), m.trackingInputs[entryBeginTS].Value(), time.Local)
if err != nil {
m.errorMessage = err.Error()
Expand All @@ -56,21 +53,31 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

}

if ok {
cmds = append(cmds, insertManualEntry(m.db, issue.IssueKey, beginTS.Local(), endTS.Local(), comment))
}

for i := range m.trackingInputs {
m.trackingInputs[i].SetValue("")
}
issue, ok := m.issueList.SelectedItem().(Issue)
if ok {
switch m.worklogSaveType {
case worklogInsert:
cmds = append(cmds, insertManualEntry(m.db, issue.IssueKey, beginTS.Local(), endTS.Local(), comment))
m.activeView = IssueListView
case worklogUpdate:
wl, ok := m.worklogList.SelectedItem().(WorklogEntry)
if ok {
cmds = append(cmds, updateManualEntry(m.db, wl.Id, wl.IssueKey, beginTS.Local(), endTS.Local(), comment))
m.activeView = WorklogView
}
}
}
return m, tea.Batch(cmds...)
}
case "esc":
switch m.activeView {
case AskForCommentView:
m.activeView = IssueListView
m.trackingInputs[entryComment].SetValue("")
case EntryTrackingView:
case ManualWorklogEntryView:
m.activeView = IssueListView
for i := range m.trackingInputs {
m.trackingInputs[i].SetValue("")
Expand All @@ -80,7 +87,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch m.activeView {
case IssueListView:
m.activeView = WorklogView
case EntryTrackingView:
case WorklogView:
m.activeView = IssueListView
case ManualWorklogEntryView:
switch m.trackingFocussedField {
case entryBeginTS:
m.trackingFocussedField = entryEndTS
Expand All @@ -98,7 +107,9 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch m.activeView {
case WorklogView:
m.activeView = IssueListView
case EntryTrackingView:
case IssueListView:
m.activeView = WorklogView
case ManualWorklogEntryView:
switch m.trackingFocussedField {
case entryBeginTS:
m.trackingFocussedField = entryComment
Expand All @@ -120,7 +131,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.trackingInputs[entryComment], cmd = m.trackingInputs[entryComment].Update(msg)
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
case EntryTrackingView:
case ManualWorklogEntryView:
for i := range m.trackingInputs {
m.trackingInputs[i], cmd = m.trackingInputs[i].Update(msg)
cmds = append(cmds, cmd)
Expand Down Expand Up @@ -169,7 +180,8 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
case "ctrl+s":
if m.activeView == IssueListView {
m.activeView = EntryTrackingView
m.activeView = ManualWorklogEntryView
m.worklogSaveType = worklogInsert
m.trackingFocussedField = entryBeginTS
currentTime := time.Now()
dateString := currentTime.Format("2006/01/02")
Expand All @@ -182,6 +194,25 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.trackingInputs[i].Blur()
}
m.trackingInputs[m.trackingFocussedField].Focus()
} else if m.activeView == WorklogView {
wl, ok := m.worklogList.SelectedItem().(WorklogEntry)
if ok {
m.activeView = ManualWorklogEntryView
m.worklogSaveType = worklogUpdate
m.trackingFocussedField = entryBeginTS

beginTSStr := wl.BeginTS.Format(timeFormat)
endTSStr := wl.EndTS.Format(timeFormat)

m.trackingInputs[entryBeginTS].SetValue(beginTSStr)
m.trackingInputs[entryEndTS].SetValue(endTSStr)
m.trackingInputs[entryComment].SetValue(wl.Comment)

for i := range m.trackingInputs {
m.trackingInputs[i].Blur()
}
m.trackingInputs[m.trackingFocussedField].Focus()
}
}
case "ctrl+d":
switch m.activeView {
Expand Down Expand Up @@ -281,14 +312,26 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case ManualEntryInserted:
if msg.err != nil {
message := msg.err.Error()
m.message = message
m.message = "Error inserting worklog: " + message
m.messages = append(m.messages, message)
} else {
m.message = "Manual entry saved"
for i := range m.trackingInputs {
m.trackingInputs[i].SetValue("")
}
}
case ManualEntryUpdated:
if msg.err != nil {
message := msg.err.Error()
m.message = "Error updating worklog: " + message
m.messages = append(m.messages, message)
} else {
m.message = "Worklog updated"
for i := range m.trackingInputs {
m.trackingInputs[i].SetValue("")
}
cmds = append(cmds, fetchLogEntries(m.db))
}
case LogEntriesFetchedMsg:
if msg.err != nil {
message := msg.err.Error()
Expand Down
2 changes: 1 addition & 1 deletion ui/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (m model) View() string {
for i := 0; i < m.terminalHeight-20+10; i++ {
content += "\n"
}
case EntryTrackingView:
case ManualWorklogEntryView:

content = fmt.Sprintf(
`
Expand Down

0 comments on commit 458870d

Please sign in to comment.