diff --git a/convex/portfolios.ts b/convex/portfolios.ts index acd0fe4..c3fb38a 100644 --- a/convex/portfolios.ts +++ b/convex/portfolios.ts @@ -30,7 +30,11 @@ export const getPortfolios = query({ export const getAllPortfolios = query({ handler: async (ctx) => { - return await ctx.db.query('portfolios').collect(); + return await ctx.db + .query('portfolios') + .withIndex('by_name') + .order('asc') + .collect(); }, }); @@ -171,3 +175,21 @@ export const deletePortfolioImage = mutation({ await ctx.storage.delete(args.storageId); }, }); + +export const delelePortfolio = mutation({ + args: { + portfolioId: v.id('portfolios'), + portfolioImageId: v.id('_storage'), + }, + + handler: async (ctx, args) => { + const portfolio = await ctx.db.get(args.portfolioId); + + if (!portfolio) { + throw new ConvexError('Portfolio not found'); + } + + await ctx.db.delete(portfolio._id); + await ctx.storage.delete(args.portfolioImageId); + }, +}); diff --git a/src/app/(authenticated)/(main)/admin/_components/delete-portfolio-form.tsx b/src/app/(authenticated)/(main)/admin/_components/delete-portfolio-form.tsx new file mode 100644 index 0000000..f402664 --- /dev/null +++ b/src/app/(authenticated)/(main)/admin/_components/delete-portfolio-form.tsx @@ -0,0 +1,79 @@ +'use client'; + +import React from 'react'; + +import { Button } from '@/components/ui/button'; +import { Form } from '@/components/ui/form'; +import { api } from '@/convex/_generated/api'; +import { Id } from '@/convex/_generated/dataModel'; +import { zodResolver } from '@hookform/resolvers/zod'; +import { useMutation } from 'convex/react'; +import { Loader2 } from 'lucide-react'; +import { useForm } from 'react-hook-form'; +import { toast } from 'sonner'; +import { z } from 'zod'; + +const deletePortfolioFormSchema = z.object({ + portfolioId: z.string(), +}); + +export default function DeletePortfolioForm({ + setOpen, + portfolioId, + portfolioImageId, +}: { + setOpen: any; + portfolioId: Id<'portfolios'>; + portfolioImageId: Id<'_storage'>; +}) { + const form = useForm>({ + resolver: zodResolver(deletePortfolioFormSchema), + defaultValues: { portfolioId: portfolioId }, + }); + const isLoading = form.formState.isSubmitting; + const delelePortfolio = useMutation(api.portfolios.delelePortfolio); + + const onSubmit = async () => { + try { + await delelePortfolio({ portfolioId, portfolioImageId }); + setOpen(false); + toast.success('Deleted successfully!'); + } catch (error) { + console.log(error); + } + }; + + return ( +
+ +
+ + +
+
+ + ); +} diff --git a/src/app/(authenticated)/(main)/admin/page.tsx b/src/app/(authenticated)/(main)/admin/page.tsx index 3dcaf2c..c46923d 100644 --- a/src/app/(authenticated)/(main)/admin/page.tsx +++ b/src/app/(authenticated)/(main)/admin/page.tsx @@ -17,8 +17,10 @@ import { import { api } from '@/convex/_generated/api'; import { cn } from '@/lib/utils'; import { useQuery } from 'convex/react'; +import { set } from 'date-fns'; import AddPortfolioForm from './_components/add-portfolio-form'; +import DeletePortfolioForm from './_components/delete-portfolio-form'; import DeleteSubmissionForm from './_components/delete-submission-form'; import EditPortfolioForm from './_components/edit-portfolio-form'; @@ -28,7 +30,8 @@ export default function Admin() { const portfolios = useQuery(api.portfolios.getAllPortfolios); const [isAddPortfolio, setIsAddPortfolio] = useState(false); const [isEditOpen, setIsEditOpen] = useState(false); - const [isDeleteOpen, setIsDeleteOpen] = useState(false); + const [isDeleteSubmissionOpen, setIsDeleteSubmissionOpen] = useState(false); + const [isDeletePortfolioOpen, setIsDeletePortfolioOpen] = useState(false); const user = useQuery(api.users.getMyUser); if (user?.isAdmin !== true) { @@ -99,7 +102,7 @@ export default function Admin() { variant="destructive" onClick={() => { setItem(submission); - setIsDeleteOpen(true); + setIsDeleteSubmissionOpen(true); }} > Delete @@ -153,6 +156,16 @@ export default function Admin() { > Update + @@ -173,18 +186,38 @@ export default function Admin() { {/* Delete Submission Form */} - + Delete submission + {/* Delete Portfolio Form */} + + + + Delete portfolio + + + + + {/* Edit Portfolio Form */}