summaryrefslogtreecommitdiff
path: root/internal/tui/form.go
blob: b0ac18de8e64b16e87042546a224d9353727a495 (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package tui

import (
	"errors"
	"fmt"
	"strconv"
	"strings"
	"time"

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

	"github.com/charmbracelet/bubbles/textinput"
	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"
)

type suggestionType int

const (
	noSuggestions suggestionType = iota
	suggestClients
	suggestProjects
	suggestReportType
)

type FormField struct {
	textinput.Model
	label       string
	suggestions suggestionType
}

func (ff FormField) Update(msg tea.Msg) (FormField, tea.Cmd) {
	field, cmd := ff.Model.Update(msg)
	ff.Model = field
	return ff, cmd
}

func newTimestampField(label string) FormField {
	f := FormField{
		Model: textinput.New(),
		label: label,
	}
	f.Validate = func(s string) error {
		if _, err := time.Parse(time.DateTime, s); err != nil {
			return fmt.Errorf("timestamps must be written like \"%s\"", time.DateTime)
		}
		return nil
	}
	return f
}

func newOptionalTimestampField(label string) FormField {
	f := FormField{
		Model: textinput.New(),
		label: label,
	}
	f.Validate = func(s string) error {
		if s == "" {
			return nil
		}
		if _, err := time.Parse(time.DateTime, s); err != nil {
			return fmt.Errorf("timestamps must be written like \"%s\"", time.DateTime)
		}
		return nil
	}
	return f
}

func newOptionalFloatField(label string) FormField {
	f := FormField{
		Model: textinput.New(),
		label: label,
	}
	f.Validate = func(s string) error {
		if s == "" {
			return nil
		}
		_, err := strconv.ParseFloat(s, 64)
		if err != nil {
			return errors.New("numerical values only")
		}
		return nil
	}
	return f
}

func newDateRangeField(label string) FormField {
	f := FormField{
		Model: textinput.New(),
		label: label,
	}
	f.Validate = func(s string) error {
		if s == "" {
			return errors.New("date range is required")
		}
		_, err := reports.ParseDateRange(s)
		if err != nil {
			return fmt.Errorf("invalid date range: %v", err)
		}
		return nil
	}
	return f
}

func newReportTypeField(label string) FormField {
	f := FormField{
		Model:       textinput.New(),
		label:       label,
		suggestions: suggestReportType,
	}
	f.Validate = func(s string) error {
		switch strings.ToLower(s) {
		case "invoice", "timesheet", "unified":
			return nil
		}
		return errors.New("pick one of invoice, timesheet, or unified")
	}
	return f
}

type Form struct {
	fields []FormField
	selIdx int
	err    error

	SelectedStyle   *lipgloss.Style
	UnselectedStyle *lipgloss.Style
}

func NewForm(fields []FormField) Form {
	return Form{fields: fields}
}

func (f Form) Error() error {
	for _, field := range f.fields {
		if field.Err != nil {
			return field.Err
		}
	}
	return nil
}

func NewEntryEditorForm() Form {
	form := NewForm([]FormField{
		newTimestampField("Start time"),
		newOptionalTimestampField("End time"),
		{Model: textinput.New(), label: "Client", suggestions: suggestClients},
		{Model: textinput.New(), label: "Project", suggestions: suggestProjects},
		{Model: textinput.New(), label: "Description"},
		newOptionalFloatField("Hourly Rate"),
	})
	form.SelectedStyle = &modalFocusedInputStyle
	form.UnselectedStyle = &modalBlurredInputStyle
	return form
}

func NewClientForm() Form {
	form := NewForm([]FormField{
		{Model: textinput.New(), label: "Name"},
		{Model: textinput.New(), label: "Email"},
		newOptionalFloatField("Hourly Rate"),
	})
	form.SelectedStyle = &modalFocusedInputStyle
	form.UnselectedStyle = &modalBlurredInputStyle
	return form
}

func NewProjectCreateForm() Form {
	form := NewForm([]FormField{
		{Model: textinput.New(), label: "Name"},
		{Model: textinput.New(), label: "Client", suggestions: suggestClients},
		newOptionalFloatField("Hourly Rate"),
	})
	form.SelectedStyle = &modalFocusedInputStyle
	form.UnselectedStyle = &modalBlurredInputStyle
	return form
}

func NewProjectEditForm() Form {
	form := NewForm([]FormField{
		{Model: textinput.New(), label: "Name"},
		newOptionalFloatField("Hourly Rate"),
	})
	form.SelectedStyle = &modalFocusedInputStyle
	form.UnselectedStyle = &modalBlurredInputStyle
	return form
}

func NewHistoryFilterForm() Form {
	form := NewForm([]FormField{
		newDateRangeField("Date Range"),
		{Model: textinput.New(), label: "Client (optional)", suggestions: suggestClients},
		{Model: textinput.New(), label: "Project (optional)", suggestions: suggestProjects},
	})
	form.SelectedStyle = &modalFocusedInputStyle
	form.UnselectedStyle = &modalBlurredInputStyle
	return form
}

func NewGenerateReportForm() Form {
	form := NewForm([]FormField{
		newReportTypeField("Report Type"),
		newDateRangeField("Date Range"),
		{Model: textinput.New(), label: "Client", suggestions: suggestClients},
		{Model: textinput.New(), label: "Project (optional)", suggestions: suggestProjects},
		{Model: textinput.New(), label: "Output Path (optional)"},
		{Model: textinput.New(), label: "Timezone (optional)"},
	})
	form.SelectedStyle = &modalFocusedInputStyle
	form.UnselectedStyle = &modalBlurredInputStyle
	return form
}

func NewContractorForm() Form {
	form := NewForm([]FormField{
		{Model: textinput.New(), label: "Your Name"},
		{Model: textinput.New(), label: "Label for your work"},
		{Model: textinput.New(), label: "Your Email"},
	})
	form.SelectedStyle = &modalFocusedInputStyle
	form.UnselectedStyle = &modalBlurredInputStyle
	return form
}

func (f Form) Update(msg tea.Msg) (Form, tea.Cmd) {
	if msg, ok := msg.(tea.KeyMsg); ok {
		switch msg.String() {
		case "tab":
			f.fields[f.selIdx].Blur()
			f.selIdx = (f.selIdx + 1) % len(f.fields)
			return f, f.fields[f.selIdx].Focus()
		case "shift+tab":
			f.fields[f.selIdx].Blur()
			f.selIdx--
			if f.selIdx < 0 {
				f.selIdx += len(f.fields)
			}
			return f, f.fields[f.selIdx].Focus()
		}
	}

	field, cmd := f.fields[f.selIdx].Update(msg)
	f.fields[f.selIdx] = field
	return f, cmd
}

func (f Form) View() string {
	content := ""

	if f.err != nil {
		content += errorStyle.Render(f.err.Error()) + "\n\n"
	}

	for i, field := range f.fields {
		if i > 0 {
			content += "\n\n"
		}
		content += field.label + ":\n"

		style := f.UnselectedStyle
		if i == f.selIdx {
			style = f.SelectedStyle
		}
		if style != nil {
			content += style.Render(field.View())
		} else {
			content += field.View()
		}

		if field.Err != nil {
			content += "\n" + errorStyle.Render(field.Err.Error())
		}
	}
	return content
}

func (f *Form) SetSuggestions(m AppModel) {
	for i := range f.fields {
		ff := &f.fields[i]
		switch ff.suggestions {
		case suggestClients:
			clients := make([]string, len(m.projectsBox.clients))
			for i, cl := range m.projectsBox.clients {
				clients[i] = cl.Name
			}
			ff.SetSuggestions(clients)
			ff.ShowSuggestions = true
		case suggestProjects:
			projNames := make([]string, 0, 10)
			for _, cl := range m.projectsBox.clients {
				for _, proj := range m.projectsBox.projects[cl.ID] {
					projNames = append(projNames, proj.Name)
				}
			}
			ff.SetSuggestions(projNames)
			ff.ShowSuggestions = true
		case suggestReportType:
			ff.SetSuggestions([]string{"Invoice", "Timesheet", "Unified"})
			ff.ShowSuggestions = true
		}
	}
}

var (
	modalFocusedInputStyle = lipgloss.NewStyle().
				Border(lipgloss.DoubleBorder()).
				BorderForeground(lipgloss.Color("238"))
	modalBlurredInputStyle = lipgloss.NewStyle().
				Border(lipgloss.NormalBorder()).
				BorderForeground(lipgloss.Color("238"))
	errorStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("196"))
)