generated from ebenezerdon/simple-support-tracker-appwrite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
73 lines (59 loc) · 2.14 KB
/
script.js
File metadata and controls
73 lines (59 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const client = new Appwrite.Client()
client
.setEndpoint('https://fra.cloud.appwrite.io/v1') // e.g., https://fra.cloud.appwrite.io/v1
.setProject('66f6aece003e147b3811') // Replace with your Project ID
const databases = new Appwrite.Databases(client)
const databaseId = 'support-db' // Replace with your Database ID
const collectionId = 'tickets' // Replace with your Collection ID
const form = document.getElementById('ticket-form')
form.addEventListener('submit', async (e) => {
e.preventDefault()
const title = document.getElementById('title').value.trim()
const body = document.getElementById('body').value.trim()
if (!title || !body) return
try {
await databases.createDocument(databaseId, collectionId, 'unique()', {
title,
body,
})
form.reset()
loadTickets()
} catch (error) {
console.error(error)
}
})
async function loadTickets() {
try {
const response = await databases.listDocuments(databaseId, collectionId, [
Appwrite.Query.orderDesc('$sequence'),
])
const ticketList = document.getElementById('ticket-list')
const ticketCount = document.getElementById('ticket-count')
const emptyState = document.getElementById('empty-state')
ticketList.innerHTML = ''
if (response.documents.length === 0) {
emptyState.style.display = 'block'
ticketCount.textContent = '0 tickets'
} else {
emptyState.style.display = 'none'
ticketCount.textContent = `${response.documents.length} ticket${
response.documents.length === 1 ? '' : 's'
}`
response.documents.forEach((ticket, index) => {
const ticketElement = document.createElement('div')
ticketElement.className = 'card u-padding-24'
ticketElement.innerHTML = `
<div class="tag is-color-primary u-margin-block-end-12">
#${String(ticket.$sequence).padStart(3, '0')}
</div>
<h3 class="heading-level-6 u-margin-block-end-8">${ticket.title}</h3>
<p class="body-text-2">${ticket.body}</p>
`
ticketList.appendChild(ticketElement)
})
}
} catch (error) {
console.error(error)
}
}
loadTickets()