Skip to content

Commit

Permalink
fix: refactoring after review
Browse files Browse the repository at this point in the history
  • Loading branch information
yasell committed Jan 29, 2025
1 parent 3fc3320 commit 3b62ed9
Show file tree
Hide file tree
Showing 14 changed files with 226 additions and 350 deletions.
56 changes: 22 additions & 34 deletions apps/gitness/src/framework/context/AppContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ interface AppContextType {
setCurrentUser: (value: TypesUser) => void
fetchUser: () => Promise<void>
updateUserProfile: (data: { display_name?: string; email?: string }) => Promise<void>
updateUserPassword: (newPassword: string) => Promise<void>
isUpdateUserLoading: boolean
updateUserLoadingError: {
isUpdatingUser: boolean
isLoadingUser: boolean
updateUserError: {
type: ProfileSettingsErrorType
message: string
} | null
Expand All @@ -39,64 +39,52 @@ const AppContext = createContext<AppContextType>({
setCurrentUser: noop,
fetchUser: async () => {},
updateUserProfile: async () => {},
updateUserPassword: async () => {},
isUpdateUserLoading: false,
updateUserLoadingError: null
isUpdatingUser: false,
isLoadingUser: false,
updateUserError: null
})

export const AppProvider: FC<{ children: ReactNode }> = ({ children }) => {
const [spaces, setSpaces] = useState<TypesSpace[]>([])
const [currentUser, setCurrentUser] = useLocalStorage<TypesUser>('currentUser', {})
const [isUpdateUserLoading, setIsUpdateUserLoading] = useState(false)
const [updateUserLoadingError, setUpdateUserLoadingError] = useState<{
const [isLoadingUser, setIsLoadingUser] = useState(false)
const [isUpdatingUser, setIsUpdatingUser] = useState(false)
const [updateUserError, setUpdateUserError] = useState<{
type: ProfileSettingsErrorType
message: string
} | null>(null)

const fetchUser = async (): Promise<void> => {
setIsLoadingUser(true)
setUpdateUserError(null)
try {
const userResponse = await getUser({})
setCurrentUser(userResponse.body)
} catch (error) {
const typedError = error as GetUserErrorResponse
setUpdateUserLoadingError({
setUpdateUserError({
type: ProfileSettingsErrorType.PROFILE,
message: typedError.message || 'An unknown fetch user error occurred.'
})
} finally {
setIsLoadingUser(false)
}
}

const updateUserProfile = async (data: { display_name?: string; email?: string }): Promise<void> => {
setIsUpdateUserLoading(true)
setUpdateUserLoadingError(null)
setIsUpdatingUser(true)
setUpdateUserError(null)
try {
const response = await updateUser({ body: data })
setCurrentUser(response.body)
setIsUpdateUserLoading(false)
} catch (error) {
const typedError = error as UpdateUserErrorResponse
setUpdateUserLoadingError({
setUpdateUserError({
type: ProfileSettingsErrorType.PROFILE,
message: typedError.message || 'An unknown update user error occurred.'
})
setIsUpdateUserLoading(false)
}
}

const updateUserPassword = async (newPassword: string): Promise<void> => {
setIsUpdateUserLoading(true)
setUpdateUserLoadingError(null)
try {
const response = await updateUser({ body: { password: newPassword } })
setCurrentUser(response.body)
setIsUpdateUserLoading(false)
} catch (error) {
const typedError = error as UpdateUserErrorResponse
setUpdateUserLoadingError({
type: ProfileSettingsErrorType.PASSWORD,
message: typedError.message || 'An unknown update password error occurred.'
})
setIsUpdateUserLoading(false)
} finally {
setIsUpdatingUser(false)
}
}

Expand Down Expand Up @@ -129,9 +117,9 @@ export const AppProvider: FC<{ children: ReactNode }> = ({ children }) => {
setCurrentUser,
fetchUser,
updateUserProfile,
updateUserPassword,
isUpdateUserLoading,
updateUserLoadingError
isLoadingUser,
isUpdatingUser,
updateUserError
}}
>
{children}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { FC, useEffect, useState } from 'react'

import {
GetUserErrorResponse,
UpdateUserErrorResponse,
UpdateUserRequestBody,
useGetUserQuery,
useUpdateUserMutation
} from '@harnessio/code-service-client'
import { updateUser, UpdateUserErrorResponse } from '@harnessio/code-service-client'
import {
PasswordFields,
ProfileFields,
Expand All @@ -19,110 +13,75 @@ import { useTranslationStore } from '../../i18n/stores/i18n-store'
import { useProfileSettingsStore } from './stores/profile-settings-store'

export const SettingsProfileGeneralPage: FC = () => {
const { updateUserProfile, updateUserPassword, isUpdateUserLoading, updateUserLoadingError } = useAppContext()
const { currentUser, updateUserProfile, isLoadingUser, isUpdatingUser, updateUserError } = useAppContext()
const { setUserData } = useProfileSettingsStore()
const [apiError, setApiError] = useState<{ type: ProfileSettingsErrorType; message: string } | null>(null)
const [isProfileUpdated, setIsProfileUpdated] = useState(false)
const [isPasswordUpdated, setIsPasswordUpdated] = useState(false)
const [isUpdatingPassword, setIsUpdatingPassword] = useState(false)
const [updatePasswordError, setPasswordError] = useState<{
type: ProfileSettingsErrorType
message: string
} | null>(null)

const { data: { body: userData } = {}, isLoading: isLoadingUserData } = useGetUserQuery(
{},
{
onError: (error: GetUserErrorResponse) => {
const message = error.message || 'An unknown error occurred.'
setApiError({ type: ProfileSettingsErrorType.PROFILE, message: message })
}
}
)

const updateUserMutation = useUpdateUserMutation(
{},
{
onSuccess: ({ body: data }) => {
setUserData({
name: data.display_name || '',
username: data.uid || '',
email: data.email || ''
})
},
onError: (error: UpdateUserErrorResponse) => {
const message = error.message || 'An unknown error occurred.'
setApiError({ type: ProfileSettingsErrorType.PROFILE, message: message })
}
}
)

const updatePasswordMutation = useUpdateUserMutation(
{},
{
onSuccess: ({ body: data }) => {
setUserData({
name: data.display_name || '',
username: data.uid || '',
email: data.email || ''
})
},
onError: (error: UpdateUserErrorResponse) => {
const message = error.message || 'An unknown error occurred.'
setApiError({ type: ProfileSettingsErrorType.PASSWORD, message: message })
}
const updateUserPassword = async (newPassword: string): Promise<void> => {
setIsUpdatingPassword(true)
setPasswordError(null)
try {
await updateUser({ body: { password: newPassword } })
} catch (error) {
const typedError = error as UpdateUserErrorResponse
setPasswordError({
type: ProfileSettingsErrorType.PASSWORD,
message: typedError.message || 'An unknown update password error occurred.'
})
} finally {
setIsUpdatingPassword(false)
}
)
}

const handleUpdateUser = (updatedUserData: Omit<ProfileFields, 'username'>) => {
const updateUserBody: UpdateUserRequestBody = {
display_name: updatedUserData.name,
email: updatedUserData.email
}

updateUserProfile({
display_name: updatedUserData.name,
email: updatedUserData.email
}).then(() => {
updateUserMutation.mutate({
body: updateUserBody
})
setIsProfileUpdated(true)
})
}

const handleUpdatePassword = (updatedPasswordData: PasswordFields) => {
const handleUpdateUserPassword = (updatedPasswordData: PasswordFields) => {
if (updatedPasswordData.newPassword !== updatedPasswordData.confirmPassword) {
setApiError({ type: ProfileSettingsErrorType.PASSWORD, message: 'Passwords do not match' })
setPasswordError({ type: ProfileSettingsErrorType.PASSWORD, message: 'Passwords do not match' })
return
}

const updatePasswordBody: UpdateUserRequestBody = {
password: updatedPasswordData.newPassword
}

updateUserPassword(updatedPasswordData.newPassword).then(() => {
updatePasswordMutation.mutate({
body: updatePasswordBody
})
setIsPasswordUpdated(true)
})
}

useEffect(() => {
if (userData) {
if (currentUser) {
setUserData({
name: userData.display_name || '',
username: userData.uid || '',
email: userData.email || ''
name: currentUser.display_name || '',
username: currentUser.uid || '',
email: currentUser.email || ''
})
}
}, [userData, setUserData])
}, [currentUser, setUserData])

return (
<>
<SettingsAccountGeneralPage
useProfileSettingsStore={useProfileSettingsStore}
useTranslationStore={useTranslationStore}
isLoadingUser={isLoadingUserData}
isUpdatingUser={isUpdateUserLoading}
isUpdatingPassword={isUpdateUserLoading}
error={apiError || updateUserLoadingError}
isLoadingUser={isLoadingUser}
isUpdatingUser={isUpdatingUser}
isUpdatingPassword={isUpdatingPassword}
error={updateUserError || updatePasswordError}
onUpdateUser={handleUpdateUser}
onUpdatePassword={handleUpdatePassword}
profileUpdateSuccess={updateUserMutation.isSuccess}
passwordUpdateSuccess={updatePasswordMutation.isSuccess}
onUpdatePassword={handleUpdateUserPassword}
profileUpdateSuccess={isProfileUpdated}
passwordUpdateSuccess={isPasswordUpdated}
/>
</>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
ApiErrorType,
ProfileSettingsKeysCreateDialog,
ProfileSettingsTokenCreateDialog,
ProfileSettingsTokenSuccessDialog,
SettingsAccountKeysPage,
TokensList
} from '@harnessio/ui/views'
Expand All @@ -35,18 +34,9 @@ import { useProfileSettingsStore } from './stores/profile-settings-store'
const CONVERT_DAYS_TO_NANO_SECONDS = 24 * 60 * 60 * 1000 * 1000000

export const SettingsProfileKeysPage = () => {
const {
createdTokenData,
setCreatedTokenData,
setPublicKeys,
setTokens,
deleteToken,
addToken,
addPublicKey,
deletePublicKey
} = useProfileSettingsStore()
const { setCreatedTokenData, setPublicKeys, setTokens, deleteToken, addToken, addPublicKey, deletePublicKey } =
useProfileSettingsStore()
const [openCreateTokenDialog, setCreateTokenDialog] = useState(false)
const [openSuccessTokenDialog, setSuccessTokenDialog] = useState(false)
const [saveSshKeyDialog, setSshKeyDialog] = useState(false)
const [isAlertDeleteDialogOpen, setIsAlertDeleteDialogOpen] = useState(false)
const [alertParams, setAlertParams] = useState<AlertDeleteParams | null>(null)
Expand All @@ -57,8 +47,6 @@ export const SettingsProfileKeysPage = () => {
message: string
} | null>(null)

const closeSuccessTokenDialog = () => setSuccessTokenDialog(false)

const openTokenDialog = () => {
setCreateTokenDialog(true)
setApiError(null)
Expand Down Expand Up @@ -134,8 +122,6 @@ export const SettingsProfileKeysPage = () => {
}

setCreatedTokenData(tokenData)
setSuccessTokenDialog(true)
closeTokenDialog()
addToken(newToken.token as TokensList)
},
onError: (error: CreateTokenErrorResponse) => {
Expand Down Expand Up @@ -235,6 +221,7 @@ export const SettingsProfileKeysPage = () => {
handleCreateToken={handleCreateToken}
error={apiError}
isLoading={createTokenMutation.isLoading}
useProfileSettingsStore={useProfileSettingsStore}
useTranslationStore={useTranslationStore}
/>
<ProfileSettingsKeysCreateDialog
Expand All @@ -244,12 +231,6 @@ export const SettingsProfileKeysPage = () => {
error={apiError}
useTranslationStore={useTranslationStore}
/>
<ProfileSettingsTokenSuccessDialog
open={openSuccessTokenDialog && !!createdTokenData}
onClose={closeSuccessTokenDialog}
useProfileSettingsStore={useProfileSettingsStore}
useTranslationStore={useTranslationStore}
/>
<DeleteAlertDialog
open={isAlertDeleteDialogOpen}
onClose={closeAlertDeleteDialog}
Expand Down
8 changes: 4 additions & 4 deletions packages/ui/locales/en/views.json
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,8 @@
"enterNamePlaceholder": "Enter the name",
"publicKey": "Public key",
"cancel": "Cancel",
"saveButton": "Save",
"savingButton": "Saving...",
"save": "Save",
"saving": "Saving...",
"name": "Name",
"added": "Added",
"lastUsedDate": "Last used date",
Expand Down Expand Up @@ -318,8 +318,8 @@
"newPassword": "New password",
"confirmPasswordPlaceholder": "Confirm your new password",
"confirmPassword": "Confirm password",
"updatingPasswordButton": "Updating...",
"updatePasswordButton": "Update password",
"updating": "Updating...",
"updatePassword": "Update password",
"keysAndTokens": "Keys and Tokens",
"personalAccessToken": "Personal access token",
"addToken": "Add new token",
Expand Down
8 changes: 4 additions & 4 deletions packages/ui/locales/es/views.json
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@
"enterNamePlaceholder": "Enter the name",
"publicKey": "Public key",
"cancel": "Cancel",
"saveButton": "Save",
"savingButton": "Saving...",
"save": "Save",
"saving": "Saving...",
"name": "Name",
"added": "Added",
"lastUsedDate": "Last used date",
Expand Down Expand Up @@ -308,8 +308,8 @@
"newPassword": "New password",
"confirmPasswordPlaceholder": "Confirm your new password",
"confirmPassword": "Confirm password",
"updatingPasswordButton": "Updating...",
"updatePasswordButton": "Update password",
"updating": "Updating...",
"updatePassword": "Update password",
"keysAndTokens": "Keys and Tokens",
"personalAccessToken": "Personal access token",
"addToken": "Add new token",
Expand Down
Loading

0 comments on commit 3b62ed9

Please sign in to comment.