Skip to content
This repository was archived by the owner on Oct 7, 2025. It is now read-only.

Commit 116de16

Browse files
HasithDeAlwisMFarabi619
authored andcommitted
fix(portal): dietary restrictions, allergies, and graduation date not updating
1 parent 32221bd commit 116de16

File tree

8 files changed

+71
-28
lines changed

8 files changed

+71
-28
lines changed

libs/db/collections/models/Users.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -365,16 +365,16 @@ export const Users: CollectionConfig = {
365365
{ label: '🕌 Halal', value: 'halal' },
366366
{ label: '✡ Kosher', value: 'kosher' },
367367
{ label: '🐟 Pescatarian', value: 'pescatarian' },
368-
{ label: '🥛 Dairy-Free', value: 'dairy-free' },
369-
{ label: '🌾 Gluten-Free', value: 'gluten-free' },
370-
{ label: '🍤 Shellfish-Free', value: 'shellfish-free' },
371-
{ label: '🥜 Nut-Free', value: 'nut-free' },
368+
{ label: '🥛 Dairy-Free', value: 'dairy_free' },
369+
{ label: '🌾 Gluten-Free', value: 'gluten_free' },
370+
{ label: '🍤 Shellfish-Free', value: 'shellfish_free' },
371+
{ label: '🥜 Nut-Free', value: 'nut_free' },
372372
{ label: '🥩 Keto', value: 'keto' },
373-
{ label: '🧀 Low-Lactose', value: 'low-lactose' },
374-
{ label: '🍚 Low-Carb', value: 'low-carb' },
373+
{ label: '🧀 Low-Lactose', value: 'low_lactose' },
374+
{ label: '🍚 Low-Carb', value: 'low_carb' },
375375
{ label: '🍖 Paleo', value: 'paleo' },
376-
{ label: '⚡ High-Protein', value: 'high-protein' },
377-
{ label: '🌿 Raw Vegan', value: 'raw-vegan' },
376+
{ label: '⚡ High-Protein', value: 'high_protein' },
377+
{ label: '🌿 Raw Vegan', value: 'raw_vegan' },
378378
{ label: '🥒 Whole30', value: 'whole30' },
379379
{ label: '🍵 Fasting', value: 'fasting' },
380380
{ label: '❓ Other', value: 'other' },

libs/portal/features/profile/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ export const RESTRICTIONS = {
514514
{ label: 'Paleo', value: 'paleo' },
515515
{ label: 'Low-Carb', value: 'low_carb' },
516516
{ label: 'Low-Sodium', value: 'low_sodium' },
517+
{ label: 'Fasting', value: 'fasting' },
517518
{ label: 'Other', value: 'other' },
518519
],
519520

libs/portal/features/profile/hooks/use-profile-schema.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,38 @@ import { YearStandings } from '@cuhacking/portal/types/year-standings'
33
import { zodResolver } from '@hookform/resolvers/zod'
44
import { useForm } from 'react-hook-form'
55
import * as z from 'zod'
6+
import { RESTRICTIONS } from '../constants'
67

8+
function refinedUserData(user: Partial<UserDetails>) {
9+
let graduationDate
10+
if (user.expectedGraduationDate) {
11+
graduationDate = new Date(user.expectedGraduationDate)
12+
}
13+
let refinedAllergies
14+
if (user.allergies) {
15+
refinedAllergies = user.allergies
16+
.map(value => RESTRICTIONS.ALLERGIES.find(
17+
allergy => allergy.value === value,
18+
))
19+
.filter(Boolean)
20+
}
21+
22+
let refinedRestrictions
23+
if (user.dietaryRestrictions) {
24+
refinedRestrictions = user.dietaryRestrictions
25+
.map(value => RESTRICTIONS.DIETARY.find(
26+
restriction => restriction.value === value,
27+
))
28+
.filter(Boolean)
29+
}
30+
return { ...user, dietaryRestrictions: refinedRestrictions, allergies: refinedAllergies, expectedGraduationDate: graduationDate }
31+
}
732
export function useProfileSchema(
833
user: Partial<UserDetails>,
934
isStudent: boolean,
1035
) {
36+
const refinedUser = refinedUserData(user)
37+
1138
const profileSchema = z.object({
1239
firstName: z
1340
.string()
@@ -132,7 +159,7 @@ export function useProfileSchema(
132159

133160
const profile = useForm<UserDetails>({
134161
resolver: zodResolver(profileSchema),
135-
defaultValues: user,
162+
defaultValues: refinedUser,
136163
mode: 'onBlur',
137164
})
138165
return {

libs/portal/features/profile/ui/questions.tsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import {
3737

3838
interface ProfileFormProps {
3939
user: Partial<UserDetails>
40-
status: boolean
40+
isComplete: boolean
4141
onSubmit: (values: z.infer<any>, isComplete: boolean, cookie: string | null, apiUrl: string) => Promise<Response>
4242
}
4343

@@ -48,8 +48,8 @@ const initialSocialMediaHandles = {
4848
behance: '',
4949
}
5050

51-
export function Questions({ user, status, onSubmit }: ProfileFormProps) {
52-
const [isStudent, setIsStudent] = useState<boolean>(false)
51+
export function Questions({ user, isComplete, onSubmit }: ProfileFormProps) {
52+
const [isStudent, setIsStudent] = useState<boolean>(!!user.expectedGraduationDate)
5353
// will setup the social media handler once BE is up
5454
const [socialMediaHandles, _setSocialMediaHandles] = useState<
5555
typeof initialSocialMediaHandles
@@ -74,15 +74,23 @@ export function Questions({ user, status, onSubmit }: ProfileFormProps) {
7474

7575
async function handleSubmit(values: z.infer<any>) {
7676
setIsLoading(true)
77-
const res = await onSubmit(values, false, cookie, API_URL)
77+
const refinedAllergies = values.allergies.map((allergy: { value: string, option: string }) =>
78+
allergy.value,
79+
)
80+
const refinedDietaryRestrictions = values.dietaryRestrictions.map((restriction: { value: string, option: string }) =>
81+
restriction.value,
82+
)
83+
84+
const refinedValues = { ...values, dietaryRestrictions: refinedDietaryRestrictions, allergies: refinedAllergies }
85+
const res = await onSubmit(refinedValues, false, cookie, API_URL)
7886
setIsLoading(false)
79-
if (res.status === 200 && status) {
87+
if (res.status === 200 && !isComplete) {
8088
navigate('/dashboard')
8189
}
8290
}
8391

84-
const buttonMessage = status ? 'Update Profile' : 'Create Profile'
85-
const disabled = isValid || !isDirty || isLoading
92+
const buttonMessage = isComplete ? 'Update Profile' : 'Create Profile'
93+
const disabled = !isValid || isDirty || isLoading
8694

8795
return (
8896
<Form {...profile}>

libs/portal/pages/profile/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export function ProfilePage({ user }: { user: UserDetails }) {
3838
lastName={user.lastName || ''}
3939
avatarUrl={user.avatar?.url || ''}
4040
/>
41-
<Questions onSubmit={handleSubmit} status={status} user={user} />
41+
<Questions onSubmit={handleSubmit} isComplete={status} user={user} />
4242
</div>
4343
</Layout>
4444
)

libs/portal/shared/features/form/ui/month-year-field.tsx

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,8 @@ export function MonthYearField({
4747
isDisabled,
4848
info,
4949
}: DateFieldProps) {
50-
// If there is an initial date value, extract its month and year.
51-
const initialDate = form.getValues(name)
52-
const initialMonth = initialDate ? (initialDate.getMonth() + 1).toString() : ''
53-
const initialYear = initialDate ? initialDate.getFullYear().toString() : ''
54-
55-
const [selectedMonth, setSelectedMonth] = useState<string>(initialMonth)
56-
const [selectedYear, setSelectedYear] = useState<string>(initialYear)
50+
const [selectedMonth, setSelectedMonth] = useState<string>('')
51+
const [selectedYear, setSelectedYear] = useState<string>('')
5752
const [monthOpen, setMonthOpen] = useState<boolean>(false)
5853
const [yearOpen, setYearOpen] = useState<boolean>(false)
5954
const [validInput, setValidInput] = useState<boolean>(false)
@@ -81,7 +76,6 @@ export function MonthYearField({
8176
return { value: year, label: year }
8277
})
8378

84-
// Update the form value when both month and year have been selected.
8579
const updateDate = (month: string, year: string) => {
8680
if (month && year) {
8781
const parsedMonth = Number.parseInt(month)
@@ -97,6 +91,19 @@ export function MonthYearField({
9791
}
9892
}, [selectedMonth, selectedYear])
9993

94+
useEffect(() => {
95+
if (!form.getValues(name)) {
96+
return
97+
}
98+
const initialDate = new Date(form.getValues(name))
99+
const initialMonth = initialDate ? (initialDate.getMonth() + 1).toString() : ''
100+
const initialYear = initialDate ? initialDate.getFullYear().toString() : ''
101+
setSelectedMonth(initialMonth)
102+
setSelectedYear(initialYear)
103+
updateDate(initialMonth, initialYear)
104+
// eslint-disable-next-line react-hooks/exhaustive-deps
105+
}, []) // want this to run only on start
106+
100107
return (
101108
<GlassmorphicCard
102109
className={cn(

libs/portal/types/user.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ export interface UserDetails {
2727
emergencyContactRelationship: string
2828
middleName?: string
2929
gender: string
30-
dietaryRestrictions?: { label: string, value: string }[]
31-
allergies?: { label: string, value: string }[]
30+
dietaryRestrictions?: { label: string, value: string }[] | string[]
31+
allergies?: { label: string, value: string }[] | string[]
3232
discordHandle?: string
3333
githubHandle?: string
3434
instagramHandle?: string

libs/portal/ui/dashboard/user-status/user-status.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export function UserStatus({ name, status = 'pending', className }: UserStatusPr
2727
<TerminalText>WE HOPE TO CUHACKING!</TerminalText>
2828
<Button
2929
className="w-full text-black"
30-
onClick={() => {}}
30+
type="button"
3131
variant="secondary"
3232
>
3333
RSVP

0 commit comments

Comments
 (0)