Add render data

This commit is contained in:
2025-03-30 15:12:52 +05:00
parent 71a2bec099
commit cceca6b8bf
4 changed files with 97 additions and 25 deletions

View File

@@ -57,14 +57,14 @@ const getContact = async id => {
return data
} catch (error) {
console.error('Ошибка при получении токенов:', error)
console.error('Ошибка при получении контактов:', error)
throw error
}
}
const getLeads = async (page = 1, limit = 2) => {
try {
const response = await fetch(`https://${DOMAIN}/api/v4/leads?page=${page}&limit=${limit}&with=contacts`, {
const response = await fetch(`https://${DOMAIN}/api/v4/leads?page=${page}&limit=${limit}&with=contacts&order[id]=desc`, {
method: 'GET',
headers: {
'Authorization': `Bearer ${ACCESS_TOKEN}`,
@@ -78,22 +78,21 @@ const getLeads = async (page = 1, limit = 2) => {
const data = await response.json()
const leads = []
data._embedded.leads.forEach(async lead => {
const contact =
lead._embedded.contacts.length
? await getContact(lead._embedded.contacts[0].id)
: null
const leads = await Promise.all(data._embedded.leads.map(async lead => {
const contact =
lead._embedded.contacts.length
? await getContact(lead._embedded.contacts[0].id)
: null
leads.push({
return {
lead: lead,
contact: contact
})
})
}
}))
return leads
} catch (error) {
console.error('Ошибка при получении токенов:', error)
console.error('Заявок больше нет')
throw error
}
}
@@ -103,7 +102,29 @@ const getAllLeads = async () => {
while (true) {
try {
const leads = await getLeads(page++)
console.log(leads)
for (const lead of leads) {
console.log(lead)
const row = document.getElementById('table').insertRow()
const cell1 = row.insertCell()
const cell2 = row.insertCell()
const cell3 = row.insertCell()
const cell4 = row.insertCell()
const cell5 = row.insertCell()
cell1.textContent = lead.lead.id
cell2.textContent = lead.lead.name
cell3.textContent = lead.lead.price
cell4.textContent = lead.contact?.name
cell5.textContent = lead.contact
?.custom_fields_values[0]
?.values[0]?.value
}
} catch (error) {
document.getElementById('loader').classList.add('none')
console.log(error)
throw error
break
}
await sleep(1000)
@@ -112,5 +133,5 @@ const getAllLeads = async () => {
(async () => {
await getAllLeads()
})()