Skip to content

Commit

Permalink
feat: Add useAuth hook to EditProfile screen
Browse files Browse the repository at this point in the history
This commit adds the useAuth hook to the EditProfile screen in order to retrieve the userId. The userId is used to retrieve the user's full name and display it in the EditProfile screen. This improves the user experience by pre-filling the full name input field with the user's current full name.

Code changes:
- Added useAuth hook to retrieve userId
- Pre-filled full name input field with user's current full name
  • Loading branch information
noriega2112 committed Aug 30, 2024
1 parent 65974df commit 411bfd6
Showing 1 changed file with 12 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Ionicons } from '@expo/vector-icons'
import colors from '@utils/colors'
import { BButton } from '@components/Button'
import { isAxiosError } from 'axios'
import { useAuth } from '@stores/auth'

const Separator = () => {
return (
Expand All @@ -26,6 +27,7 @@ export const EditProfile = () => {
navio.goBack()
}
const { data: user } = useUser()
const { userId } = useAuth()
const [fullName, setFullName] = useState(user?.fullName ?? '')
const [errors, setErrors] = useState<string[] | undefined>()

Expand All @@ -39,11 +41,13 @@ export const EditProfile = () => {
const qClient = useQueryClient()

const { mutate: save, isPending: isSaving } = useMutation({
mutationFn: userApi.csc.patchUser,
mutationFn: userApi.update,
onMutate: async () => {
const userSnapshot = qClient.getQueryData<UserShape>(userQueries.all())?.fullName
await qClient.cancelQueries({ queryKey: userQueries.all() })
await qClient.setQueryData(userQueries.all(), (input?: UserShape) => {
const userSnapshot = qClient.getQueryData<UserShape>(
userQueries.retrieve(userId).queryKey,
)?.fullName
await qClient.cancelQueries({ queryKey: userQueries.retrieve(userId).queryKey })
qClient.setQueryData(userQueries.retrieve(userId).queryKey, (input?: UserShape) => {
return input ? { ...input } : undefined
})
return { userSnapshot }
Expand All @@ -53,8 +57,10 @@ export const EditProfile = () => {
},
onError: (e, _, context) => {
//rollback update
qClient.setQueryData(userQueries.all(), (input?: UserShape) => {
return input ? { ...input, fullName: context?.userSnapshot ?? '' } : undefined
qClient.setQueryData(userQueries.retrieve(userId).queryKey, (input?: UserShape) => {
return input
? { ...input, fullName: context?.userSnapshot ?? user?.fullName ?? '' }
: undefined
})

if (isAxiosError(e)) {
Expand Down

0 comments on commit 411bfd6

Please sign in to comment.