61 lines
1.9 KiB
JavaScript
61 lines
1.9 KiB
JavaScript
// 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("");
|
|
|
|
/**
|
|
* @returns {Promise<{success: boolean, error: string|null}>}
|
|
*/
|
|
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 }),
|
|
});
|
|
|
|
if (!response.ok) {
|
|
throw new Error(t('post.statusUpdateError'));
|
|
}
|
|
|
|
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<{success: boolean, error: string|null}>}
|
|
*/
|
|
const reopenPost = useCallback((postId) => updateStatus(postId, 'ACTIVE'), [updateStatus]);
|
|
|
|
return {
|
|
markAsFound,
|
|
reopenPost,
|
|
error,
|
|
};
|
|
} |