-
Notifications
You must be signed in to change notification settings - Fork 0
implement basic volunteer profile settings #142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
472d3eb
implement basic volunteer profile settings
jansm04 dc3032a
implement basic volunteer profile settings
jansm04 b4c9ef1
Merge branch '131-settings-dialog-profile-section' of https://github.…
jansm04 abb3a28
implement profile form
jansm04 ad8d160
fix admin/instructor side
jansm04 4e85423
Merge branch 'main' into 131-settings-dialog-profile-section
theosiemensrhodes 8cbd215
fix: update logic and use our internal form components
theosiemensrhodes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
43 changes: 43 additions & 0 deletions
43
src/components/settings/pages/profile/general/general-form-provider.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| "use client"; | ||
|
|
||
| import { zodResolver } from "@hookform/resolvers/zod"; | ||
| import { createContext, useContext } from "react"; | ||
| import { useForm, type UseFormReturn } from "react-hook-form"; | ||
|
|
||
| import { GeneralProfileSchema, type GeneralProfileSchemaType } from "./schema"; | ||
|
|
||
| const GeneralProfileFormContext = createContext<{ | ||
| form: UseFormReturn<GeneralProfileSchemaType>; | ||
| } | null>(null); | ||
|
|
||
| export function GeneralProfileFormProvider({ | ||
| initial, | ||
| onSubmit, | ||
| children, | ||
| }: { | ||
| initial: GeneralProfileSchemaType; | ||
| onSubmit: (data: GeneralProfileSchemaType) => void; | ||
| children: React.ReactNode; | ||
| }) { | ||
| const form = useForm<GeneralProfileSchemaType>({ | ||
| resolver: zodResolver(GeneralProfileSchema), | ||
| defaultValues: initial, | ||
| mode: "onSubmit", | ||
| reValidateMode: "onChange", | ||
| }); | ||
|
|
||
| return ( | ||
| <GeneralProfileFormContext.Provider value={{ form }}> | ||
| <form onSubmit={form.handleSubmit(onSubmit)}>{children}</form> | ||
| </GeneralProfileFormContext.Provider> | ||
| ); | ||
| } | ||
|
|
||
| export function useGeneralProfileForm() { | ||
| const ctx = useContext(GeneralProfileFormContext); | ||
| if (!ctx) | ||
| throw new Error( | ||
| "useGeneralProfileForm must be used inside GeneralProfileFormProvider", | ||
| ); | ||
| return ctx; | ||
| } |
69 changes: 69 additions & 0 deletions
69
src/components/settings/pages/profile/general/general-profile-section.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| "use client"; | ||
|
|
||
| import { useGeneralProfileForm } from "./general-form-provider"; | ||
| import { ProfileImageInput } from "./profile-image-input"; | ||
|
|
||
| import { FieldLabel } from "@/components/ui/field"; | ||
| import { FormInputField } from "@/components/form/FormInput"; | ||
| import { Card, CardHeader, CardTitle, CardContent } from "@/components/ui/card"; | ||
| import { Button } from "@/components/primitives/button"; | ||
|
|
||
| export function GeneralProfileSection({ | ||
| fallbackName, | ||
| isPending, | ||
| }: { | ||
| fallbackName: string; | ||
| isPending: boolean; | ||
| }) { | ||
| const { | ||
| form: { control, watch, setValue }, | ||
| } = useGeneralProfileForm(); | ||
|
|
||
| return ( | ||
| <Card> | ||
| <CardHeader> | ||
| <CardTitle>Profile Information</CardTitle> | ||
| </CardHeader> | ||
|
|
||
| <CardContent className="grid gap-4"> | ||
| <div className="flex flex-col gap-2"> | ||
| <FieldLabel>Profile Picture</FieldLabel> | ||
| <ProfileImageInput | ||
| watch={watch} | ||
| setValue={setValue} | ||
| fallbackName={fallbackName} | ||
| /> | ||
| </div> | ||
|
|
||
| <div className="flex gap-8"> | ||
| <FormInputField | ||
| name="firstName" | ||
| control={control} | ||
| label="First Name" | ||
| required | ||
| /> | ||
| <FormInputField | ||
| name="lastName" | ||
| control={control} | ||
| label="Last Name" | ||
| required | ||
| /> | ||
| </div> | ||
|
|
||
| <FormInputField | ||
| name="email" | ||
| control={control} | ||
| label="Email" | ||
| type="email" | ||
| disabled | ||
| /> | ||
|
|
||
| <div className="flex justify-end"> | ||
| <Button type="submit" pending={isPending}> | ||
| Save Changes | ||
| </Button> | ||
| </div> | ||
| </CardContent> | ||
| </Card> | ||
| ); | ||
| } |
50 changes: 50 additions & 0 deletions
50
src/components/settings/pages/profile/general/hooks/use-general-profile-submit.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { useMutation } from "@tanstack/react-query"; | ||
| import { toast } from "sonner"; | ||
| import { authClient } from "@/lib/auth/client"; | ||
| import { getBetterAuthErrorMessage } from "@/lib/auth/extensions/get-better-auth-error"; | ||
| import { useImageUpload } from "@/hooks/use-image-upload"; | ||
| import type { GeneralProfileSchemaType } from "../schema"; | ||
|
|
||
| export function useGeneralProfileSubmit() { | ||
| const { refetch: refetchSession } = authClient.useSession(); | ||
| const { uploadImage } = useImageUpload(); | ||
|
|
||
| const mutation = useMutation({ | ||
| mutationFn: async (data: GeneralProfileSchemaType) => { | ||
| let imageKey: string | null = null; | ||
|
|
||
| if (data.image?.startsWith("blob:")) { | ||
| // New image selected - upload it | ||
| imageKey = await uploadImage(data.image); | ||
| } else if (data.image) { | ||
| // Existing image URL - extract key from URL or use as-is | ||
| const parts = data.image.split("/"); | ||
| imageKey = parts[parts.length - 1] ?? data.image; | ||
| } | ||
| // If data.image is null/undefined, imageKey stays null (clear image) | ||
|
|
||
| const { error } = await authClient.updateUser({ | ||
| name: data.firstName, | ||
| lastName: data.lastName, | ||
| image: imageKey, | ||
| }); | ||
|
|
||
| if (error) { | ||
| throw new Error(getBetterAuthErrorMessage(error.code)); | ||
| } | ||
|
|
||
| await refetchSession(); | ||
| }, | ||
| onSuccess: () => { | ||
| toast.success("Your profile has been successfully updated!"); | ||
| }, | ||
| onError: (error: Error) => { | ||
| toast.error(error.message); | ||
| }, | ||
| }); | ||
|
|
||
| return { | ||
| onSubmit: mutation.mutateAsync, | ||
| isPending: mutation.isPending, | ||
| }; | ||
| } |
121 changes: 121 additions & 0 deletions
121
src/components/settings/pages/profile/general/profile-image-input.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| "use client"; | ||
|
|
||
| import { useEffect, useRef } from "react"; | ||
| import type { UseFormSetValue, UseFormWatch } from "react-hook-form"; | ||
|
|
||
| import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"; | ||
| import { Button } from "@/components/ui/button"; | ||
| import { | ||
| Dropzone, | ||
| DropzoneArea, | ||
| DropzoneDescription, | ||
| DropzoneHeader, | ||
| DropzoneHint, | ||
| DropzoneMedia, | ||
| } from "@/components/ui/dropzone"; | ||
| import { cropImageToSquare } from "@/lib/crop-image"; | ||
| import { Trash2, Upload } from "lucide-react"; | ||
| import { toast } from "sonner"; | ||
|
|
||
| type Props = { | ||
| watch: UseFormWatch<any>; | ||
| setValue: UseFormSetValue<any>; | ||
| fallbackName?: string; | ||
| }; | ||
|
|
||
| export function ProfileImageInput({ | ||
| watch, | ||
| setValue, | ||
| fallbackName = "U", | ||
| }: Props) { | ||
| const imageValue = watch("image") as string | undefined; | ||
| const previousImageRef = useRef<string | null>(imageValue ?? null); | ||
|
|
||
| // Clean up old blob URLs | ||
| useEffect(() => { | ||
| const prev = previousImageRef.current; | ||
| const next = imageValue ?? null; | ||
|
|
||
| if (prev && prev !== next && prev.startsWith("blob:")) { | ||
| URL.revokeObjectURL(prev); | ||
| } | ||
|
|
||
| previousImageRef.current = next; | ||
| }, [imageValue]); | ||
|
|
||
| // Clean up on unmount | ||
| useEffect(() => { | ||
| return () => { | ||
| const last = previousImageRef.current; | ||
| if (last?.startsWith("blob:")) { | ||
| URL.revokeObjectURL(last); | ||
| } | ||
| }; | ||
| }, []); | ||
|
|
||
| return ( | ||
| <Dropzone | ||
| accept="image/*" | ||
| multiple={false} | ||
| maxSize={4 * 1024 * 1024} | ||
| onFilesChange={async (files) => { | ||
| const file = files[0]?.file; | ||
|
|
||
| if (file instanceof File) { | ||
| const { previewUrl } = await cropImageToSquare(file, { | ||
| size: 512, | ||
| mimeType: "image/webp", | ||
| quality: 0.8, | ||
| }); | ||
|
|
||
| setValue("image", previewUrl, { | ||
| shouldDirty: true, | ||
| shouldTouch: true, | ||
| }); | ||
| } else { | ||
| setValue("image", undefined); | ||
| } | ||
| }} | ||
| onError={(errors) => toast.error(errors[0])} | ||
| > | ||
| <div className="flex gap-4 items-start"> | ||
| <div className="flex flex-col items-center gap-4"> | ||
| <Avatar className="aspect-square size-[140px] shrink-0 rounded-md pointer-events-none"> | ||
| <AvatarImage | ||
| src={imageValue ?? undefined} | ||
| className="rounded-md object-cover" | ||
| /> | ||
| <AvatarFallback className="rounded-md text-4xl"> | ||
| {fallbackName.slice(0, 2).toUpperCase()} | ||
| </AvatarFallback> | ||
| </Avatar> | ||
|
|
||
| <Button | ||
| type="button" | ||
| size="sm" | ||
| variant="outline" | ||
| onClick={() => setValue("image", null, { shouldDirty: true })} | ||
| className="w-full max-w-[140px] text-destructive hover:text-destructive hover:bg-destructive/10" | ||
| > | ||
| <Trash2 className="mr-2 h-4 w-4" /> | ||
| Clear | ||
| </Button> | ||
| </div> | ||
|
|
||
| <DropzoneArea> | ||
| <DropzoneHeader> | ||
| <DropzoneMedia variant="icon"> | ||
| <Upload /> | ||
| </DropzoneMedia> | ||
|
|
||
| <DropzoneDescription> | ||
| Drag and drop or click to upload | ||
| </DropzoneDescription> | ||
|
|
||
| <DropzoneHint>JPG, PNG, WEBP (max 4MB)</DropzoneHint> | ||
| </DropzoneHeader> | ||
| </DropzoneArea> | ||
| </div> | ||
| </Dropzone> | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { z } from "zod"; | ||
|
|
||
| export const GeneralProfileSchema = z.object({ | ||
| firstName: z.string().nonempty("Please enter your first name."), | ||
| lastName: z.string().nonempty("Please enter your last name."), | ||
| email: z.string().email("Please enter a valid email address."), | ||
| image: z.string().nullable(), | ||
| }); | ||
|
|
||
| export type GeneralProfileSchemaType = z.infer<typeof GeneralProfileSchema>; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Setting to
undefinedwhen file is invalid, but schema expectsnull. This should benullto match the schema definition.Prompt To Fix With AI