package components
import (
"context"
"fmt"
"strings"
)
type RoleFormData struct {
Role *Role
IsNew bool
ErrorMessage string
AllPermissions []string
}
templ AdminRoleForm(ctx context.Context, data RoleFormData) {
@LayoutWithContext(getFormTitle(data.IsNew), ctx) {
if data.ErrorMessage != "" {
Error! { data.ErrorMessage }
}
}
}
// Helper functions
func getFormTitle(isNew bool) string {
if isNew {
return "New Role"
}
return "Edit Role"
}
func getFormAction(isNew bool, role *Role) templ.SafeURL {
if isNew {
return "/admin/roles"
}
return templ.SafeURL("/admin/roles/" + fmt.Sprint(role.ID))
}
func hasPermission(role *Role, permission string) bool {
if role == nil || role.Permissions == nil {
return false
}
for _, p := range role.Permissions {
if p == permission {
return true
}
}
return false
}
func formatPermissionLabel(permission string) string {
// Convert permission strings like "users.create" to "Create Users"
parts := strings.Split(permission, ".")
if len(parts) != 2 {
return permission
}
resource := strings.Title(parts[0])
action := strings.Title(parts[1])
return action + " " + resource
}