Skip to content

Commit

Permalink
fix: external printing api
Browse files Browse the repository at this point in the history
  • Loading branch information
BigJk committed Sep 9, 2024
1 parent 7aa7352 commit 0df0f93
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion frontend/src/js/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
1 change: 1 addition & 0 deletions frontend/src/js/types/entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ type Entry = {
id: string;
name: string;
data: Record<string, any>;
source?: string;
};

export default Entry;
4 changes: 0 additions & 4 deletions frontend/src/js/ui/views/extern-print/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,11 @@ export default (): m.Component<ExternPrintProps> => {
return {
oninit({ attrs }) {
state.id = attrs.id;
state.json = JSON.parse(atob(attrs.json));
state.gen = attrs.gen;

// TODO: is this right?

API.exec<Generator>(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)),
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/js/ui/views/template/create-entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ export default (): m.Component<CreateTemplateEntityProps> => {
state.data = initialData(state.schema);

if (attrs.eid) {
API.exec<Entry>(API.GET_ENTRY, attrs.id, attrs.eid)
API.exec<Entry>(API.GET_ENTRY, attrs.id, atob(attrs.eid))
.then((entry) => {
state.data = entry.data;
state.name = entry.name;
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/js/ui/views/template/single.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ export default (): m.Component<SingleTemplateProps> => {
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(
Expand Down
24 changes: 18 additions & 6 deletions rpc/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/BigJk/snd/rpc/bind"
"image"
"image/png"
"io/ioutil"
Expand All @@ -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"
Expand Down Expand Up @@ -286,27 +287,38 @@ 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
}
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
Expand All @@ -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
Expand Down

0 comments on commit 0df0f93

Please sign in to comment.