push messages to telegram from contactUs form

This commit is contained in:
2025-05-22 16:40:13 +03:00
parent 29d3af2ea1
commit 2b902ef0c9
6 changed files with 32 additions and 23 deletions

View File

@@ -9,21 +9,24 @@ export const sendMessage = async (data: TelegramMessage) => {
}
try {
const body = JSON.stringify({
source: data.source,
name: data.name,
phone_number: data.phone_number,
message: data.message,
})
const response = await fetch(`${API_URL}/send-message/`, {
method: 'POST',
headers,
body: JSON.stringify({
source: data.source,
name: data.name,
phone_number: data.phone_number,
message: data.message,
}),
body,
})
if (!response.ok) {
let errorMessage = `Failed to send form data: ${response.status} ${response.statusText}`
try {
const errorData = await response.text()
if (errorData) {
errorMessage += ` - ${errorData}`
}

View File

@@ -1,15 +1,19 @@
// export type SourceType = 'main' | 'admin' | 'userAccount' | 'contactUs' | 'support'
import { SourceType } from '@/app/types'
const pathToSourceMap: Record<string, SourceType> = {
techSupport: 'contact-us', // берем в кавычки значение с дефисом
account: 'account',
'account/support': 'support',
'': 'main', // без слеша -- так работает usePathname
'contact-us': 'contactUs', // URL путь с дефисом -> значение в camelCase
}
export const getSourceFromPath = (pathname: string): SourceType => {
const cleanPath = pathname.replace(/^\//, '')
const source = pathToSourceMap[cleanPath] || 'contact-us' // дефолтное значение
// console.log('Final source:', source)
// для путей аккаунта (кроме support)
if (cleanPath.startsWith('account/') && cleanPath !== 'account/support') {
return 'userAccount'
}
const source = pathToSourceMap[cleanPath] || 'contactUs' // дефолтное значение
return source
}