2025-12-31 19:11:28 +03:00
|
|
|
|
import React, { useState, useEffect } from 'react'
|
2026-01-01 18:21:18 +03:00
|
|
|
|
import { useAuth } from './auth/AuthContext'
|
2026-01-11 15:51:28 +03:00
|
|
|
|
import LoadingError from './LoadingError'
|
|
|
|
|
|
import Toast from './Toast'
|
2025-12-31 19:11:28 +03:00
|
|
|
|
import './Integrations.css'
|
|
|
|
|
|
|
2026-01-08 00:02:06 +03:00
|
|
|
|
function TodoistIntegration({ onNavigate }) {
|
2026-01-01 18:21:18 +03:00
|
|
|
|
const { authFetch } = useAuth()
|
2026-01-02 15:34:01 +03:00
|
|
|
|
const [connected, setConnected] = useState(false)
|
|
|
|
|
|
const [todoistEmail, setTodoistEmail] = useState('')
|
2025-12-31 19:11:28 +03:00
|
|
|
|
const [loading, setLoading] = useState(true)
|
2026-01-01 18:50:55 +03:00
|
|
|
|
const [error, setError] = useState('')
|
2026-01-02 15:34:01 +03:00
|
|
|
|
const [message, setMessage] = useState('')
|
2026-01-11 15:51:28 +03:00
|
|
|
|
const [toastMessage, setToastMessage] = useState(null)
|
|
|
|
|
|
const [isLoadingError, setIsLoadingError] = useState(false)
|
2025-12-31 19:11:28 +03:00
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
2026-01-02 15:34:01 +03:00
|
|
|
|
checkStatus()
|
|
|
|
|
|
// Проверяем URL параметры для сообщений
|
|
|
|
|
|
const params = new URLSearchParams(window.location.search)
|
|
|
|
|
|
const integration = params.get('integration')
|
|
|
|
|
|
const status = params.get('status')
|
|
|
|
|
|
if (integration === 'todoist') {
|
|
|
|
|
|
if (status === 'connected') {
|
|
|
|
|
|
setMessage('✅ Todoist успешно подключен!')
|
|
|
|
|
|
// Очищаем URL параметры
|
|
|
|
|
|
window.history.replaceState({}, '', window.location.pathname)
|
|
|
|
|
|
} else if (status === 'error') {
|
|
|
|
|
|
const errorMsg = params.get('message') || 'Произошла ошибка'
|
2026-01-11 15:51:28 +03:00
|
|
|
|
setToastMessage({ text: errorMsg, type: 'error' })
|
2026-01-02 15:34:01 +03:00
|
|
|
|
window.history.replaceState({}, '', window.location.pathname)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-12-31 19:11:28 +03:00
|
|
|
|
}, [])
|
|
|
|
|
|
|
2026-01-02 15:34:01 +03:00
|
|
|
|
const checkStatus = async () => {
|
2025-12-31 19:11:28 +03:00
|
|
|
|
try {
|
|
|
|
|
|
setLoading(true)
|
2026-01-01 18:50:55 +03:00
|
|
|
|
setError('')
|
2026-01-02 15:34:01 +03:00
|
|
|
|
const response = await authFetch('/api/integrations/todoist/status')
|
2025-12-31 19:11:28 +03:00
|
|
|
|
if (!response.ok) {
|
2026-01-02 15:34:01 +03:00
|
|
|
|
throw new Error('Ошибка при проверке статуса')
|
2025-12-31 19:11:28 +03:00
|
|
|
|
}
|
|
|
|
|
|
const data = await response.json()
|
2026-01-02 15:34:01 +03:00
|
|
|
|
setConnected(data.connected || false)
|
|
|
|
|
|
if (data.connected && data.todoist_email) {
|
|
|
|
|
|
setTodoistEmail(data.todoist_email)
|
2026-01-01 18:50:55 +03:00
|
|
|
|
}
|
2025-12-31 19:11:28 +03:00
|
|
|
|
} catch (error) {
|
2026-01-02 15:34:01 +03:00
|
|
|
|
console.error('Error checking status:', error)
|
|
|
|
|
|
setError(error.message || 'Не удалось проверить статус')
|
2026-01-11 15:51:28 +03:00
|
|
|
|
setIsLoadingError(true)
|
2025-12-31 19:11:28 +03:00
|
|
|
|
} finally {
|
|
|
|
|
|
setLoading(false)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-02 15:40:06 +03:00
|
|
|
|
const handleConnect = async () => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
setLoading(true)
|
|
|
|
|
|
setError('')
|
|
|
|
|
|
// Получаем URL для редиректа через авторизованный запрос
|
|
|
|
|
|
const response = await authFetch('/api/integrations/todoist/oauth/connect')
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
|
const errorData = await response.json().catch(() => ({}))
|
|
|
|
|
|
throw new Error(errorData.error || 'Ошибка при подключении Todoist')
|
|
|
|
|
|
}
|
|
|
|
|
|
const data = await response.json()
|
|
|
|
|
|
if (data.auth_url) {
|
|
|
|
|
|
// Делаем редирект на Todoist OAuth
|
|
|
|
|
|
window.location.href = data.auth_url
|
|
|
|
|
|
} else {
|
|
|
|
|
|
throw new Error('URL для авторизации не получен')
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('Error connecting Todoist:', error)
|
2026-01-11 15:51:28 +03:00
|
|
|
|
setToastMessage({ text: error.message || 'Не удалось подключить Todoist', type: 'error' })
|
2026-01-02 15:40:06 +03:00
|
|
|
|
setLoading(false)
|
|
|
|
|
|
}
|
2026-01-02 15:34:01 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const handleDisconnect = async () => {
|
|
|
|
|
|
if (!window.confirm('Вы уверены, что хотите отключить Todoist?')) {
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-31 19:11:28 +03:00
|
|
|
|
try {
|
2026-01-02 15:34:01 +03:00
|
|
|
|
setLoading(true)
|
|
|
|
|
|
setError('')
|
|
|
|
|
|
const response = await authFetch('/api/integrations/todoist/disconnect', {
|
|
|
|
|
|
method: 'DELETE',
|
|
|
|
|
|
})
|
|
|
|
|
|
if (!response.ok) {
|
|
|
|
|
|
const errorData = await response.json().catch(() => ({}))
|
|
|
|
|
|
throw new Error(errorData.error || 'Ошибка при отключении')
|
|
|
|
|
|
}
|
|
|
|
|
|
setConnected(false)
|
|
|
|
|
|
setTodoistEmail('')
|
2026-01-11 15:51:28 +03:00
|
|
|
|
setToastMessage({ text: 'Todoist отключен', type: 'success' })
|
2025-12-31 19:11:28 +03:00
|
|
|
|
} catch (error) {
|
2026-01-02 15:34:01 +03:00
|
|
|
|
console.error('Error disconnecting:', error)
|
2026-01-11 15:51:28 +03:00
|
|
|
|
setToastMessage({ text: error.message || 'Не удалось отключить Todoist', type: 'error' })
|
2026-01-02 15:34:01 +03:00
|
|
|
|
} finally {
|
|
|
|
|
|
setLoading(false)
|
2025-12-31 19:11:28 +03:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-11 15:51:28 +03:00
|
|
|
|
if (isLoadingError && !loading) {
|
|
|
|
|
|
return (
|
|
|
|
|
|
<div className="p-4 md:p-6">
|
|
|
|
|
|
<button className="close-x-button" onClick={() => onNavigate?.('profile')} title="Закрыть">
|
|
|
|
|
|
✕
|
|
|
|
|
|
</button>
|
|
|
|
|
|
<LoadingError onRetry={checkStatus} />
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-31 19:11:28 +03:00
|
|
|
|
return (
|
|
|
|
|
|
<div className="p-4 md:p-6">
|
2026-01-08 00:02:06 +03:00
|
|
|
|
<button className="close-x-button" onClick={() => onNavigate?.('profile')} title="Закрыть">
|
2025-12-31 19:11:28 +03:00
|
|
|
|
✕
|
|
|
|
|
|
</button>
|
|
|
|
|
|
|
2026-01-02 15:34:01 +03:00
|
|
|
|
<h1 className="text-2xl font-bold mb-6">Todoist интеграция</h1>
|
2025-12-31 19:11:28 +03:00
|
|
|
|
|
2026-01-02 15:34:01 +03:00
|
|
|
|
{loading ? (
|
2026-01-11 15:32:31 +03:00
|
|
|
|
<div className="fixed inset-0 flex justify-center items-center">
|
|
|
|
|
|
<div className="flex flex-col items-center">
|
|
|
|
|
|
<div className="w-12 h-12 border-4 border-indigo-200 border-t-indigo-600 rounded-full animate-spin mb-4"></div>
|
|
|
|
|
|
<div className="text-gray-600 font-medium">Загрузка...</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
2026-01-02 15:34:01 +03:00
|
|
|
|
) : connected ? (
|
|
|
|
|
|
<div>
|
|
|
|
|
|
<div className="bg-white rounded-lg shadow-md p-6 mb-6">
|
|
|
|
|
|
<h2 className="text-lg font-semibold mb-4">Статус подключения</h2>
|
|
|
|
|
|
<div className="space-y-3">
|
|
|
|
|
|
<div className="flex items-center gap-2">
|
|
|
|
|
|
<span className="text-green-600 font-semibold">✅ Todoist подключен</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
{todoistEmail && (
|
|
|
|
|
|
<div>
|
|
|
|
|
|
<span className="text-gray-600">Email: </span>
|
|
|
|
|
|
<span className="font-medium">{todoistEmail}</span>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
<div className="bg-blue-50 border border-blue-200 rounded-lg p-6 mb-6">
|
|
|
|
|
|
<h3 className="text-lg font-semibold mb-3 text-blue-900">
|
|
|
|
|
|
Как это работает
|
|
|
|
|
|
</h3>
|
|
|
|
|
|
<p className="text-gray-700 mb-2">
|
|
|
|
|
|
✅ Todoist подключен! Закрывайте задачи в Todoist — они автоматически
|
|
|
|
|
|
появятся в Play Life.
|
|
|
|
|
|
</p>
|
|
|
|
|
|
<p className="text-gray-600 text-sm">
|
|
|
|
|
|
Никаких дополнительных настроек не требуется. Просто закрывайте задачи
|
|
|
|
|
|
в Todoist, и они будут обработаны автоматически.
|
|
|
|
|
|
</p>
|
2026-01-01 18:50:55 +03:00
|
|
|
|
</div>
|
2026-01-02 15:34:01 +03:00
|
|
|
|
|
|
|
|
|
|
<button
|
|
|
|
|
|
onClick={handleDisconnect}
|
|
|
|
|
|
className="px-4 py-2 bg-red-600 text-white rounded-lg hover:bg-red-700 transition-colors"
|
|
|
|
|
|
>
|
|
|
|
|
|
Отключить Todoist
|
|
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<div>
|
|
|
|
|
|
<div className="bg-white rounded-lg shadow-md p-6 mb-6">
|
|
|
|
|
|
<h2 className="text-lg font-semibold mb-4">Подключение Todoist</h2>
|
|
|
|
|
|
<p className="text-gray-700 mb-4">
|
|
|
|
|
|
Подключите свой Todoist аккаунт для автоматической обработки закрытых задач.
|
|
|
|
|
|
</p>
|
2025-12-31 19:11:28 +03:00
|
|
|
|
<button
|
2026-01-02 15:34:01 +03:00
|
|
|
|
onClick={handleConnect}
|
|
|
|
|
|
className="px-6 py-3 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 transition-colors font-semibold"
|
2025-12-31 19:11:28 +03:00
|
|
|
|
>
|
2026-01-02 15:34:01 +03:00
|
|
|
|
Подключить Todoist
|
2025-12-31 19:11:28 +03:00
|
|
|
|
</button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
2026-01-02 15:34:01 +03:00
|
|
|
|
<div className="bg-blue-50 border border-blue-200 rounded-lg p-6">
|
|
|
|
|
|
<h3 className="text-lg font-semibold mb-3 text-blue-900">
|
|
|
|
|
|
Что нужно сделать
|
|
|
|
|
|
</h3>
|
|
|
|
|
|
<ol className="list-decimal list-inside space-y-2 text-gray-700">
|
|
|
|
|
|
<li>Нажмите кнопку "Подключить Todoist"</li>
|
|
|
|
|
|
<li>Авторизуйтесь в Todoist</li>
|
|
|
|
|
|
<li>Готово! Закрытые задачи будут автоматически обрабатываться</li>
|
|
|
|
|
|
</ol>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
)}
|
2026-01-11 15:51:28 +03:00
|
|
|
|
{toastMessage && (
|
|
|
|
|
|
<Toast
|
|
|
|
|
|
message={toastMessage.text}
|
|
|
|
|
|
type={toastMessage.type}
|
|
|
|
|
|
onClose={() => setToastMessage(null)}
|
|
|
|
|
|
/>
|
|
|
|
|
|
)}
|
2025-12-31 19:11:28 +03:00
|
|
|
|
</div>
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export default TodoistIntegration
|