All files / src/stores ribbons.ts

100% Statements 34/34
60% Branches 3/5
100% Functions 8/8
100% Lines 32/32

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 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                                                                    6x   29x 29x 29x     29x 29x     13x 13x 13x 13x 12x   1x 1x   13x         11x 11x 11x 4x   1x 1x   5x         3x 2x 2x       4x 4x 4x 3x   4x       2x 2x     29x                          
import { defineStore } from 'pinia'
import { ref } from 'vue'
import api from '@/services/api'
 
export type RibbonMessage = string | Record<string, string>
 
export interface Ribbon {
  id: string
  message: RibbonMessage
  color: string
  active: boolean
  createdAt: string
  createdBy?: string
  updatedAt?: string
  updatedBy?: string
}
 
export interface ActiveRibbon {
  id: string
  message: RibbonMessage
  color: string
}
 
export interface RibbonInput {
  message: RibbonMessage
  color: string
  active?: boolean
}
 
/**
 * Platform-wide announcement ribbons (maintenance notices, etc.), managed by
 * platform admins (see stores/admin.ts for the analytics half of that page)
 * and displayed below the header on every page (see RibbonsBanner.vue).
 */
export const useRibbonsStore = defineStore('ribbons', () => {
  // Full list — platform-admin management UI only
  const ribbons = ref<Ribbon[]>([])
  const loading = ref(false)
  const error = ref<string | null>(null)
 
  // Active-only list — the banner shown to every authenticated user
  const activeRibbons = ref<ActiveRibbon[]>([])
  const activeRibbonsLoading = ref(false)
 
  async function fetchRibbons() {
    loading.value = true
    error.value = null
    try {
      const response = await api.get('/v1/platform-admin/ribbons')
      ribbons.value = response.data
    } catch (err: any) {
      error.value = err.response?.data?.error || err.message || 'Failed to load ribbons'
      throw err
    } finally {
      loading.value = false
    }
  }
 
  async function fetchActiveRibbons() {
    activeRibbonsLoading.value = true
    try {
      const response = await api.get('/v1/ribbons/active')
      activeRibbons.value = response.data
    } catch (err) {
      console.error('Failed to fetch active ribbons:', err)
      activeRibbons.value = []
    } finally {
      activeRibbonsLoading.value = false
    }
  }
 
  async function createRibbon(data: RibbonInput): Promise<Ribbon> {
    const response = await api.post('/v1/platform-admin/ribbons', data)
    ribbons.value.push(response.data)
    return response.data
  }
 
  async function updateRibbon(id: string, data: Partial<RibbonInput>): Promise<Ribbon> {
    const response = await api.put(`/v1/platform-admin/ribbons/${id}`, data)
    const index = ribbons.value.findIndex((r) => r.id === id)
    if (index !== -1) {
      ribbons.value[index] = response.data
    }
    return response.data
  }
 
  async function deleteRibbon(id: string) {
    await api.delete(`/v1/platform-admin/ribbons/${id}`)
    ribbons.value = ribbons.value.filter((r) => r.id !== id)
  }
 
  return {
    ribbons,
    loading,
    error,
    activeRibbons,
    activeRibbonsLoading,
    fetchRibbons,
    fetchActiveRibbons,
    createRibbon,
    updateRibbon,
    deleteRibbon
  }
})