|
|
@@ -1,8 +1,9 @@
|
|
|
-import React, {
|
|
|
- FC, useCallback, useState, useMemo,
|
|
|
-} from 'react';
|
|
|
+import type { FC } from 'react';
|
|
|
+import React, { useCallback, useState, useMemo } from 'react';
|
|
|
|
|
|
-import type { IUserGroupHasId } from '@growi/core';
|
|
|
+import {
|
|
|
+ getIdForRef, isPopulated, type IGrantedGroup, type IUserGroupHasId,
|
|
|
+} from '@growi/core';
|
|
|
import { useTranslation } from 'next-i18next';
|
|
|
import {
|
|
|
Modal, ModalHeader, ModalBody, ModalFooter,
|
|
|
@@ -17,9 +18,9 @@ import {
|
|
|
* @extends {React.Component}
|
|
|
*/
|
|
|
type Props = {
|
|
|
- userGroups: IUserGroupHasId[],
|
|
|
+ userGroups: IGrantedGroup[],
|
|
|
deleteUserGroup?: IUserGroupHasId,
|
|
|
- onDelete?: (deleteGroupId: string, actionName: string, transferToUserGroupId: string) => Promise<void> | void,
|
|
|
+ onDelete?: (deleteGroupId: string, actionName: string, transferToUserGroup: IGrantedGroup | null) => Promise<void> | void,
|
|
|
isShow: boolean,
|
|
|
onHide?: () => Promise<void> | void,
|
|
|
};
|
|
|
@@ -77,14 +78,14 @@ export const UserGroupDeleteModal: FC<Props> = (props: Props) => {
|
|
|
* State
|
|
|
*/
|
|
|
const [actionName, setActionName] = useState<string>('');
|
|
|
- const [transferToUserGroupId, setTransferToUserGroupId] = useState<string>('');
|
|
|
+ const [transferToUserGroup, setTransferToUserGroup] = useState<IGrantedGroup | null>(null);
|
|
|
|
|
|
/*
|
|
|
* Function
|
|
|
*/
|
|
|
const resetStates = useCallback(() => {
|
|
|
setActionName('');
|
|
|
- setTransferToUserGroupId('');
|
|
|
+ setTransferToUserGroup(null);
|
|
|
}, []);
|
|
|
|
|
|
const toggleHandler = useCallback(() => {
|
|
|
@@ -103,8 +104,9 @@ export const UserGroupDeleteModal: FC<Props> = (props: Props) => {
|
|
|
|
|
|
const handleGroupChange = useCallback((e) => {
|
|
|
const transferToUserGroupId = e.target.value;
|
|
|
- setTransferToUserGroupId(transferToUserGroupId);
|
|
|
- }, []);
|
|
|
+ const selectedGroup = userGroups.find(group => getIdForRef(group.item) === transferToUserGroupId) ?? null;
|
|
|
+ setTransferToUserGroup(selectedGroup);
|
|
|
+ }, [userGroups]);
|
|
|
|
|
|
const handleSubmit = useCallback((e) => {
|
|
|
if (onDelete == null || deleteUserGroup == null) {
|
|
|
@@ -116,9 +118,9 @@ export const UserGroupDeleteModal: FC<Props> = (props: Props) => {
|
|
|
onDelete(
|
|
|
deleteUserGroup._id,
|
|
|
actionName,
|
|
|
- transferToUserGroupId,
|
|
|
+ transferToUserGroup,
|
|
|
);
|
|
|
- }, [onDelete, deleteUserGroup, actionName, transferToUserGroupId]);
|
|
|
+ }, [onDelete, deleteUserGroup, actionName, transferToUserGroup]);
|
|
|
|
|
|
const renderPageActionSelector = useCallback(() => {
|
|
|
const options = availableOptions.map((opt) => {
|
|
|
@@ -145,28 +147,31 @@ export const UserGroupDeleteModal: FC<Props> = (props: Props) => {
|
|
|
}
|
|
|
|
|
|
const groups = userGroups.filter((group) => {
|
|
|
- return group._id !== deleteUserGroup._id;
|
|
|
+ return getIdForRef(group.item) !== deleteUserGroup._id;
|
|
|
});
|
|
|
|
|
|
const options = groups.map((group) => {
|
|
|
- return <option key={group._id} value={group._id}>{group.name}</option>;
|
|
|
- });
|
|
|
+ const groupId = getIdForRef(group.item);
|
|
|
+ const groupName = isPopulated(group.item) ? group.item.name : null;
|
|
|
+ return { id: groupId, name: groupName };
|
|
|
+ }).filter(obj => obj.name != null)
|
|
|
+ .map(obj => <option key={obj.id} value={obj.id}>{obj.name}</option>);
|
|
|
|
|
|
const defaultOptionText = groups.length === 0 ? t('admin:user_group_management.delete_modal.no_groups')
|
|
|
: t('admin:user_group_management.delete_modal.select_group');
|
|
|
|
|
|
return (
|
|
|
<select
|
|
|
- name="transferToUserGroupId"
|
|
|
+ name="transferToUserGroup"
|
|
|
className={`form-control ${actionName === actionForPages.transfer ? '' : 'd-none'}`}
|
|
|
- value={transferToUserGroupId}
|
|
|
+ value={transferToUserGroup != null ? getIdForRef(transferToUserGroup.item) : ''}
|
|
|
onChange={handleGroupChange}
|
|
|
>
|
|
|
<option value="" disabled>{defaultOptionText}</option>
|
|
|
{options}
|
|
|
</select>
|
|
|
);
|
|
|
- }, [deleteUserGroup, userGroups, t, actionName, transferToUserGroupId, handleGroupChange]);
|
|
|
+ }, [deleteUserGroup, userGroups, t, actionName, transferToUserGroup, handleGroupChange]);
|
|
|
|
|
|
const validateForm = useCallback(() => {
|
|
|
let isValid = true;
|
|
|
@@ -175,11 +180,11 @@ export const UserGroupDeleteModal: FC<Props> = (props: Props) => {
|
|
|
isValid = false;
|
|
|
}
|
|
|
else if (actionName === actionForPages.transfer) {
|
|
|
- isValid = transferToUserGroupId !== '';
|
|
|
+ isValid = transferToUserGroup != null;
|
|
|
}
|
|
|
|
|
|
return isValid;
|
|
|
- }, [actionName, transferToUserGroupId]);
|
|
|
+ }, [actionName, transferToUserGroup]);
|
|
|
|
|
|
return (
|
|
|
<Modal className="modal-md" isOpen={props.isShow} toggle={toggleHandler}>
|