github.com/soypat/vectytemplater@v0.0.0-20220501050640-d40b24e35168/_templates/websocket-cli/app/views/body.go (about)

     1  package views
     2  
     3  import (
     4  	"vecty-templater-project/app/store"
     5  	"vecty-templater-project/app/store/actions"
     6  
     7  	"github.com/hexops/vecty"
     8  	"github.com/hexops/vecty/elem"
     9  	"github.com/hexops/vecty/event"
    10  )
    11  
    12  type Body struct {
    13  	vecty.Core
    14  	Ctx  actions.Context `vecty:"prop"`
    15  	Info string          `vecty:"prop"`
    16  }
    17  
    18  func (b *Body) Render() vecty.ComponentOrHTML {
    19  	var mainContent vecty.MarkupOrChild
    20  	switch b.Ctx.Page {
    21  	case actions.PageLanding:
    22  		mainContent = elem.Div(
    23  			elem.Strong(vecty.Text(b.Info)),
    24  			elem.Div(elem.Button(
    25  				vecty.Markup(event.Click(b.newItem)),
    26  				vecty.Text("New item"),
    27  			)),
    28  			&Landing{
    29  				Items: store.Items,
    30  			},
    31  		)
    32  	case actions.PageNewItem:
    33  		mainContent = &NewItem{}
    34  	default:
    35  		panic("unknown Page")
    36  	}
    37  	return elem.Body(
    38  		vecty.If(b.Ctx.Referrer != nil, elem.Div(
    39  			elem.Button(
    40  				vecty.Markup(event.Click(b.backButton)),
    41  				vecty.Text("Back"),
    42  			))),
    43  		mainContent,
    44  	)
    45  }
    46  
    47  func (b *Body) backButton(*vecty.Event) {
    48  	actions.Dispatch(&actions.Back{})
    49  }
    50  
    51  func (b *Body) newItem(*vecty.Event) {
    52  	actions.Dispatch(&actions.PageSelect{Page: actions.PageNewItem})
    53  }