Now we have a working version without any JS. Way easier to do because javascript form submit blows hot wads.
This commit is contained in:
parent
3fd8169d83
commit
504433502a
3 changed files with 33 additions and 21 deletions
|
@ -3,8 +3,6 @@ package admin
|
|||
import (
|
||||
"maps"
|
||||
"strconv"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"zedshaw.games/webapp/data"
|
||||
. "zedshaw.games/webapp/common"
|
||||
|
@ -61,14 +59,16 @@ func GetPageSelectOne(c *fiber.Ctx) error {
|
|||
}
|
||||
|
||||
func PostApiUpdate(c *fiber.Ctx) error {
|
||||
user, err := ReceivePost[data.User](c)
|
||||
if err != nil { return IfErrNil(err, c) }
|
||||
fmt.Println("post received", user)
|
||||
table := c.Params("table")
|
||||
|
||||
err = Update(c.Params("table"), reflect.ValueOf(user).Elem())
|
||||
typeOf := data.Models()[table]
|
||||
obj, err := ReflectOnPost(typeOf, c)
|
||||
if err != nil { return IfErrNil(err, c) }
|
||||
|
||||
return c.JSON(fiber.Map{"status": "ok"})
|
||||
err = Update(table, obj.Elem())
|
||||
if err != nil { return IfErrNil(err, c) }
|
||||
|
||||
return c.RedirectBack("/admin/table/", 303)
|
||||
}
|
||||
|
||||
func PutApiInsert(c *fiber.Ctx) error {
|
||||
|
|
|
@ -2,6 +2,7 @@ package common
|
|||
|
||||
import (
|
||||
"log"
|
||||
"reflect"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/go-playground/validator/v10"
|
||||
)
|
||||
|
@ -40,3 +41,26 @@ func ReceivePost[T any](c *fiber.Ctx) (*T, error) {
|
|||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func ReflectOnPost(typeOf reflect.Type, c *fiber.Ctx) (reflect.Value, error) {
|
||||
var result_val reflect.Value
|
||||
|
||||
result_val = reflect.New(typeOf)
|
||||
result := result_val.Interface()
|
||||
|
||||
if err := c.BodyParser(result); err != nil {
|
||||
log.Println(err);
|
||||
return result_val, err
|
||||
}
|
||||
|
||||
var validate *validator.Validate
|
||||
validate = validator.New(validator.WithRequiredStructEnabled())
|
||||
|
||||
if err := validate.Struct(result); err != nil {
|
||||
validationErrors := err.(validator.ValidationErrors)
|
||||
log.Println(validationErrors)
|
||||
return result_val, err
|
||||
}
|
||||
|
||||
return result_val, nil
|
||||
}
|
||||
|
|
|
@ -1,17 +1,5 @@
|
|||
<script>
|
||||
let Data = new GetJson("/api/admin/table/{{ .Table }}/{{ .Id }}/");
|
||||
|
||||
const PostForm = async (url, data) => {
|
||||
console.log("DATA", JSON.stringify(data));
|
||||
return await fetch(url, {
|
||||
method: "POST",
|
||||
mode: "same-origin",
|
||||
body: JSON.stringify(data),
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
});
|
||||
}
|
||||
</script>
|
||||
|
||||
<blockstart>
|
||||
|
@ -25,14 +13,14 @@
|
|||
<template x-for="(value, key) in item">
|
||||
<div>
|
||||
<label x-text="key" x-bind:for="key"></label>
|
||||
<input x-text="value" x-model="item[key]" x-bind:id="key" />
|
||||
<input x-bind:name="key" x-text="value" x-model="item[key]" x-bind:id="key" />
|
||||
</div>
|
||||
</template>
|
||||
</middle>
|
||||
<bottom>
|
||||
<button-group>
|
||||
<button type="button">Back</button>
|
||||
<button type="button" @click.prevent="PostForm('/api/admin/table/{{ .Table }}/{{ .Id }}/', item)">Update</button>
|
||||
<button type="submit">Update</button>
|
||||
<button type="button" @click.prevent="console.log('DELETE', item.id)">Delete</button>
|
||||
</button-group>
|
||||
</bottom>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue