fixed code
This commit is contained in:
@@ -1,66 +1,61 @@
|
||||
import { useCallback } from 'react';
|
||||
// src/hooks/usePostStatus.js
|
||||
import { useState, useCallback } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { API_URL } from '../config/authConstants';
|
||||
import { apiFetch } from '../api/apiClient';
|
||||
|
||||
/**
|
||||
* Хук для изменения статуса поста (ACTIVE/CLOSED).
|
||||
* Используется для отметки "Вещь найдена".
|
||||
* Используется для отметки "Вещь найдена" и (в будущем) для reopen.
|
||||
*/
|
||||
export function usePostStatus() {
|
||||
const { t } = useTranslation();
|
||||
const [error, setError] = useState("");
|
||||
|
||||
/**
|
||||
* Изменяет статус поста на CLOSED (вещь найдена).
|
||||
* @param {number} postId - ID поста
|
||||
* @returns {Promise<boolean>} - true если операция успешна
|
||||
* @returns {Promise<{success: boolean, error: string|null}>}
|
||||
*/
|
||||
const markAsFound = useCallback(async (postId) => {
|
||||
const updateStatus = useCallback(async (postId, status) => {
|
||||
setError("");
|
||||
try {
|
||||
const response = await apiFetch(`${API_URL}/api/v1/posts/${postId}/status`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ status: 'CLOSED' }),
|
||||
body: JSON.stringify({ status }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to update status: ${response.status}`);
|
||||
throw new Error(t('post.statusUpdateError'));
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('Error marking post as found:', error);
|
||||
return false;
|
||||
return { success: true, error: null };
|
||||
} catch (err) {
|
||||
console.error('Error updating post status:', err);
|
||||
const message = err.message || t('post.statusUpdateError');
|
||||
setError(message);
|
||||
return { success: false, error: message };
|
||||
}
|
||||
}, []);
|
||||
}, [t]);
|
||||
|
||||
/**
|
||||
* Изменяет статус поста на CLOSED (вещь найдена).
|
||||
* @param {number} postId - ID поста
|
||||
* @returns {Promise<{success: boolean, error: string|null}>}
|
||||
*/
|
||||
const markAsFound = useCallback((postId) => updateStatus(postId, 'CLOSED'), [updateStatus]);
|
||||
|
||||
/**
|
||||
* Возвращает статус поста в ACTIVE (если нужно reopen).
|
||||
* @param {number} postId - ID поста
|
||||
* @returns {Promise<boolean>} - true если операция успешна
|
||||
* @returns {Promise<{success: boolean, error: string|null}>}
|
||||
*/
|
||||
const reopenPost = useCallback(async (postId) => {
|
||||
try {
|
||||
const response = await apiFetch(`${API_URL}/api/v1/posts/${postId}/status`, {
|
||||
method: 'PATCH',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify({ status: 'ACTIVE' }),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Failed to update status: ${response.status}`);
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error('Error reopening post:', error);
|
||||
return false;
|
||||
}
|
||||
}, []);
|
||||
const reopenPost = useCallback((postId) => updateStatus(postId, 'ACTIVE'), [updateStatus]);
|
||||
|
||||
return {
|
||||
markAsFound,
|
||||
reopenPost,
|
||||
error,
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user