Skip to content

Commit

Permalink
feat: add option to edit collection
Browse files Browse the repository at this point in the history
  • Loading branch information
jackMort committed Nov 27, 2024
1 parent 1884855 commit 5b9ecab
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 75 deletions.
14 changes: 13 additions & 1 deletion app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ type Collection struct {

func NewCollection() Collection {
return Collection{
ID: uuid.NewString(),
Calls: []Call{},
}
}
Expand Down Expand Up @@ -345,6 +344,7 @@ func (a *App) GetResponse(call *Call) tea.Cmd {
}

func (a *App) CreateCollection(collection Collection) tea.Cmd {
collection.ID = uuid.NewString()
return func() tea.Msg {
configDir, _ := os.UserConfigDir()
a.Collections = append(a.Collections, collection)
Expand All @@ -356,6 +356,18 @@ func (a *App) CreateCollection(collection Collection) tea.Cmd {
}
}

func (a *App) UpdateCollection(collection Collection) tea.Cmd {
for i, c := range a.Collections {
if c.ID == collection.ID {
a.Collections[i] = collection
}
}

return tea.Batch(
a.SaveCollections(),
)
}

// TODO refactor
func (a *App) SaveCollections() tea.Cmd {
return func() tea.Msg {
Expand Down
2 changes: 2 additions & 0 deletions app/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ type FetchCollectionsSuccessMsg struct{ Collections []Collection }

type CollectionSelectedMsg struct{ Collection *Collection }

type CollectionEditMsg struct{ Collection *Collection }

type CallSelectedMsg struct{ Call *Call }

type CallUpdatedMsg struct{ Call *Call }
Expand Down
94 changes: 69 additions & 25 deletions components/collections/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,72 +26,108 @@ type Authentication struct {
numberOfInputs int
collection *app.Collection
errors []string
mode string
}

func NewAuthentication(collection *app.Collection) Authentication {
return Authentication{
mode := "Create"
okText := "Create"
if collection.ID != "" {
mode = "Edit"
okText = "Save"
}

method := NONE
if collection.Auth != nil {
method = collection.Auth.Type
}

auth := Authentication{
mode: mode,
inputs: make([]textinput.Model, 0),
focused: 1,
method: NONE,
method: method,
numberOfInputs: 0,
footer: Footer{CancelText: "Back", OkText: "Create", Width: 70},
footer: Footer{CancelText: "Back", OkText: okText, Width: 70},
collection: collection,
}
auth.setBasedOnMethod()

return auth
}

// Init initializes the popup.
func (c Authentication) Init() tea.Cmd {
return textinput.Blink
}

func (c *Authentication) nextMethod() {
func (c *Authentication) setBasedOnMethod() {
username := ""
password := ""
headerName := ""
headerValue := ""
bearerToken := ""
if c.collection.Auth != nil {
username = c.collection.Auth.Username
password = c.collection.Auth.Password
headerName = c.collection.Auth.HeaderName
headerValue = c.collection.Auth.HeaderValue
bearerToken = c.collection.Auth.Token

}

switch c.method {
case NONE:

c.inputs = make([]textinput.Model, 0)
c.focused = 1
case BASIC_AUTH:
c.inputs = make([]textinput.Model, 2)
c.inputs[0] = textinput.New()
c.inputs[0].Placeholder = "username"
c.inputs[0].Prompt = " "
c.inputs[0].SetValue(username)

c.inputs[1] = textinput.New()
c.inputs[1].Placeholder = "password"
c.inputs[1].Prompt = "󰌆 "

c.method = BASIC_AUTH
c.inputs[1].SetValue(password)
c.focused = 0
case BASIC_AUTH:

case BEARER_TOKEN:
c.inputs = make([]textinput.Model, 1)
c.inputs[0] = textinput.New()
c.inputs[0].Placeholder = "token"
c.inputs[0].Prompt = "󰌆 "

c.method = BEARER_TOKEN
c.inputs[0].SetValue(bearerToken)
c.focused = 0
case BEARER_TOKEN:

case API_KEY:
c.inputs = make([]textinput.Model, 2)

c.inputs[0] = textinput.New()
c.inputs[0].Placeholder = "header name"
c.inputs[0].Focus()
c.inputs[0].Prompt = " "
c.inputs[0].SetValue(headerName)

c.inputs[1] = textinput.New()
c.inputs[1].Placeholder = "value"
c.inputs[1].Prompt = "󰌆 "
c.inputs[1].SetValue(headerValue)
c.focused = 0
}
c.numberOfInputs = len(c.inputs)
}

func (c *Authentication) nextMethod() {
switch c.method {
case NONE:
c.method = BASIC_AUTH
case BASIC_AUTH:
c.method = BEARER_TOKEN
case BEARER_TOKEN:
c.method = API_KEY
c.focused = 0
case API_KEY:
c.inputs = make([]textinput.Model, 0)

c.method = NONE
// make ok button focused
c.focused = 1
}

c.numberOfInputs = len(c.inputs)
c.setBasedOnMethod()
}

// Update handles messages.
Expand All @@ -114,9 +150,17 @@ func (c Authentication) Update(msg tea.Msg) (Authentication, tea.Cmd) {
} else if c.focused == numOfInputs-1 {
c.errors = c.collection.ValidatePartial("name", "baseUrl", "auth")
if len(c.errors) == 0 {
return c, tea.Batch(
app.GetInstance().CreateCollection(*c.collection),
func() tea.Msg { return CreateResultMsg{false} })
// TODO: refacor to use this logic in app.GetInstance().SaveCollection()
// and get rid of edit/create.go
if c.mode == "Create" {
return c, tea.Batch(
app.GetInstance().CreateCollection(*c.collection),
func() tea.Msg { return CreateResultMsg{false} })
} else {
return c, tea.Batch(
app.GetInstance().UpdateCollection(*c.collection),
func() tea.Msg { return CreateResultMsg{false} })
}
}
}

Expand Down Expand Up @@ -223,7 +267,7 @@ func (c Authentication) View() string {

}

header := Header{Steps{Current: 1}}
header := Header{Steps{Current: 1}, c.mode}

return lipgloss.JoinVertical(
lipgloss.Left,
Expand Down
11 changes: 10 additions & 1 deletion components/collections/basic_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const NUM_OF_INPUTS = 4

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

inputs[BASE_URL_IDX] = textinput.New()
inputs[BASE_URL_IDX].Placeholder = "https://sampleapi.com/api/v1"
inputs[BASE_URL_IDX].Prompt = ""
inputs[BASE_URL_IDX].SetValue(collection.BaseUrl)

mode := "Create"
if collection.ID != "" {
mode = "Edit"
}

return BasicInfo{
mode: mode,
focused: 0,
inputs: inputs,
collection: collection,
Expand Down Expand Up @@ -133,7 +142,7 @@ func (c BasicInfo) View() string {
" ",
)

header := Header{Steps{Current: 0}}
header := Header{Steps{Current: 0}, c.mode}

return lipgloss.JoinVertical(
lipgloss.Left,
Expand Down
12 changes: 12 additions & 0 deletions components/collections/delegate.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ func newItemDelegate(keys *delegateKeyMap) list.DefaultDelegate {
}
// TODO: ask for confirmation
return app.GetInstance().RemoveCollection(i)

case key.Matches(msg, keys.edit):
return func() tea.Msg {
return app.CollectionEditMsg{Collection: &i}
}
}
}

Expand All @@ -50,6 +55,7 @@ func newItemDelegate(keys *delegateKeyMap) list.DefaultDelegate {
type delegateKeyMap struct {
choose key.Binding
remove key.Binding
edit key.Binding
}

// Additional short help entries. This satisfies the help.KeyMap interface and
Expand All @@ -58,6 +64,7 @@ func (d delegateKeyMap) ShortHelp() []key.Binding {
return []key.Binding{
d.choose,
d.remove,
d.edit,
}
}

Expand All @@ -68,6 +75,7 @@ func (d delegateKeyMap) FullHelp() [][]key.Binding {
{
d.choose,
d.remove,
d.edit,
},
}
}
Expand All @@ -82,5 +90,9 @@ func newDelegateKeyMap() *delegateKeyMap {
key.WithKeys("x"),
key.WithHelp("x", "delete"),
),
edit: key.NewBinding(
key.WithKeys("e"),
key.WithHelp("e", "edit"),
),
}
}
7 changes: 6 additions & 1 deletion components/collections/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

type Header struct {
steps Steps
mode string //Create or Edit
}

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

func (h Header) View() string {
icon := ""
if h.mode == "edit" {
icon = "󰷎"
}
return lipgloss.JoinVertical(
lipgloss.Left,
config.BoxHeader.Render(" Create collection"),
config.BoxHeader.Render(icon+" "+h.mode+" collection"),
h.steps.View(),
)
}
Loading

0 comments on commit 5b9ecab

Please sign in to comment.