|
@@ -1,6 +1,7 @@
|
|
|
import React, { FC, useCallback, useState } from 'react';
|
|
import React, { FC, useCallback, useState } from 'react';
|
|
|
import { useTranslation } from 'react-i18next';
|
|
import { useTranslation } from 'react-i18next';
|
|
|
import dateFnsFormat from 'date-fns/format';
|
|
import dateFnsFormat from 'date-fns/format';
|
|
|
|
|
+import { TFunctionResult } from 'i18next';
|
|
|
|
|
|
|
|
import { withUnstatedContainers } from '../../UnstatedUtils';
|
|
import { withUnstatedContainers } from '../../UnstatedUtils';
|
|
|
import AppContainer from '~/client/services/AppContainer';
|
|
import AppContainer from '~/client/services/AppContainer';
|
|
@@ -10,19 +11,24 @@ import { CustomWindow } from '~/interfaces/global';
|
|
|
import Xss from '~/services/xss';
|
|
import Xss from '~/services/xss';
|
|
|
|
|
|
|
|
type Props = {
|
|
type Props = {
|
|
|
- userGroup: IUserGroupHasId,
|
|
|
|
|
- onSubmit?: (userGroupData: Partial<IUserGroup>) => Promise<IUserGroupHasId>
|
|
|
|
|
|
|
+ userGroup?: IUserGroupHasId,
|
|
|
|
|
+ successedMessage: TFunctionResult;
|
|
|
|
|
+ failedMessage: TFunctionResult;
|
|
|
|
|
+ submitButtonLabel: TFunctionResult;
|
|
|
|
|
+ onSubmit?: (userGroupData: Partial<IUserGroup>) => Promise<IUserGroupHasId | void>
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
const UserGroupForm: FC<Props> = (props: Props) => {
|
|
const UserGroupForm: FC<Props> = (props: Props) => {
|
|
|
const xss: Xss = (window as CustomWindow).xss;
|
|
const xss: Xss = (window as CustomWindow).xss;
|
|
|
|
|
+
|
|
|
const { t } = useTranslation();
|
|
const { t } = useTranslation();
|
|
|
|
|
|
|
|
/*
|
|
/*
|
|
|
* State
|
|
* State
|
|
|
*/
|
|
*/
|
|
|
- const [currentName, setName] = useState(props.userGroup.name);
|
|
|
|
|
- const [nameCache, setNameCache] = useState(props.userGroup.name); // to validate the same name
|
|
|
|
|
|
|
+ const [currentName, setName] = useState(props.userGroup != null ? props.userGroup.name : '');
|
|
|
|
|
+ const [currentDescription, setDescription] = useState(props.userGroup != null ? props.userGroup.description : '');
|
|
|
|
|
+ const [currentParent, setParent] = useState(props.userGroup != null ? props.userGroup.parent : '');
|
|
|
|
|
|
|
|
/*
|
|
/*
|
|
|
* Function
|
|
* Function
|
|
@@ -31,6 +37,10 @@ const UserGroupForm: FC<Props> = (props: Props) => {
|
|
|
setName(e.target.value);
|
|
setName(e.target.value);
|
|
|
}, []);
|
|
}, []);
|
|
|
|
|
|
|
|
|
|
+ const onChangeDescriptionHandler = useCallback((e) => {
|
|
|
|
|
+ setDescription(e.target.value);
|
|
|
|
|
+ }, []);
|
|
|
|
|
+
|
|
|
const onSubmitHandler = useCallback(async(e) => {
|
|
const onSubmitHandler = useCallback(async(e) => {
|
|
|
e.preventDefault(); // no reload
|
|
e.preventDefault(); // no reload
|
|
|
|
|
|
|
@@ -39,46 +49,60 @@ const UserGroupForm: FC<Props> = (props: Props) => {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
try {
|
|
|
- const newUserGroup = await props.onSubmit({ name: currentName });
|
|
|
|
|
|
|
+ await props.onSubmit({ name: currentName, description: currentDescription, parent: currentParent });
|
|
|
|
|
|
|
|
- toastSuccess(`Updated the group name to "${xss.process(newUserGroup.name)}"`);
|
|
|
|
|
- setNameCache(currentName);
|
|
|
|
|
|
|
+ toastSuccess(props.successedMessage);
|
|
|
}
|
|
}
|
|
|
catch (err) {
|
|
catch (err) {
|
|
|
- toastError(new Error('Unable to update the group name'));
|
|
|
|
|
|
|
+ toastError(props.failedMessage);
|
|
|
}
|
|
}
|
|
|
- }, [currentName, props.onSubmit]);
|
|
|
|
|
-
|
|
|
|
|
- const validateForm = useCallback(() => { return currentName !== nameCache && currentName !== '' }, [currentName, nameCache]);
|
|
|
|
|
-
|
|
|
|
|
|
|
+ }, [currentName, currentDescription, currentParent, props.onSubmit, props.successedMessage, props.failedMessage]);
|
|
|
|
|
|
|
|
return (
|
|
return (
|
|
|
<form onSubmit={onSubmitHandler}>
|
|
<form onSubmit={onSubmitHandler}>
|
|
|
|
|
+ {/* TODO 85062: improve style */}
|
|
|
|
|
+ {
|
|
|
|
|
+ props.userGroup != null && (
|
|
|
|
|
+ <div className="row mb-2">
|
|
|
|
|
+ <p className="col-md-4">{t('Created')}</p>
|
|
|
|
|
+ <p className="col">{dateFnsFormat(new Date(props.userGroup.createdAt), 'yyyy-MM-dd')}</p>
|
|
|
|
|
+ </div>
|
|
|
|
|
+ )
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
<fieldset>
|
|
<fieldset>
|
|
|
<h2 className="admin-setting-header">{t('admin:user_group_management.basic_info')}</h2>
|
|
<h2 className="admin-setting-header">{t('admin:user_group_management.basic_info')}</h2>
|
|
|
<div className="form-group row">
|
|
<div className="form-group row">
|
|
|
<label htmlFor="name" className="col-md-2 col-form-label">
|
|
<label htmlFor="name" className="col-md-2 col-form-label">
|
|
|
- {t('Name')}
|
|
|
|
|
|
|
+ {t('admin:user_group_management.group_name')}
|
|
|
</label>
|
|
</label>
|
|
|
<div className="col-md-4">
|
|
<div className="col-md-4">
|
|
|
- <input className="form-control" type="text" name="name" value={currentName} onChange={onChangeNameHandler} />
|
|
|
|
|
|
|
+ <input
|
|
|
|
|
+ className="form-control"
|
|
|
|
|
+ type="text"
|
|
|
|
|
+ name="name"
|
|
|
|
|
+ placeholder={t('admin:user_group_management.group_example')}
|
|
|
|
|
+ value={currentName}
|
|
|
|
|
+ onChange={onChangeNameHandler}
|
|
|
|
|
+ required
|
|
|
|
|
+ />
|
|
|
</div>
|
|
</div>
|
|
|
</div>
|
|
</div>
|
|
|
<div className="form-group row">
|
|
<div className="form-group row">
|
|
|
- <label className="col-md-2 col-form-label">{t('Created')}</label>
|
|
|
|
|
|
|
+ <label htmlFor="description" className="col-md-2 col-form-label">
|
|
|
|
|
+ {t('Description')}
|
|
|
|
|
+ </label>
|
|
|
<div className="col-md-4">
|
|
<div className="col-md-4">
|
|
|
- <input
|
|
|
|
|
- type="text"
|
|
|
|
|
- className="form-control"
|
|
|
|
|
- value={dateFnsFormat(new Date(props.userGroup.createdAt), 'yyyy-MM-dd')}
|
|
|
|
|
- disabled
|
|
|
|
|
- />
|
|
|
|
|
|
|
+ <textarea className="form-control" name="description" value={currentDescription} onChange={onChangeDescriptionHandler} required />
|
|
|
</div>
|
|
</div>
|
|
|
</div>
|
|
</div>
|
|
|
|
|
+
|
|
|
|
|
+ {/* TODO 85062: select parent dropdown */}
|
|
|
|
|
+
|
|
|
<div className="form-group row">
|
|
<div className="form-group row">
|
|
|
<div className="offset-md-2 col-md-10">
|
|
<div className="offset-md-2 col-md-10">
|
|
|
- <button type="submit" className="btn btn-primary" disabled={!validateForm()}>
|
|
|
|
|
- {t('Update')}
|
|
|
|
|
|
|
+ <button type="submit" className="btn btn-primary">
|
|
|
|
|
+ {props.submitButtonLabel}
|
|
|
</button>
|
|
</button>
|
|
|
</div>
|
|
</div>
|
|
|
</div>
|
|
</div>
|
|
@@ -90,6 +114,6 @@ const UserGroupForm: FC<Props> = (props: Props) => {
|
|
|
/**
|
|
/**
|
|
|
* Wrapper component for using unstated
|
|
* Wrapper component for using unstated
|
|
|
*/
|
|
*/
|
|
|
-const UserGroupFormWrapper = withUnstatedContainers(UserGroupForm, [AppContainer]);
|
|
|
|
|
|
|
+const UserGroupFormWrapper = withUnstatedContainers<unknown, Props>(UserGroupForm, [AppContainer]);
|
|
|
|
|
|
|
|
export default UserGroupFormWrapper;
|
|
export default UserGroupFormWrapper;
|