Skip to content

Commit 6f5e27b

Browse files
committed
Fix editQuestion form style and error handling
1 parent 9eb79db commit 6f5e27b

File tree

9 files changed

+90
-74
lines changed

9 files changed

+90
-74
lines changed

peerprep/api/structs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,12 @@ export type FormatResponse = {
129129
export const QuestionSchema = z.object({
130130
difficulty: z.nativeEnum(Difficulty),
131131
title: z.string().min(2, {
132-
message: "Please input a title.",
132+
message: "Please input a title of at least length 2.",
133133
}),
134134
content: z.string().min(2, {
135135
message: "Please input content.",
136136
}),
137137
topicTags: z.array(z.string()).min(1, {
138-
message: "Please input at least one topic tag.",
138+
message: "Please input at least one topic tag. Press enter to add a tag.",
139139
}),
140140
}) satisfies ZodType<QuestionFullBody>;

peerprep/app/questions/QuestionForm.tsx

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
import {
44
Form,
55
FormControl,
6+
FormDescription,
67
FormField,
78
FormItem,
89
FormLabel,
10+
FormMessage,
911
} from "@/components/ui/form";
1012
import { Input } from "@/components/ui/input";
1113
import {
@@ -41,7 +43,7 @@ type QuestionFormProps = {
4143
const QuestionForm = ({ form, onSubmit }: QuestionFormProps) => {
4244
return (
4345
<Form {...form}>
44-
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
46+
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-2">
4547
<FormField
4648
control={form.control}
4749
name="title"
@@ -51,6 +53,8 @@ const QuestionForm = ({ form, onSubmit }: QuestionFormProps) => {
5153
<FormControl>
5254
<Input placeholder="Two Sum" {...field} />
5355
</FormControl>
56+
<FormDescription>Please input a title.</FormDescription>
57+
<FormMessage />
5458
</FormItem>
5559
)}
5660
/>
@@ -72,28 +76,34 @@ const QuestionForm = ({ form, onSubmit }: QuestionFormProps) => {
7276
<SelectItem value={Difficulty.Hard}>Hard</SelectItem>
7377
</SelectContent>
7478
</Select>
79+
<FormDescription>Please select a difficulty.</FormDescription>
80+
<FormMessage />
7581
</FormItem>
7682
)}
7783
/>
7884
<FormField
7985
control={form.control}
8086
name="topicTags"
8187
render={({ field }) => (
82-
<FormItem className="flex w-full flex-col items-start">
83-
<FormLabel className="text-left">Topics</FormLabel>
84-
<FormControl className="w-full">
88+
<FormItem className="">
89+
<FormLabel className="">Topics</FormLabel>
90+
<FormControl className="">
8591
<Controller
8692
name="topicTags"
8793
control={form.control}
8894
render={({ field }) => (
8995
<InputTags
9096
value={field.value}
9197
onChange={field.onChange}
92-
placeholder="Press 'Enter' to add topics. "
98+
placeholder="Press 'Enter' to add tags."
9399
/>
94100
)}
95101
/>
96102
</FormControl>
103+
<FormDescription>
104+
Please input at least one topic tag.
105+
</FormDescription>
106+
<FormMessage />
97107
</FormItem>
98108
)}
99109
/>
@@ -109,6 +119,8 @@ const QuestionForm = ({ form, onSubmit }: QuestionFormProps) => {
109119
onChange={field.onChange}
110120
/>
111121
</FormControl>
122+
<FormDescription>Please input content.</FormDescription>
123+
<FormMessage />
112124
</FormItem>
113125
)}
114126
/>

peerprep/app/questions/edit/[question]/EditQuestion.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ const EditQuestion = ({ question }: { question: Question }) => {
2828
...values,
2929
};
3030
const status = await editQuestion(qn);
31+
console.log(status);
3132
if (status.error) {
3233
console.log("Failed to add question.");
3334
console.log(`Code ${status.status}: ${status.error}`);
35+
alert(`Failed to add question. Code ${status.status}: ${status.error}`);
3436
return;
3537
}
3638
console.log(`Successfully modified the question.`);

peerprep/app/questions/helper.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export async function editQuestion(question: Question): Promise<StatusBody> {
5959
},
6060
);
6161
if (!res.ok) {
62-
return { status: res.status };
62+
return { status: res.status, error: await res.text() };
6363
}
6464
revalidatePath("/questions");
6565
revalidatePath("/questions/edit/" + question.id);
@@ -81,7 +81,7 @@ export async function addQuestion(
8181
headers: generateJSONHeaders(),
8282
});
8383
if (!res.ok) {
84-
return { status: res.status };
84+
return { status: res.status, error: await res.text() };
8585
}
8686
revalidatePath("/questions");
8787
const json = await res.json();

