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
|
package tui
import (
"context"
"database/sql"
"fmt"
"strconv"
"time"
"punchcard/internal/actions"
"punchcard/internal/queries"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss/v2"
)
type ModalType int
const (
ModalTypeClient ModalType = iota
ModalTypeProject
ModalTypeDeleteConfirmation
ModalTypeEntry
)
func (mt ModalType) newForm() Form {
switch mt {
case ModalTypeEntry:
return NewEntryEditorForm()
case ModalTypeClient:
return NewClientForm()
case ModalTypeProject:
return NewProjectForm()
}
return Form{}
}
type ModalBoxModel struct {
Active bool
Type ModalType
form Form
editedID int64
}
func (m *ModalBoxModel) HandleKeyPress(msg tea.KeyMsg) tea.Cmd {
form, cmd := m.form.Update(msg)
m.form = form
return cmd
}
func (m ModalBoxModel) RenderCenteredOver(mainContent string, app AppModel) string {
if !m.Active {
return mainContent
}
modalContent := m.Render()
base := lipgloss.NewLayer(mainContent)
overlayStyle := lipgloss.NewStyle().Border(lipgloss.RoundedBorder()).BorderForeground(lipgloss.Color("62")).Padding(2, 4)
overlay := lipgloss.NewLayer(overlayStyle.Render(modalContent))
overlayWidth := overlay.GetWidth()
overlayHeight := overlay.GetHeight()
overlayLeft := (app.width - overlayWidth) / 2
overlayTop := (app.height - overlayHeight) / 2
canvas := lipgloss.NewCanvas(
base.Z(0),
overlay.X(overlayLeft).Y(overlayTop).Z(1),
)
return canvas.Render()
}
func (m ModalBoxModel) Render() string {
switch m.Type {
case ModalTypeEntry:
return m.RenderFormModal("⏰ Time Entry")
case ModalTypeDeleteConfirmation:
return m.RenderDeleteConfirmation()
case ModalTypeClient:
return m.RenderFormModal("👤 Client")
case ModalTypeProject:
return m.RenderFormModal("📂 Project")
default: // REMOVE ME
return "DEFAULT CONTENT"
}
}
func (m ModalBoxModel) RenderFormModal(title string) string {
return fmt.Sprintf(
"%s\n\n%s\n\n%s Submit %s Cancel",
modalTitleStyle.Render(title),
m.form.View(),
boldStyle.Render("[Enter]"),
boldStyle.Render("[Esc]"),
)
}
func (m ModalBoxModel) RenderDeleteConfirmation() string {
return fmt.Sprintf(
"%s\n\nAre you sure you want to delete this time entry?\nThis action cannot be undone.\n\n%s Delete %s Cancel",
modalTitleStyle.Render("🗑️ Delete Time Entry"),
boldStyle.Render("[Enter]"),
boldStyle.Render("[Esc]"),
)
}
func (m *ModalBoxModel) activateCreateProjectModal(am AppModel) {
m.activate(ModalTypeProject, 0)
if am.selectedBox == ProjectsBox && len(am.projectsBox.clients) > 0 {
client := am.projectsBox.clients[am.projectsBox.selectedClient]
m.form.fields[1].SetValue(client.Name)
}
m.form.fields[0].Focus()
}
func (m *ModalBoxModel) activate(t ModalType, editedID int64) {
m.Active = true
m.Type = t
m.form = t.newForm()
m.editedID = editedID
}
func (m *ModalBoxModel) deactivate() {
m.Active = false
}
var (
boldStyle = lipgloss.NewStyle().Bold(true)
modalTitleStyle = boldStyle
)
func (m *ModalBoxModel) SubmitForm(am AppModel) tea.Cmd {
switch m.Type {
case ModalTypeDeleteConfirmation:
err := am.queries.RemoveTimeEntry(context.Background(), m.editedID)
if err != nil {
return reOpenModal()
}
return tea.Sequence(am.refreshCmd, func() tea.Msg { return recheckBounds{} })
case ModalTypeEntry:
if err := m.form.Error(); err != nil {
return reOpenModal()
}
params, hasErrors := m.validateAndParseEntryForm(am)
if hasErrors {
return reOpenModal()
}
err := am.queries.EditTimeEntry(context.Background(), params)
if err != nil {
m.form.err = err
return reOpenModal()
}
msg := am.refreshCmd()
return func() tea.Msg { return msg }
case ModalTypeClient:
if err := m.form.Error(); err != nil {
return reOpenModal()
}
var rate *float64
if value := m.form.fields[2].Value(); value != "" {
r, _ := strconv.ParseFloat(value, 64)
rate = &r
}
if m.editedID != 0 {
panic("editing a client not yet implemented")
}
if _, err := actions.New(am.queries).CreateClient(
context.Background(),
m.form.fields[0].Value(),
m.form.fields[1].Value(),
rate,
); err != nil {
m.form.err = err
return reOpenModal()
}
msg := am.refreshCmd()
return func() tea.Msg { return msg }
case ModalTypeProject:
if err := m.form.Error(); err != nil {
return reOpenModal()
}
var rate *float64
if value := m.form.fields[2].Value(); value != "" {
r, _ := strconv.ParseFloat(value, 64)
rate = &r
}
if m.editedID != 0 {
panic("editing a project not yet implemented")
}
if _, err := actions.New(am.queries).CreateProject(
context.Background(),
m.form.fields[0].Value(),
m.form.fields[1].Value(),
rate,
); err != nil {
m.form.err = err
return reOpenModal()
}
msg := am.refreshCmd()
return func() tea.Msg { return msg }
}
return nil
}
func (m *ModalBoxModel) validateAndParseEntryForm(am AppModel) (queries.EditTimeEntryParams, bool) {
var params queries.EditTimeEntryParams
var hasErrors bool
// Set the entry ID
params.EntryID = m.editedID
entry, _ := am.queries.GetTimeEntryById(context.Background(), params.EntryID)
startTimeStr := m.form.fields[0].Value()
startTime, err := time.ParseInLocation(time.DateTime, startTimeStr, time.Local)
if err != nil {
m.form.fields[0].Err = fmt.Errorf("invalid start time format")
hasErrors = true
} else {
params.StartTime = startTime.UTC().Format(time.DateTime)
}
endTimeStr := m.form.fields[1].Value()
if endTimeStr != "" {
endTime, err := time.ParseInLocation(time.DateTime, endTimeStr, time.Local)
if err != nil {
m.form.fields[1].Err = fmt.Errorf("invalid end time format")
hasErrors = true
} else {
params.EndTime = endTime.UTC().Format(time.DateTime)
}
} else if entry.EndTime.Valid {
m.form.fields[1].Err = fmt.Errorf("can not re-open an entry")
hasErrors = true
}
clientStr := m.form.fields[2].Value()
if clientStr == "" {
m.form.fields[2].Err = fmt.Errorf("client is required")
hasErrors = true
} else {
client, err := actions.New(am.queries).FindClient(context.Background(), clientStr)
if err != nil {
m.form.fields[2].Err = fmt.Errorf("client not found: %s", clientStr)
hasErrors = true
} else {
params.ClientID = client.ID
}
}
projectStr := m.form.fields[3].Value()
if projectStr != "" {
project, err := actions.New(am.queries).FindProject(context.Background(), projectStr)
if err != nil {
m.form.fields[3].Err = fmt.Errorf("project not found: %s", projectStr)
hasErrors = true
} else {
params.ProjectID = sql.NullInt64{Int64: project.ID, Valid: true}
}
}
descriptionStr := m.form.fields[4].Value()
if descriptionStr != "" {
params.Description = sql.NullString{String: descriptionStr, Valid: true}
}
rateStr := m.form.fields[5].Value()
if rateStr != "" {
rate, err := strconv.ParseFloat(rateStr, 64)
if err != nil {
m.form.fields[5].Err = fmt.Errorf("invalid hourly rate")
hasErrors = true
} else {
params.HourlyRate = sql.NullInt64{Int64: int64(rate * 100), Valid: true}
}
}
return params, hasErrors
}
|