diff --git a/internal/quickstart/container.go b/internal/quickstart/container.go index 74f6914b..a0642ded 100644 --- a/internal/quickstart/container.go +++ b/internal/quickstart/container.go @@ -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 @@ -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) @@ -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: @@ -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: diff --git a/internal/quickstart/create_flag.go b/internal/quickstart/create_flag.go index 7b47055d..11e1bbf1 100644 --- a/internal/quickstart/create_flag.go +++ b/internal/quickstart/create_flag.go @@ -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 { @@ -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 @@ -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 } @@ -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()), diff --git a/internal/quickstart/messages.go b/internal/quickstart/messages.go index bb63f0c6..2c49d645 100644 --- a/internal/quickstart/messages.go +++ b/internal/quickstart/messages.go @@ -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, @@ -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} } }