@@ -4,6 +4,7 @@ import { PlanType } from "./PlanTypes";
44import { useAuth } from "./AuthContext" ;
55import { useParams } from "react-router-dom" ;
66import usePlanApi from "@/hooks/usePlanApi" ;
7+ import usePlanOptimistic from "@/hooks/usePlanOptimistic" ;
78
89type contextType = {
910 plan : PlanType | null ;
@@ -23,7 +24,8 @@ const ItineraryContext = createContext<contextType | undefined>(undefined);
2324const ItineraryProvider = ( { children } : { children : React . ReactNode } ) => {
2425 const { plan, setPlan, saving, loading, createPlan, getPlan, updatePlan } =
2526 usePlanApi ( ) ;
26- const [ optimisticPlan , setOptimisticPlan ] = useState < PlanType | null > ( null ) ;
27+ // const [optimisticPlan, setOptimisticPlan] = useState<PlanType | null>(null);
28+ const { optimisticPlan, dispatch } = usePlanOptimistic ( null ) ;
2729 const [ error , setError ] = useState < string | null > ( null ) ;
2830 const { token } = useAuth ( ) ;
2931 const { planId } = useParams ( ) ;
@@ -58,80 +60,36 @@ const ItineraryProvider = ({ children }: { children: React.ReactNode }) => {
5860 } ) ( ) ;
5961 } , [ optimisticPlan ] ) ;
6062
61- // TODO: Use useReducer for setTitle, setDescription and etc, to make optimisticPlan update more cleanly
62-
6363 const setTitle = ( title : string ) => {
64- if ( ! plan && ! title ) return ;
65- setOptimisticPlan ( ( prev ) => ( prev ? { ...prev , title } : { title } ) ) ;
64+ if ( ! plan || ! title ) return ;
65+
66+ dispatch ( { type : "setTitle" , payload : title } ) ;
6667 } ;
6768
6869 const setDescription = ( description : string ) => {
69- if ( ! plan && ! description ) return ;
70+ if ( ! plan || ! description ) return ;
7071
71- setOptimisticPlan ( ( prev ) =>
72- prev ? { ...prev , description } : { description } ,
73- ) ;
72+ dispatch ( { type : "setDescription" , payload : description } ) ;
7473 } ;
7574
7675 const addImage = ( image : string ) => {
77- if ( ! plan ) return ;
78-
79- setOptimisticPlan ( ( prev ) => {
80- if ( ! prev ) return null ;
81- if ( ! prev . images )
82- return {
83- ...prev ,
84- images : [ image ] ,
85- } ;
86- return {
87- ...prev ,
88- images : [ ...prev . images , image ] ,
89- } ;
90- } ) ;
91- } ;
76+ if ( ! plan || ! image ) return ;
9277
93- const addPlace = ( place : Place ) => {
94- if ( ! plan ) return ;
95-
96- setOptimisticPlan ( ( prev ) => {
97- // Initialize stops array using plan.stops if not existing in optimisticPlan
98- if ( ! prev || ! prev . stops ) {
99- prev = { ...prev , stops : plan . stops || [ ] } ;
100- }
78+ setPlan ( { ...plan , images : [ ...( plan . images || [ ] ) , image ] } ) ;
10179
102- if ( ! prev . stops ) {
103- prev . stops = [ place ] ;
104- return prev ;
105- }
80+ // dispatch({ type: "addImage", payload: image, init: plan.images || [] });
81+ } ;
10682
107- // Avoid adding duplicates
108- if ( prev . stops . find ( ( p ) => p . placeId === place . placeId ) ) {
109- return prev ;
110- }
83+ const addPlace = ( place : Place ) => {
84+ if ( ! plan || ! place ) return ;
11185
112- // Add new stop to the end
113- return {
114- ...prev ,
115- stops : [ ...prev . stops , place ] ,
116- } ;
117- } ) ;
86+ dispatch ( { type : "addPlace" , payload : place , init : plan . stops || [ ] } ) ;
11887 } ;
11988
12089 const removePlace = ( placeId : string ) => {
121- if ( ! plan ) return ;
122-
123- setOptimisticPlan ( ( prev ) => {
124- // Initialize stops array using plan.stops if not existing in optimisticPlan
125- if ( ! prev || ! prev . stops ) {
126- prev = { ...prev , stops : plan . stops || [ ] } ;
127- }
90+ if ( ! plan || ! placeId ) return ;
12891
129- if ( ! prev . stops ) return prev ;
130- return {
131- ...prev ,
132- stops : prev . stops . filter ( ( p ) => p . placeId !== placeId ) ,
133- } ;
134- } ) ;
92+ dispatch ( { type : "removePlace" , payload : placeId , init : plan . stops || [ ] } ) ;
13593 } ;
13694
13795 return (
0 commit comments