Skip to content

Commit

Permalink
fix: create trip now opens newly created trip
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaisaact committed Nov 3, 2024
1 parent 5e6467c commit fa29e78
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
19 changes: 14 additions & 5 deletions src/hooks/trip/useCreateTrip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useNavigate } from 'react-router-dom';
import { useAppContext } from '@/hooks/useAppContext';
import { createTrip } from '@/services/apiService';
import { supabase } from '@/lib/supabase';
import { Trip } from '@/types/types';

interface CreateTripData {
days: number;
Expand All @@ -21,20 +22,28 @@ export function useCreateTrip(onClose?: () => void) {
dispatch({ type: 'SET_LOADING', payload: true });

const formattedData = {
userId: 'e92ad976-973d-406d-92d4-34b6ef182e1a', // TODO: Replace with auth
days: data.days,
location: data.location,
startDate: data.startDate ? data.startDate.toISOString() : null,
itinerary: [],
};

try {
const result = await createTrip(supabase, formattedData);
dispatch({ type: 'ADD_TRIP', payload: result.trip });

if (result.trip?.tripId) {
// The trip data is in result.trip, not result.data
const newTrip: Trip = {
tripId: result.trip.trip_id, // Changed from result.data.id
destinationName: result.trip.destination_name, // Changed from location
numDays: result.trip.num_days, // Changed from days
startDate: result.trip.startDate,
itinerary: result.trip.itinerary || [],
};

dispatch({ type: 'ADD_TRIP', payload: newTrip });

if (newTrip.tripId) {
onClose?.();
navigate(`/trips/${result.trip.tripId}`);
navigate(`/trips/${newTrip.tripId}`);
}
} catch (error) {
console.error('Error creating trip:', error);
Expand Down
3 changes: 2 additions & 1 deletion src/services/apiService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const fetchTrips = async (supabase: SupabaseClient) => {
export const createTrip = async (
supabase: SupabaseClient,
tripData: {
days: number; // removed userId since it comes from auth
days: number;
location: string;
startDate: string | null;
},
Expand All @@ -83,6 +83,7 @@ export const createTrip = async (
}

const result = await response.json();
console.log('API Response:', result); // Log the API response
return result;
};

Expand Down

0 comments on commit fa29e78

Please sign in to comment.