import React, { useMemo, useState } from "react"; import { observer } from "mobx-react-lite"; import { Controller, useForm } from "react-hook-form"; // types import { IUser } from "@plane/types"; // ui import { Button, Input, Spinner, TOAST_TYPE, setToast } from "@plane/ui"; // components import { UserImageUploadModal } from "@/components/accounts"; // hooks import { useMobxStore } from "@/lib/mobx/store-provider"; // services import fileService from "@/services/file.service"; type TProfileSetupFormValues = { first_name: string; last_name: string; avatar?: string | null; }; const defaultValues: Partial = { first_name: "", last_name: "", avatar: "", }; type Props = { user?: IUser; finishOnboarding: () => Promise; }; export const OnBoardingForm: React.FC = observer((props) => { const { user, finishOnboarding } = props; // states const [isRemoving, setIsRemoving] = useState(false); const [isImageUploadModalOpen, setIsImageUploadModalOpen] = useState(false); // store hooks const { user: { updateCurrentUser }, } = useMobxStore(); // form info const { getValues, handleSubmit, control, watch, setValue, formState: { errors, isSubmitting, isValid }, } = useForm({ defaultValues: { ...defaultValues, first_name: user?.first_name, last_name: user?.last_name, avatar: user?.avatar, }, mode: "onChange", }); const onSubmit = async (formData: TProfileSetupFormValues) => { if (!user) return; const userDetailsPayload: Partial = { first_name: formData.first_name, last_name: formData.last_name, avatar: formData.avatar, }; try { await updateCurrentUser(userDetailsPayload).then(() => { setToast({ type: TOAST_TYPE.SUCCESS, title: "Success", message: "Profile setup completed!", }); finishOnboarding(); }); } catch { setToast({ type: TOAST_TYPE.ERROR, title: "Error", message: "Profile setup failed. Please try again!", }); } }; const handleDelete = (url: string | null | undefined) => { if (!url) return; setIsRemoving(true); fileService.deleteUserFile(url).finally(() => { setValue("avatar", ""); setIsRemoving(false); }); }; const isButtonDisabled = useMemo(() => (isValid && !isSubmitting ? false : true), [isSubmitting, isValid]); return (
( setIsImageUploadModalOpen(false)} isRemoving={isRemoving} handleDelete={() => handleDelete(getValues("avatar"))} onSuccess={(url) => { onChange(url); setIsImageUploadModalOpen(false); }} value={value && value.trim() !== "" ? value : null} /> )} />
( )} /> {errors.first_name && {errors.first_name.message}}
( )} /> {errors.last_name && {errors.last_name.message}}
); });