|
| 1 | +package tabs |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strconv" |
| 6 | + |
| 7 | + "github.com/charmbracelet/bubbles/textinput" |
| 8 | + tea "github.com/charmbracelet/bubbletea" |
| 9 | + "github.com/charmbracelet/lipgloss" |
| 10 | + "github.com/maniac-en/req/global" |
| 11 | + "github.com/maniac-en/req/internal/endpoints" |
| 12 | + "github.com/maniac-en/req/internal/messages" |
| 13 | +) |
| 14 | + |
| 15 | +type AddEndpointTab struct { |
| 16 | + name string |
| 17 | + inputs []textinput.Model |
| 18 | + focusedInput int |
| 19 | + state *global.State |
| 20 | + focused bool |
| 21 | +} |
| 22 | + |
| 23 | +func NewAddEndpointTab(globalState *global.State) *AddEndpointTab { |
| 24 | + name := textinput.New() |
| 25 | + name.Placeholder = "Enter your endpoint's name... " |
| 26 | + name.CharLimit = 100 |
| 27 | + name.Width = 50 |
| 28 | + |
| 29 | + method := textinput.New() |
| 30 | + method.Placeholder = "Enter your method... " |
| 31 | + method.CharLimit = 100 |
| 32 | + method.Width = 50 |
| 33 | + |
| 34 | + url := textinput.New() |
| 35 | + url.Placeholder = "Enter your url..." |
| 36 | + url.CharLimit = 100 |
| 37 | + url.Width = 50 |
| 38 | + |
| 39 | + name.Focus() |
| 40 | + |
| 41 | + return &AddEndpointTab{ |
| 42 | + name: "Add Endpoint", |
| 43 | + inputs: []textinput.Model{ |
| 44 | + name, |
| 45 | + method, |
| 46 | + url, |
| 47 | + }, |
| 48 | + focused: true, |
| 49 | + state: globalState, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func (a *AddEndpointTab) Name() string { |
| 54 | + return a.name |
| 55 | +} |
| 56 | +func (a *AddEndpointTab) Instructions() string { |
| 57 | + return "none" |
| 58 | +} |
| 59 | +func (a *AddEndpointTab) Init() tea.Cmd { |
| 60 | + return textinput.Blink |
| 61 | +} |
| 62 | +func (a *AddEndpointTab) Update(msg tea.Msg) (Tab, tea.Cmd) { |
| 63 | + switch msg := msg.(type) { |
| 64 | + case tea.KeyMsg: |
| 65 | + switch msg.String() { |
| 66 | + case "enter": |
| 67 | + if a.inputs[0].Value() != "" && a.inputs[1].Value() != "" && a.inputs[2].Value() != "" { |
| 68 | + return a.addEndpoint(a.inputs[0].Value(), a.inputs[1].Value(), a.inputs[2].Value()) |
| 69 | + } |
| 70 | + return a, nil |
| 71 | + case "tab": |
| 72 | + a.inputs[a.focusedInput].Blur() |
| 73 | + a.focusedInput = (a.focusedInput + 1) % len(a.inputs) |
| 74 | + a.inputs[a.focusedInput].Focus() |
| 75 | + case "esc": |
| 76 | + return a, func() tea.Msg { |
| 77 | + return messages.SwitchTabMsg{TabIndex: 3} |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + a.inputs[a.focusedInput], _ = a.inputs[a.focusedInput].Update(msg) |
| 83 | + return a, nil |
| 84 | +} |
| 85 | +func (a *AddEndpointTab) View() string { |
| 86 | + titleStyle := lipgloss.NewStyle(). |
| 87 | + Bold(true). |
| 88 | + Foreground(lipgloss.Color("205")). |
| 89 | + MarginBottom(2) |
| 90 | + |
| 91 | + inputStyle := lipgloss.NewStyle(). |
| 92 | + Border(lipgloss.RoundedBorder()). |
| 93 | + BorderForeground(lipgloss.Color("62")). |
| 94 | + Padding(1, 2). |
| 95 | + MarginBottom(2) |
| 96 | + |
| 97 | + form := lipgloss.JoinVertical(lipgloss.Center, |
| 98 | + titleStyle.Render("Create New Endpoint"), |
| 99 | + inputStyle.Render(a.inputs[0].View()), |
| 100 | + inputStyle.Render(a.inputs[1].View()), |
| 101 | + inputStyle.Render(a.inputs[2].View()), |
| 102 | + ) |
| 103 | + |
| 104 | + containerStyle := lipgloss.NewStyle(). |
| 105 | + Width(60). |
| 106 | + Height(20). |
| 107 | + Align(lipgloss.Center, lipgloss.Center) |
| 108 | + |
| 109 | + return containerStyle.Render(form) |
| 110 | +} |
| 111 | + |
| 112 | +func (a *AddEndpointTab) OnFocus() tea.Cmd { |
| 113 | + a.inputs[a.focusedInput].Focus() |
| 114 | + a.focused = true |
| 115 | + return textinput.Blink |
| 116 | +} |
| 117 | + |
| 118 | +func (a *AddEndpointTab) OnBlur() tea.Cmd { |
| 119 | + a.inputs[0].Blur() |
| 120 | + a.inputs[1].Blur() |
| 121 | + a.inputs[2].Blur() |
| 122 | + a.focused = false |
| 123 | + return nil |
| 124 | +} |
| 125 | + |
| 126 | +func (a *AddEndpointTab) addEndpoint(name, method, url string) (Tab, tea.Cmd) { |
| 127 | + ctx := global.GetAppContext() |
| 128 | + |
| 129 | + collectionId := a.state.GetCurrentCollection() |
| 130 | + int64Collection, err := strconv.ParseInt(collectionId, 10, 64) |
| 131 | + if err != nil { |
| 132 | + return a, func() tea.Msg { |
| 133 | + return messages.SwitchTabMsg{TabIndex: 3} |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + _, _ = ctx.Endpoints.CreateEndpoint(context.Background(), endpoints.EndpointData{ |
| 138 | + Name: name, |
| 139 | + Method: method, |
| 140 | + URL: url, |
| 141 | + CollectionID: int64Collection, |
| 142 | + }) |
| 143 | + |
| 144 | + // newOption := OptionPair{ |
| 145 | + // Label: collection.GetName(), |
| 146 | + // Value: string(collection.GetID()), |
| 147 | + // } |
| 148 | + |
| 149 | + a.inputs[0].SetValue("") |
| 150 | + a.inputs[1].SetValue("") |
| 151 | + a.inputs[2].SetValue("") |
| 152 | + |
| 153 | + return a, func() tea.Msg { |
| 154 | + return messages.SwitchTabMsg{TabIndex: 3} |
| 155 | + } |
| 156 | +} |
0 commit comments