Skip to content

Commit 2bd4acb

Browse files
authored
Merge pull request #5 from flotiq/feature/maciejl/24765
ref #24765 - update "Go to page" link with parsed key, add disabled on create new item, add "build on save" configuration, update on submit, fix multiple instance message, button loader
2 parents f3e2b55 + 1a32bc2 commit 2bd4acb

File tree

18 files changed

+478
-287
lines changed

18 files changed

+478
-287
lines changed

.docs/images/netlify_plugin.png

-46.6 KB
Loading

.vscode/extensions.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"recommendations": [
3+
// code quality plugins
4+
"dbaeumer.vscode-eslint",
5+
"esbenp.prettier-vscode",
6+
7+
// plugin that helps with html strings in js files
8+
"tobermory.es6-string-html"
9+
]
10+
}

common/plugin-element-cache.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ export const addElementToCache = (element, key, data = {}) => {
66
data,
77
};
88

9-
setTimeout(
10-
() =>
11-
element.addEventListener('flotiq.detached', () => {
12-
delete appRoots[key];
13-
}),
14-
50,
15-
);
9+
element.addEventListener('flotiq.detached', () => {
10+
setTimeout(() => {
11+
return delete appRoots[key];
12+
}, 50);
13+
});
1614
};
1715

1816
export const getCachedElement = (key) => {

common/plugin-helpers.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Read key value deep inside object
3+
* @param {string} key
4+
* @param {object} object
5+
* @returns {*} example: read 'object[0].key' from 'object: [{key: value}]
6+
*/
7+
export const deepReadKeyValue = (key, object) => {
8+
return key
9+
.split(/[[.\]]/)
10+
.filter((kp) => !!kp)
11+
.reduce((nestedOptions, keyPart) => {
12+
return nestedOptions?.[keyPart];
13+
}, object);
14+
};
15+
16+
/**
17+
* Generate string based on tempalte and object with values
18+
*
19+
* @param {string} value - template string with keys in {{}}
20+
* @param {*} object - object with values
21+
* @returns {string}
22+
*/
23+
export const getKeyPattern = (value, object) => {
24+
return value.replace(/{(?<key>[^{}]+)}/g, (...params) => {
25+
const { key } = params[4];
26+
return deepReadKeyValue(key, object) || '';
27+
});
28+
};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
},
1818
"dependencies": {
1919
"chalk": "^5.3.0",
20-
"esbuild-plugin-copy": "^2.1.1"
20+
"esbuild-plugin-copy": "^2.1.1",
21+
"semver": "^7.6.2"
2122
}
2223
}

plugin-manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"id": "flotiq.deploy-netlify",
33
"name": "Deploy Netlify",
44
"description": "Integration with Netlify deploy. With this plugin, you can easily trigger Netlify builds within Content Object changes on form submission. You will also be able to quickly navigate to your Netlify pages from the Flotiq content editor.",
5-
"version": "1.1.2",
5+
"version": "1.1.3",
66
"repository": "https://github.com/flotiq/flotiq-ui-plugin-deploy-netlify",
77
"url": "https://localhost:3053/index.js",
88
"permissions": []

