Skip to content

Commit

Permalink
feat: show success message after creating a flag (#134)
Browse files Browse the repository at this point in the history
* show success message after creating a flag

* use new flagDetail type

* change variable name

* move sendConfirmedFlagMsg to enter case

* allow going back within flag create step but no further

* fix comments

* pr feedback
  • Loading branch information
k3llymariee authored Apr 9, 2024
1 parent eecddb0 commit f817856
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 15 deletions.
12 changes: 7 additions & 5 deletions internal/quickstart/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const (
stepToggleFlag
)

// ContainerModel is a high level container model that controls the nested models wher each
// ContainerModel is a high level container model that controls the nested models where each
// represents a step in the quick-start flow.
type ContainerModel struct {
accessToken string
Expand Down Expand Up @@ -69,7 +69,9 @@ func (m ContainerModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(msg, pressableKeys.Back):
switch m.currentStep {
case stepCreateFlag:
// can't go back
// can only go back if a flag has been created but not confirmed,
// so let the model handle the Update
m.currentModel, cmd = m.currentModel.Update(msg)
case stepChooseSDK:
m.currentStep -= 1
m.currentModel = NewCreateFlagModel(m.flagsClient, m.accessToken, m.baseUri)
Expand Down Expand Up @@ -105,9 +107,9 @@ func (m ContainerModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmd = m.currentModel.Init()
m.sdk = msg.sdk
m.currentStep += 1
case createdFlagMsg:
case confirmedFlagMsg:
m.currentModel = NewChooseSDKModel(0)
m.flagKey = msg.flagKey // TODO: figure out if we maintain state here or pass in another message
m.flagKey = msg.flag.key
m.currentStep += 1
m.err = nil
case errMsg:
Expand All @@ -122,7 +124,7 @@ func (m ContainerModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.sdk.kind,
)
m.currentStep += 1
case fetchedSDKInstructions, fetchedEnv, selectedSDKMsg, toggledFlagMsg, spinner.TickMsg:
case fetchedSDKInstructions, fetchedEnv, selectedSDKMsg, toggledFlagMsg, spinner.TickMsg, createdFlagMsg:
m.currentModel, cmd = m.currentModel.Update(msg)
m.err = nil
case showToggleFlagMsg:
Expand Down
42 changes: 35 additions & 7 deletions internal/quickstart/create_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,22 @@ import (

const defaultFlagName = "My New Flag"

type flag struct {
key string
name string
}

type createFlagModel struct {
accessToken string
baseUri string
client flags.Client
err error
help help.Model
helpKeys keyMap
textInput textinput.Model
accessToken string
baseUri string
client flags.Client
err error
existingFlagUsed bool
flag flag
help help.Model
helpKeys keyMap
showSuccessView bool
textInput textinput.Model
}

func NewCreateFlagModel(client flags.Client, accessToken, baseUri string) tea.Model {
Expand Down Expand Up @@ -53,6 +61,10 @@ func (m createFlagModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyMsg:
switch {
case key.Matches(msg, pressableKeys.Enter):
if m.showSuccessView {
return m, sendConfirmedFlagMsg(m.flag)
}

input := m.textInput.Value()
if input == "" {
input = defaultFlagName
Expand All @@ -63,9 +75,17 @@ func (m createFlagModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

return m, sendCreateFlagMsg(m.client, m.accessToken, m.baseUri, input, flagKey, defaultProjKey)
case key.Matches(msg, pressableKeys.Back):
if m.showSuccessView {
m.showSuccessView = false
}
default:
m.textInput, cmd = m.textInput.Update(msg)
}
case createdFlagMsg:
m.showSuccessView = true
m.existingFlagUsed = msg.existingFlagUsed
m.flag = msg.flag
case errMsg:
m.err = msg.err
}
Expand All @@ -77,6 +97,14 @@ func (m createFlagModel) View() string {
style := lipgloss.NewStyle().
MarginLeft(2)

if m.showSuccessView {
successMessage := fmt.Sprintf("Flag %q created successfully!", m.flag.name)
if m.existingFlagUsed {
successMessage = fmt.Sprintf("Using existing flag %q for setup.", m.flag.name)
}
return successMessage + " Press enter to continue."
}

return fmt.Sprintf(
"Name your first feature flag (enter for default value):%s",
style.Render(m.textInput.View()),
Expand Down
24 changes: 21 additions & 3 deletions internal/quickstart/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,24 @@ func sendToggleFlagMsg(client flags.Client, accessToken, baseUri, flagKey string
}

type createdFlagMsg struct {
flagKey string
flag flag
existingFlagUsed bool
}

type confirmedFlagMsg struct {
flag flag
}

func sendConfirmedFlagMsg(flag flag) tea.Cmd {
return func() tea.Msg {
return confirmedFlagMsg{flag}
}
}

func sendCreateFlagMsg(client flags.Client, accessToken, baseUri, flagName, flagKey, projKey string) tea.Cmd {
return func() tea.Msg {
var existingFlag bool

_, err := client.Create(
context.Background(),
accessToken,
Expand All @@ -66,12 +79,17 @@ func sendCreateFlagMsg(client flags.Client, accessToken, baseUri, flagName, flag
Message string `json:"message"`
}
_ = json.Unmarshal([]byte(err.Error()), &e)
if e.Code != "conflict" {
existingFlag = e.Code == "conflict"
if !existingFlag {
return errMsg{err: errors.NewError(fmt.Sprintf("Error creating flag: %s. Press \"ctrl + c\" to quit.", e.Message))}
}

}

return createdFlagMsg{flagKey: flagKey}
return createdFlagMsg{flag: flag{
key: flagKey,
name: flagName,
}, existingFlagUsed: existingFlag}
}
}

Expand Down

0 comments on commit f817856

Please sign in to comment.