Compare commits

...

2 Commits

Author SHA1 Message Date
c7798c5784 Add the ability to start the tracker when submitting an entry
All checks were successful
CI / test (push) Successful in 1m1s
2025-06-20 17:08:39 +02:00
6116e89aac Return the time entry ID when creating 2025-06-20 17:08:15 +02:00
2 changed files with 74 additions and 36 deletions

View File

@ -51,8 +51,10 @@ type Project struct {
func (p Project) String() string {
return fmt.Sprintf("%d: %s (%s)", p.ID, p.Name, p.CustomerName)
}
func (p Project) GetID() int { return p.ID }
func (p Project) GetName() string { return p.Name + " (" + p.CustomerName + ")" }
func (p Project) GetID() int { return p.ID }
func (p Project) GetName() string {
return p.Name + " (" + p.CustomerName + ")"
}
func (a APIClient) GetProjects() (Projects, error) {
// GET /projects.json
@ -238,7 +240,9 @@ type requestAddTimeEntry struct {
} `json:"time_entry"`
}
func (a APIClient) AddTimeEntry(date string, minutes int, notes string, projectId, serviceId int) error {
type apiPostTimeEntry timeEntryHolder
func (a APIClient) AddTimeEntry(date string, minutes int, notes string, projectId, serviceId int) (int, error) {
// POST /time_entries.json
// {
// "time_entry": {
@ -254,8 +258,10 @@ func (a APIClient) AddTimeEntry(date string, minutes int, notes string, projectI
req.RequestTimeEntryHolder.ProjectID = projectId
req.RequestTimeEntryHolder.ServiceID = serviceId
err := a.post("/time_entries.json", req)
return err
res := apiPostTimeEntry{}
err := a.post("/time_entries.json", req, &res)
return res.TimeEntry.ID, err
}
type apiTimeTrackerEntry struct {
@ -398,9 +404,9 @@ func (a APIClient) patch(path string, data any) error {
return nil
}
func (a APIClient) post(path string, data any) error {
func (a APIClient) post(path string, dataIn any, dataOut any) error {
b, err := json.Marshal(data)
b, err := json.Marshal(dataIn)
if err != nil {
return err
}
@ -421,6 +427,11 @@ func (a APIClient) post(path string, data any) error {
return fmt.Errorf("expected 2XX, got %d", resp.StatusCode)
}
err = json.NewDecoder(resp.Body).Decode(&dataOut)
if err != nil {
return err
}
return nil
}

View File

@ -35,6 +35,7 @@ func (m model) updateForm(msg tea.Msg) (tea.Model, tea.Cmd) {
projectID := m.formData.form.GetString("project")
description := m.formData.form.GetString("description")
minutes := m.formData.form.GetString("minutes")
tracker := m.formData.form.GetBool("tracker")
minutesInt, err1 := strconv.ParseInt(minutes, 10, 64)
serviceIDInt, err2 := strconv.ParseInt(serviceID, 10, 64)
@ -45,14 +46,27 @@ func (m model) updateForm(msg tea.Msg) (tea.Model, tea.Cmd) {
m.tuiMode = MODE_CAL
} else {
err := m.miteAPI.AddTimeEntry(m.dest.Format(time.DateOnly), int(minutesInt), description, int(projectIDInt), int(serviceIDInt))
id, err := m.miteAPI.AddTimeEntry(m.dest.Format(time.DateOnly), int(minutesInt), description, int(projectIDInt), int(serviceIDInt))
if err != nil {
m.statusBarMessage = err.Error()
m.tuiMode = MODE_CAL
} else {
m.statusBarMessage = "Successfully logged time"
m.tuiMode = MODE_CAL
return m, m.fetchMiteData()
if tracker {
_, _, err = m.miteAPI.StartTimeTracker(id)
if err != nil {
m.statusBarMessage = err.Error()
m.tuiMode = MODE_CAL
} else {
m.statusBarMessage = "Successfully logged time and started tracker"
m.tuiMode = MODE_CAL
return m, m.fetchMiteData()
}
} else {
m.statusBarMessage = "Successfully logged time"
m.tuiMode = MODE_CAL
return m, m.fetchMiteData()
}
}
}
@ -101,38 +115,51 @@ func (m model) buildForm() *huh.Form {
Key("project").
Title("Project").
Options(projOptions...).
Height(10)
Height(6)
description := huh.NewText().
Key("description").
Title("description").
Validate(func(s string) error {
if s == "" {
return errors.New("must enter a description")
}
return nil
})
defMinutes := "0"
minutes := huh.NewInput().
Key("minutes").
CharLimit(5).
Validate(
func(s string) error {
h, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return err
}
if h < 0 {
return errors.New("must be positive")
}
return err
}).
Title("Minutes").
Value(&defMinutes)
confirmTracker := huh.NewConfirm().
Key("tracker").
Affirmative("Yes").
Negative("No").
Title("Start tracker")
form := huh.NewForm(
huh.NewGroup(
pl,
sl,
description,
),
huh.NewGroup(
huh.NewText().
Key("description").
Title("description").
Validate(func(s string) error {
if s == "" {
return errors.New("must enter a description")
}
return nil
}),
huh.NewInput().
Key("minutes").
CharLimit(5).
Validate(
func(s string) error {
h, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return err
}
if h < 0 {
return errors.New("must be positive")
}
return err
}).
Title("Minutes"),
minutes,
confirmTracker,
),
)
return form