From 0df0f93e5e79c35bde44cd4d7e57528ebcac548e Mon Sep 17 00:00:00 2001 From: Daniel Schmidt Date: Mon, 9 Sep 2024 09:55:51 +0200 Subject: [PATCH] fix: external printing api --- frontend/src/js/app.ts | 2 +- frontend/src/js/types/entry.ts | 1 + .../src/js/ui/views/extern-print/generator.ts | 4 ---- .../src/js/ui/views/template/create-entity.ts | 2 +- frontend/src/js/ui/views/template/single.ts | 3 ++- rpc/print.go | 24 ++++++++++++++----- 6 files changed, 23 insertions(+), 13 deletions(-) diff --git a/frontend/src/js/app.ts b/frontend/src/js/app.ts index e8e3120c..00e43735 100644 --- a/frontend/src/js/app.ts +++ b/frontend/src/js/app.ts @@ -75,7 +75,7 @@ store.actions.loadAll().then(() => { '/workshop/:id': WorkshopSingle, '/workshop/:id/:repo': WorkshopRepo, '/extern-print/template/:id/:json/:config': ExternPrintTemplate, - '/extern-print/generator/:id/:json/:config': ExternPrintGenerator, + '/extern-print/generator/:id/:config': ExternPrintGenerator, }); document.addEventListener('keydown', (e) => { diff --git a/frontend/src/js/types/entry.ts b/frontend/src/js/types/entry.ts index 17eecf62..4ad9ab8a 100644 --- a/frontend/src/js/types/entry.ts +++ b/frontend/src/js/types/entry.ts @@ -2,6 +2,7 @@ type Entry = { id: string; name: string; data: Record; + source?: string; }; export default Entry; diff --git a/frontend/src/js/ui/views/extern-print/generator.ts b/frontend/src/js/ui/views/extern-print/generator.ts index 09cc4a3c..e287eee5 100644 --- a/frontend/src/js/ui/views/extern-print/generator.ts +++ b/frontend/src/js/ui/views/extern-print/generator.ts @@ -28,15 +28,11 @@ export default (): m.Component => { return { oninit({ attrs }) { state.id = attrs.id; - state.json = JSON.parse(atob(attrs.json)); state.gen = attrs.gen; - // TODO: is this right? - API.exec(API.GET_GENERATOR, state.id) .then((gen) => { render(gen.printTemplate, { - it: state.json, sources: gen.dataSources, images: gen.images, config: JSON.parse(atob(attrs.config)), diff --git a/frontend/src/js/ui/views/template/create-entity.ts b/frontend/src/js/ui/views/template/create-entity.ts index 3f8446c3..21faa99d 100644 --- a/frontend/src/js/ui/views/template/create-entity.ts +++ b/frontend/src/js/ui/views/template/create-entity.ts @@ -173,7 +173,7 @@ export default (): m.Component => { state.data = initialData(state.schema); if (attrs.eid) { - API.exec(API.GET_ENTRY, attrs.id, attrs.eid) + API.exec(API.GET_ENTRY, attrs.id, atob(attrs.eid)) .then((entry) => { state.data = entry.data; state.name = entry.name; diff --git a/frontend/src/js/ui/views/template/single.ts b/frontend/src/js/ui/views/template/single.ts index d51204be..f92e9c52 100644 --- a/frontend/src/js/ui/views/template/single.ts +++ b/frontend/src/js/ui/views/template/single.ts @@ -230,7 +230,8 @@ export default (): m.Component => { size: 'sm', icon: 'create', className: '.mr2', - onClick: () => m.route.set(`/template/${buildId('template', state.template!)}/edit/${entry.id}`), + disabled: entry.source?.startsWith('ds:'), + onClick: () => m.route.set(`/template/${buildId('template', state.template!)}/edit/${btoa(entry.id)}`), }), ), m( diff --git a/rpc/print.go b/rpc/print.go index d7834d75..a197afa3 100644 --- a/rpc/print.go +++ b/rpc/print.go @@ -6,7 +6,6 @@ import ( "encoding/json" "errors" "fmt" - "github.com/BigJk/snd/rpc/bind" "image" "image/png" "io/ioutil" @@ -18,6 +17,8 @@ import ( "strings" "time" + "github.com/BigJk/snd/rpc/bind" + "github.com/BigJk/snd" "github.com/BigJk/snd/database" "github.com/patrickmn/go-cache" @@ -286,7 +287,7 @@ func RegisterPrint(route *echo.Group, extern *echo.Group, db database.Database, return nil } - bind.MustBind(route, "/printTemplate", func(id string, entry snd.Entry, config interface{}) error { + bind.MustBind(route, "/printTemplate", func(id string, entry snd.Entry, config map[string]any) error { _, err := db.GetTemplate(id) if err != nil { return err @@ -294,19 +295,30 @@ func RegisterPrint(route *echo.Group, extern *echo.Group, db database.Database, return externPrintTemplate(id, entry, config) }) - bind.MustBind(route, "/printTemplateEntry", func(id string, eid string, config interface{}) error { - _, err := db.GetTemplate(id) + bind.MustBind(route, "/printTemplateEntry", func(id string, eid string, config map[string]any) error { + tmpl, err := db.GetTemplate(id) if err != nil { return err } + ent, err := db.GetEntry(id, eid) if err != nil { + // Try to find the entry in the linked data sources + for _, dsid := range tmpl.DataSources { + if ds, err := db.GetSource(dsid); err == nil { + if ent, err = db.GetEntry(ds.ID(), eid); err == nil { + return externPrintTemplate(id, ent, config) + } + } + } + return err } + return externPrintTemplate(id, ent, config) }) - externPrintGenerator := func(genId string, config any) error { + externPrintGenerator := func(genId string, config map[string]any) error { configJson, err := json.Marshal(config) if err != nil { return err @@ -324,7 +336,7 @@ func RegisterPrint(route *echo.Group, extern *echo.Group, db database.Database, return nil } - bind.MustBind(route, "/printGenerator", func(id string, config interface{}) error { + bind.MustBind(route, "/printGenerator", func(id string, config map[string]any) error { _, err := db.GetGenerator(id) if err != nil { return err