peerprep/components/modifyQuestion/Tiptap.tsx

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,26 @@ const Tiptap = ({ defaultContent, onChange }: TipTapProps) => {
2323
Subscript,
2424
Superscript,
2525
Underline,
26-
2726
Link,
2827
Placeholder.configure({
2928
placeholder: "Add your question here",
3029
}),
3130
],
3231
content: defaultContent,
33-
immediatelyRender: false, // needed if used in a server component
32+
immediatelyRender: false,
3433
onUpdate({ editor }) {
3534
onChange(editor.getHTML());
3635
},
3736
});
3837

3938
return (
40-
<div className="h-96">
39+
<div className="flex min-h-80 flex-col">
4140
<MenuBar editor={editor} />
4241

43-
<div
44-
className="editor-content h-80 resize-y overflow-y-auto overflow-x-hidden rounded-md border bg-gray-1 p-2"
45-
style={{ wordBreak: "break-word", overflowWrap: "break-word" }}
46-
>
47-
<EditorContent editor={editor} />
48-
</div>
42+
<EditorContent
43+
editor={editor}
44+
className="flex h-full flex-1 resize-y flex-col overflow-y-auto overflow-x-hidden rounded-md border bg-[#121212] p-2"
45+
/>
4946
</div>
5047
);
5148
};

peerprep/components/navbar/Navbar.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ const NavigationList = () => {
8585

8686
const Navbar = () => {
8787
return (
88-
<Disclosure as="nav" className="mb-5 bg-gray-800">
88+
<Disclosure as="nav" className="bg-gray-800">
8989
<div className="mx-auto max-w-7xl px-2 sm:px-6 lg:px-8">
9090
<div className="relative flex h-16 items-center justify-between">
9191
<MobileMenu />

peerprep/components/ui/form.tsx

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,58 @@
1-
"use client"
1+
"use client";
22

3-
import * as React from "react"
4-
import * as LabelPrimitive from "@radix-ui/react-label"
5-
import { Slot } from "@radix-ui/react-slot"
3+
import * as React from "react";
4+
import * as LabelPrimitive from "@radix-ui/react-label";
5+
import { Slot } from "@radix-ui/react-slot";
66
import {
77
Controller,
88
ControllerProps,
99
FieldPath,
1010
FieldValues,
1111
FormProvider,
1212
useFormContext,
13-
} from "react-hook-form"
13+
} from "react-hook-form";
1414

15-
import { cn } from "@/lib/utils"
16-
import { Label } from "@/components/ui/label"
15+
import { cn } from "@/lib/utils";
16+
import { Label } from "@/components/ui/label";
1717

18-
const Form = FormProvider
18+
const Form = FormProvider;
1919

2020
type FormFieldContextValue<
2121
TFieldValues extends FieldValues = FieldValues,
22-
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
22+
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
2323
> = {
24-
name: TName
25-
}
24+
name: TName;
25+
};
2626

2727
const FormFieldContext = React.createContext<FormFieldContextValue>(
28-
{} as FormFieldContextValue
29-
)
28+
{} as FormFieldContextValue,
29+
);
3030

3131
const FormField = <
3232
TFieldValues extends FieldValues = FieldValues,
33-
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>
33+
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
3434
>({
3535
...props
3636
}: ControllerProps<TFieldValues, TName>) => {
3737
return (
3838
<FormFieldContext.Provider value={{ name: props.name }}>
3939
<Controller {...props} />
4040
</FormFieldContext.Provider>
41-
)
42-
}
41+
);
42+
};
4343

4444
const useFormField = () => {
45-
const fieldContext = React.useContext(FormFieldContext)
46-
const itemContext = React.useContext(FormItemContext)
47-
const { getFieldState, formState } = useFormContext()
45+
const fieldContext = React.useContext(FormFieldContext);
46+
const itemContext = React.useContext(FormItemContext);
47+
const { getFieldState, formState } = useFormContext();
4848

49-
const fieldState = getFieldState(fieldContext.name, formState)
49+
const fieldState = getFieldState(fieldContext.name, formState);
5050

5151
if (!fieldContext) {
52-
throw new Error("useFormField should be used within <FormField>")
52+
throw new Error("useFormField should be used within <FormField>");
5353
}
5454

55-
const { id } = itemContext
55+
const { id } = itemContext;
5656

5757
return {
5858
id,
@@ -61,53 +61,54 @@ const useFormField = () => {
6161
formDescriptionId: `${id}-form-item-description`,
6262
formMessageId: `${id}-form-item-message`,
6363
...fieldState,
64-
}
65-
}
64+
};
65+
};
6666

