You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
152 lines
4.4 KiB
152 lines
4.4 KiB
|
6 years ago
|
import { TextInput, Select, Field } from '@rocket.chat/fuselage';
|
||
|
5 years ago
|
import { capitalize } from '@rocket.chat/string-helpers';
|
||
|
5 years ago
|
import React, { useMemo, useEffect, useState } from 'react';
|
||
|
6 years ago
|
|
||
|
6 years ago
|
import { useSetting } from '../contexts/SettingsContext';
|
||
|
|
import { useTranslation } from '../contexts/TranslationContext';
|
||
|
5 years ago
|
import { useComponentDidUpdate } from '../hooks/useComponentDidUpdate';
|
||
|
5 years ago
|
import { useForm } from '../hooks/useForm';
|
||
|
6 years ago
|
|
||
|
4 years ago
|
const CustomTextInput = ({ label, name, required, minLength, maxLength, setState, state, className, setCustomFieldsError = () => [] }) => {
|
||
|
6 years ago
|
const t = useTranslation();
|
||
|
5 years ago
|
|
||
|
5 years ago
|
const [inputError, setInputError] = useState('');
|
||
|
|
|
||
|
6 years ago
|
const verify = useMemo(() => {
|
||
|
5 years ago
|
const errors = [];
|
||
|
|
if (!state && required) {
|
||
|
5 years ago
|
errors.push(t('The_field_is_required', label || name));
|
||
|
5 years ago
|
}
|
||
|
|
|
||
|
|
if (state.length < minLength && state.length > 0) {
|
||
|
|
errors.push(t('Min_length_is', minLength));
|
||
|
|
}
|
||
|
|
|
||
|
|
return errors.join(', ');
|
||
|
5 years ago
|
}, [state, required, minLength, t, label, name]);
|
||
|
5 years ago
|
|
||
|
|
useEffect(() => {
|
||
|
4 years ago
|
setCustomFieldsError((oldErrors) => (verify ? [...oldErrors, { name }] : oldErrors.filter((item) => item.name !== name)));
|
||
|
5 years ago
|
}, [name, setCustomFieldsError, verify]);
|
||
|
|
|
||
|
|
useComponentDidUpdate(() => {
|
||
|
|
setInputError(verify);
|
||
|
|
}, [verify]);
|
||
|
6 years ago
|
|
||
|
5 years ago
|
return useMemo(
|
||
|
|
() => (
|
||
|
|
<Field className={className}>
|
||
|
|
<Field.Label>
|
||
|
|
{label || t(name)}
|
||
|
|
{required && '*'}
|
||
|
|
</Field.Label>
|
||
|
|
<Field.Row>
|
||
|
|
<TextInput
|
||
|
|
name={name}
|
||
|
|
error={inputError}
|
||
|
|
maxLength={maxLength}
|
||
|
|
flexGrow={1}
|
||
|
|
value={state}
|
||
|
|
onChange={(e) => setState(e.currentTarget.value)}
|
||
|
|
/>
|
||
|
|
</Field.Row>
|
||
|
|
<Field.Error>{inputError}</Field.Error>
|
||
|
|
</Field>
|
||
|
|
),
|
||
|
|
[className, label, t, name, required, inputError, maxLength, state, setState],
|
||
|
|
);
|
||
|
6 years ago
|
};
|
||
|
|
|
||
|
4 years ago
|
const CustomSelect = ({ label, name, required, options = {}, setState, state, className, setCustomFieldsError = () => [] }) => {
|
||
|
6 years ago
|
const t = useTranslation();
|
||
|
5 years ago
|
const [selectError, setSelectError] = useState('');
|
||
|
|
|
||
|
4 years ago
|
const mappedOptions = useMemo(() => Object.values(options).map((value) => [value, value]), [options]);
|
||
|
5 years ago
|
const verify = useMemo(
|
||
|
|
() => (!state.length && required ? t('The_field_is_required', label || name) : ''),
|
||
|
|
[name, label, required, state.length, t],
|
||
|
|
);
|
||
|
5 years ago
|
|
||
|
|
useEffect(() => {
|
||
|
4 years ago
|
setCustomFieldsError((oldErrors) => (verify ? [...oldErrors, { name }] : oldErrors.filter((item) => item.name !== name)));
|
||
|
5 years ago
|
}, [name, setCustomFieldsError, verify]);
|
||
|
|
|
||
|
|
useComponentDidUpdate(() => {
|
||
|
|
setSelectError(verify);
|
||
|
|
}, [verify]);
|
||
|
6 years ago
|
|
||
|
5 years ago
|
return useMemo(
|
||
|
|
() => (
|
||
|
|
<Field className={className}>
|
||
|
|
<Field.Label>
|
||
|
|
{label || t(name)}
|
||
|
|
{required && '*'}
|
||
|
|
</Field.Label>
|
||
|
|
<Field.Row>
|
||
|
4 years ago
|
<Select name={name} error={selectError} flexGrow={1} value={state} options={mappedOptions} onChange={(val) => setState(val)} />
|
||
|
5 years ago
|
</Field.Row>
|
||
|
|
<Field.Error>{selectError}</Field.Error>
|
||
|
|
</Field>
|
||
|
|
),
|
||
|
|
[className, label, t, name, required, selectError, state, mappedOptions, setState],
|
||
|
|
);
|
||
|
6 years ago
|
};
|
||
|
|
|
||
|
5 years ago
|
const CustomFieldsAssembler = ({ formValues, formHandlers, customFields, ...props }) =>
|
||
|
|
Object.entries(customFields).map(([key, value]) => {
|
||
|
|
const extraProps = {
|
||
|
|
key,
|
||
|
|
name: key,
|
||
|
|
setState: formHandlers[`handle${capitalize(key)}`],
|
||
|
|
state: formValues[key],
|
||
|
|
...value,
|
||
|
|
};
|
||
|
|
|
||
|
|
if (value.type === 'select') {
|
||
|
|
return <CustomSelect {...extraProps} {...props} />;
|
||
|
|
}
|
||
|
5 years ago
|
|
||
|
5 years ago
|
if (value.type === 'text') {
|
||
|
|
return <CustomTextInput {...extraProps} {...props} />;
|
||
|
|
}
|
||
|
5 years ago
|
|
||
|
5 years ago
|
return null;
|
||
|
|
});
|
||
|
6 years ago
|
|
||
|
4 years ago
|
export default function CustomFieldsForm({ jsonCustomFields, customFieldsData, setCustomFieldsData, onLoadFields = () => {}, ...props }) {
|
||
|
5 years ago
|
const accountsCustomFieldsJson = useSetting('Accounts_CustomFields');
|
||
|
6 years ago
|
|
||
|
6 years ago
|
const [customFields] = useState(() => {
|
||
|
|
try {
|
||
|
5 years ago
|
return jsonCustomFields || JSON.parse(accountsCustomFieldsJson || '{}');
|
||
|
6 years ago
|
} catch {
|
||
|
|
return {};
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
const hasCustomFields = Boolean(Object.values(customFields).length);
|
||
|
5 years ago
|
const defaultFields = useMemo(
|
||
|
5 years ago
|
() =>
|
||
|
|
Object.entries(customFields).reduce((data, [key, value]) => {
|
||
|
|
data[key] = value.defaultValue ?? '';
|
||
|
|
return data;
|
||
|
|
}, {}),
|
||
|
5 years ago
|
[customFields],
|
||
|
|
);
|
||
|
6 years ago
|
|
||
|
|
const { values, handlers } = useForm({ ...defaultFields, ...customFieldsData });
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
5 years ago
|
onLoadFields(hasCustomFields);
|
||
|
6 years ago
|
if (hasCustomFields) {
|
||
|
|
setCustomFieldsData(values);
|
||
|
|
}
|
||
|
5 years ago
|
}, [hasCustomFields, onLoadFields, setCustomFieldsData, values]);
|
||
|
6 years ago
|
|
||
|
6 years ago
|
if (!hasCustomFields) {
|
||
|
6 years ago
|
return null;
|
||
|
|
}
|
||
|
|
|
||
|
4 years ago
|
return <CustomFieldsAssembler formValues={values} formHandlers={handlers} customFields={customFields} {...props} />;
|
||
|
6 years ago
|
}
|