Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First pass at refinery.js #62

Merged
merged 7 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Sample e-commerce site for testing Bandit ML
# Repo of JS snippets + sample e-commerce site for testing snippets

Installation:
Create a virtualenv and install requirements
Expand Down
91 changes: 91 additions & 0 deletions static/js/refinery.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
window.refinery = window.refinery || {};

refinery.RefineryAPI = function (config = {}) {
this.storage = window.localStorage;
this.refineryApikey = this.getApiKeyFromScriptTag();

let defaultConfig = {
debugMode: false,
refineryHostUrl: "https://app.banditml.com/api/v2",
};
this.config = Object.assign(defaultConfig, config);

this.refineryLiveRefinementEndpoint = `${this.config.refineryHostUrl}changesets`;
};

refinery.RefineryAPI.prototype.getApiKeyFromScriptTag = function () {
return document.currentScript.getAttribute('apiKey');
};

refinery.RefineryAPI.prototype.asyncGetRequest = async function(
url,
params = {},
headers = {}
) {
if (params && Object.keys(params).length) {
url += '?'
}
for (const paramName in params) {
let body = params[paramName];
if (paramName != null && body != null) {
const bodyType = typeof body;
let data;
// no need to encode numbers / strings
if (bodyType === "number" || bodyType === "string") {
data = body;
} else {
data = encodeURIComponent(JSON.stringify(body));
}
url += `${paramName}=${data}&`;
}
}

const response = await fetch(url, {
method: 'GET',
headers: headers
});
return await response.json();
};

refinery.RefineryAPI.prototype.getLiveRefinement = function() {
const self = this;
const changesPromise = self.asyncGetRequest(
url = self.refineryLiveRefinementEndpoint,
params = {},
headers = {
"x-chrome-installation": `${self.refineryApikey}`
}
);
return changesPromise.then(response => {
return response.results;
}).catch(e => {
console.error("Failed to fetch changes", e);
});
};

refinery.RefineryAPI.prototype.applyChanges = function() {
const self = this;
self.getLiveRefinement().then(response => {
// right now we assume 1 changeset only, so grab the first i.e. [0]
let changes = response[0].changes;
changes.forEach(change => {
if (window.location.href == change.href) {
econti marked this conversation as resolved.
Show resolved Hide resolved
let domPath = document.querySelector(change.domPath);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - probably more readable to have this be let elem since it's an HTMLElement that gets loaded up with querySelector

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah nice!

if (domPath.innerHTML != change.beforeHtml) {
econti marked this conversation as resolved.
Show resolved Hide resolved
console.warn(`Can't apply change on '${change.beforeHtml}' because copy doesn't match.\
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice

'${change.beforeHtml}' vs. '${domPath.innerHTML}'.`);
} else {
domPath.innerHTML = change.afterHtml;
}
} else {
console.warn(`
Can't apply change on '${change.beforeHtml}' because href's don't match.\
'${change.href}' vs. '${window.location.href}'.`);
}
});
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is great stuff, we'll probably eventually want to move this into a shareable module so that refinerybrowser can use this (for deleted nodes, prevent accidentally losing changes, etc.)!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! good idea


}

let refineryAPI = new refinery.RefineryAPI();
refineryAPI.applyChanges();
File renamed without changes.
Empty file added static/minified/refinery.min.js
Empty file.
2 changes: 1 addition & 1 deletion teamies/splash.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const App = () => {
<div>
<Navigation />
<Header data={landingPageData.Header} />
<Hero text="Virtual Team Building Events" />
<Hero text="Team Building Events for All Team Sizes" />
<Features data={landingPageData.Features} />
<About data={landingPageData.About} />
<Services data={landingPageData.Services} />
Expand Down
7 changes: 4 additions & 3 deletions templates/home.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Welcome</title>
<link rel="stylesheet" href={{ url_for('static', filename='css/home.css') }} />
<link rel="stylesheet" href={{ url_for('static', filename='css/topStyle.css') }} />
<title>Welcome</title>
<link rel="stylesheet" href={{ url_for('static', filename='css/home.css') }} />
<link rel="stylesheet" href={{ url_for('static', filename='css/topStyle.css') }} />
<script src={{ url_for('static', filename='js/refinery.js')}} defer="defer" apiKey="a6a0c233-988b-4d6f-b940-bab1573aef95"></script>
</head>
<body>
<div id="title">
Expand Down