- Suppression des favoris
This commit is contained in:
@ -38,44 +38,72 @@
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{{ dump(book.id) }}
|
||||
|
||||
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"
|
||||
{% if app.user %}
|
||||
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"
|
||||
class="absolute top-2 right-2 w-6 h-6 cursor-pointer transition-colors duration-300"
|
||||
x-data="{ liked: false }"
|
||||
:fill="liked ? 'red' : 'none'"
|
||||
:stroke="liked ? 'red' : 'currentColor'"
|
||||
@click="toggleLike('{{ book.id }}'); liked = !liked">
|
||||
@click="toggleLike('{{ book.id }} ', liked); liked = !liked">
|
||||
<path stroke-linecap="round" stroke-linejoin="round"
|
||||
d="M21 8.25c0-2.485-2.099-4.5-4.688-4.5-1.935 0-3.597 1.126-4.312 2.733-.715-1.607-2.377-2.733-4.313-2.733C5.1 3.75 3 5.765 3 8.25c0 7.22 9 12 9 12s9-4.78 9-12Z"/>
|
||||
</svg>
|
||||
{% endif %}
|
||||
|
||||
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
|
||||
<script>
|
||||
function toggleLike(idGoogle) {
|
||||
fetch("{{ path('like', {'idGoogle': 'PLACEHOLDER'}) }}".replace('PLACEHOLDER', idGoogle), {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
// tu peux envoyer des données ici si nécessaire
|
||||
}),
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.liked) {
|
||||
// Mettre à jour le SVG ou l'état du bouton
|
||||
console.log('Like effectué');
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Erreur AJAX', error);
|
||||
});
|
||||
function toggleLike(idGoogle, liked) {
|
||||
// Si l'élément est déjà aimé (liked = true), on le retire des favoris
|
||||
if (liked) {
|
||||
fetch("{{ path('unlike', {'idGoogle': 'PLACEHOLDER'}) }}".replace('PLACEHOLDER', idGoogle), {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
// D'autres données peuvent être envoyées si nécessaire
|
||||
}),
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.status === 'removed') {
|
||||
// Mettre à jour le SVG ou l'état du bouton pour retirer le like
|
||||
console.log('Livre retiré des favoris');
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Erreur AJAX', error);
|
||||
});
|
||||
} else { // Si l'élément n'est pas encore aimé (liked = false), on l'ajoute aux favoris
|
||||
fetch("{{ path('like', {'idGoogle': 'PLACEHOLDER'}) }}".replace('PLACEHOLDER', idGoogle), {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
// D'autres données peuvent être envoyées si nécessaire
|
||||
}),
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.status === 'added') {
|
||||
// Mettre à jour le SVG ou l'état du bouton pour ajouter le like
|
||||
console.log('Livre ajouté aux favoris');
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
console.error('Erreur AJAX', error);
|
||||
});
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
{% endblock %}
|
Reference in New Issue
Block a user