From 118dd3622ad0f2e07073ff24a035f324f309f6d6 Mon Sep 17 00:00:00 2001 From: MTG2000 Date: Wed, 10 Aug 2022 16:16:27 +0300 Subject: [PATCH] feat: change how the 'save changes' look and function in the profile settings --- .../pages/EditProfilePage/EditProfilePage.tsx | 2 +- .../SaveChangesCard/SaveChangesCard.tsx | 63 +++ .../UpdateMyProfileCard.tsx | 360 +++++++++--------- .../CommentsSettingsCard.tsx | 122 +++--- src/utils/helperFunctions.tsx | 3 +- src/utils/hooks/index.ts | 2 + src/utils/hooks/usePrompt.tsx | 56 +++ 7 files changed, 365 insertions(+), 243 deletions(-) create mode 100644 src/features/Profiles/pages/EditProfilePage/SaveChangesCard/SaveChangesCard.tsx create mode 100644 src/utils/hooks/usePrompt.tsx diff --git a/src/features/Profiles/pages/EditProfilePage/EditProfilePage.tsx b/src/features/Profiles/pages/EditProfilePage/EditProfilePage.tsx index 787c952..cbfc3fa 100644 --- a/src/features/Profiles/pages/EditProfilePage/EditProfilePage.tsx +++ b/src/features/Profiles/pages/EditProfilePage/EditProfilePage.tsx @@ -93,7 +93,7 @@ export default function EditProfilePage() { } -
+
} /> } /> diff --git a/src/features/Profiles/pages/EditProfilePage/SaveChangesCard/SaveChangesCard.tsx b/src/features/Profiles/pages/EditProfilePage/SaveChangesCard/SaveChangesCard.tsx new file mode 100644 index 0000000..c1d72f7 --- /dev/null +++ b/src/features/Profiles/pages/EditProfilePage/SaveChangesCard/SaveChangesCard.tsx @@ -0,0 +1,63 @@ +import React from 'react' +import { Link } from 'react-router-dom' +import Button from 'src/Components/Button/Button' +import Avatar from 'src/features/Profiles/Components/Avatar/Avatar' +import { useProfileQuery } from 'src/graphql' +import { trimText } from 'src/utils/helperFunctions' +import { useAppSelector } from 'src/utils/hooks' +import { createRoute } from 'src/utils/routing' + +interface Props { + isLoading?: boolean; + isDirty?: boolean; + onSubmit?: () => void + onCancel?: () => void; +} + +export default function SaveChangesCard(props: Props) { + + const userId = useAppSelector(state => state.user.me?.id!) + const profileQuery = useProfileQuery({ + variables: { + profileId: userId, + }, + }) + + if (!profileQuery.data?.profile) + return <> + + return ( +
+
+ + + +
+

{profileQuery.data.profile ? trimText(profileQuery.data.profile.name, 30) : "Anonymouse"}

+ {profileQuery.data.profile.jobTitle &&

{profileQuery.data.profile.jobTitle}

} +
+ {/* {showTimeAgo &&

+ {dayjs().diff(props.date, 'hour') < 24 ? `${dayjs().diff(props.date, 'hour')}h ago` : undefined} +

} */} +
+

{trimText(profileQuery.data.profile.bio, 120)}

+
+ + +
+
+ ) +} diff --git a/src/features/Profiles/pages/EditProfilePage/UpdateMyProfileCard/UpdateMyProfileCard.tsx b/src/features/Profiles/pages/EditProfilePage/UpdateMyProfileCard/UpdateMyProfileCard.tsx index 1053c4e..027dfef 100644 --- a/src/features/Profiles/pages/EditProfilePage/UpdateMyProfileCard/UpdateMyProfileCard.tsx +++ b/src/features/Profiles/pages/EditProfilePage/UpdateMyProfileCard/UpdateMyProfileCard.tsx @@ -5,8 +5,8 @@ import { NotificationsService } from "src/services/notifications.service"; import * as yup from "yup"; import { yupResolver } from "@hookform/resolvers/yup"; import Avatar from "src/features/Profiles/Components/Avatar/Avatar"; - - +import { usePrompt } from "src/utils/hooks"; +import SaveChangesCard from "../SaveChangesCard/SaveChangesCard"; interface Props { data: Pick = yup.object({ export default function UpdateMyProfileCard({ data, onClose }: Props) { - const { register, formState: { errors }, handleSubmit } = useForm({ + const { register, formState: { errors, isDirty, }, handleSubmit, reset } = useForm({ defaultValues: data, resolver: yupResolver(schema), mode: 'onBlur', @@ -77,6 +77,8 @@ export default function UpdateMyProfileCard({ data, onClose }: Props) { + usePrompt('You may have some unsaved changes. You still want to leave?', isDirty) + const onSubmit: SubmitHandler = data => { mutate({ @@ -94,6 +96,9 @@ export default function UpdateMyProfileCard({ data, onClose }: Props) { twitter: data.twitter, website: data.website, } + }, + onCompleted: () => { + reset(data); } }).catch(() => { NotificationsService.error('A network error happened'); @@ -102,186 +107,179 @@ export default function UpdateMyProfileCard({ data, onClose }: Props) { }; return ( -
-
-
- +
+
+
+
+ +
+
+
+
+

+ Name +

+
+ +
+ {errors.name &&

+ {errors.name.message} +

} +

+ Avatar +

+
+ +
+ {errors.avatar &&

+ {errors.avatar.message} +

} +

+ Bio +

+
+