Skip to content

Commit

Permalink
add profile viewing and editing
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanfaerman committed Jan 29, 2024
1 parent 199a10c commit 3225d57
Show file tree
Hide file tree
Showing 5 changed files with 647 additions and 25 deletions.
92 changes: 76 additions & 16 deletions internal/handlers/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handlers

import (
"net/http"
"strings"

"github.com/go-chi/chi"

Expand All @@ -19,14 +20,17 @@ func init() {
}

func (h account) Routes(r chi.Router) {

r.Use(services.Session.Middleware)
r.Group(func(r chi.Router) {
r.Use(middleware.HTMXOnly)
// TODO: add an authenticated only middleware
//
r.Post(named.Route("account-setup-apply", "/account/setup"), h.Setup)
})

r.Get(named.Route("account-profile", "/profile/{callsign}"), h.Show)
r.Get(named.Route("account-edit", "/profile/{callsign}/edit"), h.Edit)
r.Post(named.Route("account-edit-save", "/profile/{callsign}/edit/-/save"), h.Update)
}

func (h account) Setup(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -54,24 +58,10 @@ func (h account) Setup(w http.ResponseWriter, r *http.Request) {
return
}

// callsign, err := hamdb.Lookup(r.Context(), input.Callsign)
// if err != nil { ,
// inputErrs.Callsign = "Could not validate callsign"
// if err != hamdb.ErrNotFound {
// global.log.Error("hamdb lookup failed", "error", err)
// }
//
// ctx := services.CSRF.GetContext(r.Context(), r)
//
// (views.Dashboard{}).SetupaccountWithErrors(input, inputErrs).Render(ctx, w)
// return
// }
// spew.Dump(callsign)

account, err := services.Session.GetAccount(r.Context())
if err != nil {
global.log.Error("unable to get account from session", "error", err)
//views.Errors{}.Internal().Render(r.Context(), w)
// views.Errors{}.Internal().Render(r.Context(), w)
return
}
if err := services.Account.Setup(r.Context(), account.ID, input.Name, input.Callsign); err != nil {
Expand All @@ -95,3 +85,73 @@ func (h account) Setup(w http.ResponseWriter, r *http.Request) {

// TODO: update the profile, kick back errors if there are any
}

func (h account) Show(w http.ResponseWriter, r *http.Request) {
callsign := chi.URLParam(r, "callsign")
ctx := services.CSRF.GetContext(r.Context(), r)

a, err := services.Account.FindByCallsign(ctx, callsign)
if err != nil {
ErrorHandler(err)(w, r)
return
}

v := views.Account{
Account: a,
}
v.Profile().Render(ctx, w)
}

func (h account) Edit(w http.ResponseWriter, r *http.Request) {
callsign := chi.URLParam(r, "callsign")
ctx := services.CSRF.GetContext(r.Context(), r)
a, err := services.Account.FindByCallsign(ctx, callsign)
if err != nil {
ErrorHandler(err)(w, r)
return
}
v := views.Account{
Account: a,
}
v.Edit().Render(ctx, w)
}

func (h account) Update(w http.ResponseWriter, r *http.Request) {
r.ParseForm()

callsign := chi.URLParam(r, "callsign")
ctx := services.CSRF.GetContext(r.Context(), r)
a, err := services.Account.FindByCallsign(ctx, callsign)
if err != nil {
ErrorHandler(err)(w, r)
return
}
v := views.Account{
Account: a,
}

inputErrs := views.AccountEditFormErrors{}
input := views.AccountEditFormInput{
Name: strings.TrimSpace(r.Form.Get("name")),
}
a.Name = input.Name
err = services.Account.Update(r.Context(), a)
if err != nil {
if errs, ok := err.(services.ValidationError); ok {
for field, e := range errs {
switch field {
case "Account.Name":
inputErrs.Name = e
}
}
v.EditFormWithErrors(input, inputErrs).Render(ctx, w)
return
}
ErrorHandler(err)(w, r)
return
}
// TODO: return this from the account.update method
v.Account = a

v.EditForm().Render(ctx, w)
}
14 changes: 13 additions & 1 deletion internal/models/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const (
type Account struct {
ID int64

Name string
Name string `validate:"required"`
Kind AccountKind

CreatedAt time.Time
Expand Down Expand Up @@ -122,6 +122,18 @@ func (u *Account) Callsigns() ([]Callsign, error) {
return callsigns, nil
}

func (m *Account) Callsign() Callsign {
calls, err := m.Callsigns()
if err != nil {
return Callsign{}
}
if len(calls) == 0 {
return Callsign{}
}

return calls[0]
}

var (
ErrAccountNeedsCallsign = errors.New("user needs a callsign")
ErrAccountNeedsName = errors.New("user needs a name")
Expand Down
22 changes: 14 additions & 8 deletions internal/services/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func (account) FindByEmail(ctx context.Context, email string) (*models.Account,
return models.FindAccountByEmail(ctx, email)
}

func (account) FindByCallsign(ctx context.Context, callsign string) (*models.Account, error) {
return models.FindAccountByCallsign(ctx, callsign)
}

func (s account) CreateWithEmail(ctx context.Context, email string) (*models.Account, error) {
if u, err := s.FindByEmail(ctx, email); err != nil {
if err != sql.ErrNoRows {
Expand Down Expand Up @@ -67,7 +71,6 @@ var (
)

func (s account) Setup(ctx context.Context, id int64, name, callsign string) error {

account, err := global.dao.UpdateAccount(ctx, dao.UpdateAccountParams{
ID: id,
Name: name,
Expand All @@ -85,7 +88,6 @@ func (s account) Setup(ctx context.Context, id int64, name, callsign string) err
if account.ID != accountCallsign.ID {
return ErrAccountSetupCallsignTaken
}

}

fccCallsign, err := hamdb.Lookup(ctx, callsign)
Expand Down Expand Up @@ -144,9 +146,13 @@ func (s account) Setup(ctx context.Context, id int64, name, callsign string) err
return tx.Commit()
}

// func (s account) AddCallsign(ctx context.Context, id int64, callsign string) error {
// return global.dao.AddCallsignForaccount(ctx, dao.AddCallsignForaccountParams{
// accountID: id,
// Callsign: callsign,
// })
// }
func (s account) Update(ctx context.Context, m *models.Account) error {
if err := Validation.Apply(m); err != nil {
return err
}
_, err := global.dao.UpdateAccount(ctx, dao.UpdateAccountParams{
ID: m.ID,
Name: m.Name,
})
return err
}
131 changes: 131 additions & 0 deletions internal/views/account.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package views

import "github.com/ryanfaerman/netctl/internal/models"
import "github.com/ryanfaerman/netctl/web/named"

type Account struct {
Account *models.Account
}

templ (v Account) Profile() {
@Page() {
<header class="app-header">
@v.LocalBar()
</header>
<div class="grid-container split">
<div>
"hello there { v.Account.Name }!"
</div>
<div>
<!-- sidebar -->
<a href={ templ.URL(named.URLFor("account-edit", v.Account.Callsign().Call)) } class="button">
Edit Profile
</a>
</div>
</div>
}
}

templ (v Account) Edit() {
@Page() {
<header class="app-header">
@v.LocalBar()
</header>
<div class="grid-container split">
<div>
"Editing { v.Account.Name }!"
@v.EditForm()
</div>
<div>
<!-- sidebar -->
<a href={ templ.URL(named.URLFor("account-profile", v.Account.Callsign().Call)) } class="button">
View Profile
</a>
</div>
</div>
}
}

type AccountEditFormInput struct {
Name string
About string
}

type AccountEditFormErrors struct {
Name string
About string
}

templ (v Account) EditForm() {
@v.EditFormWithErrors(
AccountEditFormInput{
Name: v.Account.Name,
},
AccountEditFormErrors{},
)
}

templ (v Account) EditFormWithErrors(input AccountEditFormInput, inputErrs AccountEditFormErrors) {
@Form("edit-account", FormAttrs{
Action: named.URLFor("account-edit-save", v.Account.Callsign().Call),
}) {
@InputText("name", InputAttrs{
Label: "Name",
Value: input.Name,
Error: inputErrs.Name,
HelpText: "Your name as you'd like to be called on the air",
})
@InputSubmit(InputAttrs{
Value: "Save",
})
}
}

templ (v Account) LocalBar() {
<div class="local-bar">
<nav>
<ul>
<li class="app-name">
<a href="/">
@Icon("tower-cell")
Net Control
</a>
</li>
<li>
<a href="#" class="tab">
@Icon("file-lines")
Preamble
</a>
</li>
<li>
<a href="#" class="tab active">
@Icon("list-check")
Check Ins
</a>
</li>
<li>
<a href="#" class="tab">
@Icon("receipt")
Reports
</a>
</li>
<li>
<a href="#" class="tab">
@Icon("sliders")
Settings
</a>
</li>
</ul>
</nav>
<nav>
<ul>
<li>
<a href="#" class="tab">
@Icon("user")
Account
</a>
</li>
</ul>
</nav>
</div>
}
Loading

0 comments on commit 3225d57

Please sign in to comment.