diff --git a/web/modals/threads/members/change-member-role-modal.react.js b/web/modals/threads/members/change-member-role-modal.react.js index 2d0229bce..74bb0079f 100644 --- a/web/modals/threads/members/change-member-role-modal.react.js +++ b/web/modals/threads/members/change-member-role-modal.react.js @@ -1,88 +1,128 @@ // @flow import invariant from 'invariant'; import * as React from 'react'; +import { + changeThreadMemberRoles, + changeThreadMemberRolesActionTypes, +} from 'lib/actions/thread-actions.js'; import { useModalContext } from 'lib/components/modal-provider.react.js'; import type { RelativeMemberInfo, ThreadInfo } from 'lib/types/thread-types'; +import { + useDispatchActionPromise, + useServerCall, +} from 'lib/utils/action-utils.js'; import { values } from 'lib/utils/objects.js'; import css from './change-member-role-modal.css'; import UserAvatar from '../../../avatars/user-avatar.react.js'; import Button, { buttonThemes } from '../../../components/button.react.js'; import Dropdown from '../../../components/dropdown.react.js'; import Modal from '../../modal.react.js'; import UnsavedChangesModal from '../../unsaved-changes-modal.react.js'; type ChangeMemberRoleModalProps = { +memberInfo: RelativeMemberInfo, +threadInfo: ThreadInfo, }; function ChangeMemberRoleModal(props: ChangeMemberRoleModalProps): React.Node { const { memberInfo, threadInfo } = props; const { pushModal, popModal } = useModalContext(); + const dispatchActionPromise = useDispatchActionPromise(); + const callChangeThreadMemberRoles = useServerCall(changeThreadMemberRoles); const roleOptions = React.useMemo( () => values(threadInfo.roles).map(role => ({ id: role.id, name: role.name, })), [threadInfo.roles], ); const initialSelectedRole = memberInfo.role; invariant(initialSelectedRole, "Member's role must be defined"); const [selectedRole, setSelectedRole] = React.useState(initialSelectedRole); const onBackClick = React.useCallback(() => { if (selectedRole === initialSelectedRole) { popModal(); return; } pushModal(); }, [initialSelectedRole, popModal, pushModal, selectedRole]); + const onSave = React.useCallback(() => { + if (selectedRole === initialSelectedRole) { + popModal(); + return; + } + + const createChangeThreadMemberRolesPromise = () => { + return callChangeThreadMemberRoles( + threadInfo.id, + [memberInfo.id], + selectedRole, + ); + }; + + dispatchActionPromise( + changeThreadMemberRolesActionTypes, + createChangeThreadMemberRolesPromise(), + ); + popModal(); + }, [ + callChangeThreadMemberRoles, + dispatchActionPromise, + initialSelectedRole, + memberInfo.id, + popModal, + selectedRole, + threadInfo.id, + ]); + return (
Members can only be assigned to one role at a time. Changing a member’s role will replace their previously assigned role.
{memberInfo.username}
); } export default ChangeMemberRoleModal;