forked from NdoleStudio/httpsms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreads.ts
More file actions
122 lines (107 loc) · 3 KB
/
Copy paththreads.ts
File metadata and controls
122 lines (107 loc) · 3 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { defineStore } from 'pinia'
import type {
EntitiesMessageThread,
EntitiesMessage,
} from '~~/shared/types/api'
export const useThreadsStore = defineStore('threads', () => {
const threads = ref<EntitiesMessageThread[]>([])
const threadId = ref<string | null>(null)
const loadingThreads = ref(true)
const archivedThreads = ref(false)
const { apiFetch } = useApi()
const notificationsStore = useNotificationsStore()
const currentThread = computed<EntitiesMessageThread | null>(() => {
return threads.value.find((x) => x.id === threadId.value) ?? null
})
const hasThread = computed(
() => threadId.value != null && !loadingThreads.value,
)
function hasThreadId(id: string): boolean {
return threads.value.find((x) => x.id === id) !== undefined
}
async function loadThreads() {
const phonesStore = usePhonesStore()
if (phonesStore.owner === null && phonesStore.phones.length === 0) {
loadingThreads.value = false
return
}
const response = await apiFetch<{ data: EntitiesMessageThread[] }>(
'/v1/message-threads',
{
params: {
owner: phonesStore.owner ?? phonesStore.phones[0]?.phone_number,
limit: 100,
is_archived: archivedThreads.value,
},
},
)
phonesStore.getHeartbeat().catch(console.error)
threads.value = [...response.data]
loadingThreads.value = false
}
async function loadThreadMessages(
id: string | null,
): Promise<EntitiesMessage[]> {
threadId.value = id
const thread = currentThread.value
if (!thread) throw new Error(`Cannot find thread with id ${id}`)
const response = await apiFetch<{ data: EntitiesMessage[] }>(
'/v1/messages',
{
params: {
contact: thread.contact,
owner: thread.owner,
limit: 50,
},
},
)
return response.data
}
function setThreadId(id: string | null) {
threadId.value = id
}
function toggleArchive() {
archivedThreads.value = !archivedThreads.value
}
async function updateThread(payload: {
threadId: string
isArchived: boolean
}) {
await apiFetch(`/v1/message-threads/${payload.threadId}`, {
method: 'PUT',
body: { is_archived: payload.isArchived },
})
archivedThreads.value = payload.isArchived
await loadThreads()
}
async function deleteThread(id: string) {
await apiFetch(`/v1/message-threads/${id}`, { method: 'DELETE' })
threadId.value = null
notificationsStore.addNotification({
message: 'The message thread has been deleted successfully',
type: 'success',
})
}
function resetState() {
threads.value = []
threadId.value = null
archivedThreads.value = false
loadingThreads.value = true
}
return {
threads,
threadId,
loadingThreads,
archivedThreads,
currentThread,
hasThread,
hasThreadId,
loadThreads,
loadThreadMessages,
setThreadId,
toggleArchive,
updateThread,
deleteThread,
resetState,
}
})