Skip to content

Commit 5b9ecab

Browse files
committed
feat: add option to edit collection
1 parent 1884855 commit 5b9ecab

File tree

8 files changed

+126
-75
lines changed

8 files changed

+126
-75
lines changed

app/app.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ type Collection struct {
3535

3636
func NewCollection() Collection {
3737
return Collection{
38-
ID: uuid.NewString(),
3938
Calls: []Call{},
4039
}
4140
}
@@ -345,6 +344,7 @@ func (a *App) GetResponse(call *Call) tea.Cmd {
345344
}
346345

347346
func (a *App) CreateCollection(collection Collection) tea.Cmd {
347+
collection.ID = uuid.NewString()
348348
return func() tea.Msg {
349349
configDir, _ := os.UserConfigDir()
350350
a.Collections = append(a.Collections, collection)
@@ -356,6 +356,18 @@ func (a *App) CreateCollection(collection Collection) tea.Cmd {
356356
}
357357
}
358358

359+
func (a *App) UpdateCollection(collection Collection) tea.Cmd {
360+
for i, c := range a.Collections {
361+
if c.ID == collection.ID {
362+
a.Collections[i] = collection
363+
}
364+
}
365+
366+
return tea.Batch(
367+
a.SaveCollections(),
368+
)
369+
}
370+
359371
// TODO refactor
360372
func (a *App) SaveCollections() tea.Cmd {
361373
return func() tea.Msg {

app/message.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ type FetchCollectionsSuccessMsg struct{ Collections []Collection }
88

99
type CollectionSelectedMsg struct{ Collection *Collection }
1010

11+
type CollectionEditMsg struct{ Collection *Collection }
12+
1113
type CallSelectedMsg struct{ Call *Call }
1214

1315
type CallUpdatedMsg struct{ Call *Call }

components/collections/authentication.go

Lines changed: 69 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,72 +26,108 @@ type Authentication struct {
2626
numberOfInputs int
2727
collection *app.Collection
2828
errors []string
29+
mode string
2930
}
3031

3132
func NewAuthentication(collection *app.Collection) Authentication {
32-
return Authentication{
33+
mode := "Create"
34+
okText := "Create"
35+
if collection.ID != "" {
36+
mode = "Edit"
37+
okText = "Save"
38+
}
39+
40+
method := NONE
41+
if collection.Auth != nil {
42+
method = collection.Auth.Type
43+
}
44+
45+
auth := Authentication{
46+
mode: mode,
3347
inputs: make([]textinput.Model, 0),
3448
focused: 1,
35-
method: NONE,
49+
method: method,
3650
numberOfInputs: 0,
37-
footer: Footer{CancelText: "Back", OkText: "Create", Width: 70},
51+
footer: Footer{CancelText: "Back", OkText: okText, Width: 70},
3852
collection: collection,
3953
}
54+
auth.setBasedOnMethod()
55+
56+
return auth
4057
}
4158

4259
// Init initializes the popup.
4360
func (c Authentication) Init() tea.Cmd {
4461
return textinput.Blink
4562
}
4663

47-
func (c *Authentication) nextMethod() {
64+
func (c *Authentication) setBasedOnMethod() {
65+
username := ""
66+
password := ""
67+
headerName := ""
68+
headerValue := ""
69+
bearerToken := ""
70+
if c.collection.Auth != nil {
71+
username = c.collection.Auth.Username
72+
password = c.collection.Auth.Password
73+
headerName = c.collection.Auth.HeaderName
74+
headerValue = c.collection.Auth.HeaderValue
75+
bearerToken = c.collection.Auth.Token
76+
77+
}
78+
4879
switch c.method {
4980
case NONE:
50-
81+
c.inputs = make([]textinput.Model, 0)
82+
c.focused = 1
83+
case BASIC_AUTH:
5184
c.inputs = make([]textinput.Model, 2)
5285
c.inputs[0] = textinput.New()
5386
c.inputs[0].Placeholder = "username"
5487
c.inputs[0].Prompt = " "
88+
c.inputs[0].SetValue(username)
5589

5690
c.inputs[1] = textinput.New()
5791
c.inputs[1].Placeholder = "password"
5892
c.inputs[1].Prompt = "󰌆 "
59-
60-
c.method = BASIC_AUTH
93+
c.inputs[1].SetValue(password)
6194
c.focused = 0
62-
case BASIC_AUTH:
63-
95+
case BEARER_TOKEN:
6496
c.inputs = make([]textinput.Model, 1)
6597
c.inputs[0] = textinput.New()
6698
c.inputs[0].Placeholder = "token"
6799
c.inputs[0].Prompt = "󰌆 "
68-
69-
c.method = BEARER_TOKEN
100+
c.inputs[0].SetValue(bearerToken)
70101
c.focused = 0
71-
case BEARER_TOKEN:
72-
102+
case API_KEY:
73103
c.inputs = make([]textinput.Model, 2)
74-
75104
c.inputs[0] = textinput.New()
76105
c.inputs[0].Placeholder = "header name"
77106
c.inputs[0].Focus()
78107
c.inputs[0].Prompt = " "
108+
c.inputs[0].SetValue(headerName)
79109

80110
c.inputs[1] = textinput.New()
81111
c.inputs[1].Placeholder = "value"
82112
c.inputs[1].Prompt = "󰌆 "
113+
c.inputs[1].SetValue(headerValue)
114+
c.focused = 0
115+
}
116+
c.numberOfInputs = len(c.inputs)
117+
}
83118

119+
func (c *Authentication) nextMethod() {
120+
switch c.method {
121+
case NONE:
122+
c.method = BASIC_AUTH
123+
case BASIC_AUTH:
124+
c.method = BEARER_TOKEN
125+
case BEARER_TOKEN:
84126
c.method = API_KEY
85-
c.focused = 0
86127
case API_KEY:
87-
c.inputs = make([]textinput.Model, 0)
88-
89128
c.method = NONE
90-
// make ok button focused
91-
c.focused = 1
92129
}
93-
94-
c.numberOfInputs = len(c.inputs)
130+
c.setBasedOnMethod()
95131
}
96132

97133
// Update handles messages.
@@ -114,9 +150,17 @@ func (c Authentication) Update(msg tea.Msg) (Authentication, tea.Cmd) {
114150
} else if c.focused == numOfInputs-1 {
115151
c.errors = c.collection.ValidatePartial("name", "baseUrl", "auth")
116152
if len(c.errors) == 0 {
117-
return c, tea.Batch(
118-
app.GetInstance().CreateCollection(*c.collection),
119-
func() tea.Msg { return CreateResultMsg{false} })
153+
// TODO: refacor to use this logic in app.GetInstance().SaveCollection()
154+
// and get rid of edit/create.go
155+
if c.mode == "Create" {
156+
return c, tea.Batch(
157+
app.GetInstance().CreateCollection(*c.collection),
158+
func() tea.Msg { return CreateResultMsg{false} })
159+
} else {
160+
return c, tea.Batch(
161+
app.GetInstance().UpdateCollection(*c.collection),
162+
func() tea.Msg { return CreateResultMsg{false} })
163+
}
120164
}
121165
}
122166

@@ -223,7 +267,7 @@ func (c Authentication) View() string {
223267

224268
}
225269

226-
header := Header{Steps{Current: 1}}
270+
header := Header{Steps{Current: 1}, c.mode}
227271

228272
return lipgloss.JoinVertical(
229273
lipgloss.Left,

components/collections/basic_info.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const NUM_OF_INPUTS = 4
3333

3434
// Create is a popup that presents a yes/no choice to the user.
3535
type BasicInfo struct {
36+
mode string
3637
focused int
3738
err error
3839
footer Footer
@@ -48,12 +49,20 @@ func NewBasicInfo(collection *app.Collection) BasicInfo {
4849
inputs[TITLE_IDX].Placeholder = "My Collection"
4950
inputs[TITLE_IDX].Focus()
5051
inputs[TITLE_IDX].Prompt = ""
52+
inputs[TITLE_IDX].SetValue(collection.Name)
5153

5254
inputs[BASE_URL_IDX] = textinput.New()
5355
inputs[BASE_URL_IDX].Placeholder = "https://sampleapi.com/api/v1"
5456
inputs[BASE_URL_IDX].Prompt = ""
57+
inputs[BASE_URL_IDX].SetValue(collection.BaseUrl)
58+
59+
mode := "Create"
60+
if collection.ID != "" {
61+
mode = "Edit"
62+
}
5563

5664
return BasicInfo{
65+
mode: mode,
5766
focused: 0,
5867
inputs: inputs,
5968
collection: collection,
@@ -133,7 +142,7 @@ func (c BasicInfo) View() string {
133142
" ",
134143
)
135144

136-
header := Header{Steps{Current: 0}}
145+
header := Header{Steps{Current: 0}, c.mode}
137146

138147
return lipgloss.JoinVertical(
139148
lipgloss.Left,

components/collections/delegate.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ func newItemDelegate(keys *delegateKeyMap) list.DefaultDelegate {
2828
}
2929
// TODO: ask for confirmation
3030
return app.GetInstance().RemoveCollection(i)
31+
32+
case key.Matches(msg, keys.edit):
33+
return func() tea.Msg {
34+
return app.CollectionEditMsg{Collection: &i}
35+
}
3136
}
3237
}
3338

@@ -50,6 +55,7 @@ func newItemDelegate(keys *delegateKeyMap) list.DefaultDelegate {
5055
type delegateKeyMap struct {
5156
choose key.Binding
5257
remove key.Binding
58+
edit key.Binding
5359
}
5460

5561
// Additional short help entries. This satisfies the help.KeyMap interface and
@@ -58,6 +64,7 @@ func (d delegateKeyMap) ShortHelp() []key.Binding {
5864
return []key.Binding{
5965
d.choose,
6066
d.remove,
67+
d.edit,
6168
}
6269
}
6370

@@ -68,6 +75,7 @@ func (d delegateKeyMap) FullHelp() [][]key.Binding {
6875
{
6976
d.choose,
7077
d.remove,
78+
d.edit,
7179
},
7280
}
7381
}
@@ -82,5 +90,9 @@ func newDelegateKeyMap() *delegateKeyMap {
8290
key.WithKeys("x"),
8391
key.WithHelp("x", "delete"),
8492
),
93+
edit: key.NewBinding(
94+
key.WithKeys("e"),
95+
key.WithHelp("e", "edit"),
96+
),
8597
}
8698
}

components/collections/header.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
type Header struct {
1111
steps Steps
12+
mode string //Create or Edit
1213
}
1314

1415
func (h Header) Init() tea.Cmd {
@@ -20,9 +21,13 @@ func (h Header) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
2021
}
2122

2223
func (h Header) View() string {
24+
icon := ""
25+
if h.mode == "edit" {
26+
icon = "󰷎"
27+
}
2328
return lipgloss.JoinVertical(
2429
lipgloss.Left,
25-
config.BoxHeader.Render(" Create collection"),
30+
config.BoxHeader.Render(icon+" "+h.mode+" collection"),
2631
h.steps.View(),
2732
)
2833
}

0 commit comments

Comments
 (0)