Source: stores/topics.js

/*
 * Copyright © 2024-2025 The CTAN Team and individual authors
 *
 * This file is distributed under the 3-clause BSD license.
 * See file LICENSE for details.
 */
import { defineStore } from 'pinia'

/**
 * This is the topics store.
 *
 * @author <a href="mailto:gene@ctan.org">Gerd Neugebauer</a>
 */
export const useTopicsStore = defineStore('topics', {
  state: () => ({
    /**
     * The error status message.
     */
    error: '',
    /**
     * The indicator that the back-end communication is in progress.
     */
    loading: false,
    /**
     * The list of known topics.
     */
    list: []
  }),

  actions: {
    /**
     * Reset the error status. 
     */
    clearError () {
      this.error = ''
    },

    /**
     * Load the data.
     */
    async ensureLoaded () {
      if (this.list.length > 0) {
        return
      }
      this.loading = true
      this.error = ''
      try {
        const data = await $fetch('/api/3.0/topic/list', {
          query: { lang: 'en' }
        })
        this.list = data
      } catch (error) {
        this.error = error
        this.topics = []
      } finally {
        this.loading = false
      }
    }
  }
})