Skip to content

Commit

Permalink
refactor before pr
Browse files Browse the repository at this point in the history
  • Loading branch information
John-Paul-Larkin committed Feb 14, 2024
1 parent 3f7685a commit af526f4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 47 deletions.
59 changes: 29 additions & 30 deletions app/alpha/developer/sign-up/_client.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
"use client";

import { redirect } from "next/navigation";
import React, { useEffect, useState } from "react";
import { redirect } from "next/navigation";
import { useSession } from "next-auth/react";
import { DeveloperDetailsSchema } from "@/schema/developerDetails";
import { FormProvider, useForm, useFormContext } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import type { Prisma } from "@prisma/client";
import { handleFormSubmit } from "./actions";
import { toast } from "sonner";
import {
type TypeDeveloperDetailsSchema,
professionalOrStudentOptions,
Expand All @@ -11,12 +17,6 @@ import {
levelOfStudyOptions,
months,
} from "@/schema/developerDetails";
import { DeveloperDetailsSchema } from "@/schema/developerDetails";
import { FormProvider, useForm, useFormContext } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import type { Prisma } from "@prisma/client";
import { handleMyFormSubmit } from "./actions";
import { toast } from "sonner";

type SlideProps = {
setCurrentSlide: React.Dispatch<React.SetStateAction<number>>;
Expand Down Expand Up @@ -75,8 +75,8 @@ export default function SignUp({

const [currentSlide, setCurrentSlide] = useState(0);

const onMyFormSubmit = async (data: TypeDeveloperDetailsSchema) => {
const isSuccess = await handleMyFormSubmit(data);
const onFormSubmit = async (data: TypeDeveloperDetailsSchema) => {
const isSuccess = await handleFormSubmit(data);

if (isSuccess) {
toast.success("Saved");
Expand All @@ -91,7 +91,7 @@ export default function SignUp({
<FormProvider {...useFormObject}>
<form
className="min-h-[41rem]"
onSubmit={useFormObject.handleSubmit((data) => onMyFormSubmit(data))}
onSubmit={useFormObject.handleSubmit((data) => onFormSubmit(data))}
>
<SignupProgressBar currentSlide={currentSlide} />

Expand All @@ -111,7 +111,12 @@ export default function SignUp({

function SlideOne(props: SlideProps) {
const { setCurrentSlide } = props;
const { register, trigger, getFieldState } = useFormContext();
const {
register,
trigger,
getFieldState,
formState: { errors },
} = useFormContext();

let nameError = getFieldState("name").error;
let usernameError = getFieldState("username").error;
Expand Down Expand Up @@ -172,7 +177,6 @@ function SlideOne(props: SlideProps) {
</label>
<div className="mt-2">
<input
// id="last_name"
type="text"
placeholder="And your last name?"
disabled
Expand Down Expand Up @@ -245,13 +249,10 @@ function SlideTwo(props: SlideProps) {
} = useFormContext();

const dob = getValues("developerDetails.dateOfBirth");

const [year, setYear] = useState<number | undefined>(dob?.getFullYear());
const [month, setMonth] = useState<number | undefined>(dob?.getMonth());
const [day, setDay] = useState<number | undefined>(dob?.getDate());

console.log("dob ==", dob, year, month, day);

const [listOfDaysInSelectedMonth, setListOfDaysInSelectedMonth] = useState([
0,
]);
Expand Down Expand Up @@ -280,7 +281,6 @@ function SlideTwo(props: SlideProps) {
selectedDate = new Date(year, month, day);
}
setValue("developerDetails.dateOfBirth", selectedDate);
console.log("dob == in", dob, year, month, day);
}
}, [year, month, day]);

Expand Down Expand Up @@ -378,7 +378,6 @@ function SlideTwo(props: SlideProps) {
name="month"
value={month !== undefined ? months[month] : ""}
required
// value={month !== undefined ? months[month] : ""}
className="mt-2 block rounded-md border-0 py-1.5 pl-3 pr-10 text-gray-900 ring-1 ring-inset ring-gray-300 focus:ring-2 focus:ring-red-600 sm:text-sm sm:leading-6"
onChange={(e) => setMonth(months.indexOf(e.target.value))}
>
Expand All @@ -400,7 +399,6 @@ function SlideTwo(props: SlideProps) {
name="day"
value={day ? day : ""}
className="mt-2 block rounded-md border-0 py-1.5 pl-3 pr-10 text-gray-900 ring-1 ring-inset ring-gray-300 focus:ring-2 focus:ring-red-600 sm:text-sm sm:leading-6"
// value={day ? day : ""}
disabled={!year || month === undefined}
required
onChange={(e) => setDay(Number(e.target.value))}
Expand Down Expand Up @@ -570,7 +568,6 @@ function SlideThree(props: SlideProps) {
{...register("developerDetails.jobTitle")}
type="text"
placeholder="Codú corp"
// required , { required: " required" }
className="block w-full rounded-md border-0 bg-white/5 py-1.5 text-white shadow-sm ring-1 ring-inset ring-white/10 focus:ring-2 focus:ring-inset focus:ring-red-500 sm:text-sm sm:leading-6"
/>
</div>
Expand Down Expand Up @@ -641,20 +638,14 @@ function SlideFour(props: SlideProps) {

const {
getValues,
register,
watch,
trigger,
handleSubmit,
getFieldState,

formState: { errors },
} = useFormContext();

const name = getValues("name");
const username = getValues("username");
const location = getValues("developerDetails.location");
const gender = getValues("developerDetails.gender");
// const dateOfBirth = getValues("developerDetails.dateOfBirth");
const dateOfBirth = getValues("developerDetails.dateOfBirth");
const professionalOrStudent = getValues(
"developerDetails.professionalOrStudent",
);
Expand All @@ -673,7 +664,7 @@ function SlideFour(props: SlideProps) {
<h2 className="mt-10 text-2xl font-bold leading-9 tracking-tight text-white">
Review
</h2>
<p>Please review your details before submitting.</p>
<p>Please review the details before submitting.</p>
</div>

<div className="mt-10... sm:mx-auto sm:w-full sm:max-w-sm">
Expand All @@ -696,6 +687,17 @@ function SlideFour(props: SlideProps) {
<p className="w-1/2">Gender: </p> <p className="w-1/2"> {gender}</p>
</div>

<div className="flex min-w-[20rem]">
<p className="w-1/2">Date of birth: </p>{" "}
<p className="w-1/2">
{" "}
{dateOfBirth.getDate()}
{" / "}
{dateOfBirth.getMonth() + 1} {" / "}
{dateOfBirth.getFullYear()}
</p>
</div>

<div className="flex min-w-[20rem]">
<p className="w-1/2">Occupation Status: </p>{" "}
<p className="w-1/2"> {professionalOrStudent}</p>
Expand Down Expand Up @@ -737,9 +739,6 @@ function SlideFour(props: SlideProps) {
</div>
);
}
{
/* <div>{dateOfBirth}</div> */
}

function SignupProgressBar({ currentSlide }: { currentSlide: number }) {
const progressPercentage = (100 / 4) * (currentSlide + 1);
Expand Down
15 changes: 2 additions & 13 deletions app/alpha/developer/sign-up/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,13 @@ import { getServerAuthSession } from "@/server/auth";
import prisma from "@/server/db/client";
import { redirect } from "next/navigation";

export async function handleMyFormSubmit(
dataInput: TypeDeveloperDetailsSchema,
) {
export async function handleFormSubmit(dataInput: TypeDeveloperDetailsSchema) {
const session = await getServerAuthSession();

if (!session || !session.user) {
redirect("/get-started");
}

try {
const user = await prisma.user.findUnique({
where: {
id: session.user.id,
},
include: {
developerDetails: true,
},
});

await prisma.user.update({
where: {
id: session.user.id,
Expand All @@ -38,6 +26,7 @@ export async function handleMyFormSubmit(
// A record will always exist in the User model
// So first we need to check to see if a record exists in the DeveloperDetails model for the user id
// Then either update it or create it

const isExistDeveloperDetails = await prisma.developerDetails.findUnique({
where: { id: session.user.id },
});
Expand Down
8 changes: 4 additions & 4 deletions schema/developerDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,21 @@ export const DeveloperDetailsSchema = z.object({
.string()
.trim()
.min(2, "Min name length is 2 characters.")
.max(30, "Max name length is 30 characters."),
.max(50, "Max name length is 30 characters."),
username: z
.string()
.trim()
.min(2, "Min name length is 2 characters.")
.max(30, "Max name length is 30 characters."),
.min(3, "Min name length is 2 characters.")
.max(40, "Max name length is 30 characters."),

developerDetails: z
.object({
location: z.string().min(1, "Location is required"),
gender: z.string().min(1, "Gender is required"),
dateOfBirth: z.date(),
professionalOrStudent: z.string().min(1, "Select an option"),
workplace: z.string(),

workplace: z.string().max(30, "Max length is 30 characters."),
jobTitle: z.string().max(30, "Max length is 30 characters."),
levelOfStudy: z.string(),
course: z.string().max(30, "Max name length is 30 characters."),
Expand Down

0 comments on commit af526f4

Please sign in to comment.