I have an action which I've defined on a component. The component code looks like this:
package components
import (
"log"
"net/http"
"github.com/gin-gonic/gin"
"github.com/yuriizinets/go-ssc"
"github.com/kodah/blog/controller"
"github.com/kodah/blog/service"
)
type SignIn struct {
Context *gin.Context
Username string
Password string
Errors []string
}
func (c *SignIn) Actions() ssc.ActionsMap {
return ssc.ActionsMap{
"DoSignIn": func(args ...interface{}) {
var configService service.ConfigService = service.ConfigurationService("")
if configService.Error() != nil {
log.Printf("Error while connecting to DB service. error=%s", configService.Error())
c.Errors = append(c.Errors, "Internal server error")
return
}
log.Printf("Triggered sign in")
var dbService service.DBService = service.SQLiteDBService("")
if dbService.Error() != nil {
log.Printf("Error while connecting to DB service. error=%s", dbService.Error())
c.Errors = append(c.Errors, "Internal server error")
return
}
var loginService service.LoginService = service.DynamicLoginService(dbService)
var jwtService service.JWTService = service.JWTAuthService()
var loginController controller.LoginController = controller.LoginHandler(loginService, jwtService)
c.Context.Set("Username", c.Username)
c.Context.Set("Password", c.Password)
session := service.NewSessionService(c.Context, false)
token := loginController.Login(c.Context)
session.Set("token", token)
err := session.Save()
if err != nil {
log.Printf("Error while saving session. error=%s", err)
}
log.Printf("Login successful. user=%s", c.Username)
c.Context.Redirect(http.StatusFound, "/")
},
}
}
The template:
{{ define "SignIn" }}
<div {{ componentattrs . }}>
<h1 class="title is-4">Sign in</h1>
<p id="errorFeedback" class="help has-text-danger is-hidden">
{{ .Username }} {{ .Password }}
</p>
<div class="field">
<div class="control">
<input class="input is-medium" value="{{ .Username }}" oninput="{{ bind `Username` }}" type="text" placeholder="username">
</div>
</div>
<div class="field">
<div class="control">
<input class="input is-medium" value="{{ .Password }}" oninput="{{ bind `Password` }}" type="password" placeholder="password">
</div>
</div>
<button onclick="{{ action `DoSignIn` `{}` }}" class="button is-block is-primary is-fullwidth is-medium">Submit</button>
<br />
<small><em>Be nice to the auth system.</em></small>
</div>
{{ end }}
and is included like this:
<div class="column sign-in has-text-centered">
{{ template "SignIn" .SignInComponent }}
</div>
Where the component inclusion looks like this:
package frontend
import (
"html/template"
"github.com/gin-gonic/gin"
"github.com/yuriizinets/go-ssc"
"github.com/kodah/blog/frontend/components"
)
type PageSignIn struct {
ctx *gin.Context
SignInComponent ssc.Component
}
func (p *PageSignIn) Template() *template.Template {
return template.Must(template.New("page.signin.html").Funcs(ssc.Funcs()).ParseGlob("frontend/new_templates/*/*.html"))
}
func (p *PageSignIn) Init() {
p.SignInComponent = ssc.RegC(p, &components.SignIn{
Context: p.ctx,
})
return
}
func (*PageSignIn) Meta() ssc.Meta {
return ssc.Meta{
Title: "Test kodah's blog",
Description: "Test description",
Canonical: "",
Hreflangs: nil,
Additional: nil,
}
}
When I run the DoSignIn
action it is not executed though;
<button onclick="{{ action `DoSignIn` `{}` }}" class="button is-block is-primary is-fullwidth is-medium">Submit</button>
I realize there's not a lot of documentation but I went off of the examples and this seems right.
question