route handlers for account/main
This commit is contained in:
41
frontend/lib/telegram/sendMessage.ts
Normal file
41
frontend/lib/telegram/sendMessage.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { TelegramMessage } from '@/app/types'
|
||||
|
||||
export const sendMessage = async (data: TelegramMessage) => {
|
||||
const API_URL = process.env.NEXT_PUBLIC_API_URL
|
||||
|
||||
const headers: Record<string, string> = {
|
||||
'Content-Type': 'application/json',
|
||||
Accept: 'application/json',
|
||||
}
|
||||
|
||||
try {
|
||||
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,
|
||||
}),
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
let errorMessage = `Failed to send form data: ${response.status} ${response.statusText}`
|
||||
try {
|
||||
const errorData = await response.text()
|
||||
if (errorData) {
|
||||
errorMessage += ` - ${errorData}`
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('Error parsing error response:', e)
|
||||
}
|
||||
throw new Error(errorMessage)
|
||||
}
|
||||
const text = await response.text()
|
||||
return text ? JSON.parse(text) : null
|
||||
} catch (error) {
|
||||
console.error('Error sending form data:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
15
frontend/lib/utils/pathMapper.ts
Normal file
15
frontend/lib/utils/pathMapper.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { SourceType } from '@/app/types'
|
||||
|
||||
const pathToSourceMap: Record<string, SourceType> = {
|
||||
techSupport: 'contact-us', // берем в кавычки значение с дефисом
|
||||
account: 'account',
|
||||
}
|
||||
|
||||
export const getSourceFromPath = (pathname: string): SourceType => {
|
||||
const cleanPath = pathname.replace(/^\//, '')
|
||||
|
||||
const source = pathToSourceMap[cleanPath] || 'contact-us' // дефолтное значение
|
||||
// console.log('Final source:', source)
|
||||
|
||||
return source
|
||||
}
|
||||
Reference in New Issue
Block a user