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 type { Report } from '@/domain/schema/report'
|
||||
import type { Observation } from '@/domain/schema/observation'
|
||||
import { generateId } from '@/lib/id'
|
||||
import { nowIso } from '@/lib/dates'
|
||||
|
||||
export interface ReportStore {
|
||||
/** Currently open report, or null if no session active */
|
||||
report: Report | null
|
||||
|
||||
/** Index of the currently active observation (0-based) */
|
||||
activeObservationIndex: number
|
||||
|
||||
/** Starts a new report from a partial template */
|
||||
initReport: (draft: Omit<Report, 'id' | 'createdAt' | 'version'>) => void
|
||||
|
||||
/** Replaces the entire report (used after PDF import) */
|
||||
hydrateReport: (report: Report) => void
|
||||
|
||||
/** Adds a new empty observation and sets it as active */
|
||||
addObservation: () => void
|
||||
|
||||
/** Removes observation at given index */
|
||||
removeObservation: (index: number) => void
|
||||
|
||||
/** Moves observation up (index - 1) */
|
||||
moveObservationUp: (index: number) => void
|
||||
|
||||
/** Moves observation down (index + 1) */
|
||||
moveObservationDown: (index: number) => void
|
||||
|
||||
/** Replaces the observation at given index */
|
||||
updateObservation: (index: number, obs: Observation) => void
|
||||
|
||||
/** Sets active observation index */
|
||||
setActiveObservation: (index: number) => void
|
||||
|
||||
/** Updates conclusion text */
|
||||
setConclusion: (text: string) => void
|
||||
|
||||
/** Updates recommendations text */
|
||||
setRecommendations: (text: string) => void
|
||||
|
||||
/** Clears the store (after export or cancel) */
|
||||
setNextAppointment: (next: Report['nextAppointment']) => 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,
|
||||
activeObservationIndex: 0,
|
||||
|
||||
initReport: () => {
|
||||
throw new Error('not implemented')
|
||||
},
|
||||
hydrateReport: () => {
|
||||
throw new Error('not implemented')
|
||||
},
|
||||
addObservation: () => {
|
||||
throw new Error('not implemented')
|
||||
},
|
||||
removeObservation: () => {
|
||||
throw new Error('not implemented')
|
||||
},
|
||||
moveObservationUp: () => {
|
||||
throw new Error('not implemented')
|
||||
},
|
||||
moveObservationDown: () => {
|
||||
throw new Error('not implemented')
|
||||
},
|
||||
updateObservation: () => {
|
||||
throw new Error('not implemented')
|
||||
},
|
||||
setActiveObservation: () => {
|
||||
throw new Error('not implemented')
|
||||
},
|
||||
setConclusion: () => {
|
||||
throw new Error('not implemented')
|
||||
},
|
||||
setRecommendations: () => {
|
||||
throw new Error('not implemented')
|
||||
},
|
||||
reset: () => {
|
||||
throw new Error('not implemented')
|
||||
},
|
||||
initReport: (draft) =>
|
||||
set({
|
||||
report: { ...draft, version: 1, id: generateId(), createdAt: nowIso() },
|
||||
activeObservationIndex: 0,
|
||||
}),
|
||||
|
||||
hydrateReport: (report) => set({ report, activeObservationIndex: 0 }),
|
||||
|
||||
addObservation: () =>
|
||||
set((s) => {
|
||||
if (!s.report) return s
|
||||
const obs = emptyObservation()
|
||||
const observations = [...s.report.observations, obs]
|
||||
return {
|
||||
report: { ...s.report, observations },
|
||||
activeObservationIndex: observations.length - 1,
|
||||
}
|
||||
}),
|
||||
|
||||
removeObservation: (index) =>
|
||||
set((s) => {
|
||||
if (!s.report) return s
|
||||
const observations = s.report.observations.filter((_, i) => i !== index)
|
||||
const active = Math.min(
|
||||
s.activeObservationIndex,
|
||||
Math.max(0, observations.length - 1),
|
||||
)
|
||||
return {
|
||||
report: { ...s.report, observations },
|
||||
activeObservationIndex: active,
|
||||
}
|
||||
}),
|
||||
|
||||
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 }),
|
||||
}))
|
||||
Reference in new issue
Block a user