Skip to content

Commit

Permalink
delete itineraries
Browse files Browse the repository at this point in the history
  • Loading branch information
jacksors committed Jan 28, 2024
1 parent c978d4b commit e524562
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 3 deletions.
17 changes: 15 additions & 2 deletions components/itinerary/itinerarycard.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import {Card} from "@/components/ui/card";
import {Button} from "@/components/ui/button";

export interface Props {
itinerary: {
Expand All @@ -22,8 +23,10 @@ export interface Props {
location: string
}
}
}[]
};
}[],
},
isOnItineraryIndex?: boolean,
deleteCallback?: () => void
}

type Itinerary = {
Expand Down Expand Up @@ -116,6 +119,16 @@ export const ItineraryCard = (props: Props) => {
<p className='text-3xl'>{finalDestination}</p>
<p>{arriveTime?.toDateString()}</p>
</div>
{
props.isOnItineraryIndex
? (<Button className='bg-destructive' onClick={props.deleteCallback}>
<span className="material-symbols-outlined">
close
</span>
</Button>)
: <></>
}

</Card>
);
};
20 changes: 20 additions & 0 deletions pages/api/itineraries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,26 @@ const handle = async (req: NextApiRequest, res: NextApiResponse) => {
});

res.status(200).json(create);
break;
}
case 'DELETE': {
const {id} = req.query;
if (!id) return res.status(400).json({message: 'Itinerary ID is required'});
if (typeof id !== 'string') return res.status(400).json({message: 'Itinerary ID must be a string'});
const itineraryId = parseInt(id);
const itinerary = await prisma.itenerary.findFirst({
where: {
id: itineraryId
}
});
if (!itinerary) return res.status(400).json({message: 'Itinerary not found'});
const deleteItinerary = await prisma.itenerary.delete({
where: {
id: itineraryId
}
});
res.status(200).json(deleteItinerary);
break;
}
}
}
Expand Down
Empty file removed pages/api/itineraries/[id].ts
Empty file.
13 changes: 12 additions & 1 deletion pages/itineraries/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ const Index = () => {
}
}, [wingmanUser])

const onDelete = (id: number) => {
fetch(`/api/itineraries?id=${id}`, {
method: 'DELETE',
}).then(res => {
console.log(res);
if (res.status === 200) {
setItineraries(itineraries.filter(itinerary => itinerary.id !== id));
}
})
}

if (!user) return (
<div className='flex flex-col justify-center items-center gap-5 px-24 py-12 pt-24'>
<p className='text-xl text-gray-700 italic'>Please log in to view your itineraries!</p>
Expand All @@ -50,7 +61,7 @@ const Index = () => {
<div className='flex flex-col justify-center items-center gap-5 px-24 py-12 pt-24'>
<h1 className='text-3xl'>Current Itineraries</h1>
{itineraries.map((itinerary, index) => {
return <ItineraryCard key={'itinerary-' + index} itinerary={itinerary} />
return <ItineraryCard key={'itinerary-' + index} itinerary={itinerary} isOnItineraryIndex deleteCallback={() => onDelete(itinerary.id)}/>
})}
<Button className='fixed bottom-3'><Link href='/itineraries/create'>Create New Itinerary</Link></Button>
</div>
Expand Down

0 comments on commit e524562

Please sign in to comment.