Skip to content

Instantly share code, notes, and snippets.

@rodrigophpweb
Created April 23, 2024 19:45
Show Gist options
  • Save rodrigophpweb/c2f948a0a90fd0d8ba75c74785788ee1 to your computer and use it in GitHub Desktop.
Save rodrigophpweb/c2f948a0a90fd0d8ba75c74785788ee1 to your computer and use it in GitHub Desktop.
Ajax request code to list the activities of the units related to the event
const url = wp.ajaxurl; // Sua URL de solicitação
const urlComParametros = `${url}?action=listActivities&slug=${slug}&level=${level}&school=${school}&timestamp=${new Date().getTime()}`;
const updateActivities = (slug, level, school) => {
fetch(urlComParametros, {
method: 'POST',
credentials: 'same-origin',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Cache-Control': 'no-cache',
},
body: new URLSearchParams({
action: 'listActivities',
slug: slug,
level: level,
school: school,
}),
})
.then((response) => response.text())
.then((data) => {
document.querySelector('#listActivities').innerHTML = data;
sortPostsByDateTime();
})
.catch((error) => {
console.log(error);
});
};
const slug = document.location.pathname.split('/')[2];
const level = document.querySelector('article.events').getAttribute('class').split(' ')[0];
const schoolFilter = document.querySelector('#filterSchool');
if (schoolFilter) {
schoolFilter.addEventListener('change', function () {
const selectedSchool = this.value;
updateActivities(slug, level, selectedSchool);
});
}
sortPostsByDateTime();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment