-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #118 from GTZ-STUDIO/develop
Merge develop into master
- Loading branch information
Showing
9 changed files
with
133 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
from rest_framework import serializers | ||
|
||
from .models import Favourite | ||
|
||
|
||
class FavouriteSerializer(serializers.ModelSerializer): | ||
blog_id = serializers.IntegerField(source='blog.id') | ||
blog_title = serializers.CharField(source='blog.title') | ||
blog_game = serializers.CharField(source='blog.game') | ||
|
||
class Meta: | ||
model = Favourite | ||
fields = "__all__" | ||
fields = ('blog_id', 'blog_title', 'blog_game') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import axios from 'axios'; | ||
|
||
const FavoriteButton = ({ blogId, initialIsFavorited, onToggle }) => { | ||
const backendUrl = process.env.REACT_APP_BACKEND_URL; | ||
|
||
const [loading, setLoading] = useState(false); | ||
const [isFavorited, setIsFavorited] = useState(initialIsFavorited); | ||
|
||
useEffect(() => { | ||
const fetchFavorites = async () => { | ||
try { | ||
const response = await axios.get(`${backendUrl}/favourite/list/`); | ||
const favorites = response.data.map((favorite) => favorite.blog_id); | ||
setIsFavorited(favorites.includes(blogId)); | ||
} catch (error) { | ||
console.error('Failed to fetch favorites:', error); | ||
} | ||
}; | ||
|
||
fetchFavorites(); | ||
}, [backendUrl, blogId]); | ||
|
||
const handleToggleFavorite = async () => { | ||
setLoading(true); | ||
setIsFavorited(!isFavorited); // Update the state immediately | ||
|
||
try { | ||
const csrfToken = getCookie('csrftoken'); | ||
const action = isFavorited ? 'unsubscribe' : 'subscribe'; | ||
const method = isFavorited ? 'DELETE' : 'POST'; | ||
|
||
await axios.request({ | ||
url: `${backendUrl}/favourite/${action}/${blogId}/`, | ||
method: method, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
'X-CSRFToken': csrfToken, | ||
}, | ||
withCredentials: true, | ||
}); | ||
onToggle(); | ||
} catch (error) { | ||
console.error('Failed to toggle favorite:', error); | ||
|
||
setIsFavorited(!isFavorited); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
const getCookie = (name) => { | ||
const cookieValue = document.cookie | ||
.split('; ') | ||
.find((row) => row.startsWith(`${name}=`)); | ||
if (cookieValue) { | ||
return cookieValue.split('=')[1]; | ||
} | ||
return null; | ||
}; | ||
|
||
return ( | ||
<button onClick={handleToggleFavorite} disabled={loading}> | ||
{isFavorited ? 'Unfavorite' : 'Favorite'} | ||
</button> | ||
); | ||
}; | ||
|
||
export default FavoriteButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters