|
| 1 | +import { zodResolver } from "@hookform/resolvers/zod"; |
| 2 | +import { useEffect } from "react"; |
| 3 | +import { useForm } from "react-hook-form"; |
| 4 | +import { z } from "zod"; |
| 5 | + |
| 6 | +import dayjs from "@calcom/dayjs"; |
| 7 | +import { useTimePreferences } from "@calcom/features/bookings/lib"; |
| 8 | +import { FULL_NAME_LENGTH_MAX_LIMIT } from "@calcom/lib/constants"; |
| 9 | +import { useLocale } from "@calcom/lib/hooks/useLocale"; |
| 10 | +import { telemetryEventTypes, useTelemetry } from "@calcom/lib/telemetry"; |
| 11 | +import { trpc } from "@calcom/trpc/react"; |
| 12 | +import { Button, TimezoneSelect, Icon, Input } from "@calcom/ui"; |
| 13 | + |
| 14 | +import { UsernameAvailabilityField } from "@components/ui/UsernameAvailability"; |
| 15 | + |
| 16 | +interface IUserSettingsProps { |
| 17 | + nextStep: () => void; |
| 18 | + hideUsername?: boolean; |
| 19 | +} |
| 20 | + |
| 21 | +const UserSettings = (props: IUserSettingsProps) => { |
| 22 | + const { nextStep } = props; |
| 23 | + const [user] = trpc.viewer.me.useSuspenseQuery(); |
| 24 | + const { t } = useLocale(); |
| 25 | + const { setTimezone: setSelectedTimeZone, timezone: selectedTimeZone } = useTimePreferences(); |
| 26 | + const telemetry = useTelemetry(); |
| 27 | + const userSettingsSchema = z.object({ |
| 28 | + name: z |
| 29 | + .string() |
| 30 | + .min(1) |
| 31 | + .max(FULL_NAME_LENGTH_MAX_LIMIT, { |
| 32 | + message: t("max_limit_allowed_hint", { limit: FULL_NAME_LENGTH_MAX_LIMIT }), |
| 33 | + }), |
| 34 | + }); |
| 35 | + const { |
| 36 | + register, |
| 37 | + handleSubmit, |
| 38 | + formState: { errors }, |
| 39 | + } = useForm<z.infer<typeof userSettingsSchema>>({ |
| 40 | + defaultValues: { |
| 41 | + name: user?.name || "", |
| 42 | + }, |
| 43 | + reValidateMode: "onChange", |
| 44 | + resolver: zodResolver(userSettingsSchema), |
| 45 | + }); |
| 46 | + |
| 47 | + useEffect(() => { |
| 48 | + telemetry.event(telemetryEventTypes.onboardingStarted); |
| 49 | + }, [telemetry]); |
| 50 | + |
| 51 | + const utils = trpc.useUtils(); |
| 52 | + const onSuccess = async () => { |
| 53 | + await utils.viewer.me.invalidate(); |
| 54 | + nextStep(); |
| 55 | + }; |
| 56 | + const mutation = trpc.viewer.updateProfile.useMutation({ |
| 57 | + onSuccess: onSuccess, |
| 58 | + }); |
| 59 | + |
| 60 | + const onSubmit = handleSubmit((data) => { |
| 61 | + mutation.mutate({ |
| 62 | + name: data.name, |
| 63 | + timeZone: selectedTimeZone, |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + return ( |
| 68 | + <form onSubmit={onSubmit}> |
| 69 | + <div className="space-y-6"> |
| 70 | + {/* Username textfield: when not coming from signup */} |
| 71 | + {!props.hideUsername && <UsernameAvailabilityField />} |
| 72 | + |
| 73 | + {/* Full name textfield */} |
| 74 | + <div className="w-full"> |
| 75 | + <label htmlFor="name" className="text-default mb-2 block text-sm font-medium"> |
| 76 | + {t("full_name")} |
| 77 | + </label> |
| 78 | + <Input |
| 79 | + {...register("name", { |
| 80 | + required: true, |
| 81 | + })} |
| 82 | + id="name" |
| 83 | + name="name" |
| 84 | + type="text" |
| 85 | + autoComplete="off" |
| 86 | + autoCorrect="off" |
| 87 | + /> |
| 88 | + {errors.name && ( |
| 89 | + <p data-testid="required" className="py-2 text-xs text-red-500"> |
| 90 | + {errors.name.message} |
| 91 | + </p> |
| 92 | + )} |
| 93 | + </div> |
| 94 | + {/* Timezone select field */} |
| 95 | + <div className="w-full"> |
| 96 | + <label htmlFor="timeZone" className="text-default block text-sm font-medium"> |
| 97 | + {t("timezone")} |
| 98 | + </label> |
| 99 | + |
| 100 | + <TimezoneSelect |
| 101 | + id="timeZone" |
| 102 | + value={selectedTimeZone} |
| 103 | + onChange={({ value }) => setSelectedTimeZone(value)} |
| 104 | + className="mt-2 w-full rounded-md text-sm" |
| 105 | + /> |
| 106 | + |
| 107 | + <p className="text-subtle mt-3 flex flex-row font-sans text-xs leading-tight"> |
| 108 | + {t("current_time")} {dayjs().tz(selectedTimeZone).format("LT").toString().toLowerCase()} |
| 109 | + </p> |
| 110 | + </div> |
| 111 | + </div> |
| 112 | + <Button |
| 113 | + type="submit" |
| 114 | + className="mt-8 flex w-full flex-row justify-center" |
| 115 | + loading={mutation.isPending} |
| 116 | + disabled={mutation.isPending}> |
| 117 | + {t("next_step_text")} |
| 118 | + <Icon name="arrow-right" className="ml-2 h-4 w-4 self-center" aria-hidden="true" /> |
| 119 | + </Button> |
| 120 | + </form> |
| 121 | + ); |
| 122 | +}; |
| 123 | + |
| 124 | +export { UserSettings }; |
0 commit comments