Spaces:
Sleeping
Sleeping
axeronis commited on
Commit ·
a749c2f
1
Parent(s): 2561713
Perf: Migrate notifications and CRM pages to React Query caching
Browse files
frontend/app/settings/crm/page.tsx
CHANGED
|
@@ -1,7 +1,9 @@
|
|
| 1 |
'use client'
|
| 2 |
|
| 3 |
-
import {
|
|
|
|
| 4 |
import { useAuth } from '@/contexts/AuthContext'
|
|
|
|
| 5 |
import { Plus, Settings, X, Loader2, CheckCircle2, AlertCircle, Eye, EyeOff, Zap, Trash2 } from 'lucide-react'
|
| 6 |
import Image from 'next/image'
|
| 7 |
|
|
@@ -59,9 +61,8 @@ const AVAILABLE_CRMS = ['salesforce', 'hubspot', 'zoho'] as const
|
|
| 59 |
|
| 60 |
export default function CRMSettingsPage() {
|
| 61 |
const { user } = useAuth()
|
| 62 |
-
const
|
| 63 |
const [saving, setSaving] = useState(false)
|
| 64 |
-
const [integrations, setIntegrations] = useState<CRMIntegration[]>([])
|
| 65 |
const [showModal, setShowModal] = useState(false)
|
| 66 |
const [modalMode, setModalMode] = useState<'add' | 'configure'>('add')
|
| 67 |
const [selectedCRM, setSelectedCRM] = useState<string | null>(null)
|
|
@@ -80,31 +81,15 @@ export default function CRMSettingsPage() {
|
|
| 80 |
sync_only_hot_leads: true
|
| 81 |
})
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
}
|
| 93 |
-
|
| 94 |
-
try {
|
| 95 |
-
const response = await fetch(
|
| 96 |
-
`${process.env.NEXT_PUBLIC_API_URL}/api/v1/crm/${user.tenant_id}/integrations`,
|
| 97 |
-
{ headers: { 'Authorization': `Bearer ${token}` } }
|
| 98 |
-
)
|
| 99 |
-
if (response.ok) {
|
| 100 |
-
setIntegrations(await response.json())
|
| 101 |
-
}
|
| 102 |
-
} catch (error) {
|
| 103 |
-
console.error('Failed to fetch integrations:', error)
|
| 104 |
-
} finally {
|
| 105 |
-
setLoading(false)
|
| 106 |
-
}
|
| 107 |
-
}
|
| 108 |
|
| 109 |
const openConfigureModal = (crmType: string) => {
|
| 110 |
setSelectedCRM(crmType)
|
|
@@ -127,39 +112,21 @@ export default function CRMSettingsPage() {
|
|
| 127 |
|
| 128 |
const handleSaveIntegration = async () => {
|
| 129 |
if (!selectedCRM || !user?.tenant_id) return
|
| 130 |
-
const token = localStorage.getItem('auth_token')
|
| 131 |
-
if (!token) return
|
| 132 |
|
| 133 |
setSaving(true)
|
| 134 |
setErrorMessage('')
|
| 135 |
|
| 136 |
try {
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
...formData
|
| 148 |
-
})
|
| 149 |
-
}
|
| 150 |
-
)
|
| 151 |
-
|
| 152 |
-
if (response.ok) {
|
| 153 |
-
setSuccessMessage('Интеграция подключена!')
|
| 154 |
-
setShowModal(false)
|
| 155 |
-
fetchIntegrations()
|
| 156 |
-
setTimeout(() => setSuccessMessage(''), 3000)
|
| 157 |
-
} else {
|
| 158 |
-
const error = await response.json()
|
| 159 |
-
setErrorMessage(error.detail || 'Ошибка подключения')
|
| 160 |
-
}
|
| 161 |
-
} catch (error) {
|
| 162 |
-
setErrorMessage('Ошибка сети')
|
| 163 |
} finally {
|
| 164 |
setSaving(false)
|
| 165 |
}
|
|
@@ -167,22 +134,13 @@ export default function CRMSettingsPage() {
|
|
| 167 |
|
| 168 |
const handleDeleteIntegration = async (id: string) => {
|
| 169 |
if (!confirm('Удалить интеграцию?')) return
|
| 170 |
-
|
| 171 |
-
if (!token || !user?.tenant_id) return
|
| 172 |
|
| 173 |
try {
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
headers: { 'Authorization': `Bearer ${token}` }
|
| 179 |
-
}
|
| 180 |
-
)
|
| 181 |
-
if (response.ok) {
|
| 182 |
-
setSuccessMessage('Интеграция удалена')
|
| 183 |
-
fetchIntegrations()
|
| 184 |
-
setTimeout(() => setSuccessMessage(''), 3000)
|
| 185 |
-
}
|
| 186 |
} catch (error) {
|
| 187 |
console.error('Failed to delete:', error)
|
| 188 |
}
|
|
|
|
| 1 |
'use client'
|
| 2 |
|
| 3 |
+
import { useState } from 'react'
|
| 4 |
+
import { useQuery, useQueryClient } from '@tanstack/react-query'
|
| 5 |
import { useAuth } from '@/contexts/AuthContext'
|
| 6 |
+
import { api } from '@/lib/api'
|
| 7 |
import { Plus, Settings, X, Loader2, CheckCircle2, AlertCircle, Eye, EyeOff, Zap, Trash2 } from 'lucide-react'
|
| 8 |
import Image from 'next/image'
|
| 9 |
|
|
|
|
| 61 |
|
| 62 |
export default function CRMSettingsPage() {
|
| 63 |
const { user } = useAuth()
|
| 64 |
+
const queryClient = useQueryClient()
|
| 65 |
const [saving, setSaving] = useState(false)
|
|
|
|
| 66 |
const [showModal, setShowModal] = useState(false)
|
| 67 |
const [modalMode, setModalMode] = useState<'add' | 'configure'>('add')
|
| 68 |
const [selectedCRM, setSelectedCRM] = useState<string | null>(null)
|
|
|
|
| 81 |
sync_only_hot_leads: true
|
| 82 |
})
|
| 83 |
|
| 84 |
+
// React Query for CRM integrations - cached!
|
| 85 |
+
const { data: integrations = [], isLoading: loading } = useQuery<CRMIntegration[]>({
|
| 86 |
+
queryKey: ['crm-integrations', user?.tenant_id],
|
| 87 |
+
queryFn: async () => {
|
| 88 |
+
const response = await api.get(`/api/v1/crm/${user!.tenant_id}/integrations`)
|
| 89 |
+
return response.data
|
| 90 |
+
},
|
| 91 |
+
enabled: !!user?.tenant_id,
|
| 92 |
+
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
const openConfigureModal = (crmType: string) => {
|
| 95 |
setSelectedCRM(crmType)
|
|
|
|
| 112 |
|
| 113 |
const handleSaveIntegration = async () => {
|
| 114 |
if (!selectedCRM || !user?.tenant_id) return
|
|
|
|
|
|
|
| 115 |
|
| 116 |
setSaving(true)
|
| 117 |
setErrorMessage('')
|
| 118 |
|
| 119 |
try {
|
| 120 |
+
await api.post(`/api/v1/crm/${user.tenant_id}/integrations`, {
|
| 121 |
+
crm_type: selectedCRM,
|
| 122 |
+
...formData
|
| 123 |
+
})
|
| 124 |
+
setSuccessMessage('Интеграция подключена!')
|
| 125 |
+
setShowModal(false)
|
| 126 |
+
queryClient.invalidateQueries({ queryKey: ['crm-integrations'] })
|
| 127 |
+
setTimeout(() => setSuccessMessage(''), 3000)
|
| 128 |
+
} catch (error: any) {
|
| 129 |
+
setErrorMessage(error.response?.data?.detail || 'Ошибка подключения')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
} finally {
|
| 131 |
setSaving(false)
|
| 132 |
}
|
|
|
|
| 134 |
|
| 135 |
const handleDeleteIntegration = async (id: string) => {
|
| 136 |
if (!confirm('Удалить интеграцию?')) return
|
| 137 |
+
if (!user?.tenant_id) return
|
|
|
|
| 138 |
|
| 139 |
try {
|
| 140 |
+
await api.delete(`/api/v1/crm/${user.tenant_id}/integrations/${id}`)
|
| 141 |
+
setSuccessMessage('Интеграция удалена')
|
| 142 |
+
queryClient.invalidateQueries({ queryKey: ['crm-integrations'] })
|
| 143 |
+
setTimeout(() => setSuccessMessage(''), 3000)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
} catch (error) {
|
| 145 |
console.error('Failed to delete:', error)
|
| 146 |
}
|
frontend/app/settings/notifications/page.tsx
CHANGED
|
@@ -1,8 +1,10 @@
|
|
| 1 |
'use client'
|
| 2 |
|
| 3 |
import { useState, useEffect } from 'react'
|
|
|
|
| 4 |
import { useAuth } from '@/contexts/AuthContext'
|
| 5 |
import { useToast } from '@/contexts/ToastContext'
|
|
|
|
| 6 |
import { Bell, Send, Bot, CheckCircle2, Loader2, ShoppingBag, Calendar, Flame, Save } from 'lucide-react'
|
| 7 |
import { motion } from 'framer-motion'
|
| 8 |
import { cn } from '@/lib/utils'
|
|
@@ -10,7 +12,7 @@ import { cn } from '@/lib/utils'
|
|
| 10 |
export default function NotificationSettingsPage() {
|
| 11 |
const { user } = useAuth()
|
| 12 |
const toast = useToast()
|
| 13 |
-
const
|
| 14 |
const [saving, setSaving] = useState(false)
|
| 15 |
|
| 16 |
const [chatId, setChatId] = useState('')
|
|
@@ -18,50 +20,39 @@ export default function NotificationSettingsPage() {
|
|
| 18 |
const [notifyBooking, setNotifyBooking] = useState(true)
|
| 19 |
const [notifyLeads, setNotifyLeads] = useState(true)
|
| 20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
useEffect(() => {
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
const fetchSettings = async () => {
|
| 26 |
-
try {
|
| 27 |
-
const token = localStorage.getItem('auth_token')
|
| 28 |
-
const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/v1/tenants/me`, {
|
| 29 |
-
headers: { 'Authorization': `Bearer ${token}` }
|
| 30 |
-
})
|
| 31 |
-
const data = await res.json()
|
| 32 |
-
|
| 33 |
-
const notif = data.notification_settings || {}
|
| 34 |
setChatId(notif.telegram_chat_id || '')
|
| 35 |
setNotifyOrder(notif.notify_on_order !== false)
|
| 36 |
setNotifyBooking(notif.notify_on_booking !== false)
|
| 37 |
setNotifyLeads(notif.notify_on_lead_heat !== false)
|
| 38 |
-
} catch {
|
| 39 |
-
console.error('Failed to load')
|
| 40 |
-
} finally {
|
| 41 |
-
setLoading(false)
|
| 42 |
}
|
| 43 |
-
}
|
| 44 |
|
| 45 |
const handleSave = async () => {
|
| 46 |
setSaving(true)
|
| 47 |
try {
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
}
|
| 55 |
-
body: JSON.stringify({
|
| 56 |
-
notification_settings: {
|
| 57 |
-
telegram_chat_id: chatId,
|
| 58 |
-
notify_on_order: notifyOrder,
|
| 59 |
-
notify_on_booking: notifyBooking,
|
| 60 |
-
notify_on_lead_heat: notifyLeads
|
| 61 |
-
}
|
| 62 |
-
})
|
| 63 |
})
|
| 64 |
-
|
| 65 |
toast.success('Успех!', 'Настройки сохранены')
|
| 66 |
} catch {
|
| 67 |
toast.error('Ошибка', 'Не удалось сохранить')
|
|
|
|
| 1 |
'use client'
|
| 2 |
|
| 3 |
import { useState, useEffect } from 'react'
|
| 4 |
+
import { useQuery, useQueryClient } from '@tanstack/react-query'
|
| 5 |
import { useAuth } from '@/contexts/AuthContext'
|
| 6 |
import { useToast } from '@/contexts/ToastContext'
|
| 7 |
+
import { api } from '@/lib/api'
|
| 8 |
import { Bell, Send, Bot, CheckCircle2, Loader2, ShoppingBag, Calendar, Flame, Save } from 'lucide-react'
|
| 9 |
import { motion } from 'framer-motion'
|
| 10 |
import { cn } from '@/lib/utils'
|
|
|
|
| 12 |
export default function NotificationSettingsPage() {
|
| 13 |
const { user } = useAuth()
|
| 14 |
const toast = useToast()
|
| 15 |
+
const queryClient = useQueryClient()
|
| 16 |
const [saving, setSaving] = useState(false)
|
| 17 |
|
| 18 |
const [chatId, setChatId] = useState('')
|
|
|
|
| 20 |
const [notifyBooking, setNotifyBooking] = useState(true)
|
| 21 |
const [notifyLeads, setNotifyLeads] = useState(true)
|
| 22 |
|
| 23 |
+
// React Query for notification settings - cached!
|
| 24 |
+
const { data: tenantData, isLoading: loading } = useQuery({
|
| 25 |
+
queryKey: ['notification-settings', user?.tenant_id],
|
| 26 |
+
queryFn: async () => {
|
| 27 |
+
const res = await api.get('/api/v1/tenants/me')
|
| 28 |
+
return res.data
|
| 29 |
+
},
|
| 30 |
+
enabled: !!user,
|
| 31 |
+
})
|
| 32 |
+
|
| 33 |
+
// Sync local state with query data
|
| 34 |
useEffect(() => {
|
| 35 |
+
if (tenantData) {
|
| 36 |
+
const notif = tenantData.notification_settings || {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
setChatId(notif.telegram_chat_id || '')
|
| 38 |
setNotifyOrder(notif.notify_on_order !== false)
|
| 39 |
setNotifyBooking(notif.notify_on_booking !== false)
|
| 40 |
setNotifyLeads(notif.notify_on_lead_heat !== false)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
}
|
| 42 |
+
}, [tenantData])
|
| 43 |
|
| 44 |
const handleSave = async () => {
|
| 45 |
setSaving(true)
|
| 46 |
try {
|
| 47 |
+
await api.patch('/api/v1/tenants/me', {
|
| 48 |
+
notification_settings: {
|
| 49 |
+
telegram_chat_id: chatId,
|
| 50 |
+
notify_on_order: notifyOrder,
|
| 51 |
+
notify_on_booking: notifyBooking,
|
| 52 |
+
notify_on_lead_heat: notifyLeads
|
| 53 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
})
|
| 55 |
+
queryClient.invalidateQueries({ queryKey: ['notification-settings'] })
|
| 56 |
toast.success('Успех!', 'Настройки сохранены')
|
| 57 |
} catch {
|
| 58 |
toast.error('Ошибка', 'Не удалось сохранить')
|