@antscorp/antsomi-ui 1.3.5-beta.993 → 1.3.5-beta.994
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -2,7 +2,10 @@ import React from 'react';
|
|
|
2
2
|
import { CodeStructureData } from './type';
|
|
3
3
|
interface CodeStructureProps {
|
|
4
4
|
initialData: CodeStructureData;
|
|
5
|
-
onChange: (data
|
|
5
|
+
onChange: ({ data, isError }: {
|
|
6
|
+
data: CodeStructureData;
|
|
7
|
+
isError: boolean;
|
|
8
|
+
}) => void;
|
|
6
9
|
}
|
|
7
10
|
export declare const CodeStructure: React.FC<CodeStructureProps>;
|
|
8
11
|
export {};
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
// Libraries
|
|
3
|
-
import {
|
|
3
|
+
import { useEffect } from 'react';
|
|
4
4
|
import { Form, InputNumber, Radio, Select, Typography, Flex, Input, Row, Col } from 'antd';
|
|
5
5
|
import { useImmer } from 'use-immer';
|
|
6
6
|
import { translate, translations } from '@antscorp/antsomi-locales';
|
|
@@ -18,12 +18,10 @@ export const CodeStructure = ({ initialData, onChange }) => {
|
|
|
18
18
|
const [state, setState] = useImmer({
|
|
19
19
|
codeSample: '',
|
|
20
20
|
maxCodes: 0,
|
|
21
|
-
refillThreshold: 0,
|
|
22
21
|
refillInterval: 'auto',
|
|
23
|
-
refillUnit: 'codes',
|
|
24
22
|
codeWithPrefixAndSuffix: '',
|
|
25
23
|
});
|
|
26
|
-
const { codeSample, maxCodes
|
|
24
|
+
const { codeSample, maxCodes } = state;
|
|
27
25
|
const setCodeSample = (value) => {
|
|
28
26
|
setState(draft => {
|
|
29
27
|
draft.codeSample = value;
|
|
@@ -43,37 +41,24 @@ export const CodeStructure = ({ initialData, onChange }) => {
|
|
|
43
41
|
draft.codeWithPrefixAndSuffix = value;
|
|
44
42
|
});
|
|
45
43
|
};
|
|
46
|
-
const setRefillThreshold = useCallback((value) => {
|
|
47
|
-
setState(draft => {
|
|
48
|
-
draft.refillThreshold = value;
|
|
49
|
-
});
|
|
50
|
-
}, [setState]);
|
|
51
|
-
const setRefillUnit = useCallback((value) => {
|
|
52
|
-
if (value === RefillUnitType.PERCENT) {
|
|
53
|
-
setRefillThreshold(1);
|
|
54
|
-
}
|
|
55
|
-
else {
|
|
56
|
-
setRefillThreshold(0);
|
|
57
|
-
}
|
|
58
|
-
setState(draft => {
|
|
59
|
-
draft.refillUnit = value;
|
|
60
|
-
});
|
|
61
|
-
}, [setState, setRefillThreshold]);
|
|
62
44
|
const debouncedOnChange = useDebouncedCallbackV2(onChange, 350);
|
|
63
|
-
const updateFormValues = (
|
|
45
|
+
const updateFormValues = async (fieldKeyChange, isError) => {
|
|
46
|
+
// Get all values from the form
|
|
64
47
|
const allValues = form.getFieldsValue();
|
|
65
48
|
const numLetters = form.getFieldValue('alphabetLetters');
|
|
66
49
|
const numDigits = form.getFieldValue('digitalNumbers');
|
|
67
50
|
const type = form.getFieldValue('characterType');
|
|
68
51
|
form.setFieldsValue(allValues);
|
|
69
52
|
let totalPredicted = maxCodes;
|
|
70
|
-
const isRelevantChangeMaxCode =
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
53
|
+
const isRelevantChangeMaxCode = fieldKeyChange === 'alphabetLetters' ||
|
|
54
|
+
fieldKeyChange === 'digitalNumbers' ||
|
|
55
|
+
fieldKeyChange === 'characterType';
|
|
56
|
+
const isRelevantChangeCodeKey = fieldKeyChange !== 'quantity' &&
|
|
57
|
+
fieldKeyChange !== 'suffix' &&
|
|
58
|
+
fieldKeyChange !== 'prefix' &&
|
|
59
|
+
fieldKeyChange !== 'refillInterval' &&
|
|
60
|
+
fieldKeyChange !== 'separator';
|
|
61
|
+
const isChangePrefix = fieldKeyChange === 'suffix' || fieldKeyChange === 'prefix' || fieldKeyChange === 'separator';
|
|
77
62
|
if (isRelevantChangeMaxCode) {
|
|
78
63
|
const total = calculateTotalCodes(numLetters, numDigits, type);
|
|
79
64
|
totalPredicted = total;
|
|
@@ -88,29 +73,46 @@ export const CodeStructure = ({ initialData, onChange }) => {
|
|
|
88
73
|
const dataOnChange = {
|
|
89
74
|
...allValues,
|
|
90
75
|
totalPredicted,
|
|
91
|
-
...(allValues.refillInterval === 'auto' ? { refillThreshold, refillUnit } : {}),
|
|
92
76
|
};
|
|
93
|
-
debouncedOnChange(
|
|
77
|
+
debouncedOnChange({
|
|
78
|
+
data: dataOnChange,
|
|
79
|
+
isError,
|
|
80
|
+
});
|
|
94
81
|
};
|
|
95
|
-
// Effects
|
|
96
|
-
useEffect(() => {
|
|
97
|
-
setRefillThreshold(initialData.refillThreshold || 0);
|
|
98
|
-
}, [initialData.refillThreshold, setRefillThreshold]);
|
|
99
|
-
useEffect(() => {
|
|
100
|
-
setRefillUnit(initialData.refillUnit || RefillUnitType.CODES);
|
|
101
|
-
}, [initialData.refillUnit, setRefillUnit]);
|
|
102
82
|
useEffect(() => {
|
|
103
83
|
setCodeSample(generateRandomCodeWithConfig(form.getFieldsValue()));
|
|
104
84
|
setMaxCodes(calculateTotalCodes(form.getFieldValue('alphabetLetters'), form.getFieldValue('digitalNumbers'), form.getFieldValue('characterType')));
|
|
105
|
-
}, []);
|
|
85
|
+
}, [initialData]);
|
|
106
86
|
useEffect(() => {
|
|
107
87
|
form.setFieldsValue(initialData);
|
|
108
88
|
}, [initialData, form]);
|
|
109
|
-
return (_jsx(CodeStructureWrapper, { children: _jsxs(Form, { form: form, layout: "horizontal", labelCol: { span: 6 }, colon: false, wrapperCol: { span: 24 }, labelAlign: "left", initialValues: initialData,
|
|
89
|
+
return (_jsx(CodeStructureWrapper, { children: _jsxs(Form, { form: form, layout: "horizontal", labelCol: { span: 6 }, colon: false, wrapperCol: { span: 24 }, labelAlign: "left", initialValues: initialData, onFieldsChange: (changedFields, _allFields) => {
|
|
90
|
+
const fieldKeyChange = changedFields[0].name[0];
|
|
91
|
+
const isValidate = form.getFieldsError().some(field => field.errors.length > 0);
|
|
92
|
+
updateFormValues(fieldKeyChange, isValidate);
|
|
93
|
+
}, children: [_jsx(Title, { level: 5, children: translate(translations._POOL_RULE_SETTING_CODE_STRUCTURE, 'Code Structure') }), _jsx(Form.Item, { label: translate(translations._POOL_RULE_SETTING_CODE_STRUCTURE_PREFIX, 'Prefix'), name: "prefix", children: _jsx(Input, { placeholder: "", style: {
|
|
110
94
|
maxWidth: 465,
|
|
111
95
|
} }) }), _jsxs(Row, { children: [_jsx(Col, { span: 6, offset: 0, children: translate(translations._POOL_RULE_SETTING_CODE_STRUCTURE_INFIX, 'Infix') }), _jsx(Col, { span: 16, offset: 0, children: _jsxs(Flex, { vertical: true, gap: 10, children: [_jsx(Row, { children: _jsx(Col, { offset: 0, children: translate(translations._POOL_RULE_SETTING_CODE_STRUCTURE_CHAC_TYPE, 'Character Type') }) }), _jsx(Form.Item, { name: "characterType", children: _jsx(Radio.Group, { options: CHARACTER_TYPE_OPTIONS }) })] }) })] }), _jsx(Row, { children: _jsx(Col, { offset: 6, children: _jsxs(Flex, { gap: 10, align: "center", children: [_jsx(Form.Item, { name: "digitalNumbers", children: _jsx(InputNumber, { min: 1, max: 50, style: { width: '40px' } }) }), _jsx("div", { style: { marginBottom: '15px' }, children: translate(translations._POOL_RULE_SETTING_CODE_STRUCTURE_CHAC_TYPE_DIG_NO, 'digital numbers') })] }) }) }), _jsx(Row, { children: _jsx(Col, { offset: 6, children: _jsxs(Flex, { gap: 10, align: "center", children: [_jsx(Form.Item, { name: "alphabetLetters", children: _jsx(InputNumber, { min: 1, max: 50, style: { width: '40px' } }) }), _jsx("div", { style: { marginBottom: '15px' }, children: translate(translations._POOL_RULE_SETTING_CODE_STRUCTURE_CHAC_TYPE_LET_NO, 'alphabet letters') })] }) }) }), _jsxs(Flex, { vertical: true, gap: 10, children: [_jsx(Row, { children: _jsx(Col, { offset: 6, children: translate(translations._POOL_RULE_SETTING_CODE_STRUCTURE_CAP, 'Letters Capitalization') }) }), _jsx(Form.Item, { label: _jsx("span", { children: "\u00A0" }), name: "capitalization", children: _jsx(Radio.Group, { options: CAPITALIZATION_OPTIONS }) })] }), _jsxs(Flex, { vertical: true, gap: 10, children: [_jsx(Row, { children: _jsx(Col, { offset: 6, children: translate(translations._POOL_RULE_SETTING_CODE_STRUCTURE_CHAC_ORDER, 'Character Order') }) }), _jsx(Form.Item, { label: _jsx("span", { children: "\u00A0" }), name: "characterOrder", children: _jsx(Radio.Group, { options: CHARACTER_ORDER_OPTIONS }) })] }), _jsx(Form.Item, { label: translate(translations._POOL_RULE_SETTING_CODE_STRUCTURE_SUFFIX, 'Suffix'), name: "suffix", children: _jsx(Input, { placeholder: "", style: {
|
|
112
96
|
maxWidth: 465,
|
|
113
|
-
} }) }), _jsx(Form.Item, { label: translate(translations._POOL_RULE_SETTING_CODE_STRUCTURE_SEPARATOR, 'Separator'), name: "separator", children: _jsx(Select, { options: SEPARATOR_OPTIONS, style: { width: '180px' } }) }), _jsxs(Row, { children: [_jsx(Col, { span: 6, children: _jsxs(Flex, { justify: "start", align: "center", gap: 5, children: [translate(translations._POOL_RULE_SETTING_CODE_STRUCTURE_QUANTITY, 'Quantity'), ' ', _jsx("span", { style: { color: 'red' }, children: "*" })] }) }), _jsx(Col, { children: _jsxs(Flex, { gap: 10, align: "center", children: [_jsx(Form.Item, { name: "quantity", children: _jsx(InputNumber, { min: 1, max: 10000 }) }), _jsx("div", { style: { marginBottom: '15px' }, children: "code(s) per time" })] }) })] }), _jsxs(Form.Item, { label: translate(translations._ACT_PREVIEW, 'Preview'), children: ["Maximum of ", _jsx("b", { children: maxCodes.toLocaleString() }), " codes (sample", ' ', generateCodeWithPrefixAndSuffix(codeSample, form.getFieldValue('prefix'), form.getFieldValue('suffix'), form.getFieldValue('separator')), ") to be generated"] }), _jsx(Title, { level: 5, children: "Code Refill" }), _jsx(Form.Item, { label: "Refill Interval", name: "refillInterval", children: _jsxs(Radio.Group, { style: { display: 'flex', flexDirection: 'column', gap: '10px' },
|
|
114
|
-
|
|
115
|
-
|
|
97
|
+
} }) }), _jsx(Form.Item, { label: translate(translations._POOL_RULE_SETTING_CODE_STRUCTURE_SEPARATOR, 'Separator'), name: "separator", children: _jsx(Select, { options: SEPARATOR_OPTIONS, style: { width: '180px' } }) }), _jsxs(Row, { children: [_jsx(Col, { span: 6, children: _jsxs(Flex, { justify: "start", align: "center", gap: 5, children: [translate(translations._POOL_RULE_SETTING_CODE_STRUCTURE_QUANTITY, 'Quantity'), ' ', _jsx("span", { style: { color: 'red' }, children: "*" })] }) }), _jsx(Col, { children: _jsxs(Flex, { gap: 10, align: "center", children: [_jsx(Form.Item, { required: true, name: "quantity", rules: [{ required: true }], children: _jsx(InputNumber, { min: 1, max: 10000 }) }), _jsx("div", { style: { marginBottom: '15px' }, children: "code(s) per time" })] }) })] }), _jsxs(Form.Item, { label: translate(translations._ACT_PREVIEW, 'Preview'), children: ["Maximum of ", _jsx("b", { children: maxCodes.toLocaleString() }), " codes (sample", ' ', generateCodeWithPrefixAndSuffix(codeSample, form.getFieldValue('prefix'), form.getFieldValue('suffix'), form.getFieldValue('separator')), ") to be generated"] }), _jsx(Title, { level: 5, children: "Code Refill" }), _jsx(Form.Item, { label: "Refill Interval", name: "refillInterval", children: _jsxs(Radio.Group, { style: { display: 'flex', flexDirection: 'column', gap: '10px' }, children: [_jsx(Radio, { value: "manual", children: "Manual refill only" }), _jsx(Radio, { value: "auto", children: _jsxs(Flex, { gap: 10, align: "center", children: [_jsx(Typography.Text, { style: { whiteSpace: 'nowrap' }, children: "Add codes when less than" }), _jsx(Form.Item, { noStyle: true, shouldUpdate: true, children: ({ getFieldValue }) => {
|
|
98
|
+
const refillUnit = getFieldValue('refillUnit');
|
|
99
|
+
return (_jsx(Form.Item, { name: "refillThreshold", noStyle: true, rules: [
|
|
100
|
+
{ required: true, message: 'Field is required!' },
|
|
101
|
+
{
|
|
102
|
+
validator: (_, value) => {
|
|
103
|
+
if (refillUnit === RefillUnitType.PERCENT) {
|
|
104
|
+
if (value < 1 || value > 100) {
|
|
105
|
+
return Promise.reject(new Error('Must be between 1 and 100'));
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
else if (refillUnit === RefillUnitType.CODES) {
|
|
109
|
+
if (value < 0) {
|
|
110
|
+
return Promise.reject(new Error('Must be ≥ 0 codes'));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
return Promise.resolve();
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
], children: _jsx(InputNumber, { min: refillUnit === RefillUnitType.CODES ? 0 : 1, max: refillUnit === RefillUnitType.PERCENT ? 100 : undefined, style: { width: '63px' } }) }));
|
|
117
|
+
} }), _jsx(Form.Item, { name: "refillUnit", noStyle: true, children: _jsx(Select, { options: REFILL_UNIT_OPTIONS, style: { width: '63px' }, onClick: e => e.preventDefault() }) }), "remaining"] }) })] }) })] }) }));
|
|
116
118
|
};
|
|
@@ -57,7 +57,8 @@ export const calculateTotalCodes = (numberOfLetters, numberOfDigits, characterTy
|
|
|
57
57
|
totalCodes = 26 * 26 ** (numberOfLetters - 1) * 10 ** numberOfDigits;
|
|
58
58
|
break;
|
|
59
59
|
default:
|
|
60
|
-
|
|
60
|
+
totalCodes = 0;
|
|
61
|
+
break;
|
|
61
62
|
}
|
|
62
63
|
return totalCodes;
|
|
63
64
|
};
|