6767
type FormItemContextValue = {
68-
id: string
69-
}
68+
id: string;
69+
};
7070

7171
const FormItemContext = React.createContext<FormItemContextValue>(
72-
{} as FormItemContextValue
73-
)
72+
{} as FormItemContextValue,
73+
);
7474

7575
const FormItem = React.forwardRef<
7676
HTMLDivElement,
7777
React.HTMLAttributes<HTMLDivElement>
7878
>(({ className, ...props }, ref) => {
79-
const id = React.useId()
79+
const id = React.useId();
8080

8181
return (
8282
<FormItemContext.Provider value={{ id }}>
8383
<div ref={ref} className={cn("space-y-2", className)} {...props} />
8484
</FormItemContext.Provider>
85-
)
86-
})
87-
FormItem.displayName = "FormItem"
85+
);
86+
});
87+
FormItem.displayName = "FormItem";
8888

8989
const FormLabel = React.forwardRef<
9090
React.ElementRef<typeof LabelPrimitive.Root>,
9191
React.ComponentPropsWithoutRef<typeof LabelPrimitive.Root>
9292
>(({ className, ...props }, ref) => {
93-
const { error, formItemId } = useFormField()
93+
const { error, formItemId } = useFormField();
9494

9595
return (
9696
<Label
9797
ref={ref}
98-
className={cn(error && "text-destructive", className)}
98+
className={cn(error && "text-destructive", className, "text-md")}
9999
htmlFor={formItemId}
100100
{...props}
101101
/>
102-
)
103-
})
104-
FormLabel.displayName = "FormLabel"
102+
);
103+
});
104+
FormLabel.displayName = "FormLabel";
105105

106106
const FormControl = React.forwardRef<
107107
React.ElementRef<typeof Slot>,
108108
React.ComponentPropsWithoutRef<typeof Slot>
109109
>(({ ...props }, ref) => {
110-
const { error, formItemId, formDescriptionId, formMessageId } = useFormField()
110+
const { error, formItemId, formDescriptionId, formMessageId } =
111+
useFormField();
111112

112113
return (
113114
<Slot
@@ -121,15 +122,15 @@ const FormControl = React.forwardRef<
121122
aria-invalid={!!error}
122123
{...props}
123124
/>
124-
)
125-
})
126-
FormControl.displayName = "FormControl"
125+
);
126+
});
127+
FormControl.displayName = "FormControl";
127128

128129
const FormDescription = React.forwardRef<
129130
HTMLParagraphElement,
130131
React.HTMLAttributes<HTMLParagraphElement>
131132
>(({ className, ...props }, ref) => {
132-
const { formDescriptionId } = useFormField()
133+
const { formDescriptionId } = useFormField();
133134

134135
return (
135136
<p
@@ -138,19 +139,19 @@ const FormDescription = React.forwardRef<
138139
className={cn("text-[0.8rem] text-muted-foreground", className)}
139140
{...props}
140141
/>
141-
)
142-
})
143-
FormDescription.displayName = "FormDescription"
142+
);
143+
});
144+
FormDescription.displayName = "FormDescription";
144145

145146
const FormMessage = React.forwardRef<
146147
HTMLParagraphElement,
147148
React.HTMLAttributes<HTMLParagraphElement>
148149
>(({ className, children, ...props }, ref) => {
149-
const { error, formMessageId } = useFormField()
150-
const body = error ? String(error?.message) : children
150+
const { error, formMessageId } = useFormField();
151+
const body = error ? String(error?.message) : children;
151152

152153
if (!body) {
153-
return null
154+
return null;
154155
}
155156

156157
return (
@@ -162,9 +163,9 @@ const FormMessage = React.forwardRef<
162163
>
163164
{body}
164165
</p>
165-
)
166-
})
167-
FormMessage.displayName = "FormMessage"
166+
);
167+
});
168+
FormMessage.displayName = "FormMessage";
168169

169170
export {
170171
useFormField,
@@ -175,4 +176,4 @@ export {
175176
FormDescription,
176177
FormMessage,
177178
FormField,
178-
}
179+
};

0 commit comments

Comments
 (0)