-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
18 changed files
with
478 additions
and
287 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"recommendations": [ | ||
// code quality plugins | ||
"dbaeumer.vscode-eslint", | ||
"esbenp.prettier-vscode", | ||
|
||
// plugin that helps with html strings in js files | ||
"tobermory.es6-string-html" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* Read key value deep inside object | ||
* @param {string} key | ||
* @param {object} object | ||
* @returns {*} example: read 'object[0].key' from 'object: [{key: value}] | ||
*/ | ||
export const deepReadKeyValue = (key, object) => { | ||
return key | ||
.split(/[[.\]]/) | ||
.filter((kp) => !!kp) | ||
.reduce((nestedOptions, keyPart) => { | ||
return nestedOptions?.[keyPart]; | ||
}, object); | ||
}; | ||
|
||
/** | ||
* Generate string based on tempalte and object with values | ||
* | ||
* @param {string} value - template string with keys in {{}} | ||
* @param {*} object - object with values | ||
* @returns {string} | ||
*/ | ||
export const getKeyPattern = (value, object) => { | ||
return value.replace(/{(?<key>[^{}]+)}/g, (...params) => { | ||
const { key } = params[4]; | ||
return deepReadKeyValue(key, object) || ''; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { getKeyPattern } from '../common/plugin-helpers'; | ||
|
||
/** | ||
* | ||
* @param {object} buttonSettings | ||
* @param {*} contentObject | ||
* @param {string} id | ||
* @returns | ||
*/ | ||
export const onBuildHandler = (buttonSettings, contentObject, id) => { | ||
const buildWebhookURL = getKeyPattern( | ||
buttonSettings?.build_webhook_url, | ||
contentObject, | ||
); | ||
const buildInstance = getKeyPattern( | ||
buttonSettings?.build_instance_url, | ||
contentObject, | ||
); | ||
const buttonId = `${id}-button`; | ||
const statusBoxId = `${id}-status`; | ||
|
||
const statusMessageContainer = document.getElementById(statusBoxId); | ||
const buttonElement = document.getElementById(buttonId); | ||
|
||
const updateStatus = (message) => { | ||
statusMessageContainer.innerHTML = message; | ||
}; | ||
|
||
const pluginLink = /* html */ ` | ||
<a | ||
class="plugin-deploy-netlify__link" | ||
href="${buildInstance}" | ||
target="_blank"> | ||
Go to page: ${buildInstance} | ||
</a> | ||
`; | ||
|
||
if (!buildWebhookURL) { | ||
updateStatus(pluginLink); | ||
return; | ||
} else { | ||
updateStatus('Updating preview link...'); | ||
} | ||
|
||
buttonElement.disabled = true; | ||
buttonElement.classList.add('plugin-deploy-netlify__button--loading'); | ||
return fetch(buildWebhookURL, { | ||
mode: 'no-cors', | ||
method: 'POST', | ||
body: JSON.stringify(contentObject), | ||
headers: { | ||
'content-type': 'application/json;charset=UTF-8', | ||
}, | ||
}) | ||
.then(() => updateStatus(pluginLink)) | ||
.catch((error) => { | ||
if (error.message) { | ||
updateStatus(error.message); | ||
} else { | ||
updateStatus('Failed to fetch'); | ||
} | ||
}) | ||
.finally(() => { | ||
buttonElement.disabled = false; | ||
buttonElement.classList.remove('plugin-deploy-netlify__button--loading'); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,29 @@ | ||
export const handleAfterSubmitPlugin = ( | ||
{ success, contentObject }, | ||
toast, | ||
getPluginSettings, | ||
) => { | ||
import { onBuildHandler } from '../build-handler'; | ||
|
||
export const handleAfterSubmitPlugin = (data, getPluginSettings) => { | ||
const { success, contentObject } = data; | ||
const ctdName = contentObject?.internal?.contentType; | ||
const settings = getPluginSettings(); | ||
const netlifySettings = getPluginSettings(); | ||
|
||
if (!success || !ctdName || !netlifySettings) return; | ||
|
||
if (!success || !ctdName || !settings) return; | ||
const settings = JSON.parse(netlifySettings); | ||
|
||
const settingsForCtd = JSON.parse(settings)?.builds?.filter( | ||
(plugin) => | ||
plugin.content_types.length === 0 || | ||
plugin.content_types.find((ctd) => ctd === ctdName), | ||
); | ||
const settingsForCtd = settings?.builds | ||
?.filter( | ||
(buttonSettings) => | ||
buttonSettings.content_types.length === 0 || | ||
buttonSettings.content_types.find((ctd) => ctd === ctdName), | ||
) | ||
.filter((buttonSettings) => buttonSettings.build_on_save); | ||
|
||
if (!settingsForCtd.length) return null; | ||
if (!settingsForCtd.length) return; | ||
|
||
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}`, | ||
); | ||
} | ||
}), | ||
); | ||
settingsForCtd.forEach((buttonSettings, index) => { | ||
onBuildHandler( | ||
buttonSettings, | ||
contentObject, | ||
`netlify-item-child-${index}`, | ||
); | ||
}); | ||
}; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.