Skip to main content

Overview

The useNoticiasStore manages news articles (noticias) with support for location-based filtering by parish (parroquia) and neighborhood (barrio). It handles CRUD operations and provides different views for citizens and administrators.

State Properties

noticias
Noticia[]
Array of news articles
noticiaActual
Noticia | null
Currently selected/viewed news article
loading
boolean
Loading state for async operations
error
string | null
Error message from last failed operation

Actions

fetchNoticias

Fetches news articles with optional location filters.
const result = await noticiasStore.fetchNoticias(filtros)
filtros
object
Optional filters for news articles
return
Promise<{ success: boolean; data?: Noticia[]; error?: string }>
Result object with news articles array
Filtering Logic:
  • No filters: Returns only global news (parroquia_destino = null)
  • With parroquia only: Returns global news + news for that parish
  • With parroquia and barrio: Returns global news + parish news + neighborhood-specific news

fetchNoticia

Fetches a single news article by ID.
const result = await noticiasStore.fetchNoticia(id)
id
string
required
News article ID
return
Promise<{ success: boolean; data?: Noticia; error?: string }>
Result object with news article data
Behavior:
  • Clears previous noticiaActual state before fetching
  • Updates noticiaActual with fetched article
  • Returns error if article not found

crearNoticia

Creates a new news article (admin only).
const result = await noticiasStore.crearNoticia(noticia)
noticia
Omit<InsertNoticia, 'administrador_id'>
required
News article data (administrador_id is added automatically)
return
Promise<{ success: boolean; data?: Noticia; error?: string }>
Result object with created article data
Behavior:
  • Verifies current user is an administrator
  • Adds administrador_id automatically from auth store
  • Prepends new article to local noticias array

actualizarNoticia

Updates an existing news article.
const result = await noticiasStore.actualizarNoticia(id, updates)
id
string
required
News article ID to update
updates
Partial<Omit<InsertNoticia, 'administrador_id'>>
required
Fields to update
return
Promise<{ success: boolean; data?: Noticia; error?: string }>
Result object with updated article data
Behavior:
  • Automatically updates updated_at timestamp
  • Updates article in local noticias array if present

eliminarNoticia

Deletes a news article.
const result = await noticiasStore.eliminarNoticia(id)
id
string
required
News article ID to delete
return
Promise<{ success: boolean; error?: string }>
Result object indicating success or failure
Behavior:
  • Removes article from local noticias array after successful deletion

fetchNoticiasUsuario

Fetches news articles filtered for a specific user’s location.
const result = await noticiasStore.fetchNoticiasUsuario(parroquia, barrio)
parroquia
string
required
User’s parish
barrio
string
User’s neighborhood (optional)
return
Promise<{ success: boolean; data?: Noticia[]; error?: string }>
Result object with filtered news articles
Filtering Logic:
  1. Global news (parroquia_destino = null)
  2. Parish-wide news (parroquia_destino = user’s parish AND barrio_destino = null)
  3. If user has barrio: Neighborhood-specific news (parroquia_destino = user’s parish AND barrio_destino = user’s barrio)

fetchTodasLasNoticias

Fetches ALL news articles without filters (admin only).
const result = await noticiasStore.fetchTodasLasNoticias()
return
Promise<{ success: boolean; data?: Noticia[]; error?: string }>
Result object with all news articles
Behavior:
  • Returns every news article regardless of location filters
  • Intended for administrator dashboard views

Usage Examples

<script setup>
import { useNoticiasStore } from '@/stores/noticias.store'
import { useAuthStore } from '@/stores/auth.store'
import { onMounted } from 'vue'

const noticiasStore = useNoticiasStore()
const authStore = useAuthStore()

onMounted(async () => {
  const usuario = authStore.usuario
  
  if (usuario) {
    // Fetch news for user's location
    await noticiasStore.fetchNoticiasUsuario(
      usuario.parroquia,
      usuario.barrio
    )
    
    console.log('Noticias cargadas:', noticiasStore.noticias.length)
  }
})
</script>

Location-Based Filtering

News Visibility Hierarchy:
  1. Global News (parroquia_destino = null): Visible to everyone
  2. Parish News (parroquia_destino set, barrio_destino = null): Visible to all users in that parish
  3. Neighborhood News (both parroquia_destino and barrio_destino set): Visible only to users in that specific neighborhood
The store prevents concurrent fetches by returning current data if a fetch is already in progress. This avoids duplicate requests and improves performance.

Notes

Use fetchNoticiasUsuario() for citizen views and fetchTodasLasNoticias() for admin dashboards to ensure appropriate data filtering.

Build docs developers (and LLMs) love