Redmage/server/routes/device_validate_name.go

68 lines
2 KiB
Go
Raw Normal View History

2024-05-07 20:51:00 +07:00
package routes
import (
"net/http"
"github.com/gosimple/slug"
"github.com/tigorlazuardi/redmage/pkg/errs"
"github.com/tigorlazuardi/redmage/pkg/log"
"github.com/tigorlazuardi/redmage/views/devicesview/adddevice"
)
func (routes *Routes) DevicesValidateNameHTMX(rw http.ResponseWriter, req *http.Request) {
ctx, span := tracer.Start(req.Context(), "*Routes.ValidateName")
defer span.End()
var nameData adddevice.NameInputData
nameData.Value = req.FormValue("name")
nameComponent := adddevice.NameInput(nameData)
s := req.FormValue("slug")
if s != "" || nameData.Value == "" {
if err := nameComponent.Render(ctx, rw); err != nil {
log.New(ctx).Err(err).Error("failed to render name input")
}
return
}
s = slug.Make(nameData.Value)
slugData := adddevice.SlugInputData{
Value: s,
HXSwapOOB: true,
}
if s == "" {
slugData.Error = "Identifier resolves to empty value. Please set a valid identifier manually."
_ = nameComponent.Render(ctx, rw)
if err := adddevice.SlugInput(slugData).Render(ctx, rw); err != nil {
log.New(ctx).Err(err).Error("failed to render name input")
}
return
}
2024-05-07 20:51:00 +07:00
exist, err := routes.API.DevicesValidateSlug(ctx, s)
if err != nil {
log.New(ctx).Err(err).Error("failed to validate slug")
_, message := errs.HTTPMessage(err)
slugData.Error = message
_ = nameComponent.Render(ctx, rw)
if err := adddevice.SlugInput(slugData).Render(ctx, rw); err != nil {
log.New(ctx).Err(err).Error("failed to render name input")
}
}
if exist {
slugData.Error = "Device with this identifier already exist. Please change the value manually."
_ = nameComponent.Render(ctx, rw)
if err := adddevice.SlugInput(slugData).Render(ctx, rw); err != nil {
log.New(ctx).Err(err).Error("failed to render name input")
}
return
}
slugData.Valid = "Identifier is available."
2024-05-07 20:51:00 +07:00
_ = nameComponent.Render(ctx, rw)
if err := adddevice.SlugInput(slugData).Render(ctx, rw); err != nil {
log.New(ctx).Err(err).Error("failed to render name input")
}
}