feat(behandelplan): E3 UI componenten en documentatie updates
Behandelplan UI (E3): - page-client.tsx: Client-side behandelplan pagina - actions.ts: Server actions voor CRUD operaties - behandelplan-view.tsx: Volledige behandelplan weergave - behandelplan-list.tsx: Lijst van behandelplannen - editable-section.tsx: Herbruikbare edit sectie component - sections/: Goal, intervention en behandelstructuur forms UI Componenten: - components/ui/checkbox.tsx (shadcn) - components/ui/input.tsx (shadcn) - components/ui/select.tsx (shadcn) Documentatie: - agenda-systeem.mdx toegevoegd - _index.json en metadata.json bijgewerkt Dependencies: - @radix-ui/react-checkbox toegevoegd 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
520
app/epd/patients/[id]/behandelplan/actions.ts
Normal file
520
app/epd/patients/[id]/behandelplan/actions.ts
Normal file
@@ -0,0 +1,520 @@
|
||||
'use server';
|
||||
|
||||
import { createClient } from '@/lib/auth/server';
|
||||
import { revalidatePath } from 'next/cache';
|
||||
import type { GeneratedPlan, SmartGoal, Intervention, Behandelstructuur } from '@/lib/types/behandelplan';
|
||||
import type { LifeDomainScore } from '@/lib/types/leefgebieden';
|
||||
import type { Json } from '@/lib/supabase/database.types';
|
||||
|
||||
/**
|
||||
* Get care plans for a patient
|
||||
*/
|
||||
export async function getCarePlans(patientId: string) {
|
||||
const supabase = await createClient();
|
||||
|
||||
const { data, error } = await supabase
|
||||
.from('care_plans')
|
||||
.select('*')
|
||||
.eq('patient_id', patientId)
|
||||
.order('created_at', { ascending: false });
|
||||
|
||||
if (error) {
|
||||
console.error('Error fetching care plans:', error);
|
||||
return [];
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the latest active care plan for a patient
|
||||
*/
|
||||
export async function getActiveCarePlan(patientId: string) {
|
||||
const supabase = await createClient();
|
||||
|
||||
const { data, error } = await supabase
|
||||
.from('care_plans')
|
||||
.select('*')
|
||||
.eq('patient_id', patientId)
|
||||
.in('status', ['draft', 'active'])
|
||||
.order('version', { ascending: false })
|
||||
.limit(1)
|
||||
.single();
|
||||
|
||||
if (error && error.code !== 'PGRST116') {
|
||||
console.error('Error fetching active care plan:', error);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get intakes for a patient (to select for plan generation)
|
||||
*/
|
||||
export async function getPatientIntakes(patientId: string) {
|
||||
const supabase = await createClient();
|
||||
|
||||
const { data, error } = await supabase
|
||||
.from('intakes')
|
||||
.select('id, title, status, start_date, life_domains, notes')
|
||||
.eq('patient_id', patientId)
|
||||
.order('start_date', { ascending: false });
|
||||
|
||||
if (error) {
|
||||
console.error('Error fetching intakes:', error);
|
||||
return [];
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get conditions for a patient
|
||||
*/
|
||||
export async function getPatientConditions(patientId: string) {
|
||||
const supabase = await createClient();
|
||||
|
||||
const { data, error } = await supabase
|
||||
.from('conditions')
|
||||
.select('id, category, code_display, severity_code, severity_display, recorded_date')
|
||||
.eq('patient_id', patientId)
|
||||
.order('recorded_date', { ascending: false });
|
||||
|
||||
if (error) {
|
||||
console.error('Error fetching conditions:', error);
|
||||
return [];
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new care plan from generated plan
|
||||
*/
|
||||
export async function createCarePlan(
|
||||
patientId: string,
|
||||
intakeId: string,
|
||||
generatedPlan: GeneratedPlan,
|
||||
title: string = 'Behandelplan'
|
||||
) {
|
||||
const supabase = await createClient();
|
||||
|
||||
// Get current version
|
||||
const { data: existing } = await supabase
|
||||
.from('care_plans')
|
||||
.select('version')
|
||||
.eq('patient_id', patientId)
|
||||
.order('version', { ascending: false })
|
||||
.limit(1)
|
||||
.single();
|
||||
|
||||
const nextVersion = (existing?.version || 0) + 1;
|
||||
|
||||
const { data, error } = await supabase
|
||||
.from('care_plans')
|
||||
.insert({
|
||||
patient_id: patientId,
|
||||
based_on_intake_id: intakeId,
|
||||
title: `${title} v${nextVersion}`,
|
||||
status: 'draft',
|
||||
intent: 'plan',
|
||||
version: nextVersion,
|
||||
goals: generatedPlan.doelen as unknown as Json,
|
||||
activities: generatedPlan.interventies as unknown as Json,
|
||||
behandelstructuur: generatedPlan.behandelstructuur as unknown as Json,
|
||||
evaluatiemomenten: generatedPlan.evaluatiemomenten as unknown as Json,
|
||||
sessie_planning: generatedPlan.sessiePlanning as unknown as Json,
|
||||
veiligheidsplan: (generatedPlan.veiligheidsplan || null) as unknown as Json,
|
||||
period_start: new Date().toISOString(),
|
||||
})
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (error) {
|
||||
console.error('Error creating care plan:', error);
|
||||
throw new Error('Kon behandelplan niet opslaan');
|
||||
}
|
||||
|
||||
revalidatePath(`/epd/patients/${patientId}/behandelplan`);
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update care plan status
|
||||
*/
|
||||
export async function updateCarePlanStatus(
|
||||
carePlanId: string,
|
||||
patientId: string,
|
||||
status: 'draft' | 'active' | 'on-hold' | 'completed' | 'revoked'
|
||||
) {
|
||||
const supabase = await createClient();
|
||||
|
||||
const updateData: Record<string, unknown> = { status };
|
||||
|
||||
// Set published_at when activating
|
||||
if (status === 'active') {
|
||||
updateData.published_at = new Date().toISOString();
|
||||
}
|
||||
|
||||
const { error } = await supabase
|
||||
.from('care_plans')
|
||||
.update(updateData)
|
||||
.eq('id', carePlanId);
|
||||
|
||||
if (error) {
|
||||
console.error('Error updating care plan status:', error);
|
||||
throw new Error('Kon status niet bijwerken');
|
||||
}
|
||||
|
||||
revalidatePath(`/epd/patients/${patientId}/behandelplan`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update care plan goals
|
||||
*/
|
||||
export async function updateCarePlanGoals(
|
||||
carePlanId: string,
|
||||
patientId: string,
|
||||
goals: GeneratedPlan['doelen']
|
||||
) {
|
||||
const supabase = await createClient();
|
||||
|
||||
const { error } = await supabase
|
||||
.from('care_plans')
|
||||
.update({ goals: goals as unknown as Json })
|
||||
.eq('id', carePlanId);
|
||||
|
||||
if (error) {
|
||||
console.error('Error updating goals:', error);
|
||||
throw new Error('Kon doelen niet bijwerken');
|
||||
}
|
||||
|
||||
revalidatePath(`/epd/patients/${patientId}/behandelplan`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save life domains to intake
|
||||
*/
|
||||
export async function saveLifeDomains(
|
||||
intakeId: string,
|
||||
patientId: string,
|
||||
lifeDomains: LifeDomainScore[]
|
||||
) {
|
||||
const supabase = await createClient();
|
||||
|
||||
const { error } = await supabase
|
||||
.from('intakes')
|
||||
.update({ life_domains: lifeDomains as unknown as Json })
|
||||
.eq('id', intakeId);
|
||||
|
||||
if (error) {
|
||||
console.error('Error saving life domains:', error);
|
||||
throw new Error('Kon leefgebieden niet opslaan');
|
||||
}
|
||||
|
||||
revalidatePath(`/epd/patients/${patientId}/behandelplan`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a care plan
|
||||
*/
|
||||
export async function deleteCarePlan(carePlanId: string, patientId: string) {
|
||||
const supabase = await createClient();
|
||||
|
||||
const { error } = await supabase
|
||||
.from('care_plans')
|
||||
.delete()
|
||||
.eq('id', carePlanId);
|
||||
|
||||
if (error) {
|
||||
console.error('Error deleting care plan:', error);
|
||||
throw new Error('Kon behandelplan niet verwijderen');
|
||||
}
|
||||
|
||||
revalidatePath(`/epd/patients/${patientId}/behandelplan`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new empty/manual care plan
|
||||
*/
|
||||
export async function createEmptyCarePlan(
|
||||
patientId: string,
|
||||
intakeId?: string,
|
||||
title: string = 'Behandelplan'
|
||||
) {
|
||||
const supabase = await createClient();
|
||||
|
||||
// Get current version
|
||||
const { data: existing } = await supabase
|
||||
.from('care_plans')
|
||||
.select('version')
|
||||
.eq('patient_id', patientId)
|
||||
.order('version', { ascending: false })
|
||||
.limit(1)
|
||||
.single();
|
||||
|
||||
const nextVersion = (existing?.version || 0) + 1;
|
||||
|
||||
const { data, error } = await supabase
|
||||
.from('care_plans')
|
||||
.insert({
|
||||
patient_id: patientId,
|
||||
based_on_intake_id: intakeId || null,
|
||||
title: `${title} v${nextVersion}`,
|
||||
status: 'draft',
|
||||
intent: 'plan',
|
||||
version: nextVersion,
|
||||
goals: [],
|
||||
activities: [],
|
||||
period_start: new Date().toISOString(),
|
||||
})
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (error) {
|
||||
console.error('Error creating empty care plan:', error);
|
||||
throw new Error('Kon behandelplan niet aanmaken');
|
||||
}
|
||||
|
||||
revalidatePath(`/epd/patients/${patientId}/behandelplan`);
|
||||
return data;
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// BEHANDELSTRUCTUUR
|
||||
// =============================================================================
|
||||
|
||||
/**
|
||||
* Update behandelstructuur
|
||||
*/
|
||||
export async function updateBehandelstructuur(
|
||||
carePlanId: string,
|
||||
patientId: string,
|
||||
behandelstructuur: Behandelstructuur
|
||||
) {
|
||||
const supabase = await createClient();
|
||||
|
||||
const { error } = await supabase
|
||||
.from('care_plans')
|
||||
.update({ behandelstructuur: behandelstructuur as unknown as Json })
|
||||
.eq('id', carePlanId);
|
||||
|
||||
if (error) {
|
||||
console.error('Error updating behandelstructuur:', error);
|
||||
throw new Error('Kon behandelstructuur niet bijwerken');
|
||||
}
|
||||
|
||||
revalidatePath(`/epd/patients/${patientId}/behandelplan`);
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// GOALS (DOELEN)
|
||||
// =============================================================================
|
||||
|
||||
/**
|
||||
* Add a goal to a care plan
|
||||
*/
|
||||
export async function addGoal(
|
||||
carePlanId: string,
|
||||
patientId: string,
|
||||
goal: SmartGoal
|
||||
) {
|
||||
const supabase = await createClient();
|
||||
|
||||
// Get current goals
|
||||
const { data: plan } = await supabase
|
||||
.from('care_plans')
|
||||
.select('goals')
|
||||
.eq('id', carePlanId)
|
||||
.single();
|
||||
|
||||
const currentGoals = (plan?.goals as unknown as SmartGoal[]) || [];
|
||||
const updatedGoals = [...currentGoals, goal];
|
||||
|
||||
const { error } = await supabase
|
||||
.from('care_plans')
|
||||
.update({ goals: updatedGoals as unknown as Json })
|
||||
.eq('id', carePlanId);
|
||||
|
||||
if (error) {
|
||||
console.error('Error adding goal:', error);
|
||||
throw new Error('Kon doel niet toevoegen');
|
||||
}
|
||||
|
||||
revalidatePath(`/epd/patients/${patientId}/behandelplan`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a single goal in a care plan
|
||||
*/
|
||||
export async function updateGoal(
|
||||
carePlanId: string,
|
||||
patientId: string,
|
||||
goalId: string,
|
||||
updatedGoal: SmartGoal
|
||||
) {
|
||||
const supabase = await createClient();
|
||||
|
||||
// Get current goals
|
||||
const { data: plan } = await supabase
|
||||
.from('care_plans')
|
||||
.select('goals')
|
||||
.eq('id', carePlanId)
|
||||
.single();
|
||||
|
||||
const currentGoals = (plan?.goals as unknown as SmartGoal[]) || [];
|
||||
const updatedGoals = currentGoals.map((g) =>
|
||||
g.id === goalId ? updatedGoal : g
|
||||
);
|
||||
|
||||
const { error } = await supabase
|
||||
.from('care_plans')
|
||||
.update({ goals: updatedGoals as unknown as Json })
|
||||
.eq('id', carePlanId);
|
||||
|
||||
if (error) {
|
||||
console.error('Error updating goal:', error);
|
||||
throw new Error('Kon doel niet bijwerken');
|
||||
}
|
||||
|
||||
revalidatePath(`/epd/patients/${patientId}/behandelplan`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a goal from a care plan
|
||||
*/
|
||||
export async function deleteGoal(
|
||||
carePlanId: string,
|
||||
patientId: string,
|
||||
goalId: string
|
||||
) {
|
||||
const supabase = await createClient();
|
||||
|
||||
// Get current goals
|
||||
const { data: plan } = await supabase
|
||||
.from('care_plans')
|
||||
.select('goals')
|
||||
.eq('id', carePlanId)
|
||||
.single();
|
||||
|
||||
const currentGoals = (plan?.goals as unknown as SmartGoal[]) || [];
|
||||
const updatedGoals = currentGoals.filter((g) => g.id !== goalId);
|
||||
|
||||
const { error } = await supabase
|
||||
.from('care_plans')
|
||||
.update({ goals: updatedGoals as unknown as Json })
|
||||
.eq('id', carePlanId);
|
||||
|
||||
if (error) {
|
||||
console.error('Error deleting goal:', error);
|
||||
throw new Error('Kon doel niet verwijderen');
|
||||
}
|
||||
|
||||
revalidatePath(`/epd/patients/${patientId}/behandelplan`);
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// INTERVENTIONS (INTERVENTIES)
|
||||
// =============================================================================
|
||||
|
||||
/**
|
||||
* Add an intervention to a care plan
|
||||
*/
|
||||
export async function addIntervention(
|
||||
carePlanId: string,
|
||||
patientId: string,
|
||||
intervention: Intervention
|
||||
) {
|
||||
const supabase = await createClient();
|
||||
|
||||
// Get current interventions
|
||||
const { data: plan } = await supabase
|
||||
.from('care_plans')
|
||||
.select('activities')
|
||||
.eq('id', carePlanId)
|
||||
.single();
|
||||
|
||||
const currentInterventions = (plan?.activities as unknown as Intervention[]) || [];
|
||||
const updatedInterventions = [...currentInterventions, intervention];
|
||||
|
||||
const { error } = await supabase
|
||||
.from('care_plans')
|
||||
.update({ activities: updatedInterventions as unknown as Json })
|
||||
.eq('id', carePlanId);
|
||||
|
||||
if (error) {
|
||||
console.error('Error adding intervention:', error);
|
||||
throw new Error('Kon interventie niet toevoegen');
|
||||
}
|
||||
|
||||
revalidatePath(`/epd/patients/${patientId}/behandelplan`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a single intervention in a care plan
|
||||
*/
|
||||
export async function updateIntervention(
|
||||
carePlanId: string,
|
||||
patientId: string,
|
||||
interventionId: string,
|
||||
updatedIntervention: Intervention
|
||||
) {
|
||||
const supabase = await createClient();
|
||||
|
||||
// Get current interventions
|
||||
const { data: plan } = await supabase
|
||||
.from('care_plans')
|
||||
.select('activities')
|
||||
.eq('id', carePlanId)
|
||||
.single();
|
||||
|
||||
const currentInterventions = (plan?.activities as unknown as Intervention[]) || [];
|
||||
const updatedInterventions = currentInterventions.map((i) =>
|
||||
i.id === interventionId ? updatedIntervention : i
|
||||
);
|
||||
|
||||
const { error } = await supabase
|
||||
.from('care_plans')
|
||||
.update({ activities: updatedInterventions as unknown as Json })
|
||||
.eq('id', carePlanId);
|
||||
|
||||
if (error) {
|
||||
console.error('Error updating intervention:', error);
|
||||
throw new Error('Kon interventie niet bijwerken');
|
||||
}
|
||||
|
||||
revalidatePath(`/epd/patients/${patientId}/behandelplan`);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete an intervention from a care plan
|
||||
*/
|
||||
export async function deleteIntervention(
|
||||
carePlanId: string,
|
||||
patientId: string,
|
||||
interventionId: string
|
||||
) {
|
||||
const supabase = await createClient();
|
||||
|
||||
// Get current interventions
|
||||
const { data: plan } = await supabase
|
||||
.from('care_plans')
|
||||
.select('activities')
|
||||
.eq('id', carePlanId)
|
||||
.single();
|
||||
|
||||
const currentInterventions = (plan?.activities as unknown as Intervention[]) || [];
|
||||
const updatedInterventions = currentInterventions.filter((i) => i.id !== interventionId);
|
||||
|
||||
const { error } = await supabase
|
||||
.from('care_plans')
|
||||
.update({ activities: updatedInterventions as unknown as Json })
|
||||
.eq('id', carePlanId);
|
||||
|
||||
if (error) {
|
||||
console.error('Error deleting intervention:', error);
|
||||
throw new Error('Kon interventie niet verwijderen');
|
||||
}
|
||||
|
||||
revalidatePath(`/epd/patients/${patientId}/behandelplan`);
|
||||
}
|
||||
420
app/epd/patients/[id]/behandelplan/page-client.tsx
Normal file
420
app/epd/patients/[id]/behandelplan/page-client.tsx
Normal file
@@ -0,0 +1,420 @@
|
||||
'use client';
|
||||
|
||||
import { useState, useCallback, useMemo } from 'react';
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { BehandelplanView, BehandelplanList } from '@/components/behandelplan';
|
||||
import {
|
||||
createCarePlan,
|
||||
updateCarePlanStatus,
|
||||
createEmptyCarePlan,
|
||||
updateBehandelstructuur,
|
||||
addGoal,
|
||||
updateGoal,
|
||||
deleteGoal,
|
||||
addIntervention,
|
||||
updateIntervention,
|
||||
deleteIntervention,
|
||||
} from './actions';
|
||||
import type { GeneratedPlan, SmartGoal, Intervention, Sessie, Evaluatiemoment, Veiligheidsplan, Behandelstructuur } from '@/lib/types/behandelplan';
|
||||
import type { LifeDomainScore } from '@/lib/types/leefgebieden';
|
||||
import type { Json } from '@/lib/supabase/database.types';
|
||||
|
||||
// Database row types (what we get from Supabase)
|
||||
interface DbCarePlan {
|
||||
id: string;
|
||||
title: string;
|
||||
status: string;
|
||||
version: number | null;
|
||||
goals: Json | null;
|
||||
activities: Json | null;
|
||||
behandelstructuur: Json | null;
|
||||
sessie_planning: Json | null;
|
||||
evaluatiemomenten: Json | null;
|
||||
veiligheidsplan: Json | null;
|
||||
created_at: string | null;
|
||||
published_at: string | null;
|
||||
period_start: string | null;
|
||||
}
|
||||
|
||||
interface DbIntake {
|
||||
id: string;
|
||||
title: string;
|
||||
status: string;
|
||||
start_date: string;
|
||||
life_domains: Json | null;
|
||||
notes: string | null;
|
||||
}
|
||||
|
||||
interface DbCondition {
|
||||
id: string;
|
||||
category: string;
|
||||
code_display: string;
|
||||
severity_code: string | null;
|
||||
severity_display: string | null;
|
||||
}
|
||||
|
||||
// Mapped types for the view component
|
||||
interface ViewCarePlan {
|
||||
id: string;
|
||||
title: string;
|
||||
status: 'draft' | 'active' | 'on-hold' | 'completed' | 'revoked' | 'entered-in-error' | 'unknown';
|
||||
version: number | null;
|
||||
goals: SmartGoal[] | null;
|
||||
activities: Intervention[] | null;
|
||||
behandelstructuur: GeneratedPlan['behandelstructuur'] | null;
|
||||
sessie_planning: Sessie[] | null;
|
||||
evaluatiemomenten: Evaluatiemoment[] | null;
|
||||
veiligheidsplan: Veiligheidsplan | null;
|
||||
created_at: string | null;
|
||||
published_at: string | null;
|
||||
period_start: string | null;
|
||||
}
|
||||
|
||||
interface ViewIntake {
|
||||
id: string;
|
||||
title: string;
|
||||
status: string;
|
||||
start_date: string;
|
||||
life_domains: LifeDomainScore[] | null;
|
||||
notes: string | null;
|
||||
}
|
||||
|
||||
interface ViewCondition {
|
||||
id: string;
|
||||
category: string;
|
||||
code_display: string;
|
||||
severity_code: string | null;
|
||||
severity_display: string | null;
|
||||
}
|
||||
|
||||
interface BehandelplanPageClientProps {
|
||||
patientId: string;
|
||||
allPlans: DbCarePlan[];
|
||||
intakes: DbIntake[];
|
||||
conditions: DbCondition[];
|
||||
}
|
||||
|
||||
// Helper to map database types to view types
|
||||
function mapCarePlan(dbPlan: DbCarePlan | null): ViewCarePlan | null {
|
||||
if (!dbPlan) return null;
|
||||
return {
|
||||
...dbPlan,
|
||||
status: dbPlan.status as ViewCarePlan['status'],
|
||||
goals: dbPlan.goals as SmartGoal[] | null,
|
||||
activities: dbPlan.activities as Intervention[] | null,
|
||||
behandelstructuur: dbPlan.behandelstructuur as GeneratedPlan['behandelstructuur'] | null,
|
||||
sessie_planning: dbPlan.sessie_planning as Sessie[] | null,
|
||||
evaluatiemomenten: dbPlan.evaluatiemomenten as Evaluatiemoment[] | null,
|
||||
veiligheidsplan: dbPlan.veiligheidsplan as Veiligheidsplan | null,
|
||||
};
|
||||
}
|
||||
|
||||
function mapIntakes(dbIntakes: DbIntake[]): ViewIntake[] {
|
||||
return dbIntakes.map((intake) => ({
|
||||
...intake,
|
||||
life_domains: intake.life_domains as LifeDomainScore[] | null,
|
||||
}));
|
||||
}
|
||||
|
||||
export function BehandelplanPageClient({
|
||||
patientId,
|
||||
allPlans: initialPlans,
|
||||
intakes,
|
||||
conditions,
|
||||
}: BehandelplanPageClientProps) {
|
||||
const router = useRouter();
|
||||
|
||||
// State voor alle plannen en selectie
|
||||
const [plans, setPlans] = useState<DbCarePlan[]>(initialPlans);
|
||||
const [selectedPlanId, setSelectedPlanId] = useState<string | null>(
|
||||
// Selecteer standaard het nieuwste actieve/draft plan, of het eerste plan
|
||||
initialPlans.find(p => p.status === 'active')?.id ||
|
||||
initialPlans.find(p => p.status === 'draft')?.id ||
|
||||
initialPlans[0]?.id ||
|
||||
null
|
||||
);
|
||||
const [isCreatingNew, setIsCreatingNew] = useState(false);
|
||||
const [showCreateView, setShowCreateView] = useState(false);
|
||||
|
||||
// Geselecteerd plan
|
||||
const selectedPlan = useMemo(() => {
|
||||
const plan = plans.find(p => p.id === selectedPlanId);
|
||||
return plan ? mapCarePlan(plan) : null;
|
||||
}, [plans, selectedPlanId]);
|
||||
|
||||
// Handler voor plan selectie
|
||||
const handleSelectPlan = useCallback((planId: string) => {
|
||||
setSelectedPlanId(planId);
|
||||
setShowCreateView(false);
|
||||
}, []);
|
||||
|
||||
// Handler voor nieuw plan aanmaken (toont create view)
|
||||
const handleShowCreateView = useCallback(() => {
|
||||
setSelectedPlanId(null);
|
||||
setShowCreateView(true);
|
||||
}, []);
|
||||
|
||||
const handleGenerate = useCallback(
|
||||
async (intakeId: string) => {
|
||||
// Get the first condition if available
|
||||
const conditionId = conditions[0]?.id;
|
||||
|
||||
// Call the generate API
|
||||
const response = await fetch('/api/behandelplan/generate', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
patientId,
|
||||
intakeId,
|
||||
conditionId,
|
||||
}),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json();
|
||||
throw new Error(error.error || 'Er ging iets mis bij het genereren');
|
||||
}
|
||||
|
||||
const generatedPlan: GeneratedPlan = await response.json();
|
||||
|
||||
// Save to database via server action
|
||||
const savedPlan = await createCarePlan(patientId, intakeId, generatedPlan);
|
||||
|
||||
// Update plans list en selecteer het nieuwe plan
|
||||
const newPlan = savedPlan as DbCarePlan;
|
||||
setPlans(prev => [newPlan, ...prev]);
|
||||
setSelectedPlanId(newPlan.id);
|
||||
setShowCreateView(false);
|
||||
|
||||
// Revalidate the page
|
||||
router.refresh();
|
||||
},
|
||||
[patientId, conditions, router]
|
||||
);
|
||||
|
||||
const handleStatusChange = useCallback(
|
||||
async (status: ViewCarePlan['status']) => {
|
||||
if (!selectedPlan) return;
|
||||
|
||||
// Only allow valid status transitions
|
||||
if (!['draft', 'active', 'on-hold', 'completed', 'revoked'].includes(status)) {
|
||||
throw new Error('Ongeldige status');
|
||||
}
|
||||
|
||||
await updateCarePlanStatus(
|
||||
selectedPlan.id,
|
||||
patientId,
|
||||
status as 'draft' | 'active' | 'on-hold' | 'completed' | 'revoked'
|
||||
);
|
||||
|
||||
// Update plans list
|
||||
setPlans(prev => prev.map(p =>
|
||||
p.id === selectedPlan.id
|
||||
? {
|
||||
...p,
|
||||
status,
|
||||
published_at: status === 'active' ? new Date().toISOString() : p.published_at
|
||||
}
|
||||
: p
|
||||
));
|
||||
|
||||
// Revalidate
|
||||
router.refresh();
|
||||
},
|
||||
[selectedPlan, patientId, router]
|
||||
);
|
||||
|
||||
const handleCreateManual = useCallback(
|
||||
async (intakeId?: string) => {
|
||||
setIsCreatingNew(true);
|
||||
try {
|
||||
// Create empty care plan via server action
|
||||
const savedPlan = await createEmptyCarePlan(patientId, intakeId);
|
||||
|
||||
// Update plans list en selecteer het nieuwe plan
|
||||
const newPlan = savedPlan as DbCarePlan;
|
||||
setPlans(prev => [newPlan, ...prev]);
|
||||
setSelectedPlanId(newPlan.id);
|
||||
setShowCreateView(false);
|
||||
|
||||
// Revalidate the page
|
||||
router.refresh();
|
||||
} finally {
|
||||
setIsCreatingNew(false);
|
||||
}
|
||||
},
|
||||
[patientId, router]
|
||||
);
|
||||
|
||||
// =============================================================================
|
||||
// EDIT HANDLERS
|
||||
// =============================================================================
|
||||
|
||||
const handleUpdateBehandelstructuur = useCallback(
|
||||
async (data: Behandelstructuur) => {
|
||||
if (!selectedPlan) return;
|
||||
|
||||
await updateBehandelstructuur(selectedPlan.id, patientId, data);
|
||||
|
||||
// Update local state
|
||||
setPlans(prev => prev.map(p =>
|
||||
p.id === selectedPlan.id
|
||||
? { ...p, behandelstructuur: data as unknown as Json }
|
||||
: p
|
||||
));
|
||||
|
||||
router.refresh();
|
||||
},
|
||||
[selectedPlan, patientId, router]
|
||||
);
|
||||
|
||||
const handleAddGoal = useCallback(
|
||||
async (goal: SmartGoal) => {
|
||||
if (!selectedPlan) return;
|
||||
|
||||
await addGoal(selectedPlan.id, patientId, goal);
|
||||
|
||||
// Update local state
|
||||
setPlans(prev => prev.map(p => {
|
||||
if (p.id !== selectedPlan.id) return p;
|
||||
const currentGoals = (p.goals as unknown as SmartGoal[]) || [];
|
||||
return { ...p, goals: [...currentGoals, goal] as unknown as Json };
|
||||
}));
|
||||
|
||||
router.refresh();
|
||||
},
|
||||
[selectedPlan, patientId, router]
|
||||
);
|
||||
|
||||
const handleUpdateGoal = useCallback(
|
||||
async (goalId: string, updatedGoal: SmartGoal) => {
|
||||
if (!selectedPlan) return;
|
||||
|
||||
await updateGoal(selectedPlan.id, patientId, goalId, updatedGoal);
|
||||
|
||||
// Update local state
|
||||
setPlans(prev => prev.map(p => {
|
||||
if (p.id !== selectedPlan.id) return p;
|
||||
const currentGoals = (p.goals as unknown as SmartGoal[]) || [];
|
||||
const newGoals = currentGoals.map(g => g.id === goalId ? updatedGoal : g);
|
||||
return { ...p, goals: newGoals as unknown as Json };
|
||||
}));
|
||||
|
||||
router.refresh();
|
||||
},
|
||||
[selectedPlan, patientId, router]
|
||||
);
|
||||
|
||||
const handleDeleteGoal = useCallback(
|
||||
async (goalId: string) => {
|
||||
if (!selectedPlan) return;
|
||||
|
||||
await deleteGoal(selectedPlan.id, patientId, goalId);
|
||||
|
||||
// Update local state
|
||||
setPlans(prev => prev.map(p => {
|
||||
if (p.id !== selectedPlan.id) return p;
|
||||
const currentGoals = (p.goals as unknown as SmartGoal[]) || [];
|
||||
const newGoals = currentGoals.filter(g => g.id !== goalId);
|
||||
return { ...p, goals: newGoals as unknown as Json };
|
||||
}));
|
||||
|
||||
router.refresh();
|
||||
},
|
||||
[selectedPlan, patientId, router]
|
||||
);
|
||||
|
||||
const handleAddIntervention = useCallback(
|
||||
async (intervention: Intervention) => {
|
||||
if (!selectedPlan) return;
|
||||
|
||||
await addIntervention(selectedPlan.id, patientId, intervention);
|
||||
|
||||
// Update local state
|
||||
setPlans(prev => prev.map(p => {
|
||||
if (p.id !== selectedPlan.id) return p;
|
||||
const currentInterventions = (p.activities as unknown as Intervention[]) || [];
|
||||
return { ...p, activities: [...currentInterventions, intervention] as unknown as Json };
|
||||
}));
|
||||
|
||||
router.refresh();
|
||||
},
|
||||
[selectedPlan, patientId, router]
|
||||
);
|
||||
|
||||
const handleUpdateIntervention = useCallback(
|
||||
async (interventionId: string, updatedIntervention: Intervention) => {
|
||||
if (!selectedPlan) return;
|
||||
|
||||
await updateIntervention(selectedPlan.id, patientId, interventionId, updatedIntervention);
|
||||
|
||||
// Update local state
|
||||
setPlans(prev => prev.map(p => {
|
||||
if (p.id !== selectedPlan.id) return p;
|
||||
const currentInterventions = (p.activities as unknown as Intervention[]) || [];
|
||||
const newInterventions = currentInterventions.map(i => i.id === interventionId ? updatedIntervention : i);
|
||||
return { ...p, activities: newInterventions as unknown as Json };
|
||||
}));
|
||||
|
||||
router.refresh();
|
||||
},
|
||||
[selectedPlan, patientId, router]
|
||||
);
|
||||
|
||||
const handleDeleteIntervention = useCallback(
|
||||
async (interventionId: string) => {
|
||||
if (!selectedPlan) return;
|
||||
|
||||
await deleteIntervention(selectedPlan.id, patientId, interventionId);
|
||||
|
||||
// Update local state
|
||||
setPlans(prev => prev.map(p => {
|
||||
if (p.id !== selectedPlan.id) return p;
|
||||
const currentInterventions = (p.activities as unknown as Intervention[]) || [];
|
||||
const newInterventions = currentInterventions.filter(i => i.id !== interventionId);
|
||||
return { ...p, activities: newInterventions as unknown as Json };
|
||||
}));
|
||||
|
||||
router.refresh();
|
||||
},
|
||||
[selectedPlan, patientId, router]
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Plannen overzicht */}
|
||||
<BehandelplanList
|
||||
plans={plans.map(p => ({
|
||||
id: p.id,
|
||||
title: p.title,
|
||||
status: p.status,
|
||||
version: p.version,
|
||||
created_at: p.created_at,
|
||||
published_at: p.published_at,
|
||||
}))}
|
||||
selectedPlanId={showCreateView ? null : selectedPlanId}
|
||||
onSelectPlan={handleSelectPlan}
|
||||
onCreateNew={handleShowCreateView}
|
||||
isCreating={isCreatingNew}
|
||||
/>
|
||||
|
||||
{/* Geselecteerd plan of create view */}
|
||||
<BehandelplanView
|
||||
patientId={patientId}
|
||||
carePlan={showCreateView ? null : selectedPlan}
|
||||
intakes={mapIntakes(intakes)}
|
||||
conditions={conditions}
|
||||
onGenerate={handleGenerate}
|
||||
onStatusChange={handleStatusChange}
|
||||
onCreateManual={handleCreateManual}
|
||||
onUpdateBehandelstructuur={handleUpdateBehandelstructuur}
|
||||
onAddGoal={handleAddGoal}
|
||||
onUpdateGoal={handleUpdateGoal}
|
||||
onDeleteGoal={handleDeleteGoal}
|
||||
onAddIntervention={handleAddIntervention}
|
||||
onUpdateIntervention={handleUpdateIntervention}
|
||||
onDeleteIntervention={handleDeleteIntervention}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
/**
|
||||
* Behandelplan Page
|
||||
* E2.S3: Placeholder for behandelplan functionality (future epic)
|
||||
* E3.S1: Server component met data loading
|
||||
*/
|
||||
|
||||
import { Calendar } from 'lucide-react';
|
||||
import { getCarePlans, getPatientIntakes, getPatientConditions } from './actions';
|
||||
import { BehandelplanPageClient } from './page-client';
|
||||
|
||||
export default async function BehandelplanPage({
|
||||
params,
|
||||
@@ -12,29 +13,21 @@ export default async function BehandelplanPage({
|
||||
}) {
|
||||
const { id } = await params;
|
||||
|
||||
// Parallel data loading - haal ALLE plannen op
|
||||
const [allPlans, intakes, conditions] = await Promise.all([
|
||||
getCarePlans(id),
|
||||
getPatientIntakes(id),
|
||||
getPatientConditions(id),
|
||||
]);
|
||||
|
||||
return (
|
||||
<div className="p-6">
|
||||
{/* Page Header */}
|
||||
<div className="mb-6">
|
||||
<h2 className="text-lg font-semibold text-slate-900">Behandelplan</h2>
|
||||
<p className="text-sm text-slate-600 mt-1">
|
||||
Behandeldoelen, interventies en planning
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{/* Placeholder */}
|
||||
<div className="bg-white rounded-lg border border-slate-200 p-12 text-center">
|
||||
<div className="inline-flex items-center justify-center w-16 h-16 rounded-full bg-indigo-50 mb-4">
|
||||
<Calendar className="h-8 w-8 text-indigo-500" />
|
||||
</div>
|
||||
<h3 className="text-lg font-semibold text-slate-900 mb-2">
|
||||
Behandelplan Module - Coming Soon
|
||||
</h3>
|
||||
<p className="text-sm text-slate-600 max-w-md mx-auto">
|
||||
De behandelplan functionaliteit wordt in een latere fase geïmplementeerd.
|
||||
Dit omvat doelstellingen, interventies en planning van de behandeling.
|
||||
</p>
|
||||
</div>
|
||||
<BehandelplanPageClient
|
||||
patientId={id}
|
||||
allPlans={allPlans}
|
||||
intakes={intakes}
|
||||
conditions={conditions}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user