Stores

Pinia stores structure

Balance uses Pinia stores and, as mentioned earlier, most of the stores will be divided into two files: index and single.

For more info check the offical Pinia documentation.

Here's an example of an index store:

interface IndexThreadState {
    data: Array<Thread>
    total: number
    query: Query
    loading: boolean
    filters: Array<Filter>
    links: Array<any>
}

export const useThreadsIndexStore = defineStore('cruds-threads-index', {
    state: (): IndexThreadState => ({
        data: [],
        total: 0,
        query: {
            sort: 'id',
            order: 'desc',
            limit: 50,
            s: '',
            page: 1,
        },
        loading: false,
        links: [],
        filters: [],
    }),

    actions: {
        async fetchIndexData() {
            this.loading = true
            const filters = this.filters
            const params: any = {
                ...this.query,
                filters,
            }
            try {
                fetchIndexDataResponse.value = executeSWRV(route, params)
                if (!fetchIndexDataResponse.value.data) {
                    await fetchIndexDataResponse.value.mutate()
                }
            } finally {
                this.loading = false
            }
        },

        async destroyData(id: string | number, confirm = false) {
            try {
                await axios.delete(`${route}/${id}`, { params: { confirm } })
                this.fetchIndexData()
            } catch (error: any) {
                //const message = error.response.data.messages
            }
        },
        setQuery(query: Query) {
            this.query = query
        },
        setFilters(filters: Array<Filter>) {
            this.filters = filters
        },
        resetState() {
            this.$reset()
        },
    },
})

Here's an example of a single store:

And here's an example of a generic store:

Last updated