-
Notifications
You must be signed in to change notification settings - Fork 0
/
form.go
180 lines (160 loc) · 4.33 KB
/
form.go
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
package main
import (
"bytes"
"encoding/gob"
"fmt"
"strconv"
"time"
"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textarea"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/redis/go-redis/v9"
)
type Form struct {
help help.Model
title textinput.Model
description textarea.Model
deadline textarea.Model
col column
index int
originalTask Task // Store the original task for comparison
}
func newDefaultForm() *Form {
return NewForm("task name", "task description", "task deadline")
}
func NewForm(title, description, deadline string) *Form {
form := Form{
help: help.New(),
title: textinput.New(),
description: textarea.New(),
deadline: textarea.New(),
}
form.title.Placeholder = title
form.description.Placeholder = description
form.deadline.Placeholder = deadline
form.title.Focus()
return &form
}
var nextTaskID int
func latestTaskID() (int, error) {
// Get the current value of the task ID counter from Redis
taskIDStr, err := client.Get(ctx, "taskIDCounter").Result()
if err != nil {
return 0, err
}
// Convert the task ID string to an integer
lastTaskID, err := strconv.Atoi(taskIDStr)
if err != nil {
return 0, err
}
return lastTaskID, nil
}
func (f Form) CreateTask() Task {
return Task{f.col.status, f.title.Value(), f.description.Value(), f.deadline.Value(), nextTaskID}
}
func (f Form) Init() tea.Cmd {
return nil
}
func (f Form) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case column:
f.col = msg
f.col.list.Index()
case tea.KeyMsg:
switch {
case key.Matches(msg, keys.Quit):
return f, tea.Quit
case key.Matches(msg, keys.Back):
return board.Update(nil)
case key.Matches(msg, keys.Enter):
if f.title.Focused() {
f.title.Blur()
f.description.Focus()
return f, textarea.Blink
}
if f.description.Focused() {
f.description.Blur()
f.deadline.Focus()
return f, textarea.Blink
}
dl, err := time.Parse("2006-01-02", f.deadline.Value())
if err != nil {
fmt.Println("Error parsing deadline:", err)
}
if f.index != APPEND {
editedTask := Task{
Taskid: f.originalTask.Taskid,
Tasktitle: f.title.Value(),
Taskdescription: f.description.Value(),
Taskdeadline: f.deadline.Value(),
Taskstatus: f.originalTask.Taskstatus,
}
if editedTask != f.originalTask {
RemoveTask(f.originalTask) // Remove the old task
AddTask(editedTask) // Add the new task
// f.col.list.SetItem(f.index, editedTask)
}
} else {
id, err := latestTaskID()
if err == nil {
nextTaskID = id + 1
} else {
nextTaskID++
}
// Create the new task
newTask := Task{
Taskid: nextTaskID,
Tasktitle: f.title.Value(),
Taskdescription: f.description.Value(),
Taskstatus: f.col.status,
Taskdeadline: f.deadline.Value(),
}
var buffer bytes.Buffer
encoder := gob.NewEncoder(&buffer)
if err := encoder.Encode(newTask); err != nil {
fmt.Printf("Error encoding custom type: %v", err)
}
// Using a pipeline to perform multiple operations
_, err = client.Pipelined(ctx, func(pipe redis.Pipeliner) error {
hashKey := fmt.Sprintf("task:%d", newTask.Taskid)
pipe.HSet(ctx, "tasks", hashKey, buffer.Bytes())
pipe.ZAdd(ctx, "tasks:sorted", redis.Z{Score: float64(dl.Unix()), Member: hashKey})
return nil
})
if err != nil {
fmt.Println("Error executing Redis pipeline:", err)
return f, nil
}
_, err = client.Incr(ctx, "taskIDCounter").Result()
if err != nil {
fmt.Println("error incrementing the counter")
}
fmt.Println("Task added successfully!")
}
return board.Update(f)
}
}
if f.title.Focused() {
f.title, cmd = f.title.Update(msg)
return f, cmd
} else if f.description.Focused() {
f.description, cmd = f.description.Update(msg)
return f, cmd
} else if f.deadline.Focused() {
f.deadline, cmd = f.deadline.Update(msg)
return f, cmd
}
return f, cmd
}
func (f Form) View() string {
return lipgloss.JoinVertical(
lipgloss.Left,
f.title.View(),
f.description.View(),
f.deadline.View(),
f.help.View(keys))
}