chart.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import { defineStore } from 'pinia'
  2. import { consultationApi, chartApi, annotationApi } from '../utils/api'
  3. export const useChartStore = defineStore('chart', {
  4. state: () => ({
  5. currentName: '',
  6. currentBirthYear: 0,
  7. currentBirthMonth: 0,
  8. currentBirthDay: 0,
  9. currentChart: null,
  10. currentRecordId: null,
  11. currentQuestions: '',
  12. records: [],
  13. annotations: [],
  14. tags: [],
  15. consultationMessages: []
  16. }),
  17. actions: {
  18. /**
  19. * Start consultation via backend API (US-1.1 §13 — main path only).
  20. * All computation is on the backend CalculatorService.
  21. */
  22. async startConsultation(name, birthday, questions) {
  23. const result = await consultationApi.start({ name, birthday, questions })
  24. const record = result.record
  25. this.currentRecordId = record.id
  26. this.currentName = record.name || name
  27. this.currentQuestions = questions || ''
  28. this.consultationMessages = result.messages || []
  29. // Parse backend chart data
  30. if (record.chartData) {
  31. const parsed = typeof record.chartData === 'string'
  32. ? JSON.parse(record.chartData) : record.chartData
  33. this.currentChart = parsed.positions || parsed
  34. }
  35. return result
  36. },
  37. async saveChart(name, birthday, questions) {
  38. const chartData = JSON.stringify(this.currentChart)
  39. const record = await chartApi.create({ name, birthday, chartData, questions })
  40. this.currentRecordId = record.id || null
  41. this.currentQuestions = questions || ''
  42. return record
  43. },
  44. async loadChart(id) {
  45. const record = await chartApi.detail(id)
  46. if (record && record.chartData) {
  47. this.currentChart = typeof record.chartData === 'string'
  48. ? JSON.parse(record.chartData) : record.chartData
  49. this.currentRecordId = id
  50. }
  51. return record
  52. },
  53. async fetchRecords() {
  54. this.records = await chartApi.list()
  55. },
  56. async fetchAnnotations(recordId) {
  57. this.annotations = await annotationApi.list(recordId)
  58. },
  59. async addAnnotation(recordId, position, content) {
  60. return await annotationApi.add({ recordId, position, content })
  61. },
  62. async fetchTags(recordId) {
  63. this.tags = await annotationApi.listTags(recordId)
  64. },
  65. async addTag(recordId, tag) {
  66. return await annotationApi.addTag({ recordId, tag })
  67. }
  68. }
  69. })