summaryrefslogtreecommitdiff
path: root/internal/tui/commands.go
blob: 90bc05f6e3d197878ac0e97b78698260f8634528 (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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package tui

import (
	"context"
	"errors"

	"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
	archiveSelectedMsg        struct {
		clientID         int64
		projectID        *int64
		restoreClientID  int64
		restoreProjectID *int64
	}
	unarchiveSelectedMsg struct {
		clientID         int64
		projectID        *int64
		restoreClientID  int64
		restoreProjectID *int64
	}
	toggleShowArchivedMsg struct {
		restoreClientID  int64
		restoreProjectID *int64
	}
	restoreSelectionMsg       struct{ clientID int64; projectID *int64 }
	showArchivedWarningMsg    struct{ params *ArchivedPunchInParams }
	reportGenerationSucceeded struct{}
	reportGenerationFailed    struct{ err error }
)

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

func punchIn(m AppModel) tea.Cmd {
	return func() tea.Msg {
		a := actions.New(m.queries)
		_, err := a.PunchInMostRecent(context.Background(), "", nil, false)
		// Handle archived errors by showing modal
		if err != nil {
			if errors.Is(err, actions.ErrArchivedClient) {
				return showArchivedWarningMsg{
					params: &ArchivedPunchInParams{
						EntityType: "client",
					},
				}
			} else if errors.Is(err, actions.ErrArchivedProject) {
				return showArchivedWarningMsg{
					params: &ArchivedPunchInParams{
						EntityType: "project",
					},
				}
			}
		}

		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
		}

		a := actions.New(m.queries)
		_, err := a.PunchIn(context.Background(), clientID, projectID, description, entryRate, false)
		// Handle archived errors by showing modal
		if err != nil {
			if errors.Is(err, actions.ErrArchivedClient) {
				return showArchivedWarningMsg{
					params: &ArchivedPunchInParams{
						ClientID:    clientID,
						ProjectID:   projectID,
						Description: description,
						Rate:        entryRate,
						EntityType:  "client",
					},
				}
			} else if errors.Is(err, actions.ErrArchivedProject) {
				return showArchivedWarningMsg{
					params: &ArchivedPunchInParams{
						ClientID:    clientID,
						ProjectID:   projectID,
						Description: description,
						Rate:        entryRate,
						EntityType:  "project",
					},
				}
			}
		}

		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(am AppModel, genFunc func(context.Context, *queries.Queries, reports.ReportParams) (*reports.ReportResult, error), params reports.ReportParams) tea.Cmd {
	return func() tea.Msg {
		if _, err := genFunc(context.Background(), am.queries, params); err != nil {
			return reportGenerationFailed{err: err}
		}
		return reportGenerationSucceeded{}
	}
}

func archiveClientOrProject(clientID int64, projectID *int64) tea.Cmd {
	return func() tea.Msg {
		return archiveSelectedMsg{
			clientID:         clientID,
			projectID:        projectID,
			restoreClientID:  clientID,
			restoreProjectID: projectID,
		}
	}
}

func unarchiveClientOrProject(clientID int64, projectID *int64) tea.Cmd {
	return func() tea.Msg {
		return unarchiveSelectedMsg{
			clientID:         clientID,
			projectID:        projectID,
			restoreClientID:  clientID,
			restoreProjectID: projectID,
		}
	}
}

func toggleShowArchived(clientID int64, projectID *int64) tea.Cmd {
	return func() tea.Msg {
		return toggleShowArchivedMsg{
			restoreClientID:  clientID,
			restoreProjectID: projectID,
		}
	}
}