Skip to content

Commit

Permalink
Add report a problem and suggest a feature dialog form.
Browse files Browse the repository at this point in the history
  • Loading branch information
bricesuazo committed Sep 25, 2023
1 parent 5e7c76c commit ccc5af5
Show file tree
Hide file tree
Showing 3 changed files with 382 additions and 158 deletions.
149 changes: 149 additions & 0 deletions apps/www/src/components/feedback-form.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
"use client";

import { useEffect } from "react";
import { Button } from "@/components/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@/components/ui/dialog";
import { api } from "@/lib/trpc/client";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { z } from "zod";

import { Icons } from "./icons";
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "./ui/form";
import { Textarea } from "./ui/textarea";
import { toast } from "./ui/use-toast";

const formSchema = z.object({
content: z.string().min(2, {
message: "Username must be at least 2 characters.",
}),
});

export default function FeedbackForm({
type,
open,
setOpen,
}: {
type: "bug" | "feature";
open: boolean;
setOpen: (open: boolean) => void;
}) {
const reportAProblemMutation = api.users.reportAProblem.useMutation({
onSuccess: () => {
setOpen(false);
toast({
title: "Reported a problem",
description: "Thanks for reporting a problem! We'll look into it",
});
},
});
const suggestAFeatureMutation = api.users.suggestAFeature.useMutation({
onSuccess: () => {
setOpen(false);
toast({
title: "Suggested a feature",
description: "Thanks for suggesting a feature! We'll look into it",
});
},
});
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
content: "",
},
});

useEffect(() => {
if (open) {
form.reset();
}
}, [open]);

return (
<Dialog open={open} onOpenChange={setOpen}>
<DialogContent className="sm:max-w-[425px]">
<DialogHeader>
<DialogTitle>
{type === "bug" ? "Report a bug" : "Request a feature"}
</DialogTitle>
<DialogDescription>
{type === "bug"
? "Found a bug? Let us know so we can fix it!"
: "Have an idea for a feature? Let us know so we can build it!"}
</DialogDescription>
</DialogHeader>
<Form {...form}>
<form
onSubmit={form.handleSubmit((values) => {
if (type === "bug") {
reportAProblemMutation.mutate({ content: values.content });
} else {
suggestAFeatureMutation.mutate({ content: values.content });
}
})}
className="space-y-8"
>
<FormField
control={form.control}
name="content"
render={({ field }) => (
<FormItem>
<FormLabel>{type === "bug" ? "Bug" : "Feature"}</FormLabel>
<FormControl>
<Textarea
placeholder={
type === "bug"
? "What went wrong?"
: "What do you want to see?"
}
disabled={
reportAProblemMutation.isLoading ||
suggestAFeatureMutation.isLoading
}
{...field}
/>
</FormControl>
<FormDescription>
Please be as detailed as possible.
</FormDescription>
<FormMessage />
</FormItem>
)}
/>

<DialogFooter>
<Button
type="submit"
disabled={
reportAProblemMutation.isLoading ||
suggestAFeatureMutation.isLoading
}
>
{(reportAProblemMutation.isLoading ||
suggestAFeatureMutation.isLoading) && (
<Icons.spinner className="mr-2 animate-spin" />
)}
Submit
</Button>
</DialogFooter>
</form>
</Form>
</DialogContent>
</Dialog>
);
}
Loading

2 comments on commit ccc5af5

@vercel
Copy link

@vercel vercel bot commented on ccc5af5 Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on ccc5af5 Sep 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.