From 7a8fa001e469c808be44bd7e2bca51bce02657fd Mon Sep 17 00:00:00 2001 From: Dalia Date: Wed, 22 May 2024 18:44:36 +0200 Subject: [PATCH] build the application on the object form submission --- plugin-manifest.json | 2 +- plugins/form-submit/index.js | 44 +++++++++++++++++++ plugins/index.js | 10 +++-- .../{deply-netlify => sidebar-panel}/index.js | 0 .../style/style.css | 0 5 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 plugins/form-submit/index.js rename plugins/{deply-netlify => sidebar-panel}/index.js (100%) rename plugins/{deply-netlify => sidebar-panel}/style/style.css (100%) diff --git a/plugin-manifest.json b/plugin-manifest.json index 4d21e9f..e0d2cf8 100644 --- a/plugin-manifest.json +++ b/plugin-manifest.json @@ -2,7 +2,7 @@ "id": "flotiq.deploy-netlify", "name": "Deploy Netlify", "description": "Integration with Netlify", - "version": "1.0.0", + "version": "1.1.0", "repository": "https://github.com/flotiq/flotiq-ui-plugin-deploy-netlify", "url": "https://localhost:3053/index.js", "permissions": [] diff --git a/plugins/form-submit/index.js b/plugins/form-submit/index.js new file mode 100644 index 0000000..3ed2f88 --- /dev/null +++ b/plugins/form-submit/index.js @@ -0,0 +1,44 @@ +export const handleAfterSubmitPlugin = ( + { success, contentObject }, + toast, + getPluginSettings, +) => { + const ctdName = contentObject?.internal?.contentType; + const settings = getPluginSettings(); + + if (!success || !ctdName || !settings) return; + + const settingsForCtd = JSON.parse(settings)?.builds?.filter( + (plugin) => + plugin.content_types.length === 0 || + plugin.content_types.find((ctd) => ctd === ctdName), + ); + + if (!settingsForCtd.length) return null; + + settingsForCtd.map((item) => + fetch(item.build_webhook_url, { + method: `POST`, + body: '{}', + headers: { + 'content-type': 'application/json;charset=UTF-8', + }, + }) + .then(async ({ ok, status }) => { + if (!ok) + throw Error( + `Failed to fetch Netlify build URL: ${item.build_instance_url}. Status: ${status}`, + ); + }) + .catch((error) => { + console.log(error); + if (error.message) { + toast.error(error.message); + } else { + toast.error( + `Failed to fetch Netlify build URL: ${item.build_instance_url}`, + ); + } + }), + ); +}; diff --git a/plugins/index.js b/plugins/index.js index 6484eb5..a88e054 100644 --- a/plugins/index.js +++ b/plugins/index.js @@ -1,10 +1,11 @@ import { registerFn } from '../common/plugin-element-cache'; import pluginInfo from '../plugin-manifest.json'; -import { handlePanelPlugin } from './deply-netlify'; +import cssString from 'inline:./sidebar-panel/style/style.css'; +import { handlePanelPlugin } from './sidebar-panel'; import { handleManagePlugin } from './manage'; -import cssString from 'inline:./deply-netlify/style/style.css'; +import { handleAfterSubmitPlugin } from './form-submit'; -registerFn(pluginInfo, (handler) => { +registerFn(pluginInfo, (handler, _, { toast, getPluginSettings }) => { if (!document.getElementById(`${pluginInfo.id}-styles`)) { const style = document.createElement('style'); style.id = `${pluginInfo.id}-styles`; @@ -18,4 +19,7 @@ registerFn(pluginInfo, (handler) => { handler.on('flotiq.form.sidebar-panel::add', (data) => handlePanelPlugin(data, pluginInfo), ); + handler.on('flotiq.form::after-submit', (data) => + handleAfterSubmitPlugin(data, toast, getPluginSettings), + ); }); diff --git a/plugins/deply-netlify/index.js b/plugins/sidebar-panel/index.js similarity index 100% rename from plugins/deply-netlify/index.js rename to plugins/sidebar-panel/index.js diff --git a/plugins/deply-netlify/style/style.css b/plugins/sidebar-panel/style/style.css similarity index 100% rename from plugins/deply-netlify/style/style.css rename to plugins/sidebar-panel/style/style.css