11import React , { createContext , useContext , useEffect , useState } from "react" ;
2- import { Place } from "@/types" ;
3- import { PlanType } from "./PlanTypes" ;
2+ import { Place , PlanDTO , PlanWithDetail } from "@/types" ;
43import { useAuth } from "./AuthContext" ;
54import { useParams } from "react-router-dom" ;
65import usePlanApi from "@/hooks/usePlanApi" ;
76import usePlanOptimistic from "@/hooks/usePlanOptimistic" ;
87
98type contextType = {
10- plan : PlanType | null ;
9+ plan : PlanWithDetail | null ;
1110 setTitle : ( title : string ) => void ;
1211 setDescription : ( description : string ) => void ;
1312 addImage : ( image : string ) => void ;
1413 setPolyline : ( polyline : string ) => void ;
1514 addPlace : ( place : Place ) => void ;
1615 removePlace : ( placeId : string ) => void ;
17- createPlan : ( plan : PlanType ) => void ;
16+ setExpense : ( placeId : string , expense : number ) => void ;
17+ setNote : ( placeId : string , note : string ) => void ;
18+ createPlan : ( plan : PlanDTO ) => void ;
1819 saving : boolean ;
1920 loading : boolean ;
2021 error : string | null ;
@@ -25,7 +26,6 @@ const ItineraryContext = createContext<contextType | undefined>(undefined);
2526const ItineraryProvider = ( { children } : { children : React . ReactNode } ) => {
2627 const { plan, setPlan, saving, loading, createPlan, getPlan, updatePlan } =
2728 usePlanApi ( ) ;
28- // const [optimisticPlan, setOptimisticPlan] = useState<PlanType | null>(null);
2929 const { optimisticPlan, dispatch } = usePlanOptimistic ( null ) ;
3030 const [ error , setError ] = useState < string | null > ( null ) ;
3131 const { token } = useAuth ( ) ;
@@ -50,7 +50,13 @@ const ItineraryProvider = ({ children }: { children: React.ReactNode }) => {
5050 ( async ( ) => {
5151 const originalPlan = plan ;
5252 // Replace plan's properties with whatever is in optimisticPlan
53- setPlan ( { ...plan , ...optimisticPlan } ) ;
53+ setPlan ( {
54+ ...plan ,
55+ title : optimisticPlan . title || plan . title ,
56+ description : optimisticPlan . description || plan . description ,
57+ images : optimisticPlan . images || plan . images ,
58+ stops : optimisticPlan . stops || plan . stops ,
59+ } ) ;
5460 try {
5561 await updatePlan ( planId , optimisticPlan ) ;
5662 } catch ( err : unknown ) {
@@ -99,6 +105,26 @@ const ItineraryProvider = ({ children }: { children: React.ReactNode }) => {
99105 dispatch ( { type : "removePlace" , payload : placeId , init : plan . stops || [ ] } ) ;
100106 } ;
101107
108+ const setExpense = ( placeId : string , expense : number ) => {
109+ if ( ! plan || ! placeId ) return ;
110+
111+ dispatch ( {
112+ type : "setExpense" ,
113+ payload : { placeId, expense } ,
114+ init : plan . stops || [ ] ,
115+ } ) ;
116+ } ;
117+
118+ const setNote = ( placeId : string , note : string ) => {
119+ if ( ! plan || ! placeId ) return ;
120+
121+ dispatch ( {
122+ type : "setNote" ,
123+ payload : { placeId, note } ,
124+ init : plan . stops || [ ] ,
125+ } ) ;
126+ } ;
127+
102128 return (
103129 < ItineraryContext . Provider
104130 value = { {
@@ -109,6 +135,8 @@ const ItineraryProvider = ({ children }: { children: React.ReactNode }) => {
109135 addImage,
110136 addPlace,
111137 removePlace,
138+ setExpense,
139+ setNote,
112140 createPlan,
113141 saving,
114142 loading,
0 commit comments