Skip to content

Commit

Permalink
removed all radix themes references
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfiej-k committed Nov 5, 2023
2 parents d8bee35 + 3b4b51f commit dfa9ff1
Show file tree
Hide file tree
Showing 9 changed files with 1,003 additions and 159 deletions.
99 changes: 99 additions & 0 deletions app/background/clinician-info-form.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
"use client";

import { toast } from "@/components/ui/use-toast";
import { zodResolver } from "@hookform/resolvers/zod";
import { createClientComponentClient } from "@supabase/auth-helpers-nextjs";
import { useRouter } from "next/navigation";
import { type BaseSyntheticEvent } from "react";
import { useForm } from "react-hook-form";

import { type Database } from "@/lib/schema";
import { clinicianSchema, type ClinicianData } from "./info-schemas";
type Clinician = Database["public"]["Tables"]["clinicians"]["Row"];

export default function ClinicianInfoForm(clinician: Clinician) {
const router = useRouter();

const defaultValues: Partial<ClinicianData> = {
first_name: clinician.first_name ?? undefined,
last_name: clinician.last_name ?? undefined,
employer: clinician.employer ?? undefined,
state: clinician.state ?? undefined,
city: clinician.city ?? undefined,
zip: clinician.zip ?? undefined,
};

const form = useForm<ClinicianData>({
resolver: zodResolver(clinicianSchema),
defaultValues,
mode: "onChange",
});

const onSubmit = async (input: ClinicianData) => {
const supabase = createClientComponentClient<Database>();
const { error } = await supabase
.from("clinicians")
.update({
user_id: clinician.user_id,
...input,
})
.eq("id", clinician.id);

if (error) {
return toast({
title: "Something went wrong.",
description: error.message,
variant: "destructive",
});
}

form.reset(input);
router.refresh();
return toast({
title: "Success!",
description: "Your information has been updated.",
variant: "default",
});
};

return (
<form onSubmit={(e: BaseSyntheticEvent) => void form.handleSubmit(onSubmit)(e)}>
<div>
<label htmlFor="firstname">First Name: &nbsp;</label>
<input type="text" id="firstname" {...form.register("first_name")} />
{form.formState.errors.first_name && <span> (Error: {form.formState.errors.first_name?.message})</span>}
</div>
<div>
<label htmlFor="lastname">Last Name: &nbsp;</label>
<input type="text" id="lastname" {...form.register("last_name")} />
{form.formState.errors.last_name && <span> (Error: {form.formState.errors.last_name?.message})</span>}
</div>
<div>
<label htmlFor="age">Employer: &nbsp;</label>
<input type="text" id="employer" {...form.register("employer")} />
{form.formState.errors.employer && <span> (Error: {form.formState.errors.employer?.message})</span>}
</div>
<div>
<label htmlFor="state">State: &nbsp;</label>
<input type="text" id="state" {...form.register("state")} />
{form.formState.errors.state && <span> (Error: {form.formState.errors.state?.message})</span>}
</div>
<div>
<label htmlFor="city">City: &nbsp;</label>
<input type="text" id="city" {...form.register("city")} />
{form.formState.errors.city && <span> (Error: {form.formState.errors.city?.message})</span>}
</div>
<div>
<label htmlFor="zip">Zip Code: &nbsp;</label>
<input type="text" id="zip" {...form.register("zip")} />
{form.formState.errors.zip && <span> (Error: {form.formState.errors.zip?.message})</span>}
</div>
<div>
<br />
<button type="submit" disabled={form.formState.isSubmitting}>
Submit
</button>
</div>
</form>
);
}
62 changes: 62 additions & 0 deletions app/background/info-schemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { z } from "zod";

const patientSchema = z.object({
first_name: z
.string()
.nullable()
.transform((val) => (val?.trim() === "" ? null : val?.trim())),
last_name: z
.string()
.nullable()
.transform((val) => (val?.trim() === "" ? null : val?.trim())),
age: z.coerce
.number()
.int()
.gte(0)
.nullable()
.transform((val) => (val == 0 ? null : val)),
state: z
.string()
.nullable()
.transform((val) => (val?.trim() === "" ? null : val?.trim())),
city: z
.string()
.nullable()
.transform((val) => (val?.trim() === "" ? null : val?.trim())),
zip: z
.string()
.nullable()
.transform((val) => (val?.trim() === "" ? null : val?.trim())),
});

const clinicianSchema = z.object({
first_name: z
.string()
.nullable()
.transform((val) => (val?.trim() === "" ? null : val?.trim())),
last_name: z
.string()
.nullable()
.transform((val) => (val?.trim() === "" ? null : val?.trim())),
employer: z
.string()
.nullable()
.transform((val) => (val?.trim() === "" ? null : val?.trim())),
state: z
.string()
.nullable()
.transform((val) => (val?.trim() === "" ? null : val?.trim())),
city: z
.string()
.nullable()
.transform((val) => (val?.trim() === "" ? null : val?.trim())),
zip: z
.string()
.nullable()
.transform((val) => (val?.trim() === "" ? null : val?.trim())),
});

type PatientData = z.infer<typeof patientSchema>;
type ClinicianData = z.infer<typeof clinicianSchema>;

export { clinicianSchema, patientSchema, type ClinicianData, type PatientData };
32 changes: 32 additions & 0 deletions app/background/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { createServerSupabaseClient } from "@/lib/server-utils";
import { redirect } from "next/navigation";
import ClinicianInfoForm from "./clinician-info-form";
import PatientInfoForm from "./patient-info-form";

export default async function BackgroundInfo() {
const supabase = createServerSupabaseClient();
const {
data: { session },
} = await supabase.auth.getSession();

if (!session) {
redirect("/");
}

// Retrieve user information
const userid = session.user.id;
const { data: patients } = await supabase.from("patients").select().eq("user_id", userid);
const { data: clinicians } = await supabase.from("clinicians").select().eq("user_id", userid);

// Allow size > 1 for now
if (!patients?.length && !clinicians?.length) {
return <>User data not found.</>;
}

return (
<>
{patients?.map((patient) => <PatientInfoForm key={patient.id} {...patient} />)}
{clinicians?.map((clinician) => <ClinicianInfoForm key={clinician.id} {...clinician} />)}
</>
);
}
94 changes: 94 additions & 0 deletions app/background/patient-info-form.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
"use client";

import { toast } from "@/components/ui/use-toast";
import { zodResolver } from "@hookform/resolvers/zod";
import { createClientComponentClient } from "@supabase/auth-helpers-nextjs";
import { useRouter } from "next/navigation";
import { type BaseSyntheticEvent } from "react";
import { useForm } from "react-hook-form";

import { type Database } from "@/lib/schema";
import { patientSchema, type PatientData } from "./info-schemas";
type Patient = Database["public"]["Tables"]["patients"]["Row"];

export default function PatientInfoForm(patient: Patient) {
const router = useRouter();

const defaultValues: Partial<PatientData> = {
first_name: patient.first_name ?? undefined,
last_name: patient.last_name ?? undefined,
age: patient.age ?? undefined,
state: patient.state ?? undefined,
city: patient.city ?? undefined,
zip: patient.zip ?? undefined,
};

const form = useForm<PatientData>({
resolver: zodResolver(patientSchema),
defaultValues,
mode: "onChange",
});

const onSubmit = async (input: PatientData) => {
const supabase = createClientComponentClient<Database>();
const { error } = await supabase
.from("patients")
.update({
user_id: patient.user_id,
...input,
})
.eq("id", patient.id);

if (error) {
return toast({
title: "Something went wrong.",
description: error.message,
variant: "destructive",
});
}

form.reset(input);
router.refresh();
};

return (
<form onSubmit={(e: BaseSyntheticEvent) => void form.handleSubmit(onSubmit)(e)}>
<div>
<label htmlFor="firstname">First Name: &nbsp;</label>
<input type="text" id="firstname" {...form.register("first_name")} />
{form.formState.errors.first_name && <span> (Error: {form.formState.errors.first_name?.message})</span>}
</div>
<div>
<label htmlFor="lastname">Last Name: &nbsp;</label>
<input type="text" id="lastname" {...form.register("last_name")} />
{form.formState.errors.last_name && <span> (Error: {form.formState.errors.last_name?.message})</span>}
</div>
<div>
<label htmlFor="age">Age: &nbsp;</label>
<input type="number" id="age" {...form.register("age")} />
{form.formState.errors.age && <span> (Error: {form.formState.errors.age?.message})</span>}
</div>
<div>
<label htmlFor="state">State: &nbsp;</label>
<input type="text" id="state" {...form.register("state")} />
{form.formState.errors.state && <span> (Error: {form.formState.errors.state?.message})</span>}
</div>
<div>
<label htmlFor="city">City: &nbsp;</label>
<input type="text" id="city" {...form.register("city")} />
{form.formState.errors.city && <span> (Error: {form.formState.errors.city?.message})</span>}
</div>
<div>
<label htmlFor="zip">Zip Code: &nbsp;</label>
<input type="text" id="zip" {...form.register("zip")} />
{form.formState.errors.zip && <span> (Error: {form.formState.errors.zip?.message})</span>}
</div>
<div>
<br />
<button type="submit" disabled={form.formState.isSubmitting}>
Submit
</button>
</div>
</form>
);
}
27 changes: 14 additions & 13 deletions app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { TypographyH2, TypographyP } from "@/components/ui/typography";
import { createServerSupabaseClient } from "@/lib/server-utils";
import { redirect } from "next/navigation";

export default function Home() {
export default async function Home() {
// Create supabase server component client and obtain user session from stored cookie
const supabase = createServerSupabaseClient();
const {
data: { session },
} = await supabase.auth.getSession();

if (!session) {
// Users who are already signed in should be redirected to dashboard
redirect("/login");
}
return (
<>
<TypographyH2>Welcome to team Therable!</TypographyH2>
<TypographyP>If you&apos;re seeing this, your setup has (most likely) gone well!</TypographyP>
<TypographyP>
This starter project is styled with Tailwind CSS and uses shadcn/ui as a component library. Things should feel
familiar, this is what your deliverable (if you&apos;re new here) was built on!
</TypographyP>
<TypographyP>
This page is an unprotected route accessible to anyone who visits the website. Log in to view authenticated
routes!
</TypographyP>
</>
<TypographyH2>Welcome to Therable!</TypographyH2>
);
}
2 changes: 1 addition & 1 deletion components/email-password-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export function UserAuthFormUI({ _onSubmit, isLoading, buttonDisplay, className,
<Input
id="email"
className="border-black"
placeholder="Username"
placeholder="Email"
type="email"
autoCapitalize="none"
autoComplete="email"
Expand Down
Loading

0 comments on commit dfa9ff1

Please sign in to comment.