summaryrefslogtreecommitdiff
path: root/internal/tui/commands.go
blob: 4fdd9e05d6051127ee3f4c9b918313e15f709112 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package tui

import (
	"context"
	"fmt"
	"strings"
	"time"

	"git.tjp.lol/punchcard/internal/actions"
	"git.tjp.lol/punchcard/internal/queries"
	"git.tjp.lol/punchcard/internal/reports"

	tea "github.com/charmbracelet/bubbletea"
)

type (
	navigationMsg             struct{ Forward bool }
	selectionMsg              struct{ Forward bool }
	selectionToEnd            struct{ Top bool }
	drillDownMsg              struct{}
	drillUpMsg                struct{}
	modalClosed               struct{}
	openTimeEntryEditor       struct{}
	openContractorEditor      struct{}
	openClientOrProjectEditor struct{}
	filterHistoryByProjectBox struct{}
	openModalUnchanged        struct{}
	openDeleteConfirmation    struct{}
	recheckBounds             struct{}
	openCreateClientModal     struct{}
	openCreateProjectModal    struct{}
	openHistoryFilterModal    struct{}
	openReportModal           struct{}
	updateHistoryFilter       HistoryFilter
)

func navigate(forward bool) tea.Cmd {
	return func() tea.Msg { return navigationMsg{forward} }
}

func punchIn(m AppModel) tea.Cmd {
	return func() tea.Msg {
		_, _ = actions.New(m.queries).PunchInMostRecent(context.Background(), "", nil)
		// TODO: use the returned TimerSession instead of re-querying everything
		return m.refreshCmd()
	}
}

func punchOut(m AppModel) tea.Cmd {
	return func() tea.Msg {
		_, _ = actions.New(m.queries).PunchOut(context.Background())
		// TODO: use the returned TimerSession instead of re-querying everything
		return m.refreshCmd()
	}
}

func punchInOnSelection(m AppModel) tea.Cmd {
	return func() tea.Msg {
		var clientID, projectID, description string
		var entryRate *float64
		switch m.selectedBox {
		case ProjectsBox:
			clientID, projectID, description, entryRate = m.projectsBox.selection()
		case HistoryBox:
			clientID, projectID, description, entryRate = m.historyBox.selection()
		}

		if clientID == "" {
			return nil
		}

		_, _ = actions.New(m.queries).PunchIn(context.Background(), clientID, projectID, description, entryRate)
		// TODO: use the returned TimerSession instead of re-querying everything
		return m.refreshCmd()
	}
}

func selectHistorySummary() tea.Cmd {
	return func() tea.Msg { return drillDownMsg{} }
}

func backToHistorySummary() tea.Cmd {
	return func() tea.Msg { return drillUpMsg{} }
}

func changeSelection(forward bool) tea.Cmd {
	return func() tea.Msg { return selectionMsg{forward} }
}

func changeSelectionToTop() tea.Cmd {
	return func() tea.Msg { return selectionToEnd{true} }
}

func changeSelectionToBottom() tea.Cmd {
	return func() tea.Msg { return selectionToEnd{false} }
}

func closeModal() tea.Cmd {
	return func() tea.Msg { return modalClosed{} }
}

func editCurrentEntry() tea.Cmd {
	return func() tea.Msg { return openTimeEntryEditor{} }
}

func editContractor() tea.Cmd {
	return func() tea.Msg { return openContractorEditor{} }
}

func editClientOrProject() tea.Cmd {
	return func() tea.Msg { return openClientOrProjectEditor{} }
}

func filterHistoryFromProjectBox() tea.Cmd {
	return func() tea.Msg { return filterHistoryByProjectBox{} }
}

func reOpenModal() tea.Cmd {
	return func() tea.Msg { return openModalUnchanged{} }
}

func confirmDeleteEntry() tea.Cmd {
	return func() tea.Msg { return openDeleteConfirmation{} }
}

func createClientModal() tea.Cmd {
	return func() tea.Msg { return openCreateClientModal{} }
}

func createProjectModal() tea.Cmd {
	return func() tea.Msg { return openCreateProjectModal{} }
}

func createHistoryFilterModal() tea.Cmd {
	return func() tea.Msg { return openHistoryFilterModal{} }
}

func createReportModal() tea.Cmd {
	return func() tea.Msg { return openReportModal{} }
}

func generateReport(m *ModalBoxModel, am AppModel) tea.Cmd {
	return func() tea.Msg {
		form := &m.form

		dateRange, err := reports.ParseDateRange(form.fields[1].Value())
		if err != nil {
			form.fields[1].Err = fmt.Errorf("invalid date range: %v", err)
			return reOpenModal()
		}

		var tz *time.Location
		tzstr := form.fields[5].Value()
		if tzstr == "" {
			tz = time.Local
		} else {
			zone, err := time.LoadLocation(tzstr)
			if err != nil {
				form.fields[5].Err = err
				return reOpenModal()
			}
			tz = zone
		}

		var genFunc func(context.Context, *queries.Queries, reports.ReportParams) (*reports.ReportResult, error)
		switch strings.ToLower(form.fields[0].Value()) {
		case "invoice":
			genFunc = reports.GenerateInvoice
		case "timesheet":
			genFunc = reports.GenerateTimesheet
		case "unified":
			genFunc = reports.GenerateUnifiedReport
		}

		params := reports.ReportParams{
			ClientName:  form.fields[2].Value(),
			ProjectName: form.fields[3].Value(),
			DateRange:   dateRange,
			OutputPath:  form.fields[4].Value(),
			Timezone:    tz,
		}
		if _, err := genFunc(context.Background(), am.queries, params); err != nil {
			form.err = err
			return reOpenModal()
		}

		return nil
	}
}