From 979b2e316dc0c83d4c64bfd4f473dc081f375b87 Mon Sep 17 00:00:00 2001 From: Yvo Brevoort Date: Sat, 16 Mar 2024 13:55:03 +0100 Subject: [PATCH] attempt to create a 'fetch from remote' flow working --- .../actions/syncRemoteComponent.js | 36 +++++++++++ .../commands/syncRemoteComponent.js | 3 + .../componentTemplates/componentRemote.html | 14 +++++ .../components/component-remote/meta.json | 1 + www/api/data/generated.html | 59 +++++++++++++++++++ .../pageTemplates/Edit base component.html | 1 + .../pageTemplates/Edit component.html | 1 + .../pageTemplates/Edit page frame.html | 1 + .../pages/page/pageTemplates/Edit page.html | 1 + 9 files changed, 117 insertions(+) create mode 100644 www/api/data/components/component-remote/actions/syncRemoteComponent.js create mode 100644 www/api/data/components/component-remote/commands/syncRemoteComponent.js create mode 100644 www/api/data/components/component-remote/componentTemplates/componentRemote.html create mode 100644 www/api/data/components/component-remote/meta.json diff --git a/www/api/data/components/component-remote/actions/syncRemoteComponent.js b/www/api/data/components/component-remote/actions/syncRemoteComponent.js new file mode 100644 index 0000000..5bd8488 --- /dev/null +++ b/www/api/data/components/component-remote/actions/syncRemoteComponent.js @@ -0,0 +1,36 @@ +function(remote) { + let original = { + "url" : simplyRawApi.url, + "headers" : simplyRawApi.headers + }; + simplyRawApi.url = remote.url; + simplyRawApi.headers = { + "accept" : "application/json" + }; + let fetchFunctions = { + "base-component" : simplyDataApi.getBaseComponent, + "component" : simplyDataApi.getComponent, + "page" : simplyDataApi.getPage, + "page-frame" : simplyDataApi.getPageFrame + }; + + fetchFunctions[remote.type](remote.component) + .then(function(parts) { + parts.forEach(function(part) { + if (part.id == "meta") { + return; + } + editor.pageData.component.parts[part.id] = JSON.parse(part.contents); + }); + }) + .then(function() { + for (i in original) { + simplyRawApi[i] = original[i]; + } + }) + .catch(function() { + for (i in original) { + simplyRawApi[i] = original[i]; + } + }); +} \ No newline at end of file diff --git a/www/api/data/components/component-remote/commands/syncRemoteComponent.js b/www/api/data/components/component-remote/commands/syncRemoteComponent.js new file mode 100644 index 0000000..2709058 --- /dev/null +++ b/www/api/data/components/component-remote/commands/syncRemoteComponent.js @@ -0,0 +1,3 @@ +function(el) { + return simplyApp.actions.syncRemoteComponent(editor.pageData.component.remote); +} \ No newline at end of file diff --git a/www/api/data/components/component-remote/componentTemplates/componentRemote.html b/www/api/data/components/component-remote/componentTemplates/componentRemote.html new file mode 100644 index 0000000..903bc39 --- /dev/null +++ b/www/api/data/components/component-remote/componentTemplates/componentRemote.html @@ -0,0 +1,14 @@ +
+ + Remote API URL + + + +
+ +
\ No newline at end of file diff --git a/www/api/data/components/component-remote/meta.json b/www/api/data/components/component-remote/meta.json new file mode 100644 index 0000000..6840e1a --- /dev/null +++ b/www/api/data/components/component-remote/meta.json @@ -0,0 +1 @@ +{"id":"component-remote","description":"","remoteURL":"","remote":{"url":"","type":"base-component","component":""}} \ No newline at end of file diff --git a/www/api/data/generated.html b/www/api/data/generated.html index 793fe33..982d80a 100644 --- a/www/api/data/generated.html +++ b/www/api/data/generated.html @@ -1256,6 +1256,42 @@ return 'name'; } }, + "syncRemoteComponent" : function(remote) { + let original = { + "url" : simplyRawApi.url, + "headers" : simplyRawApi.headers + }; + simplyRawApi.url = remote.url; + simplyRawApi.headers = { + "accept" : "application/json" + }; + let fetchFunctions = { + "base-component" : simplyDataApi.getBaseComponent, + "component" : simplyDataApi.getComponent, + "page" : simplyDataApi.getPage, + "page-frame" : simplyDataApi.getPageFrame + }; + + fetchFunctions[remote.type](remote.component) + .then(function(parts) { + parts.forEach(function(part) { + if (part.id == "meta") { + return; + } + editor.pageData.component.parts[part.id] = JSON.parse(part.contents); + }); + }) + .then(function() { + for (i in original) { + simplyRawApi[i] = original[i]; + } + }) + .catch(function() { + for (i in original) { + simplyRawApi[i] = original[i]; + } + }); + }, "browse" : function(path) { return simplyDataApi.browse(path) .catch(function(error) { @@ -1691,6 +1727,9 @@ "addRawApiMethod" : function(el) { editor.pageData.component.parts.rawApi.push({}); }, + "syncRemoteComponent" : function(el) { + return simplyApp.actions.syncRemoteComponent(editor.pageData.component.remote); + }, "addRoute" : function(el) { editor.pageData.component.parts.routes.push({}); }, @@ -2959,6 +2998,22 @@ +