Skip to content

Commit

Permalink
feat: display current flag state on toggle flag page (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunny Guduru authored Apr 18, 2024
1 parent 4e812c6 commit cfb3c1f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 11 deletions.
14 changes: 4 additions & 10 deletions internal/quickstart/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,13 @@ func (m ContainerModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.err = nil
case errMsg:
m.currentModel, cmd = m.currentModel.Update(msg)
case noInstructionsMsg:
// skip the ShowSDKInstructionsModel and move along to toggling the flag
m.currentModel = NewToggleFlagModel(
m.flagsClient,
m.accessToken,
m.baseUri,
m.flagKey,
m.sdk.kind,
)
m.currentStep += 1
case fetchedEnvMsg:
m.environment = &msg.environment
m.currentModel, cmd = m.currentModel.Update(msg)
m.err = nil
case fetchedFlagStatusMsg:
m.currentModel, cmd = m.currentModel.Update(msg)
m.err = nil
case fetchedSDKInstructionsMsg, selectedSDKMsg, toggledFlagMsg, spinner.TickMsg, createdFlagMsg:
m.gettingStarted = false
m.currentModel, cmd = m.currentModel.Update(msg)
Expand All @@ -155,6 +148,7 @@ func (m ContainerModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.flagKey,
m.sdk.kind,
)
cmd = m.currentModel.Init()
m.currentStep += 1
default:
log.Printf("container default: %T\n", msg)
Expand Down
29 changes: 29 additions & 0 deletions internal/quickstart/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,35 @@ func fetchEnv(
}
}

type fetchedFlagStatusMsg struct {
enabled bool
}

func fetchFlagStatus(
client flags.Client,
accessToken string,
baseUri string,
key,
envKey,
projKey string,
) tea.Cmd {
return func() tea.Msg {
response, err := client.Get(context.Background(), accessToken, baseUri, key, projKey, envKey)
if err != nil {
return errMsg{err: err}
}

var resp struct {
Environments map[string]interface{} `json:"environments"`
}
err = json.Unmarshal(response, &resp)
if err != nil {
return errMsg{err: err}
}
return fetchedFlagStatusMsg{enabled: resp.Environments[envKey].(map[string]interface{})["on"].(bool)}
}
}

// noInstructionsMsg is sent when we can't find the SDK instructions repository for the given SDK.
type noInstructionsMsg struct{}

Expand Down
26 changes: 25 additions & 1 deletion internal/quickstart/toggle_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/charmbracelet/bubbles/help"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"

Expand All @@ -20,12 +21,17 @@ type toggleFlagModel struct {
err error
flagKey string
flagWasEnabled bool
flagWasFetched bool
help help.Model
helpKeys keyMap
sdkKind string
spinner spinner.Model
}

func NewToggleFlagModel(client flags.Client, accessToken string, baseUri string, flagKey string, sdkKind string) tea.Model {
s := spinner.New()
s.Spinner = spinner.Points

return toggleFlagModel{
accessToken: accessToken,
baseUri: baseUri,
Expand All @@ -37,11 +43,17 @@ func NewToggleFlagModel(client flags.Client, accessToken string, baseUri string,
Quit: BindingQuit,
},
sdkKind: sdkKind,
spinner: s,
}
}

func (m toggleFlagModel) Init() tea.Cmd {
return nil
cmds := []tea.Cmd{
m.spinner.Tick,
fetchFlagStatus(m.client, m.accessToken, m.baseUri, m.flagKey, defaultEnvKey, defaultProjKey),
}

return tea.Sequence(cmds...)
}

func (m toggleFlagModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
Expand All @@ -52,11 +64,19 @@ func (m toggleFlagModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case key.Matches(msg, pressableKeys.Quit):
return m, tea.Quit
case key.Matches(msg, pressableKeys.Tab):
if !m.flagWasFetched {
return m, nil
}
m.flagWasEnabled = true
m.enabled = !m.enabled
m.err = nil
return m, toggleFlag(m.client, m.accessToken, m.baseUri, m.flagKey, m.enabled)
}
case fetchedFlagStatusMsg:
m.enabled = msg.enabled
m.flagWasFetched = true
case spinner.TickMsg:
m.spinner, cmd = m.spinner.Update(msg)
case errMsg:
msgRequestErr, err := newMsgRequestError(msg.err.Error())
if err != nil {
Expand Down Expand Up @@ -91,6 +111,10 @@ func (m toggleFlagModel) View() string {
margin = 2
toggle = "ON"
}
if !m.flagWasFetched {
margin = 1
toggle = m.spinner.View()
}

if m.flagWasEnabled && m.err == nil {
furtherInstructions = fmt.Sprintf("\n\nCheck your %s to see the change!", logTypeMap[m.sdkKind])
Expand Down

0 comments on commit cfb3c1f

Please sign in to comment.