Skip to content

Commit 724c155

Browse files
use enum for years of experience field
1 parent 5d05a0b commit 724c155

File tree

3 files changed

+59
-55
lines changed

3 files changed

+59
-55
lines changed

app/(app)/onboarding/_client.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import { Select } from "@/components/ui-components/select";
3939
import { Button } from "@/components/ui-components/button";
4040
import { Heading, Subheading } from "@/components/ui-components/heading";
4141
import { Divider } from "@/components/ui-components/divider";
42+
import { experienceRangeEnum } from "@/server/db/schema";
4243

4344
type UserDetails = {
4445
username: string;
@@ -51,7 +52,7 @@ type UserDetails = {
5152
levelOfStudy: string;
5253
jobTitle: string;
5354
workplace: string;
54-
yearsOfExperience: string;
55+
yearsOfExperience: "0-1" | "1-3" | "3-5" | "5-8" | "12+" | undefined;
5556
};
5657

5758
export default function AdditionalSignUpDetails({
@@ -358,7 +359,7 @@ function SlideTwo({ details }: { details: UserDetails }) {
358359
id="day"
359360
aria-label="day"
360361
value={day ? day : ""}
361-
disabled={!month || undefined}
362+
disabled={month === undefined || year === undefined}
362363
required
363364
onChange={(e) => setDay(Number(e.target.value))}
364365
>
@@ -424,7 +425,13 @@ function SlideThree({ details }: { details: UserDetails }) {
424425
jobTitle,
425426
course,
426427
levelOfStudy,
427-
yearsOfExperience,
428+
yearsOfExperience: yearsOfExperience as
429+
| "0-1"
430+
| "1-3"
431+
| "3-5"
432+
| "5-8"
433+
| "12+"
434+
| undefined,
428435
},
429436
});
430437

@@ -457,8 +464,6 @@ function SlideThree({ details }: { details: UserDetails }) {
457464
}
458465
};
459466

460-
const yearsOfExperienceOptions = ["0-1", "1-3", "3-5", "5-8", "12+"] as const;
461-
462467
return (
463468
<form className="mx-auto max-w-sm" onSubmit={handleSubmit(onFormSubmit)}>
464469
<div className="min-h-[32rem]">
@@ -528,11 +533,12 @@ function SlideThree({ details }: { details: UserDetails }) {
528533
<Select
529534
id="years-of-experience"
530535
{...register("yearsOfExperience")}
536+
defaultValue=""
531537
>
532538
<option value="" disabled>
533-
Select range
539+
Select years of experience
534540
</option>
535-
{Object.values(yearsOfExperienceOptions).map((range) => (
541+
{experienceRangeEnum.enumValues.map((range) => (
536542
<option key={range} value={range}>
537543
{range} years
538544
</option>

app/(app)/onboarding/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default async function Page() {
3838
levelOfStudy: details?.levelOfStudy || "",
3939
jobTitle: details?.jobTitle || "",
4040
workplace: details?.workplace || "",
41-
yearsOfExperience: details?.yearsOfExperience || "",
41+
yearsOfExperience: details?.yearsOfExperience || undefined,
4242
};
4343

4444
return <Content details={detailsWithNullsRemoved} />;

schema/additionalUserDetails.ts

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import { experienceRangeEnum } from "@/server/db/schema";
12
import z from "zod";
23

4+
export const experienceRangeValues = experienceRangeEnum.enumValues;
5+
36
export const slideOneSchema = z.object({
47
name: z
58
.string()
@@ -30,55 +33,50 @@ export const slideThreeSchema = z
3033
jobTitle: z.string().max(30, "Max length is 30 characters."),
3134
levelOfStudy: z.string(),
3235
course: z.string().max(30, "Max name length is 30 characters."),
33-
yearsOfExperience: z.string(),
36+
yearsOfExperience: z
37+
.enum(experienceRangeValues, {
38+
errorMap: () => ({ message: "Please select a valid experience range" }),
39+
})
40+
.optional(),
3441
})
3542
.superRefine((val, ctx) => {
36-
if (
37-
val.professionalOrStudent === "Current student" &&
38-
val.levelOfStudy === ""
39-
) {
40-
ctx.addIssue({
41-
path: ["levelOfStudy"],
42-
code: "custom",
43-
message: "required",
44-
});
45-
}
46-
if (val.professionalOrStudent === "Current student" && val.course === "") {
47-
ctx.addIssue({
48-
path: ["course"],
49-
code: "custom",
50-
message: "required",
51-
});
52-
}
53-
if (
54-
val.professionalOrStudent === "Working professional" &&
55-
val.workplace === ""
56-
) {
57-
ctx.addIssue({
58-
path: ["workplace"],
59-
code: "custom",
60-
message: "required",
61-
});
62-
}
63-
if (
64-
val.professionalOrStudent === "Working professional" &&
65-
val.jobTitle === ""
66-
) {
67-
ctx.addIssue({
68-
path: ["jobTitle"],
69-
code: "custom",
70-
message: "required",
71-
});
72-
}
73-
if (
74-
val.professionalOrStudent === "Working professional" &&
75-
val.yearsOfExperience === ""
76-
) {
77-
ctx.addIssue({
78-
path: ["yearsOfExperience"],
79-
code: "custom",
80-
message: "required",
81-
});
43+
if (val.professionalOrStudent === "Current student") {
44+
if (val.levelOfStudy === "") {
45+
ctx.addIssue({
46+
path: ["levelOfStudy"],
47+
code: "custom",
48+
message: "required",
49+
});
50+
}
51+
if (val.course === "") {
52+
ctx.addIssue({
53+
path: ["course"],
54+
code: "custom",
55+
message: "required",
56+
});
57+
}
58+
} else if (val.professionalOrStudent === "Working professional") {
59+
if (val.workplace === "") {
60+
ctx.addIssue({
61+
path: ["workplace"],
62+
code: "custom",
63+
message: "required",
64+
});
65+
}
66+
if (val.jobTitle === "") {
67+
ctx.addIssue({
68+
path: ["jobTitle"],
69+
code: "custom",
70+
message: "required",
71+
});
72+
}
73+
if (val.yearsOfExperience === undefined) {
74+
ctx.addIssue({
75+
path: ["yearsOfExperience"],
76+
code: "custom",
77+
message: "required",
78+
});
79+
}
8280
}
8381
});
8482

0 commit comments

Comments
 (0)