-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #58 from diggerhq/feat/delete-project
Delete projects
- Loading branch information
Showing
3 changed files
with
164 additions
and
5 deletions.
There are no files selected for viewing
144 changes: 144 additions & 0 deletions
144
...ges)/(application-pages)/project/[projectSlug]/(specific-project-pages)/DeleteProject.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
'use client'; | ||
|
||
import { Button } from '@/components/ui/button'; | ||
import { Card, CardDescription, CardFooter, CardHeader, CardTitle } from '@/components/ui/card'; | ||
import { | ||
Dialog, | ||
DialogContent, | ||
DialogDescription, | ||
DialogFooter, | ||
DialogHeader, | ||
DialogTitle, | ||
DialogTrigger, | ||
} from '@/components/ui/dialog'; | ||
import { FormControl, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form'; | ||
import { Input } from '@/components/ui/input'; | ||
import { deleteProject } from '@/data/user/projects'; | ||
import { useSAToastMutation } from '@/hooks/useSAToastMutation'; | ||
import { zodResolver } from '@hookform/resolvers/zod'; | ||
import { motion } from 'framer-motion'; | ||
import { useRouter } from 'next/navigation'; | ||
import { useState } from 'react'; | ||
import { FormProvider, useForm } from 'react-hook-form'; | ||
import { toast } from 'sonner'; | ||
import { z } from 'zod'; | ||
|
||
type DeleteProjectProps = { | ||
projectName: string; | ||
projectId: string; | ||
}; | ||
|
||
export const DeleteProject = ({ | ||
projectName, | ||
projectId, | ||
}: DeleteProjectProps) => { | ||
const [open, setOpen] = useState(false); | ||
const router = useRouter(); | ||
|
||
const formSchema = z.object({ | ||
projectName: z | ||
.string() | ||
.refine( | ||
(v) => v === `delete ${projectName}`, | ||
`Must match "delete ${projectName}"`, | ||
), | ||
}); | ||
|
||
const form = useForm<z.infer<typeof formSchema>>({ | ||
resolver: zodResolver(formSchema), | ||
defaultValues: { | ||
projectName: '', | ||
}, | ||
}); | ||
|
||
const { mutate, isLoading } = useSAToastMutation( | ||
async () => deleteProject(projectId), | ||
{ | ||
onSuccess: () => { | ||
toast.success('Project deleted'); | ||
setOpen(false); | ||
router.push('/dashboard'); | ||
}, | ||
loadingMessage: 'Deleting project...', | ||
errorMessage(error) { | ||
try { | ||
if (error instanceof Error) { | ||
return String(error.message); | ||
} | ||
return `Failed to delete project ${String(error)}`; | ||
} catch (_err) { | ||
console.warn(_err); | ||
return 'Failed to delete project'; | ||
} | ||
}, | ||
}, | ||
); | ||
|
||
const onSubmit = (values: z.infer<typeof formSchema>) => { | ||
mutate(); | ||
}; | ||
|
||
return ( | ||
<motion.div | ||
initial={{ opacity: 0, y: 20 }} | ||
animate={{ opacity: 1, y: 0 }} | ||
transition={{ duration: 0.3 }} | ||
className="mt-4" | ||
> | ||
<Card className="w-full max-w-5xl border-destructive/50 bg-destructive/5"> | ||
<CardHeader> | ||
<CardTitle> | ||
Danger Zone | ||
</CardTitle> | ||
<CardDescription> | ||
Deleting a project does not destroy associated resources! Make sure to destroy them. | ||
</CardDescription> | ||
</CardHeader> | ||
<CardFooter className="flex justify-start"> | ||
<Dialog open={open} onOpenChange={setOpen}> | ||
<DialogTrigger asChild> | ||
<Button variant="destructive">Delete Project</Button> | ||
</DialogTrigger> | ||
<DialogContent> | ||
<DialogHeader> | ||
<DialogTitle>Delete Project</DialogTitle> | ||
<DialogDescription> | ||
Type <strong>"delete {projectName}"</strong> to confirm. | ||
</DialogDescription> | ||
</DialogHeader> | ||
<FormProvider {...form}> | ||
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4"> | ||
<FormField | ||
control={form.control} | ||
name="projectName" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Confirmation</FormLabel> | ||
<FormControl> | ||
<Input {...field} /> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
<DialogFooter> | ||
<Button type="button" variant="outline" onClick={() => setOpen(false)}> | ||
Cancel | ||
</Button> | ||
<Button | ||
type="submit" | ||
variant="destructive" | ||
disabled={isLoading || !form.formState.isValid} | ||
> | ||
{isLoading ? 'Deleting...' : 'Delete'} Project | ||
</Button> | ||
</DialogFooter> | ||
</form> | ||
</FormProvider> | ||
</DialogContent> | ||
</Dialog> | ||
</CardFooter> | ||
</Card> | ||
</motion.div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters