diff --git a/apps/hackathon/src/app/_components/userInformation/ApplicationForm.tsx b/apps/hackathon/src/app/_components/userInformation/ApplicationForm.tsx new file mode 100644 index 0000000..fc3a366 --- /dev/null +++ b/apps/hackathon/src/app/_components/userInformation/ApplicationForm.tsx @@ -0,0 +1,164 @@ +'use client' +import type { FormEvent } from 'react' +import React, { useState } from 'react' +import type { UserInformation } from '@prisma/client' +import { api } from '~/trpc/react' + +interface ApplicationFormProps { + session: { + user: { + id: string + } + } +} + +export function ApplicationForm(props: ApplicationFormProps) { + const [subscribe, setSubscribe] = useState(false) + const [formData, setFormData] = useState({ + id: '1', + email: '', + first_name: '', + last_name: '', + levels_of_study: '', + major: '', + date_of_birth: new Date(0), + gender: '', + phone_number: '', + school: '', + userId: props.session?.user.id || '', + }) + + const submitApplicationForm = api.userInformation.useMutation() + const subscribeToNewsletter = api.subscribeToNewsletter.useMutation() + const handleSubmit = async (e: FormEvent) => { + e.preventDefault() + try { + if (subscribe && formData.email) { + subscribeToNewsletter.mutateAsync({ email: formData.email }) + } + await submitApplicationForm.mutateAsync(formData) + } + catch (error) { + console.error('Error submitting form:', error) + } + } + + const handleSubscribe = (e: React.ChangeEvent) => { + if (e.target && formData.email) { + setSubscribe(!subscribe) + } + else { + e.target.checked = false + } + } + + const handleOnChange = (e: React.ChangeEvent) => { + if (e.target) { + setFormData({ + ...formData, + [e.target.name]: e.target.value, + }) + if (e.target.name === 'email' && subscribe) { + handleSubscribe(e) + } + } + } + + return ( +
+ + + + + + + + + + + subscribe to newsletter with email + + + + + + + + + + + + + + + + + + + + +
+ ) +}; + +export default ApplicationForm diff --git a/apps/hackathon/src/server/api/routers/applicationForm/procedures/setUserInformation.ts b/apps/hackathon/src/server/api/routers/applicationForm/procedures/setUserInformation.ts new file mode 100644 index 0000000..b7c340a --- /dev/null +++ b/apps/hackathon/src/server/api/routers/applicationForm/procedures/setUserInformation.ts @@ -0,0 +1,40 @@ +import { z } from 'zod' + +import { + protectedProcedure, +} from '~/server/api/trpc' + +export const userInformationRouter = protectedProcedure + .input( + z.object({ + id: z.string(), + email: z.string().email().nullable(), + first_name: z.string().nullable(), + last_name: z.string().nullable(), + levels_of_study: z.string().nullable(), + major: z.string().nullable(), + date_of_birth: z.date().nullable(), + gender: z.string().nullable(), + phone_number: z.string().nullable(), + school: z.string().nullable(), + userId: z.string(), + }), + ) + .mutation(async ({ ctx, input }) => { + const data = await ctx.db.userInformation.create({ + data: { + id: input.id, + email: input.email, + first_name: input.first_name, + last_name: input.last_name, + levels_of_study: input.levels_of_study, + major: input.major, + date_of_birth: input.date_of_birth, + gender: input.gender, + phone_number: input.phone_number, + school: input.school, + userId: input.userId, + }, + }) + return data + })