All files / composables useEvents.ts

100% Statements 34/34
100% Branches 6/6
100% Functions 6/6
100% Lines 34/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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            1x     1x 26x     26x     26x         26x 2x 2x         26x 15x 15x         26x 2x 2x         26x 2x 2x         26x 2x 2x   26x   26x 26x 26x     26x 26x     26x 26x 26x 26x 26x 26x 26x 26x  
/**
 * Events Composable
 * Exposes events store state and actions with proper reactivity
 * Following Pinia storeToRefs pattern from documentation
 */
 
import { useEventsStore } from '~/stores/events'
import type { Event, CreateEventPayload, UpdateEventPayload } from '~/types'
 
export function useEvents() {
  const store = useEventsStore()
 
  // Extract reactive state and getters with storeToRefs
  const { events, loading, error, upcomingEvents, pastEvents } = storeToRefs(store)
 
  // Actions are destructured directly from store
  const { fetchEvents, createEvent, updateEvent, deleteEvent, clearError } = store
 
  /**
   * Get event by ID (reactive)
   */
  const getEventById = (id: string): Event | undefined => {
    return store.getEventById(id)
  }
 
  /**
   * Refresh events from API
   */
  const refresh = async (): Promise<void> => {
    await fetchEvents()
  }
 
  /**
   * Create event and return result
   */
  const create = async (payload: CreateEventPayload): Promise<Event | null> => {
    return await createEvent(payload)
  }
 
  /**
   * Update event and return result
   */
  const update = async (id: string, payload: UpdateEventPayload): Promise<Event | null> => {
    return await updateEvent(id, payload)
  }
 
  /**
   * Delete event and return success status
   */
  const remove = async (id: string): Promise<boolean> => {
    return await deleteEvent(id)
  }
 
  return {
    // Reactive state (refs)
    events,
    loading,
    error,
 
    // Reactive getters (computed refs)
    upcomingEvents,
    pastEvents,
 
    // Methods
    getEventById,
    refresh,
    create,
    update,
    remove,
    clearError
  }
}