-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Surveys: allow changing owner from surveys list (only system admins) (#…
…3598) * Added EditableColumn component * added SurveyOwnerColumn component * layout adjustments * layout adjustments * code cleanup * surveys table: make selected row editable --------- Co-authored-by: Stefano Ricci <[email protected]> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
026b048
commit f0aa7f7
Showing
22 changed files
with
309 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import React, { useCallback, useState } from 'react' | ||
import classNames from 'classnames' | ||
import PropTypes from 'prop-types' | ||
|
||
import { ButtonIconEdit } from '@webapp/components' | ||
import { KeyboardKeys } from '@webapp/utils/keyboardKeys' | ||
|
||
export const EditableColumn = (props) => { | ||
const { canEdit, className, item, renderItem, renderItemEditing } = props | ||
|
||
const [state, setState] = useState({ editing: false, hovering: false }) | ||
const { editing, hovering } = state | ||
|
||
const setHovering = useCallback( | ||
(hoveringNew) => { | ||
if (hoveringNew !== hovering) { | ||
setState((statePrev) => ({ ...statePrev, hovering: hoveringNew })) | ||
} | ||
}, | ||
[hovering] | ||
) | ||
|
||
const setEditing = useCallback( | ||
(editingNew) => { | ||
if (editingNew !== editing) { | ||
setState((statePrev) => ({ ...statePrev, editing: editingNew })) | ||
} | ||
}, | ||
[editing] | ||
) | ||
|
||
const onContainerMouseOver = useCallback(() => setHovering(true), [setHovering]) | ||
const onContainerMouseLeave = useCallback(() => setHovering(false), [setHovering]) | ||
|
||
const onContainerClick = useCallback((e) => { | ||
// prevent table row selection on click | ||
e.stopPropagation() | ||
e.preventDefault() | ||
}, []) | ||
|
||
const onContainerFocus = useCallback(() => setHovering(true), [setHovering]) | ||
|
||
const onContainerKeyDown = useCallback( | ||
(e) => { | ||
if (e.key === KeyboardKeys.Space) { | ||
setEditing(true) | ||
} | ||
}, | ||
[setEditing] | ||
) | ||
|
||
const onEditClick = useCallback( | ||
(e) => { | ||
e.stopPropagation() | ||
e.preventDefault() | ||
setEditing(true) | ||
}, | ||
[setEditing] | ||
) | ||
|
||
if (!canEdit) { | ||
return <div className={className}>{renderItem({ item })}</div> | ||
} | ||
|
||
return ( | ||
<div | ||
className={classNames(className, { editing })} | ||
onClick={onContainerClick} | ||
onFocus={onContainerFocus} | ||
onKeyDown={onContainerKeyDown} | ||
onMouseOver={onContainerMouseOver} | ||
onMouseLeave={onContainerMouseLeave} | ||
> | ||
{editing && renderItemEditing({ item })} | ||
{!editing && renderItem({ item })} | ||
{hovering && !editing && <ButtonIconEdit onClick={onEditClick} />} | ||
</div> | ||
) | ||
} | ||
|
||
EditableColumn.propTypes = { | ||
canEdit: PropTypes.bool, | ||
className: PropTypes.string, | ||
item: PropTypes.object.isRequired, | ||
renderItem: PropTypes.func.isRequired, | ||
renderItemEditing: PropTypes.func.isRequired, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import React, { useCallback } from 'react' | ||
import { useDispatch } from 'react-redux' | ||
import PropTypes from 'prop-types' | ||
|
||
import * as Survey from '@core/survey/survey' | ||
import * as User from '@core/user/user' | ||
|
||
import * as API from '@webapp/service/api' | ||
import { useAuthCanEditSurveyOwner } from '@webapp/store/user/hooks' | ||
import { DialogConfirmActions } from '@webapp/store/ui' | ||
import { EditableColumn } from '@webapp/components/DataGrid/EditableColumn' | ||
|
||
import { SurveyOwnerDropdown } from './SurveyOwnerDropdown' | ||
|
||
export const SurveyOwnerColumn = (props) => { | ||
const { item: surveyInfo, onSurveysUpdate } = props | ||
|
||
const dispatch = useDispatch() | ||
|
||
const surveyId = Survey.getId(surveyInfo) | ||
const ownerUuid = Survey.getOwnerUuid(surveyInfo) | ||
const surveyName = Survey.getName(surveyInfo) | ||
|
||
const canEdit = useAuthCanEditSurveyOwner() | ||
|
||
const onChangeConfirmed = useCallback( | ||
async ({ selectedOwnerUuid }) => { | ||
await API.updateSurveyOwner({ surveyId, ownerUuid: selectedOwnerUuid }) | ||
await onSurveysUpdate?.() | ||
}, | ||
[onSurveysUpdate, surveyId] | ||
) | ||
|
||
const onChange = useCallback( | ||
async (selectedOwner) => { | ||
const selectedOwnerUuid = User.getUuid(selectedOwner) | ||
if (selectedOwnerUuid !== ownerUuid) { | ||
dispatch( | ||
DialogConfirmActions.showDialogConfirm({ | ||
key: 'surveysView.confirmUpdateSurveyOwner', | ||
params: { surveyName, ownerName: User.getName(selectedOwner) }, | ||
onOk: async () => onChangeConfirmed({ selectedOwnerUuid }), | ||
}) | ||
) | ||
} | ||
}, | ||
[dispatch, onChangeConfirmed, ownerUuid, surveyName] | ||
) | ||
|
||
return ( | ||
<EditableColumn | ||
canEdit={canEdit} | ||
className="owner-col" | ||
item={surveyInfo} | ||
renderItem={({ item }) => Survey.getOwnerName(item)} | ||
renderItemEditing={() => <SurveyOwnerDropdown selectedUuid={ownerUuid} onChange={onChange} />} | ||
/> | ||
) | ||
} | ||
|
||
SurveyOwnerColumn.propTypes = { | ||
item: PropTypes.object.isRequired, | ||
onSurveysUpdate: PropTypes.func, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import React, { useCallback, useEffect, useMemo, useState } from 'react' | ||
import PropTypes from 'prop-types' | ||
|
||
import { Objects } from '@openforis/arena-core' | ||
|
||
import * as User from '@core/user/user' | ||
|
||
import { Dropdown } from '@webapp/components/form' | ||
import * as API from '@webapp/service/api' | ||
import { useI18n } from '@webapp/store/system' | ||
import { useUserIsSystemAdmin } from '@webapp/store/user' | ||
|
||
const ownerLabelFunction = (user) => [User.getName(user), User.getEmail(user)].filter(Objects.isNotEmpty).join(' - ') | ||
|
||
export const SurveyOwnerDropdown = (props) => { | ||
const { selectedUuid, onChange } = props | ||
|
||
const i18n = useI18n() | ||
const isSystemAdmin = useUserIsSystemAdmin() | ||
|
||
const [state, setState] = useState({ loading: true, users: [] }) | ||
|
||
const { users } = state | ||
const selectedUser = useMemo(() => users.find((user) => User.getUuid(user) === selectedUuid), [selectedUuid, users]) | ||
|
||
const fetchUsers = useCallback(async () => { | ||
const usersFetched = await API.fetchUsers({ | ||
onlyAccepted: true, | ||
includeSystemAdmins: isSystemAdmin, | ||
}) | ||
setState({ loading: false, users: usersFetched }) | ||
}, [isSystemAdmin]) | ||
|
||
useEffect(() => { | ||
fetchUsers() | ||
}, [fetchUsers]) | ||
|
||
return ( | ||
<Dropdown | ||
className="width100" | ||
clearable={false} | ||
items={users} | ||
itemValue={User.keys.uuid} | ||
itemLabel={ownerLabelFunction} | ||
onChange={onChange} | ||
placeholder={i18n.t('common.owner')} | ||
selection={selectedUser} | ||
/> | ||
) | ||
} | ||
|
||
SurveyOwnerDropdown.propTypes = { | ||
selectedUuid: PropTypes.string, | ||
onChange: PropTypes.func.isRequired, | ||
} |
Oops, something went wrong.