added refresh token
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { useCallback } from 'react';
|
||||
import { API_URL } from '../config/authConstants';
|
||||
import { apiFetch } from '../api/apiClient';
|
||||
|
||||
/**
|
||||
* Хук для работы с лайками (избранное).
|
||||
@@ -9,21 +10,12 @@ export function useFavorites() {
|
||||
/**
|
||||
* Переключает лайк на посте через API.
|
||||
* @param {number} postId - ID поста
|
||||
* @param {string} token - JWT токен авторизации
|
||||
* @returns {Promise<boolean>} - true если операция успешна
|
||||
*/
|
||||
const toggleFavorite = useCallback(async (postId, token) => {
|
||||
if (!token) {
|
||||
console.warn('Cannot toggle favorite: no token');
|
||||
return false;
|
||||
}
|
||||
|
||||
const toggleFavorite = useCallback(async (postId) => {
|
||||
try {
|
||||
const response = await fetch(`${API_URL}/api/v1/favorites/${postId}/toggle`, {
|
||||
const response = await apiFetch(`${API_URL}/api/v1/favorites/${postId}/toggle`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
@@ -39,20 +31,11 @@ export function useFavorites() {
|
||||
|
||||
/**
|
||||
* Загружает список лайкнутых постов текущего пользователя.
|
||||
* @param {string} token - JWT токен авторизации
|
||||
* @returns {Promise<Array>} - массив ID лайкнутых постов
|
||||
*/
|
||||
const loadFavorites = useCallback(async (token) => {
|
||||
if (!token) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const loadFavorites = useCallback(async () => {
|
||||
try {
|
||||
const response = await fetch(`${API_URL}/api/v1/favorites`, {
|
||||
headers: {
|
||||
'Authorization': `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
const response = await apiFetch(`${API_URL}/api/v1/favorites`);
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to load favorites: ${response.status}`);
|
||||
@@ -71,4 +54,4 @@ export function useFavorites() {
|
||||
toggleFavorite,
|
||||
loadFavorites,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user