forked from buggregator/frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuse-events-api.ts
More file actions
150 lines (121 loc) · 3.67 KB
/
use-events-api.ts
File metadata and controls
150 lines (121 loc) · 3.67 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { storeToRefs } from "pinia";
import type { Ref } from 'vue';
import { useEventsStore } from "../../stores";
import { EventTypes, type EventId, type EventType, type EventsPreviewMeta, type ServerEvent } from '../../types';
import { useApiTransport } from '../use-api-transport'
export type TUseEventsApi = {
items: Ref<ServerEvent<unknown>[]>
getItem: (id: EventId) => Promise<ServerEvent<EventType> | null>
getAll: () => void
loadMoreByType: (
type: EventType,
cursor?: string | null
) => Promise<{ meta: EventsPreviewMeta | null; data: ServerEvent<unknown>[] }>
removeAll: () => void
removeByType: (type: EventType) => void
removeById: (id: EventId) => void
}
export const useEventsApi = (): TUseEventsApi => {
const eventsStore = useEventsStore();
const {
lockedIds,
events,
} = storeToRefs(eventsStore)
const {
deleteEventsAll,
deleteEventsList,
deleteEventsByType,
getEventsPreviewByTypePage,
getEventsTypeCounts,
getEvent,
} = useApiTransport();
const LOADING_PAGE_SIZE = 25;
const removeList = async (uuids: EventId[]) => {
const res = await deleteEventsList(uuids)
if (res) {
eventsStore.removeByIds(uuids);
}
}
const removeAll = async () => {
if (lockedIds.value.length) {
const removedIds = events.value
.filter(({ uuid }) => !lockedIds.value.includes(uuid))
.map(({ uuid }) => uuid)
await removeList(removedIds)
return
}
const res = await deleteEventsAll()
if (res) {
eventsStore.removeAll()
}
}
const removeById = async (eventId: EventId) => {
await removeList([eventId])
}
const removeByType = async (eventType: EventType) => {
if (lockedIds.value.length) {
const removedIds = events.value
.filter(({ type, uuid }) => type === eventType && !lockedIds.value.includes(uuid))
.map(({ uuid }) => uuid)
await removeList(removedIds)
return
}
const res = await deleteEventsByType(eventType)
if (res) {
eventsStore.removeByType(eventType);
}
}
const getAll = () => {
eventsStore.initializeEvents([]);
eventsStore.resetPreviewPagination();
getEventsTypeCounts()
.then((typeCounts) => {
eventsStore.setEventCounts(typeCounts);
})
.catch((e) => {
console.error(e);
eventsStore.resetEventCounts();
});
Object.values(EventTypes).forEach((eventType) => {
getEventsPreviewByTypePage(eventType, LOADING_PAGE_SIZE)
.then(({ data, meta }) => {
if (data.length) {
eventsStore.mergeEvents(data, { updateCounts: false });
}
eventsStore.setPreviewPagination(eventType, meta);
})
.catch((e) => {
console.error(e);
})
})
}
const loadMoreByType = async (eventType: EventType, cursor?: string | null) => {
const pagination = eventsStore.previewPagination[eventType as EventTypes];
if (!pagination?.hasMore) {
return { meta: null, data: [] };
}
const requestCursor = cursor ?? pagination.cursor;
const { data, meta } = await getEventsPreviewByTypePage(
eventType,
LOADING_PAGE_SIZE,
requestCursor,
);
if (data.length) {
eventsStore.mergeEvents(data, { updateCounts: false });
if (eventsStore.cachedIds[eventType]?.length) {
eventsStore.appendCachedIds(eventType, data.map(({ uuid }) => uuid));
}
}
eventsStore.setPreviewPagination(eventType, meta);
return { meta, data };
}
return {
items: events as unknown as Ref<ServerEvent<unknown>[]>,
getItem: getEvent,
getAll,
loadMoreByType,
removeAll,
removeByType,
removeById,
}
}