feat(store): implement initReport / reset / hydrateReport
This commit is contained in:
1 parent
f65f3e7b8c
commit
38606a11e4
2 files changed
+134
-59
No files matched your search
+94
-59
@@ -1,83 +1,118 @@
|
|||||||
import { create } from 'zustand'
|
import { create } from 'zustand'
|
||||||
import type { Report } from '@/domain/schema/report'
|
import type { Report } from '@/domain/schema/report'
|
||||||
import type { Observation } from '@/domain/schema/observation'
|
import type { Observation } from '@/domain/schema/observation'
|
||||||
|
import { generateId } from '@/lib/id'
|
||||||
|
import { nowIso } from '@/lib/dates'
|
||||||
|
|
||||||
export interface ReportStore {
|
export interface ReportStore {
|
||||||
/** Currently open report, or null if no session active */
|
|
||||||
report: Report | null
|
report: Report | null
|
||||||
|
|
||||||
/** Index of the currently active observation (0-based) */
|
|
||||||
activeObservationIndex: number
|
activeObservationIndex: number
|
||||||
|
|
||||||
/** Starts a new report from a partial template */
|
|
||||||
initReport: (draft: Omit<Report, 'id' | 'createdAt' | 'version'>) => void
|
initReport: (draft: Omit<Report, 'id' | 'createdAt' | 'version'>) => void
|
||||||
|
|
||||||
/** Replaces the entire report (used after PDF import) */
|
|
||||||
hydrateReport: (report: Report) => void
|
hydrateReport: (report: Report) => void
|
||||||
|
|
||||||
/** Adds a new empty observation and sets it as active */
|
|
||||||
addObservation: () => void
|
addObservation: () => void
|
||||||
|
|
||||||
/** Removes observation at given index */
|
|
||||||
removeObservation: (index: number) => void
|
removeObservation: (index: number) => void
|
||||||
|
|
||||||
/** Moves observation up (index - 1) */
|
|
||||||
moveObservationUp: (index: number) => void
|
moveObservationUp: (index: number) => void
|
||||||
|
|
||||||
/** Moves observation down (index + 1) */
|
|
||||||
moveObservationDown: (index: number) => void
|
moveObservationDown: (index: number) => void
|
||||||
|
|
||||||
/** Replaces the observation at given index */
|
|
||||||
updateObservation: (index: number, obs: Observation) => void
|
updateObservation: (index: number, obs: Observation) => void
|
||||||
|
|
||||||
/** Sets active observation index */
|
|
||||||
setActiveObservation: (index: number) => void
|
setActiveObservation: (index: number) => void
|
||||||
|
|
||||||
/** Updates conclusion text */
|
|
||||||
setConclusion: (text: string) => void
|
setConclusion: (text: string) => void
|
||||||
|
|
||||||
/** Updates recommendations text */
|
|
||||||
setRecommendations: (text: string) => void
|
setRecommendations: (text: string) => void
|
||||||
|
setNextAppointment: (next: Report['nextAppointment']) => void
|
||||||
/** Clears the store (after export or cancel) */
|
|
||||||
reset: () => void
|
reset: () => void
|
||||||
}
|
}
|
||||||
|
|
||||||
export const useReportStore = create<ReportStore>(() => ({
|
function emptyObservation(): Observation {
|
||||||
|
return {
|
||||||
|
id: generateId(),
|
||||||
|
zones: [],
|
||||||
|
pathologyIds: [],
|
||||||
|
freeTextPathologies: [],
|
||||||
|
comment: '',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useReportStore = create<ReportStore>((set) => ({
|
||||||
report: null,
|
report: null,
|
||||||
activeObservationIndex: 0,
|
activeObservationIndex: 0,
|
||||||
|
|
||||||
initReport: () => {
|
initReport: (draft) =>
|
||||||
throw new Error('not implemented')
|
set({
|
||||||
},
|
report: { ...draft, version: 1, id: generateId(), createdAt: nowIso() },
|
||||||
hydrateReport: () => {
|
activeObservationIndex: 0,
|
||||||
throw new Error('not implemented')
|
}),
|
||||||
},
|
|
||||||
addObservation: () => {
|
hydrateReport: (report) => set({ report, activeObservationIndex: 0 }),
|
||||||
throw new Error('not implemented')
|
|
||||||
},
|
addObservation: () =>
|
||||||
removeObservation: () => {
|
set((s) => {
|
||||||
throw new Error('not implemented')
|
if (!s.report) return s
|
||||||
},
|
const obs = emptyObservation()
|
||||||
moveObservationUp: () => {
|
const observations = [...s.report.observations, obs]
|
||||||
throw new Error('not implemented')
|
return {
|
||||||
},
|
report: { ...s.report, observations },
|
||||||
moveObservationDown: () => {
|
activeObservationIndex: observations.length - 1,
|
||||||
throw new Error('not implemented')
|
}
|
||||||
},
|
}),
|
||||||
updateObservation: () => {
|
|
||||||
throw new Error('not implemented')
|
removeObservation: (index) =>
|
||||||
},
|
set((s) => {
|
||||||
setActiveObservation: () => {
|
if (!s.report) return s
|
||||||
throw new Error('not implemented')
|
const observations = s.report.observations.filter((_, i) => i !== index)
|
||||||
},
|
const active = Math.min(
|
||||||
setConclusion: () => {
|
s.activeObservationIndex,
|
||||||
throw new Error('not implemented')
|
Math.max(0, observations.length - 1),
|
||||||
},
|
)
|
||||||
setRecommendations: () => {
|
return {
|
||||||
throw new Error('not implemented')
|
report: { ...s.report, observations },
|
||||||
},
|
activeObservationIndex: active,
|
||||||
reset: () => {
|
}
|
||||||
throw new Error('not implemented')
|
}),
|
||||||
},
|
|
||||||
|
moveObservationUp: (index) =>
|
||||||
|
set((s) => {
|
||||||
|
if (!s.report || index <= 0) return s
|
||||||
|
const observations = [...s.report.observations]
|
||||||
|
;[observations[index - 1], observations[index]] = [
|
||||||
|
observations[index],
|
||||||
|
observations[index - 1],
|
||||||
|
]
|
||||||
|
return { report: { ...s.report, observations } }
|
||||||
|
}),
|
||||||
|
|
||||||
|
moveObservationDown: (index) =>
|
||||||
|
set((s) => {
|
||||||
|
if (!s.report || index >= s.report.observations.length - 1) return s
|
||||||
|
const observations = [...s.report.observations]
|
||||||
|
;[observations[index], observations[index + 1]] = [
|
||||||
|
observations[index + 1],
|
||||||
|
observations[index],
|
||||||
|
]
|
||||||
|
return { report: { ...s.report, observations } }
|
||||||
|
}),
|
||||||
|
|
||||||
|
updateObservation: (index, obs) =>
|
||||||
|
set((s) => {
|
||||||
|
if (!s.report) return s
|
||||||
|
const observations = s.report.observations.map((o, i) =>
|
||||||
|
i === index ? obs : o,
|
||||||
|
)
|
||||||
|
return { report: { ...s.report, observations } }
|
||||||
|
}),
|
||||||
|
|
||||||
|
setActiveObservation: (index) => set({ activeObservationIndex: index }),
|
||||||
|
|
||||||
|
setConclusion: (text) =>
|
||||||
|
set((s) => (s.report ? { report: { ...s.report, conclusion: text } } : s)),
|
||||||
|
|
||||||
|
setRecommendations: (text) =>
|
||||||
|
set((s) =>
|
||||||
|
s.report ? { report: { ...s.report, recommendations: text } } : s,
|
||||||
|
),
|
||||||
|
|
||||||
|
setNextAppointment: (next) =>
|
||||||
|
set((s) =>
|
||||||
|
s.report ? { report: { ...s.report, nextAppointment: next } } : s,
|
||||||
|
),
|
||||||
|
|
||||||
|
reset: () => set({ report: null, activeObservationIndex: 0 }),
|
||||||
}))
|
}))
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
import { describe, it, expect, beforeEach } from 'vitest'
|
||||||
|
import { useReportStore } from '@/state/reportStore'
|
||||||
|
|
||||||
|
const draft = {
|
||||||
|
date: '2026-05-18',
|
||||||
|
practitioner: { name: 'Dr. Dupont' },
|
||||||
|
animal: { speciesId: 'equine', name: 'Mistral' },
|
||||||
|
owner: { name: 'Jean Martin' },
|
||||||
|
observations: [],
|
||||||
|
conclusion: '',
|
||||||
|
recommendations: '',
|
||||||
|
}
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
useReportStore.getState().reset()
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('reportStore.initReport', () => {
|
||||||
|
it('sets a report with version=1, id, and createdAt', () => {
|
||||||
|
useReportStore.getState().initReport(draft)
|
||||||
|
const r = useReportStore.getState().report!
|
||||||
|
expect(r.version).toBe(1)
|
||||||
|
expect(r.id.length).toBeGreaterThan(0)
|
||||||
|
expect(r.createdAt).toMatch(/^\d{4}-\d{2}-\d{2}T/)
|
||||||
|
expect(r.practitioner.name).toBe('Dr. Dupont')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('resets activeObservationIndex to 0', () => {
|
||||||
|
useReportStore.getState().initReport(draft)
|
||||||
|
expect(useReportStore.getState().activeObservationIndex).toBe(0)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe('reportStore.reset', () => {
|
||||||
|
it('clears the report', () => {
|
||||||
|
useReportStore.getState().initReport(draft)
|
||||||
|
useReportStore.getState().reset()
|
||||||
|
expect(useReportStore.getState().report).toBeNull()
|
||||||
|
})
|
||||||
|
})
|
||||||
Reference in new issue
Block a user