From 94c789bef2e822624e79b2aa708805dc899215f9 Mon Sep 17 00:00:00 2001 From: Federico Date: Fri, 10 Dec 2021 14:30:47 +0100 Subject: [PATCH 1/2] refactor: change file extension from .vue to .js --- widget.vue | 66 ------------------------------------------------------ 1 file changed, 66 deletions(-) delete mode 100644 widget.vue diff --git a/widget.vue b/widget.vue deleted file mode 100644 index b49eea8..0000000 --- a/widget.vue +++ /dev/null @@ -1,66 +0,0 @@ - From acecee89a223c29adc35c02f419ede995405bb19 Mon Sep 17 00:00:00 2001 From: Federico Date: Fri, 10 Dec 2021 14:30:54 +0100 Subject: [PATCH 2/2] refactor: migrate to lodash-es --- .gitignore | 1 + index.js | 5 ++-- package-lock.json | 25 +++++++++++++++---- package.json | 4 +-- widget.js | 62 +++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 87 insertions(+), 10 deletions(-) create mode 100644 widget.js diff --git a/.gitignore b/.gitignore index 3c3629e..76efb07 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ node_modules +.vscode diff --git a/index.js b/index.js index 93cc7c1..0f40a55 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,5 @@ -import { isEmpty, forEach, get } from 'lodash' - -import TrustpilotWidget from './widget.vue' +import { isEmpty, forEach, get } from 'lodash-es' +import TrustpilotWidget from './widget.js' // implementation to prevent unnecessary logs in production const logger = (msg) => { diff --git a/package-lock.json b/package-lock.json index ef25555..2de551c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,13 +1,28 @@ { "name": "vue-trustpilot", "version": "0.2.2", - "lockfileVersion": 1, + "lockfileVersion": 2, "requires": true, + "packages": { + "": { + "name": "vue-trustpilot", + "version": "0.2.2", + "license": "ISC", + "dependencies": { + "lodash-es": "^4.17.21" + } + }, + "node_modules/lodash-es": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash-es/-/lodash-es-4.17.21.tgz", + "integrity": "sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==" + } + }, "dependencies": { - "lodash": { - "version": "4.17.19", - "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.19.tgz", - "integrity": "sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ==" + "lodash-es": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash-es/-/lodash-es-4.17.21.tgz", + "integrity": "sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==" } } } diff --git a/package.json b/package.json index 0a4c2c9..ba3fb09 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,6 @@ "author": "jeisonhs93@gmail.com", "license": "ISC", "dependencies": { - "lodash": "^4.17.15" + "lodash-es": "^4.17.21" } -} \ No newline at end of file +} diff --git a/widget.js b/widget.js new file mode 100644 index 0000000..099120c --- /dev/null +++ b/widget.js @@ -0,0 +1,62 @@ +import { kebabCase, reduce } from "lodash-es"; + +export default { + props: { + /** widget identifier */ + identifier: { + type: String, + required: true + } + }, + render(createElement) { + const { $attrs, $trustpilot, identifier } = this; + + // access and get the widget configuration based on a name identifier + const widget = $trustpilot(identifier); + + // format configuration to data- + kebab-case, format needed for trustpilot script + // example: templateId => data-template-id + const formattedTrustPilotConfig = reduce( + widget, + (result, value, key) => ({ + ...result, + [`data-${kebabCase(key)}`]: value + }), + {} + ); + + // formatted attributes to assign + const attrs = { + ...Object.create($attrs), + ...formattedTrustPilotConfig, + class: "trustpilot-widget", + id: `trustpilot-widget-${identifier}` + }; + + // anchor link element with url to review + // result: + const link = createElement("a", { + attrs: { + href: widget.reviewUrl, + target: "_blank" + } + }); + + // result:
+ return createElement("div", { attrs }, [link]); + }, + mounted() { + try { + this.$nextTick(() => { + const trustbox = document.getElementById( + `trustpilot-widget-${this.identifier}` + ); + if (window.Trustpilot) { + window.Trustpilot.loadFromElement(trustbox); + } + }); + } catch (error) { + console.info(error); + } + } +};