@@ -26,72 +26,108 @@ type Authentication struct {
2626 numberOfInputs int
2727 collection * app.Collection
2828 errors []string
29+ mode string
2930}
3031
3132func 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.
4360func (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 ,
0 commit comments