import React, { FC, useCallback, useState } from 'react'; import { useTranslation } from 'react-i18next'; import dateFnsFormat from 'date-fns/format'; import { TFunctionResult } from 'i18next'; import { withUnstatedContainers } from '../../UnstatedUtils'; import AppContainer from '~/client/services/AppContainer'; import { toastSuccess, toastError } from '~/client/util/apiNotification'; import { IUserGroup, IUserGroupHasId } from '~/interfaces/user'; import { CustomWindow } from '~/interfaces/global'; import Xss from '~/services/xss'; type Props = { userGroup?: IUserGroupHasId, successedMessage: TFunctionResult; failedMessage: TFunctionResult; submitButtonLabel: TFunctionResult; onSubmit?: (userGroupData: Partial) => Promise }; const UserGroupForm: FC = (props: Props) => { const xss: Xss = (window as CustomWindow).xss; const { t } = useTranslation(); /* * State */ 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 */ const onChangeNameHandler = useCallback((e) => { setName(e.target.value); }, []); const onChangeDescriptionHandler = useCallback((e) => { setDescription(e.target.value); }, []); const onSubmitHandler = useCallback(async(e) => { e.preventDefault(); // no reload if (props.onSubmit == null) { return; } try { await props.onSubmit({ name: currentName, description: currentDescription, parent: currentParent }); toastSuccess(props.successedMessage); } catch (err) { toastError(props.failedMessage); } }, [currentName, currentDescription, currentParent, props.onSubmit, props.successedMessage, props.failedMessage]); return (

{t('admin:user_group_management.basic_info')}

{/* TODO 85062: improve style */} { props.userGroup?.createdAt != null && (

{t('Created')}

{dateFnsFormat(new Date(props.userGroup.createdAt), 'yyyy-MM-dd')}

) }