11import { storeToRefs } from "pinia" ;
22import { useEventsStore , useProfileStore } from "../../stores" ;
3- import type { EventId , EventType , ServerEvent } from '../../types' ;
3+ import type { EventId , EventType , EventTypeCount , EventsPreviewMeta , ServerEvent } from '../../types' ;
44import { REST_API_URL } from "./constants" ;
55
66type TUseEventsRequests = ( ) => {
77 getAll : ( ) => Promise < ServerEvent < unknown > [ ] > ,
8+ getPreviewPageByType : (
9+ type : EventType ,
10+ limit : number ,
11+ cursor ?: string | null
12+ ) => Promise < { data : ServerEvent < unknown > [ ] ; meta : EventsPreviewMeta | null } > ,
13+ getTypeCounts : ( ) => Promise < EventTypeCount [ ] > ,
814 getSingle : ( id : EventId ) => Promise < ServerEvent < EventType > | null > ,
915 deleteAll : ( ) => Promise < void | Response > ,
1016 deleteList : ( uuids : EventId [ ] ) => Promise < void | Response > ,
@@ -22,6 +28,25 @@ export const useEventsRequests: TUseEventsRequests = () => {
2228 const headers = { "X-Auth-Token" : token . value }
2329 const getEventRestUrl = ( param : string ) : string => `${ REST_API_URL } /api/event/${ param } ${ project . value ? `?project=${ project . value } ` : '' } `
2430 const getEventsPreviewRestUrl = ( ) : string => `${ REST_API_URL } /api/events/preview${ project . value ? `?project=${ project . value } ` : '' } `
31+ const getEventsPreviewByTypeRestUrl = (
32+ type : EventType ,
33+ limit : number ,
34+ cursor ?: string | null
35+ ) : string => {
36+ const params = new URLSearchParams ( {
37+ type,
38+ limit : String ( limit ) ,
39+ ...( project . value ? { project : project . value } : { } ) ,
40+ } )
41+
42+ if ( cursor ) {
43+ params . set ( 'cursor' , cursor )
44+ }
45+
46+ return `${ REST_API_URL } /api/events/preview?${ params . toString ( ) } `
47+ }
48+ const getEventsTypeCountsRestUrl = ( ) : string =>
49+ `${ REST_API_URL } /api/events/type-counts${ project . value ? `?project=${ project . value } ` : '' } `
2550
2651 const getAll = ( ) => fetch ( getEventsPreviewRestUrl ( ) , { headers } )
2752 . then ( ( response ) => response . json ( ) )
@@ -41,6 +66,45 @@ export const useEventsRequests: TUseEventsRequests = () => {
4166 } )
4267 . then ( ( events : ServerEvent < unknown > [ ] ) => events )
4368
69+ const getPreviewPageByType = ( type : EventType , limit : number , cursor ?: string | null ) =>
70+ fetch ( getEventsPreviewByTypeRestUrl ( type , limit , cursor ) , { headers } )
71+ . then ( ( response ) => response . json ( ) )
72+ . then ( ( response ) => {
73+ if ( response ?. data ) {
74+ return {
75+ data : response . data as ServerEvent < unknown > [ ] ,
76+ meta : ( response . meta ?? null ) as EventsPreviewMeta | null ,
77+ }
78+ }
79+
80+ if ( response ?. code === 403 ) {
81+ console . error ( 'Forbidden' )
82+ return { data : [ ] , meta : null }
83+ }
84+
85+ console . error ( 'Fetch Error' )
86+
87+ return { data : [ ] , meta : null }
88+ } )
89+
90+ const getTypeCounts = ( ) => fetch ( getEventsTypeCountsRestUrl ( ) , { headers } )
91+ . then ( ( response ) => response . json ( ) )
92+ . then ( ( response ) => {
93+ if ( response ?. data ) {
94+ return response . data as EventTypeCount [ ]
95+ }
96+
97+ if ( response ?. code === 403 ) {
98+ console . error ( 'Forbidden' )
99+ return [ ] ;
100+ }
101+
102+ console . error ( 'Fetch Error' )
103+
104+ return [ ] ;
105+ } )
106+ . then ( ( counts : EventTypeCount [ ] ) => counts )
107+
44108 const getSingle = ( id : EventId ) => fetch ( getEventRestUrl ( id ) , { headers} )
45109 . then ( ( response ) => response . json ( ) )
46110 . then ( ( response ) => {
@@ -91,6 +155,8 @@ export const useEventsRequests: TUseEventsRequests = () => {
91155
92156 return {
93157 getAll,
158+ getPreviewPageByType,
159+ getTypeCounts,
94160 getSingle,
95161 deleteAll,
96162 deleteList,
0 commit comments