Skip to content

Commit

Permalink
Merge branch 'development' into 3095-add-a-download-csv-file-button-f…
Browse files Browse the repository at this point in the history
…or-ndp
  • Loading branch information
mergify[bot] authored Feb 2, 2024
2 parents 6e5a417 + 91f1e07 commit 51ed2c8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
48 changes: 28 additions & 20 deletions src/client/components/EditUserForm/EditUserForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,29 @@ import TextInputField from './TextInputField'
import UserRolePropsFields from './UserRolePropsFields'

type Props = {
user: User
targetUser: User
canEditPermissions?: boolean
canEditRoles?: boolean
canEditUser?: boolean
}

const EditUserForm: React.FC<Props> = ({ user, canEditPermissions, canEditRoles, canEditUser }) => {
const EditUserForm: React.FC<Props> = (props: Props) => {
const { targetUser, canEditPermissions, canEditRoles, canEditUser } = props
const dispatch = useAppDispatch()
const assessment = useAssessment()
const countryIso = useCountryIso()
const cycle = useCycle()
const userInfo = useUser()
const user = useUser()
const { t } = useTranslation()

const [profilePicture, setProfilePicture] = useState<File>(null)
const [userToEdit, setUserToEdit] = useState<User>(user)
const [roleToEdit, setRoleToEdit] = useState<UserRole<any, any>>(Users.getRole(user, countryIso, cycle))
const [userToEdit, setUserToEdit] = useState<User>(targetUser)
const [roleToEdit, setRoleToEdit] = useState<UserRole<any, any>>(Users.getRole(targetUser, countryIso, cycle))

const changeUser = (name: string, value: string) => setUserToEdit({ ...user, [name]: value })
const changeUser = (name: string, value: string) => setUserToEdit({ ...targetUser, [name]: value })

const changeUserProp = (name: string, value: any) =>
setUserToEdit({ ...user, props: { ...user.props, [name]: value } })
setUserToEdit({ ...targetUser, props: { ...targetUser.props, [name]: value } })

const changeUserRoleProp = (name: string, value: string) =>
setRoleToEdit({ ...roleToEdit, props: { ...roleToEdit.props, [name]: value } })
Expand Down Expand Up @@ -71,40 +72,47 @@ const EditUserForm: React.FC<Props> = ({ user, canEditPermissions, canEditRoles,
)
}, [profilePicture, userToEdit])

if (!user) return null
if (!targetUser) return null

const userRole = Users.getRole(user, countryIso, cycle)
const userRole = Users.getRole(targetUser, countryIso, cycle)

const enabled = canEditUser
const isAdmin = Users.isAdministrator(userInfo)
const showRoleSelector = !Areas.isGlobal(countryIso) && isAdmin
const isAdmin = Users.isAdministrator(user)
const isTargetAdmin = Users.isAdministrator(targetUser)
const showRoleSelector = !Areas.isGlobal(countryIso) && isAdmin && !isTargetAdmin

return (
<div className="edit-user__form-container">
<ProfilePicture
onChange={(profilePicture: File) => setProfilePicture(profilePicture)}
userId={user.id}
userId={targetUser.id}
enabled={enabled}
/>
<TextInputField
name="email"
value={user.email}
value={targetUser.email}
onChange={changeUser}
validator={Users.validEmailField}
enabled={Users.isAdministrator(userInfo)}
enabled={Users.isAdministrator(user)}
mandatory
/>
<SelectField
name="title"
value={user.props.title}
value={targetUser.props.title}
onChange={changeUserProp}
options={{ Ms: 'Ms', Mr: 'Mr', Other: 'Other' }}
enabled={enabled}
mandatory
/>
<TextInputField name="name" value={user.props.name} onChange={changeUserProp} enabled={enabled} mandatory />
<TextInputField name="surname" value={user.props.surname} onChange={changeUserProp} enabled={enabled} mandatory />
{showRoleSelector && <UserCountryRoleSelector user={user} enabled={enabled} />}
<TextInputField name="name" value={targetUser.props.name} onChange={changeUserProp} enabled={enabled} mandatory />
<TextInputField
name="surname"
value={targetUser.props.surname}
onChange={changeUserProp}
enabled={enabled}
mandatory
/>
{showRoleSelector && <UserCountryRoleSelector user={targetUser} enabled={enabled} />}

{[RoleName.NATIONAL_CORRESPONDENT, RoleName.ALTERNATE_NATIONAL_CORRESPONDENT, RoleName.COLLABORATOR].includes(
userRole?.role
Expand All @@ -116,9 +124,9 @@ const EditUserForm: React.FC<Props> = ({ user, canEditPermissions, canEditRoles,
{canEditPermissions && userRole?.role === RoleName.COLLABORATOR && (
<CollaboratorPermissions userRole={userRole as Collaborator} />
)}
{canEditRoles && <CountryRoles user={user} />}
{canEditRoles && <CountryRoles user={targetUser} />}

{isAdmin && <DisableUser user={user} changeUser={changeUser} />}
{isAdmin && <DisableUser user={targetUser} changeUser={changeUser} />}
</div>
)
}
Expand Down
8 changes: 4 additions & 4 deletions src/client/pages/User/User.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const User: React.FC = () => {
const cycle = useCycle()
const user = useUser()
const isCountry = useIsCountryRoute()
const userToEdit = useUserToEdit()
const targetUser = useUserToEdit()
const location = useLocation()
const { toaster } = useToaster()

Expand Down Expand Up @@ -54,12 +54,12 @@ const User: React.FC = () => {
}
}, [assessmentName, countryIso, cycleName, dispatch, userId])

if (!userToEdit) return null
if (!targetUser) return null

const isAdministrator = Users.isAdministrator(user)
const isReviewer = Users.isReviewer(user, countryIso, cycle)

const isSelf = user.id === userToEdit.id
const isSelf = user.id === targetUser.id

const canEditUser = isSelf || isAdministrator

Expand All @@ -77,7 +77,7 @@ const User: React.FC = () => {
canEditPermissions={canEditPermissions}
canEditRoles={canEditRoles}
canEditUser={canEditUser}
user={userToEdit}
targetUser={targetUser}
/>
<ButtonContinue />
</div>
Expand Down

0 comments on commit 51ed2c8

Please sign in to comment.