/
itp_practice
/
itp_frontend
Обзор
Документация
Войти
/
itp_practice
/
itp_frontend
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
master
src/stores/tariffs.js
47 строк
1 KB
vivanenko
add purchase create from admin
29 май 2025, 11:13
29 май 2025, 11:13
6d551be
Код
Авторство
О чём код?
import { defineStore } from 'pinia'; import { api } from '@/services/api'; export const useTariffsStore = defineStore('tariffs', { state: () => ({ tariffs: [], loading: false, error: null }), actions: { async fetchTariffs() { this.loading = true; try { const response = await api.get('/tariffs'); // Flatten the nested structure to get all child tariffs with their parent information this.tariffs = response.data.data.reduce((acc, parent) => { // For each parent tariff, add its children to the accumulator // with a reference to the parent const childTariffs = parent.children.map(child => ({ ...child, parent: { id: parent.id, name: parent.name } })); return [...acc, ...childTariffs]; }, []); } catch (error) { this.error = error.message; throw error; } finally { this.loading = false; } } }, getters: { // Получаем отформатированные опции для селекта formattedTariffs: (state) => { return state.tariffs.map(tariff => ({ ...tariff, formattedName: `${tariff.parent.name} (${tariff.name})` })); } } });