plugins/build-handler.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { getKeyPattern } from '../common/plugin-helpers';
2+
3+
/**
4+
*
5+
* @param {object} buttonSettings
6+
* @param {*} contentObject
7+
* @param {string} id
8+
* @returns
9+
*/
10+
export const onBuildHandler = (buttonSettings, contentObject, id) => {
11+
const buildWebhookURL = getKeyPattern(
12+
buttonSettings?.build_webhook_url,
13+
contentObject,
14+
);
15+
const buildInstance = getKeyPattern(
16+
buttonSettings?.build_instance_url,
17+
contentObject,
18+
);
19+
const buttonId = `${id}-button`;
20+
const statusBoxId = `${id}-status`;
21+
22+
const statusMessageContainer = document.getElementById(statusBoxId);
23+
const buttonElement = document.getElementById(buttonId);
24+
25+
const updateStatus = (message) => {
26+
statusMessageContainer.innerHTML = message;
27+
};
28+
29+
const pluginLink = /* html */ `
30+
<a
31+
class="plugin-deploy-netlify__link"
32+
href="${buildInstance}"
33+
target="_blank">
34+
Go to page: ${buildInstance}
35+
</a>
36+
`;
37+
38+
if (!buildWebhookURL) {
39+
updateStatus(pluginLink);
40+
return;
41+
} else {
42+
updateStatus('Updating preview link...');
43+
}
44+
45+
buttonElement.disabled = true;
46+
buttonElement.classList.add('plugin-deploy-netlify__button--loading');
47+
return fetch(buildWebhookURL, {
48+
mode: 'no-cors',
49+
method: 'POST',
50+
body: JSON.stringify(contentObject),
51+
headers: {
52+
'content-type': 'application/json;charset=UTF-8',
53+
},
54+
})
55+
.then(() => updateStatus(pluginLink))
56+
.catch((error) => {
57+
if (error.message) {
58+
updateStatus(error.message);
59+
} else {
60+
updateStatus('Failed to fetch');
61+
}
62+
})
63+
.finally(() => {
64+
buttonElement.disabled = false;
65+
buttonElement.classList.remove('plugin-deploy-netlify__button--loading');
66+
});
67+
};

plugins/form-submit/index.js

Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,29 @@
1-
export const handleAfterSubmitPlugin = (
2-
{ success, contentObject },
3-
toast,
4-
getPluginSettings,
5-
) => {
1+
import { onBuildHandler } from '../build-handler';
2+
3+
export const handleAfterSubmitPlugin = (data, getPluginSettings) => {
4+
const { success, contentObject } = data;
65
const ctdName = contentObject?.internal?.contentType;
7-
const settings = getPluginSettings();
6+
const netlifySettings = getPluginSettings();
7+
8+
if (!success || !ctdName || !netlifySettings) return;
89

9-
if (!success || !ctdName || !settings) return;
10+
const settings = JSON.parse(netlifySettings);
1011

11-
const settingsForCtd = JSON.parse(settings)?.builds?.filter(
12-
(plugin) =>
13-
plugin.content_types.length === 0 ||
14-
plugin.content_types.find((ctd) => ctd === ctdName),
15-
);
12+
const settingsForCtd = settings?.builds
13+
?.filter(
14+
(buttonSettings) =>
15+
buttonSettings.content_types.length === 0 ||
16+
buttonSettings.content_types.find((ctd) => ctd === ctdName),
17+
)
18+
.filter((buttonSettings) => buttonSettings.build_on_save);
1619

17-
if (!settingsForCtd.length) return null;
20+
if (!settingsForCtd.length) return;
1821

19-
settingsForCtd.map((item) =>
20-
fetch(item.build_webhook_url, {
21-
method: `POST`,
22-
body: '{}',
23-
headers: {
24-
'content-type': 'application/json;charset=UTF-8',
25-
},
26-
})
27-
.then(async ({ ok, status }) => {
28-
if (!ok)
29-
throw Error(
30-
`Failed to fetch Netlify build URL: ${item.build_instance_url}. Status: ${status}`,
31-
);
32-
})
33-
.catch((error) => {
34-
console.log(error);
35-
if (error.message) {
36-
toast.error(error.message);
37-
} else {
38-
toast.error(
39-
`Failed to fetch Netlify build URL: ${item.build_instance_url}`,
40-
);
41-
}
42-
}),
43-
);
22+
settingsForCtd.forEach((buttonSettings, index) => {
23+
onBuildHandler(
24+
buttonSettings,
25+
contentObject,
26+
`netlify-item-child-${index}`,
27+
);
28+
});
4429
};

plugins/img/netlify-logo-black.png

82.8 KB
Loading

plugins/img/netlify-logo-white.png

56.2 KB
Loading

0 commit comments

Comments
 (0)