Skip to content

Instantly share code, notes, and snippets.

@GlenMerlin
Last active August 4, 2020 19:23
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 GlenMerlin/174ca00378fd6ed56b0487c930f31568 to your computer and use it in GitHub Desktop.
Save GlenMerlin/174ca00378fd6ed56b0487c930f31568 to your computer and use it in GitHub Desktop.
This is a way to have the fictional univeristy website redirect you to the register page with too many failed attempt to like a professor
import $ from 'jquery';
var failedAttemptCount = 0
class Like {
constructor() {
this.events();
}
events() {
$(".like-box").on("click", this.ourClickDispatcher.bind(this))
}
// Methods
ourClickDispatcher(e) {
var currentLikeBox = $(e.target).closest(".like-box")
if(currentLikeBox.data('exists') == "yes") {
this.deleteLike(currentLikeBox)
} else{
this.createLike(currentLikeBox)
}
}
createLike(currentLikeBox) {
$.ajax({
beforeSend: (xhr) => {
xhr.setRequestHeader('X-WP-Nonce', universityData.nonce);
},
url: universityData.root_url + '/wp-json/university/v1/manageLike',
type: 'POST',
data: {'professorId': currentLikeBox.data('professor')},
success: (response) => {
console.log(response);
},
error: (response) => {
alert("Oops something went wrong :( \nAre you logged in?")
console.log(response)
failedAttemptCount++1;
console.log(failedAttemptCount)
if (failedAttemptCount >= 2) {
window.location.href = universityData.root_url + '/wp-login.php?action=register';
}
}
})
}
deleteLike() {
$.ajax({
url: universityData.root_url + '/wp-json/university/v1/manageLike',
type: 'DELETE',
success: (response) => {
console.log(response);
},
error: (response) => {
console.log(response)
}
})
}
}
export default Like;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment