Skip to content

Instantly share code, notes, and snippets.

@arturmkrtchyan
Last active December 9, 2021 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arturmkrtchyan/b854d2177a4a90fd66fc492c332ee8cd to your computer and use it in GitHub Desktop.
Save arturmkrtchyan/b854d2177a4a90fd66fc492c332ee8cd to your computer and use it in GitHub Desktop.
Custom Upvote
<script>
document.addEventListener("DOMContentLoaded", function() {
const pageId = 'c677b3ed-03d1-462a-a1b4-c78bd71bb8e3';
const applicationId = '8d4648c7-85ec-4901-bb16-a1d99e6f7b9e';
const baseId = 'appbQWhGxkZUzMwzP';
const candidatesTableName = 'Candidature(s)';
const votesTableName = 'Votes';
const votesUrl = 'https://' + window.location.hostname + '/v1/integrations/airtable/' + applicationId + '/' + baseId + '/' + votesTableName + '/records/new';
setInterval(function(){
$('.sw-js-single-item-container .box .sw-js-upvote-button').unbind();
$('.sw-js-single-item-container .box .sw-js-upvote-button').click(function (event) {
upvote($(this));
const candidateId = $(this).parents('.js-list-item').attr('data-recordid');
const voterId = window['logged_in_user']['airtable_record_id'];
sendToAirtable({Candidat_ID: candidateId, Votant_ID: voterId}, votesUrl, () => {}, () => {});
});
}, 500);
function getCookie(cookieName) {
var name = cookieName + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function setCookie(name, value, days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = "expires=" + date.toUTCString();
document.cookie = name + "=" + value + ";" + expires + ";" + 'SameSite=None; Secure';
}
function sendToAirtable(payload, url, successCallback, errorCallback) {
var newPayload = {
fields: payload,
typecast: true
};
$.ajax({
url: url,
type: 'POST',
data: JSON.stringify(newPayload),
contentType: 'application/json; charset=utf-8',
success: function (data) {
console.log(data);
successCallback();
},
error: function (error) {
console.log(error);
if (error && error.responseJSON && error.responseJSON.message) {
showErrorToastr(error.responseJSON.message);
}
errorCallback();
}
});
}
function upvote(target) {
var url = 'https://' + window.location.hostname + '/v1/integrations/airtable/' + applicationId + '/' + baseId + '/' + candidatesTableName + '/records';
url = url.replace('/records', '/upvote');
var mappedTo = $(target).attr('data-mappedto');
if (!mappedTo) {
return;
}
var recordId = $(target).parents('.js-list-item').attr('data-recordid');
if (!recordId) {
return;
}
var upvoteValue = parseInt($(target).attr('data-value'), 10);
var cookieName = pageId + '-' + recordId;
/* ToDo change cookie rules */
/* var existingCookie = getCookie(cookieName); */
/* if (existingCookie) { */
/* console.log('Already upvoted'); */
/* return; */
/* } */
/* setCookie(cookieName, 'upvoted', 1); */
var params = {
id: recordId,
field: mappedTo
};
$.ajax({
url: url,
type: 'POST',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(params),
success: function (res) {
upvoteValue = upvoteValue + 1;
$(target).find('.js-upvote-value').html(upvoteValue);
$(target).attr('data-value', upvoteValue);
},
error: function (error) {
/* ToDo remove from cookie */
deleteCookie(cookieName);
if (error && error.responseJSON && error.responseJSON.message) {
showErrorToastr(error.responseJSON.message);
}
console.log('Error');
console.log(error);
}
});
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment