Skip to content

Commit 4aafe89

Browse files
authored
feat(UI): pagination, collections, endpoints (#28)
1 parent 404283e commit 4aafe89

File tree

14 files changed

+658
-51
lines changed

14 files changed

+658
-51
lines changed

flake.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
devShells.x86_64-linux.default =
88
let pkgs = import nixpkgs { system = "x86_64-linux"; };
99
in pkgs.mkShell {
10-
buildInputs = [ pkgs.go pkgs.zsh ];
10+
buildInputs = [ pkgs.go pkgs.zsh pkgs.sqlite ];
1111

1212
shellHook = ''
1313
export GOPATH=$PWD/.gopath

global/context.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package global
2+
3+
import (
4+
"github.com/maniac-en/req/internal/collections"
5+
"github.com/maniac-en/req/internal/endpoints"
6+
"github.com/maniac-en/req/internal/history"
7+
"github.com/maniac-en/req/internal/http"
8+
)
9+
10+
type AppContext struct {
11+
Collections *collections.CollectionsManager
12+
Endpoints *endpoints.EndpointsManager
13+
HTTP *http.HTTPManager
14+
History *history.HistoryManager
15+
}
16+
17+
var globalAppContext *AppContext
18+
19+
func SetAppContext(ctx *AppContext) {
20+
globalAppContext = ctx
21+
}
22+
23+
func GetAppContext() *AppContext {
24+
return globalAppContext
25+
}

global/state.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package global
2+
3+
type State struct {
4+
currentCollection string
5+
}
6+
7+
func NewGlobalState() *State {
8+
return &State{
9+
currentCollection: "",
10+
}
11+
}
12+
13+
// Gets the current collection from the app state
14+
func (s *State) GetCurrentCollection() string {
15+
return s.currentCollection
16+
}
17+
18+
// Sets the current collection to the app state
19+
func (s *State) SetCurrentCollection(collection string) {
20+
s.currentCollection = collection
21+
}

internal/app/model.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package app
33
import (
44
tea "github.com/charmbracelet/bubbletea"
55
"github.com/charmbracelet/lipgloss"
6+
"github.com/maniac-en/req/global"
67
"github.com/maniac-en/req/internal/messages"
78
"github.com/maniac-en/req/internal/tabs"
89
)
@@ -12,19 +13,27 @@ type Model struct {
1213
activeTab int
1314
width int
1415
height int
16+
17+
// Global state for sharing data
18+
state *global.State
1519
}
1620

1721
func InitialModel() Model {
18-
tabList := []tabs.Tab{
19-
tabs.NewCollectionsTab(),
20-
tabs.NewAddCollectionTab(),
21-
tabs.NewEditCollectionTab(),
22-
}
22+
23+
globalState := global.NewGlobalState()
2324

2425
return Model{
25-
tabs: tabList,
26-
activeTab: 0,
26+
state: globalState,
27+
tabs: []tabs.Tab{
28+
tabs.NewCollectionsTab(globalState),
29+
tabs.NewAddCollectionTab(),
30+
tabs.NewEditCollectionTab(),
31+
tabs.NewEndpointsTab(globalState),
32+
tabs.NewAddEndpointTab(globalState),
33+
tabs.NewEditEndpointTab(globalState),
34+
},
2735
}
36+
2837
}
2938

3039
func (m Model) Init() tea.Cmd {
@@ -47,6 +56,11 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
4756
editTab.SetEditingCollection(msg.Label, msg.Value)
4857
}
4958
return m, nil
59+
case messages.EditEndpointMsg:
60+
if editTab, ok := m.tabs[5].(*tabs.EditEndpointTab); ok {
61+
editTab.SetEditingEndpoint(msg)
62+
}
63+
return m, nil
5064

5165
case tea.KeyMsg:
5266
switch msg.String() {

internal/messages/messages.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,10 @@ type EditCollectionMsg struct {
88
Label string
99
Value string
1010
}
11+
12+
type EditEndpointMsg struct {
13+
Name string
14+
Method string
15+
URL string
16+
ID string
17+
}

internal/tabs/add-collections.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package tabs
22

33
import (
4+
"context"
5+
"strconv"
6+
47
"github.com/charmbracelet/bubbles/textinput"
58
tea "github.com/charmbracelet/bubbletea"
69
"github.com/charmbracelet/lipgloss"
10+
"github.com/maniac-en/req/global"
711
"github.com/maniac-en/req/internal/messages"
812
)
913

@@ -98,9 +102,12 @@ func (a *AddCollectionTab) View() string {
98102
}
99103

100104
func (a *AddCollectionTab) addCollection(name string) (Tab, tea.Cmd) {
105+
ctx := global.GetAppContext()
106+
collection, _ := ctx.Collections.Create(context.Background(), name)
107+
value := strconv.Itoa(int(collection.GetID()))
101108
newOption := OptionPair{
102-
Label: name,
103-
Value: name,
109+
Label: collection.GetName(),
110+
Value: value,
104111
}
105112

106113
GlobalCollections = append(GlobalCollections, newOption)

internal/tabs/add-endpoint.go

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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

Comments
 (0)