Skip to content

Commit

Permalink
chore: show proper error messages on profile form submit (#1611)
Browse files Browse the repository at this point in the history
  • Loading branch information
aaryan610 committed Jul 23, 2023
1 parent ff6690a commit fdb7da4
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions apps/app/pages/[workspaceSlug]/me/profile/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ const defaultValues: Partial<IUser> = {
first_name: "",
last_name: "",
email: "",
role: "",
role: "Product / Project Manager",
};

const Profile: NextPage = () => {
const [isEditing, setIsEditing] = useState(false);
const [isRemoving, setIsRemoving] = useState(false);
const [isImageUploadModalOpen, setIsImageUploadModalOpen] = useState(false);

Expand All @@ -55,6 +54,16 @@ const Profile: NextPage = () => {
}, [myProfile, reset]);

const onSubmit = async (formData: IUser) => {
if (formData.first_name === "" || formData.last_name === "") {
setToastAlert({
type: "error",
title: "Error!",
message: "First and last names are required.",
});

return;
}

const payload: Partial<IUser> = {
first_name: formData.first_name,
last_name: formData.last_name,
Expand All @@ -67,9 +76,9 @@ const Profile: NextPage = () => {
.then((res) => {
mutateUser((prevData) => {
if (!prevData) return prevData;

return { ...prevData, ...res };
}, false);
setIsEditing(false);
setToastAlert({
type: "success",
title: "Success!",
Expand Down Expand Up @@ -146,7 +155,7 @@ const Profile: NextPage = () => {
</div>
<SettingsNavbar profilePage />
</div>
<div className="space-y-8 sm:space-y-12">
<form onSubmit={handleSubmit(onSubmit)} className="space-y-8 sm:space-y-12">
<div className="grid grid-cols-12 gap-4 sm:gap-16">
<div className="col-span-12 sm:col-span-6">
<h4 className="text-lg font-semibold text-custom-text-100">Profile Picture</h4>
Expand Down Expand Up @@ -207,9 +216,6 @@ const Profile: NextPage = () => {
error={errors.first_name}
placeholder="Enter your first name"
autoComplete="off"
validations={{
required: "This field is required.",
}}
/>
<Input
name="last_name"
Expand Down Expand Up @@ -255,6 +261,7 @@ const Profile: NextPage = () => {
value={value}
onChange={onChange}
label={value ? value.toString() : "Select your role"}
buttonClassName={errors.role ? "border-red-500 bg-red-500/10" : ""}
width="w-full"
input
position="right"
Expand All @@ -267,14 +274,15 @@ const Profile: NextPage = () => {
</CustomSelect>
)}
/>
{errors.role && <span className="text-xs text-red-500">Please select a role</span>}
</div>
</div>
<div className="sm:text-right">
<SecondaryButton onClick={handleSubmit(onSubmit)} loading={isSubmitting}>
<SecondaryButton type="submit" loading={isSubmitting}>
{isSubmitting ? "Updating..." : "Update profile"}
</SecondaryButton>
</div>
</div>
</form>
</div>
) : (
<div className="grid h-full w-full place-items-center px-4 sm:px-0">
Expand Down

0 comments on commit fdb7da4

Please sign in to comment.